-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsimple.test.ts
More file actions
82 lines (69 loc) · 2.05 KB
/
simple.test.ts
File metadata and controls
82 lines (69 loc) · 2.05 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
import {Input} from './simple';
test('should do a simple reactivity', () => {
const a = new Input(1);
const b = new Input(2);
const c = a.read(x => b.read(y => x + y));
expect(c.value).toBe(3);
a.value = 2;
expect(c.value).toBe(4);
});
test('should support dynamic reactivity', () => {
const a = new Input(1);
const b = new Input(2);
const c = new Input(true);
let aCount = 0;
let bCount = 0;
let cCount = 0;
const d = c.read(x => {
cCount++;
return x ? a.read(x => {aCount++; return x}) : b.read(x => {bCount++; return x});
});
expect(aCount).toBe(1);
expect(bCount).toBe(0);
expect(cCount).toBe(1);
expect(d.value).toBe(1);
// b is detached from d, so its updates should not affect d.
b.value = 101;
expect(aCount).toBe(1);
expect(bCount).toBe(0);
expect(cCount).toBe(1);
// a is still attached to d, so its updates should affect d.
a.value = 100;
expect(d.value).toBe(100);
expect(aCount).toBe(2);
expect(bCount).toBe(0);
expect(cCount).toBe(1);
// switch to b.
c.value = false;
expect(d.value).toBe(101);
expect(aCount).toBe(2);
expect(bCount).toBe(1);
expect(cCount).toBe(2);
// a is detached from d, so its updates should not affect d.
a.value = 1010;
expect(d.value).toBe(101);
expect(aCount).toBe(2);
expect(bCount).toBe(1);
expect(cCount).toBe(2);
// b is still attached to d, so its updates should affect d.
b.value = 1000;
expect(d.value).toBe(1000);
expect(aCount).toBe(2);
expect(bCount).toBe(2);
expect(cCount).toBe(2);
});
test('should have no glitches', () => {
const a = new Input('A');
const b = a.read(x => x + 'B');
const cs: string[] = [];
const c = a.read(x => b.read(y => {
const res = x + '->' + y;
cs.push(res);
return res;
}));
expect(cs).toEqual(['A->AB']);
a.value = 'a';
// should not have any intermediate values like a->AB.
expect(c.value).toBe('a->aB');
expect(cs).toEqual(['A->AB', 'a->aB']);
});