Skip to content

Commit

Permalink
Merge pull request #6 from dfreeman/setter-only-descriptor
Browse files Browse the repository at this point in the history
Handle setter-only classic class descriptors
  • Loading branch information
pzuraq authored Apr 7, 2019
2 parents fbb3ae8 + 0316a81 commit 712e2d0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
35 changes: 35 additions & 0 deletions tests/unit/computed-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,41 @@ module('@computed', function() {
);
});

test('it works with classic classes with setter-only desc', function(assert) {
assert.expect(4);

let expectedName = 'rob jackson';

const Foo = EmberObject.extend({
first: 'rob',
last: 'jackson',

fullName: computed('first', 'last', {
set(key, name) {
assert.equal(name, expectedName, 'setter: name matches');

const [first, last] = name.split(' ');
setProperties(this, { first, last });

return name;
},
}),
});

let obj = Foo.create();

expectedName = 'yehuda katz';
set(obj, 'fullName', 'yehuda katz');

assert.equal(obj.first, 'yehuda', 'first name was updated');
assert.equal(obj.last, 'katz', 'last name was updated');
assert.strictEqual(
get(obj, 'fullName'),
expectedName,
'return value of setter is new value of property'
);
});

test('dependent key changes invalidate the computed property', function(assert) {
class Foo {
first = 'rob';
Expand Down
4 changes: 2 additions & 2 deletions vendor/ember-decorators-polyfill/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ import {
`Attempted to apply a computed property that already has a getter/setter to a ${key}, but it is a method or an accessor. If you passed @computed a function or getter/setter (e.g. \`@computed({ get() { ... } })\`), then it must be applied to a field`,
!(
desc &&
(typeof get === 'function' || typeof 'set' === 'function') &&
(typeof get === 'function' || typeof set === 'function') &&
(typeof desc.get === 'function' || typeof desc.get === 'function')
)
);
Expand All @@ -336,7 +336,7 @@ import {

assert(
`Attempted to use @computed on ${key}, but it did not have a getter or a setter. You must either pass a get a function or getter/setter to @computed directly (e.g. \`@computed({ get() { ... } })\`) or apply @computed directly to a getter/setter`,
typeof get === 'function' || typeof 'set' === 'function'
typeof get === 'function' || typeof set === 'function'
);

if (desc !== undefined) {
Expand Down

0 comments on commit 712e2d0

Please sign in to comment.