Skip to content

Commit

Permalink
Merge pull request #12156 from cibernox/assert_presence_of_getter_in_cps
Browse files Browse the repository at this point in the history
[BUGFIX beta] CPs check presence of getter function
  • Loading branch information
rwjblue committed Aug 23, 2015
2 parents faf3018 + f50e6c4 commit b3b6e47
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
11 changes: 11 additions & 0 deletions packages/ember-metal/lib/computed.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,20 @@ function ComputedProperty(config, opts) {
if (typeof config === 'function') {
this._getter = config;
} else {
Ember.assert('Ember.computed expects a function or an object as last argument.', typeof config === 'object' && !Array.isArray(config));
Ember.assert('Config object pased to a Ember.computed can only contain `get` or `set` keys.', (function() {
let keys = Object.keys(config);
for (let i = 0; i < keys.length; i++) {
if (keys[i] !== 'get' && keys[i] !== 'set') {
return false;
}
}
return true;
})());
this._getter = config.get;
this._setter = config.set;
}
Ember.assert('Computed properties must receive a getter or a setter, you passed none.', !!this._getter || !!this._setter);
this._dependentKeys = undefined;
this._suspended = undefined;
this._meta = undefined;
Expand Down
23 changes: 23 additions & 0 deletions packages/ember-metal/tests/computed_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,29 @@ QUnit.test('computed property should be an instance of descriptor', function() {
ok(computed(function() {}) instanceof Descriptor);
});

QUnit.test('computed properties assert the presence of a getter or setter function', function() {
expectAssertion(function() {
computed('nogetternorsetter', {});
}, 'Computed properties must receive a getter or a setter, you passed none.');
});

QUnit.test('computed properties check for the presence of a function or configuration object', function() {
expectAssertion(function() {
computed('nolastargument');
}, 'Ember.computed expects a function or an object as last argument.');
});

QUnit.test('computed properties defined with an object only allow `get` and `set` keys', function() {
expectAssertion(function() {
computed({
get() {},
set() {},
other() {}
});
}, 'Config object pased to a Ember.computed can only contain `get` or `set` keys.');
});


QUnit.test('defining computed property should invoke property on get', function() {
var obj = {};
var count = 0;
Expand Down

0 comments on commit b3b6e47

Please sign in to comment.