-
-
Notifications
You must be signed in to change notification settings - Fork 221
/
serialize.test.ts
719 lines (638 loc) · 21.3 KB
/
serialize.test.ts
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
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
import {
Scope,
createStore,
createEvent,
createDomain,
allSettled,
combine,
fork,
hydrate,
serialize,
sample,
createEffect,
attach,
} from 'effector'
const consoleError = console.error
beforeAll(() => {
console.error = (message, ...args) => {
if (String(message).includes('onlyChanges')) return
consoleError(message, ...args)
}
})
afterAll(() => {
console.error = consoleError
})
it('serialize stores to object of sid as keys', () => {
const $a = createStore('value', {sid: 'a'})
const $b = createStore([], {sid: 'b'})
const $c = createStore(null as null | number, {sid: 'c'})
const $d = createStore(false, {sid: 'd'})
const scope = fork({
values: [
[$a, 'value2'],
[$b, []],
[$c, 0],
[$d, true],
],
})
expect(serialize(scope)).toEqual({
a: 'value2',
b: [],
c: 0,
d: true,
})
})
it('serialize stores with ignore parameter', () => {
const $a = createStore('value', {sid: 'a'})
const $b = createStore([], {sid: 'b'})
const $c = createStore(null as null | number, {sid: 'c'})
const $d = createStore(false, {sid: 'd'})
const scope = fork({
values: [
[$a, 'value2'],
[$b, []],
[$c, 0],
[$d, true],
],
})
expect(serialize(scope, {ignore: [$b, $d]})).toEqual({
a: 'value2',
c: 0,
})
})
test('serialize: ignore', async () => {
const inc = createEvent()
const $a = createStore(0, {sid: 'a', serialize: 'ignore'})
const $b = createStore(0, {sid: 'b'})
$a.on(inc, x => x + 1)
$b.on(inc, x => x + 1)
const scope = fork()
await allSettled(inc, {scope})
expect(serialize(scope)).toEqual({b: 1})
})
test('serialize: ignore with fork(values)', async () => {
const inc = createEvent()
const $a = createStore(0, {sid: 'a', serialize: 'ignore'})
const $b = createStore(0, {sid: 'b'})
$a.on(inc, x => x + 1)
$b.on(inc, x => x + 1)
const scope = fork({
values: [[$a, 100]],
})
await allSettled(inc, {scope})
expect(serialize(scope)).toEqual({b: 1})
})
test('serialize: ignore with fork(values) for unchanged stores', async () => {
const $a = createStore(0, {sid: 'a', serialize: 'ignore'})
const scope = fork({
values: [[$a, 1]],
})
expect(serialize(scope)).toStrictEqual({})
})
describe('serialize: custom', () => {
test('base case', async () => {
expect.assertions(4)
let currentScope: Scope
const aToStr = (v: number) => `${v}`
const inc = createEvent()
const $a = createStore(0, {
sid: 'a',
serialize: {
write: value => {
expect(currentScope.getState($a)).toEqual(value)
return aToStr(value)
},
read: json => {
expect(aToStr(currentScope.getState($a))).toEqual(json)
return Number(json)
},
},
})
const $b = createStore(0, {sid: 'b'})
$a.on(inc, x => x + 1)
$b.on(inc, x => x + 1)
const scope = fork()
await allSettled(inc, {scope})
currentScope = scope
const values = serialize(scope)
expect(values).toEqual({a: '1', b: 1})
const clientScope = fork({
values,
})
expect(clientScope.getState($a)).toEqual(1)
})
test('Map usecase', () => {
const $map = createStore(new Map<number, number>(), {
sid: 'map',
serialize: {
write: map => [...map.entries()],
read: jsonMap => new Map(jsonMap),
},
})
const scope = fork()
allSettled($map, {scope, params: new Map().set(1, 2).set(2, 3)})
const values = serialize(scope)
expect(values).toEqual({
map: [...new Map().set(1, 2).set(2, 3)],
})
const clientScope = fork({
values,
})
expect(clientScope.getState($map)).toEqual(new Map().set(1, 2).set(2, 3))
})
test('hydrate case', () => {
const domain = createDomain()
const $map = domain.createStore(new Map<number, number>(), {
sid: 'map',
serialize: {
write: map => [...map.entries()],
read: jsonMap => new Map(jsonMap),
},
})
const scope = fork(domain)
allSettled($map, {scope, params: new Map().set(1, 2).set(2, 3)})
const values = serialize(scope)
expect(values).toEqual({
map: [...new Map().set(1, 2).set(2, 3)],
})
const clientScope = fork(domain)
hydrate(clientScope, {values})
expect(clientScope.getState($map)).toEqual(new Map().set(1, 2).set(2, 3))
})
test('does not affect normal ref initialization', () => {
const parser = jest.fn()
const up = createEvent()
const $a = createStore(0, {
serialize: {
write: value => {
return `${value}`
},
read: _json => {
parser()
return 42
},
},
}).on(up, s => s + 1)
const scopeA = fork()
allSettled(up, {scope: scopeA})
expect(scopeA.getState($a)).toEqual(1)
const scopeB = fork({
values: [[$a, 6]],
})
allSettled(up, {scope: scopeB})
expect(scopeB.getState($a)).toEqual(7)
expect(parser).toBeCalledTimes(0)
})
})
it('serialize stores in nested domain', () => {
const app = createDomain()
const first = app.createDomain()
const second = app.createDomain()
const third = second.createDomain()
const $a = first.createStore('value', {sid: 'a'})
const $b = second.createStore([], {sid: 'b'})
const $c = third.createStore(null as null | number, {sid: 'c'})
const $d = app.createStore(false, {sid: 'd'})
const scope = fork(app, {
values: [
[$a, 'value2'],
[$b, []],
[$c, 0],
[$d, true],
],
})
expect(serialize(scope, {ignore: [$d, $a]})).toEqual({
b: [],
c: 0,
})
})
describe('onlyChanges: true', () => {
it('avoid serializing combined stores when they are not changed', async () => {
const newMessage = createEvent()
const messages = createStore(0, {sid: 'messages'}).on(
newMessage,
x => x + 1,
)
const stats = combine({messages})
const scope = fork()
expect(serialize(scope)).toEqual({})
await allSettled(newMessage, {scope})
expect(serialize(scope)).toEqual({messages: 1})
})
it('skip unchanged objects', async () => {
const newMessage = createEvent()
const messages = createStore(0, {sid: 'messages'}).on(
newMessage,
x => x + 1,
)
const scope = fork()
expect(serialize(scope)).toEqual({})
await allSettled(newMessage, {scope})
expect(serialize(scope)).toEqual({messages: 1})
})
it('keep store in serialization when it returns to default state', async () => {
const newMessage = createEvent()
const resetMessages = createEvent()
const messages = createStore(0, {sid: 'messages'})
.on(newMessage, x => x + 1)
.reset(resetMessages)
const scope = fork()
await allSettled(newMessage, {scope})
await allSettled(resetMessages, {scope})
expect(serialize(scope)).toEqual({messages: 0})
})
it('keep store in serialization when it filled with fork values', async () => {
const newMessage = createEvent()
const resetMessages = createEvent()
const messages = createStore(0, {sid: 'messages'})
.on(newMessage, x => x + 1)
.reset(resetMessages)
const scope = fork({
values: [[messages, 1]],
})
expect(serialize(scope)).toEqual({messages: 1})
await allSettled(resetMessages, {scope})
expect(serialize(scope)).toEqual({messages: 0})
})
it('keep store in serialization when it filled with hydrate values', async () => {
const app = createDomain()
const newMessage = app.createEvent()
const resetMessages = app.createEvent()
const messages = app
.createStore(0, {sid: 'messages'})
.on(newMessage, x => x + 1)
.reset(resetMessages)
const scope = fork(app)
hydrate(scope, {
values: [[messages, 0]],
})
expect(serialize(scope)).toEqual({messages: 0})
await allSettled(newMessage, {scope})
expect(serialize(scope)).toEqual({messages: 1})
})
describe('serializing combine', () => {
it('should not serialize combine in default case', async () => {
const trigger = createEvent()
const foo = createStore(0, {sid: 'foo'}).on(trigger, x => x + 1)
const bar = createStore(0, {sid: 'bar'}).on(trigger, x => x + 1)
const combined = combine({foo, bar})
const scope = fork()
await allSettled(trigger, {scope})
expect(serialize(scope)).toEqual({foo: 1, bar: 1})
})
it('should serialize combine when it updated by on', async () => {
const warn = jest.spyOn(console, 'error').mockImplementation(() => {})
const trigger = createEvent()
const foo = createStore(0, {sid: 'foo'})
const bar = createStore(0, {sid: 'bar'})
const combined = combine({foo, bar}).on(trigger, ({foo, bar}) => ({
foo: foo + 1,
bar: bar + 1,
}))
warn.mockRestore()
const sid = String(combined.sid)
const scope = fork()
await allSettled(trigger, {scope})
expect(serialize(scope)).toEqual({
[sid]: {foo: 1, bar: 1},
})
})
describe('don`t reuse values from user', () => {
test('with sample (more convenient)', async () => {
const triggerA = createEvent()
const triggerB = createEvent()
const foo = createStore(0, {sid: 'foo'})
const bar = createStore(0, {sid: 'bar'}).on(triggerB, x => x + 10)
const combined = combine({foo, bar})
sample({
clock: triggerA,
source: combined,
target: combined,
fn: ({foo, bar}) => ({
foo: foo + 1,
bar: bar + 1,
}),
})
const sid = String(combined.sid)
const scope = fork()
await allSettled(triggerA, {scope})
expect(serialize(scope)).toEqual({[sid]: {foo: 1, bar: 1}})
await allSettled(triggerB, {scope})
expect(serialize(scope)).toEqual({bar: 10, [sid]: {foo: 0, bar: 10}})
await allSettled(triggerA, {scope})
expect(serialize(scope)).toEqual({bar: 10, [sid]: {foo: 1, bar: 11}})
await allSettled(triggerB, {scope})
expect(serialize(scope)).toEqual({bar: 20, [sid]: {foo: 0, bar: 20}})
})
test('with on (less convenient)', async () => {
const warn = jest.spyOn(console, 'error').mockImplementation(() => {})
const triggerA = createEvent()
const triggerB = createEvent()
const foo = createStore(0, {sid: 'foo'})
const bar = createStore(0, {sid: 'bar'}).on(triggerB, x => x + 10)
const combined = combine({foo, bar})
combined.on(triggerA, ({foo, bar}) => ({
foo: foo + 1,
bar: bar + 1,
}))
warn.mockRestore()
const sid = String(combined.sid)
const scope = fork()
await allSettled(triggerA, {scope})
expect(serialize(scope)).toEqual({[sid]: {foo: 1, bar: 1}})
await allSettled(triggerB, {scope})
expect(serialize(scope)).toEqual({bar: 10, [sid]: {foo: 0, bar: 10}})
await allSettled(triggerA, {scope})
expect(serialize(scope)).toEqual({bar: 10, [sid]: {foo: 1, bar: 11}})
await allSettled(triggerB, {scope})
expect(serialize(scope)).toEqual({bar: 20, [sid]: {foo: 0, bar: 20}})
})
})
})
test('serialize: ignore', async () => {
const app = createDomain()
const inc = app.createEvent()
const $a = app.createStore(0, {sid: 'a', serialize: 'ignore'})
const $b = app.createStore(0, {sid: 'b'})
$a.on(inc, x => x + 1)
$b.on(inc, x => x + 1)
const scope = fork(app)
await allSettled(inc, {scope})
expect(serialize(scope, {onlyChanges: false})).toEqual({b: 1})
})
})
describe('onlyChanges: false', () => {
it('avoid serializing combined stores when they are not changed', async () => {
const app = createDomain()
const newMessage = app.createEvent()
const messages = app
.createStore(0, {sid: 'messages'})
.on(newMessage, x => x + 1)
const stats = combine({messages})
const scope = fork(app)
expect(serialize(scope, {onlyChanges: false})).toEqual({messages: 0})
await allSettled(newMessage, {scope})
expect(serialize(scope, {onlyChanges: false})).toEqual({messages: 1})
})
it('keep unchanged objects', async () => {
const app = createDomain()
const newMessage = app.createEvent()
const messages = app
.createStore(0, {sid: 'messages'})
.on(newMessage, x => x + 1)
const scope = fork(app)
expect(serialize(scope, {onlyChanges: false})).toEqual({messages: 0})
await allSettled(newMessage, {scope})
expect(serialize(scope, {onlyChanges: false})).toEqual({messages: 1})
})
it('keep store in serialization when it returns to default state', async () => {
const app = createDomain()
const newMessage = app.createEvent()
const resetMessages = app.createEvent()
const messages = app
.createStore(0, {sid: 'messages'})
.on(newMessage, x => x + 1)
.reset(resetMessages)
const scope = fork(app)
await allSettled(newMessage, {scope})
await allSettled(resetMessages, {scope})
expect(serialize(scope, {onlyChanges: false})).toEqual({messages: 0})
})
it('keep store in serialization when it filled with fork values', async () => {
const app = createDomain()
const newMessage = app.createEvent()
const resetMessages = app.createEvent()
const messages = app
.createStore(0, {sid: 'messages'})
.on(newMessage, x => x + 1)
.reset(resetMessages)
const scope = fork(app, {
values: [[messages, 1]],
})
expect(serialize(scope, {onlyChanges: false})).toEqual({messages: 1})
await allSettled(resetMessages, {scope})
expect(serialize(scope, {onlyChanges: false})).toEqual({messages: 0})
})
it('keep store in serialization when it filled with hydrate values', async () => {
const app = createDomain()
const newMessage = app.createEvent()
const resetMessages = app.createEvent()
const messages = app
.createStore(0, {sid: 'messages'})
.on(newMessage, x => x + 1)
.reset(resetMessages)
const scope = fork(app)
hydrate(scope, {
values: [[messages, 0]],
})
expect(serialize(scope, {onlyChanges: false})).toEqual({messages: 0})
await allSettled(newMessage, {scope})
expect(serialize(scope, {onlyChanges: false})).toEqual({messages: 1})
})
describe('serializing combine', () => {
it('should not serialize combine in default case', async () => {
const app = createDomain()
const trigger = app.createEvent()
const foo = app.createStore(0, {sid: 'foo'}).on(trigger, x => x + 1)
const bar = app.createStore(0, {sid: 'bar'}).on(trigger, x => x + 1)
const combined = combine({foo, bar})
const scope = fork(app)
await allSettled(trigger, {scope})
expect(serialize(scope, {onlyChanges: false})).toEqual({foo: 1, bar: 1})
})
it('should serialize combine when it updated by on', async () => {
const warn = jest.spyOn(console, 'error').mockImplementation(() => {})
const app = createDomain()
const trigger = app.createEvent()
const foo = app.createStore(0, {sid: 'foo'})
const bar = app.createStore(0, {sid: 'bar'})
const combined = combine({foo, bar}).on(trigger, ({foo, bar}) => ({
foo: foo + 1,
bar: bar + 1,
}))
warn.mockRestore()
const sid = String(combined.sid)
const scope = fork(app)
await allSettled(trigger, {scope})
expect(serialize(scope, {onlyChanges: false})).toEqual({
foo: 0,
bar: 0,
[sid]: {foo: 1, bar: 1},
})
})
describe('don`t reuse values from user', () => {
test('with sample (more convenient)', async () => {
const app = createDomain()
const triggerA = app.createEvent()
const triggerB = app.createEvent()
const foo = app.createStore(0, {sid: 'foo'})
const bar = app.createStore(0, {sid: 'bar'}).on(triggerB, x => x + 10)
const combined = combine({foo, bar})
sample({
clock: triggerA,
source: combined,
target: combined,
fn: ({foo, bar}) => ({
foo: foo + 1,
bar: bar + 1,
}),
})
const sid = String(combined.sid)
const scope = fork(app)
await allSettled(triggerA, {scope})
expect(serialize(scope, {onlyChanges: false})).toEqual({
foo: 0,
bar: 0,
[sid]: {foo: 1, bar: 1},
})
await allSettled(triggerB, {scope})
expect(serialize(scope, {onlyChanges: false})).toEqual({
foo: 0,
bar: 10,
[sid]: {foo: 0, bar: 10},
})
await allSettled(triggerA, {scope})
expect(serialize(scope, {onlyChanges: false})).toEqual({
foo: 0,
bar: 10,
[sid]: {foo: 1, bar: 11},
})
await allSettled(triggerB, {scope})
expect(serialize(scope, {onlyChanges: false})).toEqual({
foo: 0,
bar: 20,
[sid]: {foo: 0, bar: 20},
})
})
test('with on (less convenient)', async () => {
const warn = jest.spyOn(console, 'error').mockImplementation(() => {})
const app = createDomain()
const triggerA = app.createEvent()
const triggerB = app.createEvent()
const foo = app.createStore(0, {sid: 'foo'})
const bar = app.createStore(0, {sid: 'bar'}).on(triggerB, x => x + 10)
const combined = combine({foo, bar})
combined.on(triggerA, ({foo, bar}) => ({
foo: foo + 1,
bar: bar + 1,
}))
warn.mockRestore()
const sid = String(combined.sid)
const scope = fork(app)
await allSettled(triggerA, {scope})
expect(serialize(scope, {onlyChanges: false})).toEqual({
foo: 0,
bar: 0,
[sid]: {foo: 1, bar: 1},
})
await allSettled(triggerB, {scope})
expect(serialize(scope, {onlyChanges: false})).toEqual({
foo: 0,
bar: 10,
[sid]: {foo: 0, bar: 10},
})
await allSettled(triggerA, {scope})
expect(serialize(scope, {onlyChanges: false})).toEqual({
foo: 0,
bar: 10,
[sid]: {foo: 1, bar: 11},
})
await allSettled(triggerB, {scope})
expect(serialize(scope, {onlyChanges: false})).toEqual({
foo: 0,
bar: 20,
[sid]: {foo: 0, bar: 20},
})
})
})
})
})
test('onlyChanges: false supported only in domain-based scopes', () => {
const scope = fork()
expect(() => {
serialize(scope, {onlyChanges: false})
}).toThrowErrorMatchingInlineSnapshot(`"scope should be created from domain"`)
})
describe('serialize: missing sids', () => {
const consoleError = console.error
beforeEach(() => {
console.error = jest.fn()
})
afterEach(() => {
console.error = consoleError
})
test('serialize: warns about missing sids', () => {
// forcing missing sid
// equals to situation, if user forgot to configure babel-plugin
// or did not install the sid manually
const $store = createStore('value', {sid: ''})
const scope = fork()
allSettled($store, {scope, params: 'scope value'})
const result = serialize(scope)
expect(result).toEqual({})
expect(scope.getState($store)).toEqual('scope value')
expect(console.error).toHaveBeenCalledWith(
'There is a store without sid in this scope, its value is omitted',
)
})
test('serialize: throws if duplicated sids', () => {
const a = createStore(0, {sid: 'sameSid'})
const b = createStore(0, {sid: 'sameSid'})
const scope = fork({
values: [
[a, 1],
[b, 1],
],
})
expect(() => serialize(scope)).toThrowErrorMatchingInlineSnapshot(
`"duplicate sid found in this scope"`,
)
})
test('serialize: doesn not warn, if no sid is missing', () => {
const $store = createStore('value')
const scope = fork()
allSettled($store, {scope, params: 'scope value'})
const result = serialize(scope)
expect(result).toEqual({
[$store.sid as string]: 'scope value',
})
expect(scope.getState($store)).toEqual('scope value')
expect(console.error).toHaveBeenCalledTimes(0)
})
test('serialize: doesn not warn on mapped or combined stores', () => {
const $store = createStore('value')
const $mapped = $store.map(s => s)
// combined stores have sids, but not used in serialize
// this trick is needed to hide combine call from plugin
const $combine = {_: combine}._($store, $mapped, (_, m) => m)
const scope = fork()
allSettled($store, {scope, params: 'scope value'})
const result = serialize(scope)
expect($combine.sid).toEqual(null)
expect(scope.getState($store)).toEqual('scope value')
expect(scope.getState($combine)).toEqual('scope value')
expect(result).toEqual({
[$store.sid as string]: 'scope value',
})
expect(console.error).toHaveBeenCalledTimes(0)
})
test('serialize: does not warn on internal store changes', async () => {
const sleep = (p: number) => new Promise(r => setTimeout(r, p))
const sleepFx = createEffect(sleep)
const $sleep = createStore(1)
const sleepAttachedFx = attach({
source: $sleep,
effect: sleep,
})
const start = createEvent<number>()
sample({
clock: start,
target: [sleepFx, sleepAttachedFx],
})
const scope = fork()
await allSettled(start, {scope, params: 1})
const result = serialize(scope)
expect(result).toMatchObject({})
expect(console.error).toBeCalledTimes(0)
})
})