-
Notifications
You must be signed in to change notification settings - Fork 142
/
Copy pathreactor-fns-tests.js
579 lines (520 loc) · 15.6 KB
/
reactor-fns-tests.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
/*eslint-disable one-var, comma-dangle*/
import { Map, Set, is } from 'immutable'
import { Store } from '../src/main'
import * as fns from '../src/reactor/fns'
import { ReactorState, ObserverState } from '../src/reactor/records'
import { toImmutable } from '../src/immutable-helpers'
describe('reactor fns', () => {
describe('#registerStores', () => {
let reactorState
let store1
let store2
let nextReactorState
beforeEach(() => {
reactorState = new ReactorState()
store1 = new Store({
getInitialState() {
return toImmutable({
foo: 'bar',
})
},
})
store2 = new Store({
getInitialState() {
return 2
},
})
nextReactorState = fns.registerStores(reactorState, {
store1,
store2,
})
})
it('should update reactorState.stores', () => {
const expectedStores = Map({
store1,
store2,
})
expect(is(nextReactorState.get('stores'), expectedStores)).toBe(true)
})
it('should update reactorState.state with the store initial state', () => {
const result = nextReactorState.get('state')
const expected = Map({
store1: Map({
foo: 'bar',
}),
store2: 2,
})
expect(is(result, expected)).toBe(true)
})
it('should update reactorState.dirtyStores', () => {
const result = nextReactorState.get('dirtyStores')
const expected = Set.of('store1', 'store2')
expect(is(result, expected)).toBe(true)
})
it('should update reactorState.dirtyStores', () => {
const result = nextReactorState.get('storeStates')
const expected = Map({
store1: 1,
store2: 1,
})
expect(is(result, expected)).toBe(true)
})
it('should increment reactorState.dispatchId', () => {
const result = nextReactorState.get('dispatchId')
const expected = 1
expect(result).toBe(expected)
})
})
describe('#registerStores', () => {
let reactorState
let store1
let store2
let newStore1
let originalReactorState
let nextReactorState
beforeEach(() => {
reactorState = new ReactorState()
store1 = new Store({
getInitialState() {
return toImmutable({
foo: 'bar',
})
},
})
store2 = new Store({
getInitialState() {
return 2
},
})
newStore1 = new Store({
getInitialState() {
return toImmutable({
foo: 'newstore',
})
},
})
originalReactorState = fns.registerStores(reactorState, {
store1,
store2,
})
nextReactorState = fns.replaceStores(originalReactorState, {
store1: newStore1
})
})
it('should update reactorState.stores', () => {
const expectedReactorState = originalReactorState.set('stores', Map({
store1: newStore1,
store2: store2,
}))
expect(is(nextReactorState, expectedReactorState)).toBe(true)
})
})
describe('#dispatch', () => {
let initialReactorState, store1, store2
let nextReactorState
beforeEach(() => {
const reactorState = new ReactorState()
store1 = new Store({
getInitialState() {
return toImmutable({
foo: 'bar',
})
},
initialize() {
this.on('set1', (state, payload) => state.set('foo', payload))
},
})
store2 = new Store({
getInitialState() {
return 2
},
initialize() {
this.on('set2', (state, payload) => payload)
},
})
initialReactorState = fns.resetDirtyStores(
fns.registerStores(reactorState, { store1, store2 })
)
})
describe('when dispatching an action that updates 1 store', () => {
beforeEach(() => {
nextReactorState = fns.dispatch(initialReactorState, 'set2', 3)
})
it('should increment reactorState.dispatchId', () => {
const result = nextReactorState.get('dispatchId')
const expected = 2
expect(result).toBe(expected)
})
it('should update state', () => {
const result = nextReactorState.get('state')
const expected = Map({
store1: Map({
foo: 'bar',
}),
store2: 3,
})
expect(is(result, expected)).toBe(true)
})
it('should update dirtyStores', () => {
const result = nextReactorState.get('dirtyStores')
const expected = Set.of('store2')
expect(is(result, expected)).toBe(true)
})
it('should update storeStates', () => {
const result = nextReactorState.get('storeStates')
const expected = Map({
store1: 1,
store2: 2,
})
expect(is(result, expected)).toBe(true)
})
})
describe('when dispatching an action that doesn\'t update stores', () => {
beforeEach(() => {
nextReactorState = fns.dispatch(initialReactorState, 'noop', {})
})
it('should not increment reactorState.dispatchId', () => {
const result = nextReactorState.get('dispatchId')
const expected = 2
expect(result).toBe(expected)
})
it('should not update state', () => {
const result = nextReactorState.get('state')
const expected = Map({
store1: Map({
foo: 'bar',
}),
store2: 2,
})
expect(is(result, expected)).toBe(true)
})
it('should not update dirtyStores', () => {
const result = nextReactorState.get('dirtyStores')
const expected = Set()
expect(is(result, expected)).toBe(true)
})
it('should not update storeStates', () => {
const result = nextReactorState.get('storeStates')
const expected = Map({
store1: 1,
store2: 1,
})
expect(is(result, expected)).toBe(true)
})
})
})
describe('#loadState', () => {
let initialReactorState, nextReactorState, store1, store2
beforeEach(() => {
const stateToLoad = {
store1: {
foo: 'baz',
},
// invalid storekey -> ignore
store3: 'wtf',
}
const reactorState = new ReactorState()
store1 = new Store({
getInitialState() {
return toImmutable({
foo: 'bar',
})
},
deserialize(state) {
return toImmutable(state)
},
})
store2 = new Store({
getInitialState() {
return 2
},
})
initialReactorState = fns.resetDirtyStores(
fns.registerStores(reactorState, { store1, store2 })
)
nextReactorState = fns.loadState(initialReactorState, stateToLoad)
})
it('should update state', () => {
const result = nextReactorState.get('state')
const expected = Map({
store1: Map({
foo: 'baz',
}),
store2: 2,
})
expect(is(expected, result)).toBe(true)
})
it('should update dirtyStores', () => {
const result = nextReactorState.get('dirtyStores')
const expected = Set.of('store1')
expect(is(expected, result)).toBe(true)
})
it('should update storeStates', () => {
const result = nextReactorState.get('storeStates')
const expected = Map({
store1: 2,
store2: 1,
})
expect(is(expected, result)).toBe(true)
})
})
describe('#reset', () => {
let initialReactorState, nextReactorState, store1, store2
beforeEach(() => {
const reactorState = new ReactorState()
store1 = new Store({
getInitialState() {
return toImmutable({
foo: 'bar',
})
},
initialize() {
this.on('set1', (state, payload) => state.set('foo', payload))
},
})
store2 = new Store({
getInitialState() {
return 2
},
handleReset() {
// override reset method and return different value
return 3
},
})
initialReactorState = fns.resetDirtyStores(
fns.registerStores(reactorState, { store1, store2, })
)
// perform a dispatch then reset
nextReactorState = fns.reset(
fns.dispatch(initialReactorState, 'set1', 'baz')
)
})
it('should return state back to its reset value', () => {
const result = nextReactorState.get('state')
const expected = Map({
store1: Map({
foo: 'bar',
}),
store2: 3,
})
expect(is(expected, result)).toBe(true)
})
it('should reset dirtyStores', () => {
const result = nextReactorState.get('dirtyStores')
const expected = Set()
expect(is(expected, result)).toBe(true)
})
it('should update storeStates', () => {
const result = nextReactorState.get('storeStates')
const expected = Map({
store1: 3,
store2: 2,
})
expect(is(expected, result)).toBe(true)
})
})
describe('#addObserver', () => {
let initialObserverState, nextObserverState, entry, handler, getter
describe('when observing the identity getter', () => {
beforeEach(() => {
getter = [[], x => x]
handler = function() {}
initialObserverState = new ObserverState()
const result = fns.addObserver(initialObserverState, getter, handler)
nextObserverState = result.observerState
entry = result.entry
})
it('should update the "any" observers', () => {
const expected = Set.of(1)
const result = nextObserverState.get('any')
expect(is(expected, result)).toBe(true)
})
it('should not update the "store" observers', () => {
const expected = Map({})
const result = nextObserverState.get('stores')
expect(is(expected, result)).toBe(true)
})
it('should increment the nextId', () => {
const expected = 2
const result = nextObserverState.get('nextId')
expect(is(expected, result)).toBe(true)
})
it('should update the observerMap', () => {
const expected = Map([
[1, Map({
id: 1,
storeDeps: Set(),
getterKey: getter,
getter: getter,
handler: handler,
})],
])
const result = nextObserverState.get('observersMap')
expect(is(expected, result)).toBe(true)
})
it('should return a valid entry', () => {
const expected = Map({
id: 1,
storeDeps: Set(),
getterKey: getter,
getter: getter,
handler: handler,
})
expect(is(expected, entry)).toBe(true)
})
})
describe('when observing a store backed getter', () => {
beforeEach(() => {
getter = [
['store1'],
['store2'],
(a, b) => a + b
]
handler = function() {}
initialObserverState = new ObserverState()
const result = fns.addObserver(initialObserverState, getter, handler)
nextObserverState = result.observerState
entry = result.entry
})
it('should not update the "any" observers', () => {
const expected = Set.of()
const result = nextObserverState.get('any')
expect(is(expected, result)).toBe(true)
})
it('should not update the "store" observers', () => {
const expected = Map({
store1: Set.of(1),
store2: Set.of(1),
})
const result = nextObserverState.get('stores')
expect(is(expected, result)).toBe(true)
})
it('should increment the nextId', () => {
const expected = 2
const result = nextObserverState.get('nextId')
expect(is(expected, result)).toBe(true)
})
it('should update the observerMap', () => {
const expected = Map([
[1, Map({
id: 1,
storeDeps: Set.of('store1', 'store2'),
getterKey: getter,
getter: getter,
handler: handler,
})]
])
const result = nextObserverState.get('observersMap')
expect(is(expected, result)).toBe(true)
})
it('should return a valid entry', () => {
const expected = Map({
id: 1,
storeDeps: Set.of('store1', 'store2'),
getterKey: getter,
getter: getter,
handler: handler,
})
expect(is(expected, entry)).toBe(true)
})
})
})
describe('#removeObserver', () => {
let initialObserverState, nextObserverState, getter1, getter2, handler1, handler2, handler3
beforeEach(() => {
handler1 = () => 1
handler2 = () => 2
handler3 = () => 3
getter1 = [
['store1'],
['store2'],
(a, b) => a + b
]
getter2 = [[], x => x]
const initialObserverState1 = new ObserverState()
const result1 = fns.addObserver(initialObserverState1, getter1, handler1)
const initialObserverState2 = result1.observerState
const result2 = fns.addObserver(initialObserverState2, getter1, handler2)
const initialObserverState3 = result2.observerState
const result3 = fns.addObserver(initialObserverState3, getter2, handler3)
initialObserverState = result3.observerState
})
describe('when removing by getter', () => {
it('should return a new ObserverState with all entries containing the getter removed', () => {
nextObserverState = fns.removeObserver(initialObserverState, getter1)
const expected = Map({
any: Set.of(3),
stores: Map({
store1: Set(),
store2: Set(),
}),
nextId: 4,
observersMap: Map([
[3, Map({
id: 3,
storeDeps: Set(),
getterKey: getter2,
getter: getter2,
handler: handler3,
})]
])
})
const result = nextObserverState
expect(is(expected, result)).toBe(true)
})
})
describe('when removing by getter / handler', () => {
it('should return a new ObserverState with all entries containing the getter removed', () => {
nextObserverState = fns.removeObserver(initialObserverState, getter2, handler3)
const expected = Map({
any: Set(),
stores: Map({
store1: Set.of(1, 2),
store2: Set.of(1, 2),
}),
nextId: 4,
observersMap: Map([
[1, Map({
id: 1,
storeDeps: Set.of('store1', 'store2'),
getterKey: getter1,
getter: getter1,
handler: handler1,
})],
[2, Map({
id: 2,
storeDeps: Set.of('store1', 'store2'),
getterKey: getter1,
getter: getter1,
handler: handler2,
})]
])
})
const result = nextObserverState
expect(is(expected, result)).toBe(true)
})
})
})
describe('#getDebugOption', () => {
it('should parse the option value in a reactorState', () => {
const reactorState = new ReactorState({
options: Map({
throwOnUndefinedDispatch: true,
}),
})
const result = fns.getOption(reactorState, 'throwOnUndefinedDispatch')
expect(result).toBe(true)
})
it('should throw an error if the option doesn\'t', () => {
const reactorState = new ReactorState({
options: Map({
throwOnUndefinedDispatch: true,
}),
})
expect(function() {
fns.getOption(reactorState, 'unknownOption')
}).toThrow()
})
})
})
/*eslint-enable one-var, comma-dangle*/