Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUGFIX canary] Ensure uninitialized tracked properties "work". #17765

Merged
merged 1 commit into from
Mar 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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