-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsimple-deep.test.ts
More file actions
197 lines (157 loc) · 6 KB
/
simple-deep.test.ts
File metadata and controls
197 lines (157 loc) · 6 KB
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
import { should } from 'chai'; should();
import { SimpleDeep } from '../simple-deep';
import { State } from '../state';
import { testStateSpec } from './state.spec';
describe('SimpleDeep', () => {
describe('state-like behaviour', () => {
testStateSpec((...args: []) => new SimpleDeep(new State(...args)));
});
describe('.sub()', () => {
it('should be an instance of `SimpleDeep`', () => {
let s = new SimpleDeep(new State());
s.sub('x').should.be.instanceof(SimpleDeep);
});
it('should have the value of given index on parent state.', () => {
let s = new SimpleDeep(new State([42, 43, 44]));
let sub = s.sub(1).bind();
sub.value.should.equal(43);
s.value = [42, 45, 44];
sub.value.should.equal(45);
});
it('should reemit when the value of given index on parent state changes.', () => {
let s = new SimpleDeep(new State([42, 43, 44]));
let sub = s.sub(1).bind();
let r = 0; sub.subscribe(() => r++);
r.should.equal(1); // --> initial value
s.value = [45, 43, 46];
r.should.equal(1); // --> no change
s.value = [45, 44, 46];
r.should.equal(2); // --> change
});
it('should cause the parent state to reemit when a bound index sub\'s value changes.', () => {
let s = new SimpleDeep(new State([42, 43, 44]));
let sub = s.sub(1).bind();
let r = 0; s.subscribe(() => r++);
r.should.equal(1);
sub.value = 46;
s.value.should.eql([42, 46, 44]);
r.should.equal(2);
});
it('should not cause a reemit on parent when it receives a down-propagated value.', () => {
let s = new SimpleDeep(new State([42, 43, 44]));
s.sub(1).bind();
let r = 0; s.subscribe(() => r++);
r.should.equal(1);
s.value = [42, 45, 44];
r.should.equal(2);
});
it('should sync value of different subs with same index.', () => {
let s = new SimpleDeep(new State([42, 43, 44]));
let sub = s.sub(1).bind();
let sub2 = s.sub(1).bind();
sub.value = 46;
sub2.value.should.equal(46);
});
it('should efficiently sync value of different subs with same index.', () => {
let s = new SimpleDeep(new State([42, 43, 44]));
let sub = s.sub(1).bind();
let sub2 = s.sub(1).bind();
let r = 0; sub2.subscribe(() => r++);
r.should.equal(1); // --> initial value
s.value = [46, 43, 47];
r.should.equal(1); // --> no change
sub.value = 46;
r.should.equal(2); // --> change
});
it('should have the value of given property on parent state.', () => {
let s = new SimpleDeep(new State('hellow'));
let sub = s.sub('length').bind();
sub.value.should.equal(6);
s.value = 'world';
sub.value.should.equal(5);
});
it('should reemit when the value of given property on parent state changes.', () => {
let s = new SimpleDeep(new State('hellow'));
let sub = s.sub('length').bind();
let r = 0; sub.subscribe(() => r++);
r.should.equal(1); // --> initial value
s.value = 'world!';
r.should.equal(1); // --> no change
s.value = 'world';
r.should.equal(2); // --> change
});
it('should cause the parent state to reemit when a property index sub\'s value changes.', () => {
let s = new SimpleDeep(new State({x: 2, y: 3}));
let sub = s.sub('x').bind();
let r = 0; s.subscribe(() => r++);
r.should.equal(1);
sub.value = 46;
s.value.should.eql({x: 46, y: 3});
r.should.equal(2);
});
it('should sync value of different subs with same index.', () => {
let s = new SimpleDeep(new State({x: 2, y: 3}));
let sub = s.sub('x').bind();
let sub2 = s.sub('x').bind();
sub.value = 46;
sub2.value.should.equal(46);
});
it('should efficiently sync value of different subs with same index.', () => {
let s = new SimpleDeep(new State({x: 2, y: 3}));
let sub = s.sub('x').bind();
let sub2 = s.sub('x').bind();
let r = 0; sub2.subscribe(() => r++);
r.should.equal(1); // --> initial value
s.value = {x: 2, y: 42};
r.should.equal(1); // --> no change
sub.value = 46;
r.should.equal(2); // --> change
});
it('should propagate changes in grandchild states back to the grandparent state as well.', () => {
let gp = new SimpleDeep(new State({x : {y : 3}}));
let c = gp.sub('x').bind();
let gc = c.sub('y').bind();
let r = 0; gp.subscribe(() => r++);
r.should.equal(1); // --> initial value
let r2 = 0; c.subscribe(() => r2++);
r2.should.equal(1); // --> initial value
gc.value = 4;
r2.should.equal(2); // --> change
r.should.equal(2); // --> change
c.value.should.eql({y: 4});
gp.value.should.eql({x : { y : 4 }});
});
it('should key values of grandchild states sync.', () => {
let gp = new SimpleDeep(new State({x : {y : 3}}));
let gc1 = gp.sub('x').bind().sub('y').bind();
let gc2 = gp.sub('x').bind().sub('y').bind();
gc1.value = 4;
gc2.value.should.equal(4);
});
it('should sync values of grandchild states efficiently.', () => {
let gp = new SimpleDeep(new State({x : {y : 3}}));
let r = 0; gp.subscribe(() => r++);
let c1 = gp.sub('x').bind(); let rc1 = 0; c1.subscribe(() => rc1++);
let gc1 = c1.sub('y').bind(); let rgc1 = 0; gc1.subscribe(() => rgc1++);
let c2 = gp.sub('x').bind(); let rc2 = 0; c2.subscribe(() => rc2++);
let gc2 = c2.sub('y').bind(); let rgc2 = 0; gc2.subscribe(() => rgc2++);
r.should.equal(1); // --> initial value
rc1.should.equal(1);
rgc1.should.equal(1);
rc2.should.equal(1);
rgc2.should.equal(1);
c2.value = {y: 4};
r.should.equal(2);
rc1.should.equal(2);
rgc1.should.equal(2);
rc2.should.equal(2);
rgc2.should.equal(2);
gc1.value = 5;
r.should.equal(3);
rc1.should.equal(3);
rgc1.should.equal(3);
rc2.should.equal(3);
rgc2.should.equal(3);
});
});
});