Skip to content

Commit

Permalink
[WIP] Move tracked to an actual decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
chadhietala committed Aug 15, 2018
1 parent 8974f23 commit 0d8fd80
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 91 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"typescript.tsdk": "node_modules/typescript/lib"
}
13 changes: 8 additions & 5 deletions packages/ember-metal/lib/tracked.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,14 @@ class Tracker {
@param dependencies Optional dependents to be tracked.
*/
export function tracked(
_target: object,
key: string,
descriptor: PropertyDescriptor
): PropertyDescriptor {
export function tracked(...dependencies: string[]): MethodDecorator;
export function tracked(target: any, key: any): any;
export function tracked(target: any, key: any, descriptor: PropertyDescriptor): PropertyDescriptor;
export function tracked(...dependencies: any[]): any {
let [, key, descriptor] = dependencies;

if (!descriptor) return;

if ('value' in descriptor) {
return descriptorForDataProperty(key, descriptor);
} else {
Expand Down
86 changes: 0 additions & 86 deletions packages/ember-metal/tests/tracked/get_test.js

This file was deleted.

140 changes: 140 additions & 0 deletions packages/ember-metal/tests/tracked/get_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
import { EMBER_METAL_TRACKED_PROPERTIES } from '@ember/canary-features';
import { AbstractTestCase, moduleFor } from 'internal-test-helpers';
import { get, getWithDefault, tracked } from '../..';

if (EMBER_METAL_TRACKED_PROPERTIES) {
function createObj() {
class Obj {
@tracked string = 'string';
@tracked number = 23;
@tracked boolTrue = true;
@tracked boolFalse = false;
@tracked nullValue = null;
constructor() {
this.string = 'string';
this.number = 23;
this.boolTrue = true;
this.boolFalse = false;
this.nullValue = null;
}
}

return new Obj();
}

moduleFor(
'@tracked decorator: get',
class extends AbstractTestCase {
'@test should get arbitrary properties on an object'() {
let obj = createObj();

for (let key in obj) {
this.assert.equal(get(obj, key), obj[key], key);
}
}

'@test should retrieve a number key on an object'() {
class Obj {
@tracked 1 = 'first';
constructor() {
this[1] = 'first';
}
}

let obj = new Obj();

this.assert.equal(get(obj, 1), 'first');
}

'@test should retrieve an empty key on an object'() {
class Obj {
@tracked '' = 'empty';
constructor() {
this[''] = 'empty';
}
}

let obj = new Obj();

this.assert.equal(get(obj, ''), 'empty');
}

'@test should get a @tracked path'() {
class Key {
@tracked value = 'value';
constructor() {
this.value = 'value';
}
}

class Path {
@tracked key = new Key();
constructor() {
this.key = new Key();
}
}

class Obj {
@tracked path = new Path();
constructor() {
this.path = new Path();
}
}

let obj = new Obj();

this.assert.equal(get(obj, 'path.key.value'), 'value');
}

'@test should not access a property more than once'() {
let count = 20;

class Count {
@tracked
get id() {
return ++count;
}
}

let obj = new Count();

get(obj, 'id');

this.assert.equal(count, 21);
}
}
);

moduleFor(
'@tracked decorator: getWithDefault',
class extends AbstractTestCase {
['@test should get arbitrary properties on an object']() {
let obj = createObj();

for (let key in obj) {
this.assert.equal(getWithDefault(obj, key, 'fail'), obj[key], key);
}

class Obj {
@tracked undef: string | undefined = undefined;
constructor() {
this.undef = undefined;
}
}

let obj2 = new Obj();

this.assert.equal(
getWithDefault(obj2, 'undef', 'default'),
'default',
'explicit undefined retrieves the default'
);
this.assert.equal(
getWithDefault(obj2, 'not-present', 'default'),
'default',
'non-present key retrieves the default'
);
}
}
);
}
1 change: 1 addition & 0 deletions packages/internal-test-helpers/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module "internal-test-helpers";

0 comments on commit 0d8fd80

Please sign in to comment.