Skip to content

Commit

Permalink
add tests for TrackableObject
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Garrett committed Apr 3, 2019
1 parent 2e7a2cd commit 7d94591
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { EMBER_METAL_TRACKED_PROPERTIES } from '@ember/canary-features';
import { Object as EmberObject } from '@ember/-internals/runtime';
import { Object as EmberObject, A } from '@ember/-internals/runtime';
import { tracked, nativeDescDecorator as descriptor } from '@ember/-internals/metal';
import { moduleFor, RenderingTestCase, strip, runTask } from 'internal-test-helpers';
import GlimmerishComponent from '../../utils/glimmerish-component';
Expand Down Expand Up @@ -151,6 +151,33 @@ if (EMBER_METAL_TRACKED_PROPERTIES) {
this.assertText('1');
}

'@test array properties rerender when updated'() {
let NumListComponent = Component.extend({
numbers: tracked({ initializer: () => A([1, 2, 3]) }),

addNumber() {
this.numbers.pushObject(4);
},
});

this.registerComponent('num-list', {
ComponentClass: NumListComponent,
template: strip`
<button {{action this.addNumber}}>
{{#each this.numbers as |num|}}{{num}}{{/each}}
</button>
`,
});

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

this.assertText('123');

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

this.assertText('1234');
}

'@test getters update when dependent properties are invalidated'() {
let CountComponent = Component.extend({
count: tracked({ value: 0 }),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { EMBER_METAL_TRACKED_PROPERTIES } from '@ember/canary-features';
import { Object as EmberObject } from '@ember/-internals/runtime';
import { Object as EmberObject, A } from '@ember/-internals/runtime';
import { tracked, nativeDescDecorator as descriptor } from '@ember/-internals/metal';
import { moduleFor, RenderingTestCase, strip, runTask } from 'internal-test-helpers';

Expand Down Expand Up @@ -138,6 +138,37 @@ if (EMBER_METAL_TRACKED_PROPERTIES) {
assert.strictEqual(computeCount, 2, 'compute is called exactly 2 times');
}

'@test array properties rerender when updated'() {
let NumListComponent = Component.extend({
numbers: tracked({ initializer: () => A([1, 2, 3]) }),

addNumber() {
this.numbers.pushObject(4);
},
});

this.registerComponent('num-list', {
ComponentClass: NumListComponent,
template: strip`
<button {{action this.addNumber}}>
{{join this.numbers}}
</button>
`,
});

this.registerHelper('join', ([value]) => {
return value.join(', ');
});

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

this.assertText('1, 2, 3');

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

this.assertText('1, 2, 3, 4');
}

'@test nested getters update when dependent properties are invalidated'(assert) {
let computeCount = 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 @@ -135,7 +135,7 @@ export function tracked(...args: any[]): Decorator | DecoratorPropertyDescriptor
assert(
`The options object passed to tracked() may only contain a 'value' or 'initializer' property, not both. Received: [${keys}]`,
keys.length <= 1 &&
(keys[0] === undefined || keys[0] === 'value' || keys[0] === 'undefined')
(keys[0] === undefined || keys[0] === 'value' || keys[0] === 'initializer')
);

assert(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
import { computed, defineProperty, get, set, tagForProperty, tracked } from '../..';
import {
computed,
defineProperty,
get,
set,
tagForProperty,
tracked,
notifyPropertyChange,
} from '../..';

import {
EMBER_METAL_TRACKED_PROPERTIES,
EMBER_NATIVE_DECORATOR_SUPPORT,
} from '@ember/canary-features';
import { AbstractTestCase, moduleFor } from 'internal-test-helpers';
import { track } from './support';
import { TRACKABLE_OBJECT } from '../../../utils';

if (EMBER_METAL_TRACKED_PROPERTIES && EMBER_NATIVE_DECORATOR_SUPPORT) {
moduleFor(
Expand Down Expand Up @@ -285,6 +294,52 @@ if (EMBER_METAL_TRACKED_PROPERTIES && EMBER_NATIVE_DECORATOR_SUPPORT) {

assert.equal(get(obj, 'full'), 'Tizzle Dale');
}

['@test interaction with arrays'](assert) {
class EmberObject {
@tracked array = [];
}

let obj = new EmberObject();

let tag = tagForProperty(obj, 'array');
let snapshot = tag.value();

let array = get(obj, 'array');
assert.deepEqual(array, []);
assert.equal(tag.validate(snapshot), true);

array.push(1);
notifyPropertyChange(array, '[]');
assert.equal(
tag.validate(snapshot),
false,
'invalid after pushing an object and notifying on the array'
);
}

['@test interaction with trackable objects'](assert) {
class EmberObject {
@tracked trackable = {
[TRACKABLE_OBJECT]: true,
};
}

let obj = new EmberObject();

let tag = tagForProperty(obj, 'trackable');
let snapshot = tag.value();

let trackable = get(obj, 'trackable');
assert.equal(tag.validate(snapshot), true);

set(trackable, 'foo', 123);
assert.equal(
tag.validate(snapshot),
false,
'invalid after setting a property on the object'
);
}
}
);
}
20 changes: 20 additions & 0 deletions packages/@ember/-internals/utils/tests/trackable-object-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { TRACKABLE_OBJECT, isTrackableObject } from '..';
import { moduleFor, AbstractTestCase } from 'internal-test-helpers';

moduleFor(
'@ember/-internals/utils Trackable Object',
class extends AbstractTestCase {
['@test arrays'](assert) {
assert.equal(isTrackableObject([]), true);
}

['@test classes'](assert) {
class Test {}
Test.prototype[TRACKABLE_OBJECT] = true;

let instance = new Test();

assert.equal(isTrackableObject(instance), true);
}
}
);

0 comments on commit 7d94591

Please sign in to comment.