Skip to content

Commit

Permalink
Merge pull request #17765 from rwjblue/ensure-initializer-is-function
Browse files Browse the repository at this point in the history
[BUGFIX canary] Ensure uninitialized tracked properties "work".
  • Loading branch information
rwjblue authored Mar 18, 2019
2 parents 1de6d16 + deb3bc7 commit d529f0b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,32 @@ if (EMBER_METAL_TRACKED_PROPERTIES) {
moduleFor(
'Component Tracked Properties',
class extends RenderingTestCase {
'@test tracked properties that are uninitialized do not throw an error'() {
let CountComponent = Component.extend({
count: tracked(),

increment() {
if (!this.count) {
this.count = 0;
}
this.count++;
},
});

this.registerComponent('counter', {
ComponentClass: CountComponent,
template: '<button {{action this.increment}}>{{this.count}}</button>',
});

this.render('<Counter />');

this.assertText('');

runTask(() => this.$('button').click());

this.assertText('1');
}

'@test tracked properties rerender when updated'() {
let CountComponent = Component.extend({
count: tracked({ value: 0 }),
Expand Down
2 changes: 1 addition & 1 deletion packages/@ember/-internals/metal/lib/tracked.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ function descriptorForField([_target, key, desc]: [

// If the field has never been initialized, we should initialize it
if (!(secretKey in this)) {
this[secretKey] = initializer !== undefined ? initializer.call(this) : undefined;
this[secretKey] = typeof initializer === 'function' ? initializer.call(this) : undefined;
}

return this[secretKey];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,16 @@ if (EMBER_METAL_TRACKED_PROPERTIES) {
new Tracked();
}, "You attempted to set a default value for first with the @tracked({ value: 'default' }) syntax. You can only use this syntax with classic classes. For native classes, you can use class initializers: @tracked field = 'default';");
}

[`@test errors if options are passed to native decorator (GH#17764)`](assert) {
class Tracked {
@tracked value;
}

let obj = new Tracked();

assert.strictEqual(obj.value, undefined, 'uninitilized value defaults to undefined');
}
}
);
} else {
Expand Down

0 comments on commit d529f0b

Please sign in to comment.