-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WIP] Move tracked to an actual decorator
- Loading branch information
1 parent
8974f23
commit 0d8fd80
Showing
5 changed files
with
152 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"typescript.tsdk": "node_modules/typescript/lib" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
); | ||
} | ||
} | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
declare module "internal-test-helpers"; |