-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #115 from glimmerjs/autotrack
Autotrack, take 2
- Loading branch information
Showing
3 changed files
with
175 additions
and
29 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
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 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 |
---|---|---|
|
@@ -172,15 +172,14 @@ test('can track a computed property', (assert) => { | |
|
||
test('tracked computed properties are invalidated when their dependencies are invalidated', (assert) => { | ||
class TrackedPerson { | ||
@tracked('fullName') | ||
get salutation() { | ||
@tracked get salutation() { | ||
return `Hello, ${this.fullName}!`; | ||
} | ||
|
||
@tracked('firstName', 'lastName') | ||
get fullName() { | ||
@tracked get fullName() { | ||
return `${this.firstName} ${this.lastName}`; | ||
} | ||
|
||
set fullName(fullName) { | ||
let [firstName, lastName] = fullName.split(' '); | ||
this.firstName = firstName; | ||
|
@@ -192,8 +191,8 @@ test('tracked computed properties are invalidated when their dependencies are in | |
} | ||
|
||
let obj = new TrackedPerson(); | ||
assert.strictEqual(obj.salutation, 'Hello, Tom Dale!'); | ||
assert.strictEqual(obj.fullName, 'Tom Dale'); | ||
assert.strictEqual(obj.salutation, 'Hello, Tom Dale!', `the saluation field is valid`); | ||
assert.strictEqual(obj.fullName, 'Tom Dale', `the fullName field is valid`); | ||
|
||
let tag = tagForProperty(obj, 'salutation'); | ||
let snapshot = tag.value(); | ||
|
@@ -219,6 +218,83 @@ test('tracked computed properties are invalidated when their dependencies are in | |
assert.strictEqual(tag.validate(snapshot), true); | ||
}); | ||
|
||
test('nested @tracked in multiple objects', (assert) => { | ||
class TrackedPerson { | ||
@tracked get salutation() { | ||
return `Hello, ${this.fullName}!`; | ||
} | ||
|
||
@tracked get fullName(): string { | ||
return `${this.firstName} ${this.lastName}`; | ||
} | ||
|
||
set fullName(fullName: string) { | ||
let [firstName, lastName] = fullName.split(' '); | ||
this.firstName = firstName; | ||
this.lastName = lastName; | ||
} | ||
|
||
toString() { | ||
return this.fullName; | ||
} | ||
|
||
@tracked firstName = 'Tom'; | ||
@tracked lastName = 'Dale'; | ||
} | ||
|
||
class TrackedContact { | ||
@tracked email: string; | ||
@tracked person: TrackedPerson; | ||
|
||
constructor(person: TrackedPerson, email: string) { | ||
this.person = person; | ||
this.email = email; | ||
} | ||
|
||
@tracked get contact(): string { | ||
return `${this.person} @ ${this.email}`; | ||
} | ||
} | ||
|
||
let obj = new TrackedContact(new TrackedPerson(), '[email protected]'); | ||
assert.strictEqual(obj.contact, 'Tom Dale @ [email protected]', `the contact field is valid`); | ||
assert.strictEqual(obj.person.fullName, 'Tom Dale', `the fullName field is valid`); | ||
let person = obj.person; | ||
|
||
let tag = tagForProperty(obj, 'contact'); | ||
let snapshot = tag.value(); | ||
assert.ok(tag.validate(snapshot), 'tag should be valid to start'); | ||
|
||
person.firstName = 'Edsger'; | ||
person.lastName = 'Dijkstra'; | ||
assert.strictEqual(tag.validate(snapshot), false, 'tag is invalidated after nested dependency is set'); | ||
assert.strictEqual(person.fullName, 'Edsger Dijkstra'); | ||
assert.strictEqual(obj.contact, 'Edsger Dijkstra @ [email protected]'); | ||
|
||
snapshot = tag.value(); | ||
assert.strictEqual(tag.validate(snapshot), true); | ||
|
||
person.fullName = 'Alan Kay'; | ||
assert.strictEqual(tag.validate(snapshot), false, 'tag is invalidated after chained dependency is set'); | ||
assert.strictEqual(person.fullName, 'Alan Kay'); | ||
assert.strictEqual(person.firstName, 'Alan'); | ||
assert.strictEqual(person.lastName, 'Kay'); | ||
assert.strictEqual(obj.contact, 'Alan Kay @ [email protected]'); | ||
|
||
snapshot = tag.value(); | ||
assert.strictEqual(tag.validate(snapshot), true); | ||
|
||
obj.email = "[email protected]"; | ||
assert.strictEqual(tag.validate(snapshot), false, 'tag is invalidated after chained dependency is set'); | ||
assert.strictEqual(person.fullName, 'Alan Kay'); | ||
assert.strictEqual(person.firstName, 'Alan'); | ||
assert.strictEqual(person.lastName, 'Kay'); | ||
assert.strictEqual(obj.contact, 'Alan Kay @ [email protected]'); | ||
|
||
snapshot = tag.value(); | ||
assert.strictEqual(tag.validate(snapshot), true); | ||
}); | ||
|
||
module('[@glimmer/component] Tracked Property Warning in Development Mode'); | ||
|
||
if (DEBUG) { | ||
|