-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
314 lines (261 loc) · 7.39 KB
/
index.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*global describe, it*/
// todo - tests for invariant conditions
function timeout(t){
return new Promise(resolve => setTimeout(()=> resolve(), t));
}
require('chai').should();
import {Dis, debug} from '../src/index.js';
describe('stores', ()=>{
it('initializes with seed value', ()=>{
let dis = new Dis(),
store = dis.register({x: 1, y: 2});
store.get().should.eql({x: 1, y: 2});
// for coverage
store.subscribe();
let s2 = dis.register();
dis.dispatch('booga');
(()=> Dis()).should.throw();
});
it('responds to actions / returns current state', ()=> {
let dis = new Dis(),
s = dis.register({x: 1, y: 2},
(o, action, key, val=1) => (action === 'inc') ? Object.assign(o, {[key]: o[key] + val}) : o);
dis.dispatch('inc', 'x', 5);
s.get().x.should.eql(6);
});
it('can subscribe to changes', done => {
let dis = new Dis(),
s = dis.register({times: 0}, state => ({times: state.times + 1}));
let {dispose} = s.subscribe(()=> {
s.get().times.should.eql(1);
dispose();
done();
}, false);
dis.dispatch('gogogo');
});
it('does not emit change when state hasn\'t changed', done => {
let dis = new Dis(),
s = dis.register(3, (num, action) => (action === 'inc') ? num + 1 : num);
let {dispose} = s.subscribe(()=> done('should not fire'), false);
dis.dispatch('xyz');
dis.dispatch('xyz');
dis.dispatch('xyz');
dispose();
done();
}, false);
it('!!does not emit change when same object is mutated and returned!!!', done => {
// this is by design!
let dis = new Dis(),
s1 = dis.register({x: 0}, state => Object.assign(state, {x: state.x + 1}));
s1.subscribe(()=> {
done('should not fire');
}, false);
dis.dispatch('xyz');
// so if you're using object.assign, make sure you start with a fresh object
var s2 = dis.register({x: 0}, state => Object.assign({}, state, {x: state.x + 1}));
s2.subscribe(()=> {
done(); // will fire
}, false);
dis.dispatch('xyz');
});
it('however, you can use a custom equality check', done => {
// be careful with this.
function eql(){
return false;
}
let dis = new Dis(),
s = dis.register({x: 0}, (state) => Object.assign(state, {x: state.x + 2}), eql);
s.subscribe(newS => {
// ick, mutable shared object
newS.x.should.eql(2);
done();
}, false);
dis.dispatch('xyz');
});
it('is a react style observable', (done)=>{
let dis = new Dis(),
s = dis.register(0, x => x + 1);
var {dispose} = s.subscribe({onNext: state => (state === 1) && done() });
dis.dispatch('_');
// dis.dispatch('_');
dispose();
});
});
describe('Dis', ()=>{
it('can register|unregister stores, and send messages to all registered stores', ()=>{
var d = new Dis(),
s = d.register(0, state => state + 1);
d.dispatch('xyz'); d.dispatch('xyz'); d.dispatch('xyz');
s.get().should.eql(3);
d.unregister(s);
d.dispatch('xyz'); d.dispatch('xyz'); d.dispatch('xyz');
s.get().should.eql(3);
});
it('can waitFor stores before proceeding', ()=>{
var d = new Dis();
var s1 = d.register(0, x => x + 1);
var s3 = d.register(0, () => { d.waitFor(s1, s2); return (s1.get() + s2.get()); });
var s2 = d.register(0, x => x + 2);
d.dispatch('xyz');
s1.get().should.eql(1);
s2.get().should.eql(2);
s3.get().should.eql(3);
});
it('can detect circular dependencies', (done)=>{
var d = new Dis();
var s1 = d.register({}, o => { d.waitFor(s2); return o; });
var s2 = d.register({}, o => { d.waitFor(s1); return o; });
try{
d.dispatch('xyz');
}
catch(err){
err.should.be.ok;
done();
}
});
});
describe('act', () => {
it(`can parse descriptor objects,
and return 'action' functions at given paths,
and have dev friendly representations`, done =>{
var d = new Dis();
var messages = d.register(0, x => x + 1);
var $ = d.act({
one: '',
two: '',
three(...words) {
words.should.eql(['what', 'say', 'you']);
$.four(...words);
messages.get().should.eql(4);
done();
},
four: '',
something: ''
});
$.one();
$.two();
debug($).should.eql([
'~:one',
'~:two',
'~:three',
'~:four',
'~:something'
]);
// for coverage
$.one.done.toString();
$.one.error.toString();
$.three('what', 'say', 'you');
});
it('if an action returns a promise, it will call .done when finished', done => {
var d = new Dis();
var $ = d.act({
a: () => new Promise(resolve => resolve(true))
});
d.register({}, (o, action) => {
switch(action){
case $.a.done:
done();
return o;
default: return o;
}
});
$.a();
});
it('if an action returns a promise, it will call .error on errors', done => {
var d = new Dis();
var $ = d.act({
a: () => new Promise((resolve, reject) => { x.y.z; })
});
d.register({}, (o, action) => {
switch(action){
case $.a.error:
done();
return o;
default: return o;
}
});
$.a();
});
it('if an action is an async function, it will call .done when finished', done => {
var d = new Dis();
var $ = d.act({
b: async () => await timeout(100)
});
d.register({}, (o, action) => {
switch(action){
case $.b.done:
done();
return o;
default: return o;
}
});
$.b();
});
});
describe('record/replay', ()=> {
it('can snapshot state at any point, and goTo that point whenever', ()=> {
let d = new Dis(), r = require('../src/record').setup(d, module);
let s = d.register({x: 0}, (o, action) => {
switch(action){
case 'inc':
return {x: o.x + 1};
default:
return o;
}
});
r.snapshot();
d.dispatch('inc');
d.dispatch('inc');
r.snapshot();
d.dispatch('inc');
d.dispatch('inc');
r.snapshot();
r.goTo(0);
s.get().x.should.eql(0);
r.goTo(1);
s.get().x.should.eql(2);
r.goTo(2);
s.get().x.should.eql(4);
r.goTo(1);
s.get().x.should.eql(2);
});
it('can record a session, and replay it', done => {
let d = new Dis(), r = require('../src/record').setup(d, module), messages = 0;
let s = d.register({x: 0}, (o, action) => {
switch(action){
case 'inc':
messages++;
return {x: o.x + 1};
default:
return o;
}
});
r.record();
for(var i = 0; i < 5; i++) {
d.dispatch('inc');
}
r.stop();
s.get().x.should.eql(5);
messages.should.eql(5);
r.play().then(() => {
s.get().x.should.eql(5);
messages.should.eql(10);
r.play().then(() => {
s.get().x.should.eql(5);
messages.should.eql(15);
done();
}).catch(done);
}).catch(done);
});
});
// disabled this
// unnecessary and has the hidden effect of calling the reduce function once for no reason
// that means your stateless store now holds state, which we're trying to avoid :|
// it('can pass initial state as es6 default argument value', () => {
// let dis = new Dis(),
// store = dis.register((x = 10, a) => a === 'inc' ? x + 1 : x);
// store.get().should.eql(10);
// dis.dispatch('inc');
// dis.dispatch('inc');
// store.get().should.eql(12);
// });