forked from emberjs/ember.js
/
chains_test.js
207 lines (167 loc) · 5.57 KB
/
chains_test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import {
addObserver,
get,
ChainNode,
finishChains,
defineProperty,
computed,
alias,
notifyPropertyChange,
watch,
unwatch,
watcherCount,
} from '..';
import { meta, peekMeta } from 'ember-meta';
import { moduleFor, AbstractTestCase } from 'internal-test-helpers';
moduleFor(
'Chains',
class extends AbstractTestCase {
['@test finishChains should properly copy chains from prototypes to instances'](assert) {
function didChange() {}
let obj = {};
addObserver(obj, 'foo.bar', null, didChange);
let childObj = Object.create(obj);
let parentMeta = meta(obj);
let childMeta = meta(childObj);
finishChains(childMeta);
assert.ok(
parentMeta.readableChains() !== childMeta.readableChains(),
'The chains object is copied'
);
}
['@test does not observe primitive values'](assert) {
let obj = {
foo: { bar: 'STRING' },
};
addObserver(obj, 'foo.bar.baz', null, function() {});
let meta = peekMeta(obj);
assert.notOk(meta._object);
}
['@test observer and CP chains'](assert) {
let obj = {};
defineProperty(obj, 'foo', computed('qux.[]', function() {}));
defineProperty(obj, 'qux', computed(function() {}));
// create DK chains
get(obj, 'foo');
// create observer chain
addObserver(obj, 'qux.length', function() {});
/*
+-----+
| qux | root CP
+-----+
^
+------+-----+
| |
+--------+ +----+
| length | | [] | chainWatchers
+--------+ +----+
observer CP(foo, 'qux.[]')
*/
// invalidate qux
notifyPropertyChange(obj, 'qux');
// CP chain is blown away
/*
+-----+
| qux | root CP
+-----+
^
+------+xxxxxx
| x
+--------+ xxxxxx
| length | x [] x chainWatchers
+--------+ xxxxxx
observer CP(foo, 'qux.[]')
*/
get(obj, 'qux'); // CP chain re-recreated
assert.ok(true, 'no crash');
}
['@test checks cache correctly'](assert) {
let obj = {};
let parentChainNode = new ChainNode(null, null, obj);
let chainNode = new ChainNode(parentChainNode, 'foo');
defineProperty(
obj,
'foo',
computed(function() {
return undefined;
})
);
get(obj, 'foo');
assert.strictEqual(chainNode.value(), undefined);
}
['@test chains are watched correctly'](assert) {
let obj = { foo: { bar: { baz: 1 } } };
watch(obj, 'foo.bar.baz');
assert.equal(watcherCount(obj, 'foo'), 1);
assert.equal(watcherCount(obj, 'foo.bar'), 0);
assert.equal(watcherCount(obj, 'foo.bar.baz'), 1);
assert.equal(watcherCount(obj.foo, 'bar'), 1);
assert.equal(watcherCount(obj.foo, 'bar.baz'), 0);
assert.equal(watcherCount(obj.foo.bar, 'baz'), 1);
unwatch(obj, 'foo.bar.baz');
assert.equal(watcherCount(obj, 'foo'), 0);
assert.equal(watcherCount(obj, 'foo.bar'), 0);
assert.equal(watcherCount(obj, 'foo.bar.baz'), 0);
assert.equal(watcherCount(obj.foo, 'bar'), 0);
assert.equal(watcherCount(obj.foo, 'bar.baz'), 0);
assert.equal(watcherCount(obj.foo.bar, 'baz'), 0);
}
['@test chains with single character keys are watched correctly'](assert) {
let obj = { a: { b: { c: 1 } } };
watch(obj, 'a.b.c');
assert.equal(watcherCount(obj, 'a'), 1);
assert.equal(watcherCount(obj, 'a.b'), 0);
assert.equal(watcherCount(obj, 'a.b.c'), 1);
assert.equal(watcherCount(obj.a, 'b'), 1);
assert.equal(watcherCount(obj.a, 'b.c'), 0);
assert.equal(watcherCount(obj.a.b, 'c'), 1);
unwatch(obj, 'a.b.c');
assert.equal(watcherCount(obj, 'a'), 0);
assert.equal(watcherCount(obj, 'a.b'), 0);
assert.equal(watcherCount(obj, 'a.b.c'), 0);
assert.equal(watcherCount(obj.a, 'b'), 0);
assert.equal(watcherCount(obj.a, 'b.c'), 0);
assert.equal(watcherCount(obj.a.b, 'c'), 0);
}
['@test writable chains is not defined more than once'](assert) {
assert.expect(0);
function didChange() {}
let obj = {
foo: {
bar: {
baz: {
value: 123,
},
},
},
};
// Setup object like a constructor, which delays initializing values in chains
let parentMeta = meta(obj);
parentMeta.proto = obj;
// Define a standard computed property, which will eventually setup dependencies
defineProperty(
obj,
'bar',
computed('foo.bar', {
get() {
return this.foo.bar;
},
})
);
// Define some aliases, which will proxy chains along
defineProperty(obj, 'baz', alias('bar.baz'));
defineProperty(obj, 'value', alias('baz.value'));
// Define an observer, which will eagerly attempt to setup chains and watch
// their values. This follows the aliases eagerly, and forces the first
// computed to actually set up its values/dependencies for chains. If
// writableChains was not already defined, this results in multiple root
// chain nodes being defined on the same object meta.
addObserver(obj, 'value', null, didChange);
let childObj = Object.create(obj);
let childMeta = meta(childObj);
finishChains(childMeta);
// If we can unwatch the root computed, then chains was not overwritten
unwatch(childObj, 'foo.bar');
}
}
);