-
Notifications
You must be signed in to change notification settings - Fork 5
/
test.js
211 lines (163 loc) · 4.54 KB
/
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
208
209
210
211
'use strict';
const trkl = require('./trkl');
describe('trkl observables', ()=> {
it('Provides an accessor that may be used as a getter or setter', ()=> {
const value = {};
const accessor = trkl();
accessor(value);
expect(accessor()).toBe(value);
});
it('Can be initialized with a value', ()=> {
const value = {};
const accessor = trkl(value);
expect(accessor()).toBe(value);
});
it('Can be subscribed to', ()=> {
const oldValue = {};
const newValue = {};
const listener = jest.fn();
const observable = trkl(oldValue);
observable.subscribe(listener);
observable(newValue);
expect(listener).toHaveBeenCalledWith(newValue, oldValue);
});
it('Filters duplicate subscriptions', () => {
const listener = jest.fn();
const observable = trkl(1);
observable.subscribe(listener);
observable.subscribe(listener);
observable(2);
expect(listener).toHaveBeenCalledTimes(1);
})
it('A subscriber can be run immediately', ()=> {
const value = {};
const observable = trkl(value);
const listener = jest.fn();
observable.subscribe(listener, true);
expect(listener).toHaveBeenCalledWith(value);
});
it('Can be unsubscribed from', ()=> {
const observable = trkl();
const listener = jest.fn();
observable.subscribe(listener);
observable.unsubscribe(listener);
observable(123);
expect(listener).not.toHaveBeenCalled();
});
it('Subscribers can remove themselves without disrupting others', ()=> {
const observable = trkl();
const listener1 = jest.fn(
() => observable.unsubscribe(listener1)
);
const listener2 = jest.fn();
observable.subscribe(listener1);
observable.subscribe(listener2);
observable(123);
expect(listener1).toHaveBeenCalled();
expect(listener2).toHaveBeenCalled();
observable(456);
expect(listener1).toHaveBeenCalledTimes(1);
});
it('filters out duplicate writes (if primitives)', ()=> {
const observable = trkl(1);
const symbol = Symbol();
const listener = jest.fn();
observable.subscribe(listener);
observable(1);
expect(listener).toHaveBeenCalledTimes(0);
observable('a string');
expect(listener).toHaveBeenCalledTimes(1);
observable('a string');
expect(listener).toHaveBeenCalledTimes(1);
observable(true);
expect(listener).toHaveBeenCalledTimes(2);
observable(true);
expect(listener).toHaveBeenCalledTimes(2);
observable(null);
expect(listener).toHaveBeenCalledTimes(3);
observable(null);
expect(listener).toHaveBeenCalledTimes(3);
observable(undefined);
expect(listener).toHaveBeenCalledTimes(4);
observable(undefined);
expect(listener).toHaveBeenCalledTimes(4);
observable(symbol);
expect(listener).toHaveBeenCalledTimes(5);
observable(symbol);
expect(listener).toHaveBeenCalledTimes(5);
});
it('does not filter out duplicate writes of objects or arrays', ()=> {
const array = [];
const object = {};
const observable = trkl(array);
const listener = jest.fn();
observable.subscribe(listener);
observable(array);
expect(listener).toHaveBeenCalledTimes(1);
observable(array);
expect(listener).toHaveBeenCalledTimes(2);
observable(object);
expect(listener).toHaveBeenCalledTimes(3);
observable(object);
expect(listener).toHaveBeenCalledTimes(4);
});
});
describe('A computed', ()=> {
it('Starts at the initial result of the computation', ()=> {
const a = trkl(1);
const b = trkl(2);
const c = trkl.computed(()=> {
return a() + b();
});
expect(c()).toBe(3);
});
it('Updates when its dependencies change', ()=> {
const a = trkl(1);
const b = trkl(2);
const c = trkl.computed(()=> {
return a() + b();
});
a(5);
b(5);
expect(c()).toBe(10);
});
it('Can handle dynamic dependencies', ()=> {
const a = trkl(1);
const b = trkl(2);
const relay = trkl();
const computation = trkl.computed(()=> {
if (relay()) {
return a();
} else {
return b();
}
});
relay(true);
expect(computation()).toBe(1);
relay(false);
expect(computation()).toBe(2);
});
it('Does not allow circular references', ()=> {
const a = trkl(1);
const b = trkl.computed(()=> {
return a() + 1;
});
const attachCircular = ()=> {
trkl.computed(()=> {
a(b() + 1);
})
};
expect(attachCircular).toThrow(Error('Circular computation'));
});
});
describe('trkl.from', ()=> {
it('creates an observable', ()=> {
const result = trkl.from(()=> {});
expect(typeof result).toBe('function');
});
it('passes the observable to the executor', ()=> {
const executor = jest.fn();
const observable = trkl.from(executor);
expect(executor).toHaveBeenCalledWith(observable);
});
});