-
-
Notifications
You must be signed in to change notification settings - Fork 88
/
signal.test.tsx
684 lines (552 loc) · 15.8 KB
/
signal.test.tsx
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
import { signal, computed, effect, batch } from "@preact/signals-core";
describe("signal", () => {
it("should return value", () => {
const v = [1, 2];
const s = signal(v);
expect(s.value).to.equal(v);
});
it("should support .toString()", () => {
const s = signal(123);
expect(s.toString()).equal("123");
});
describe(".peek()", () => {
it("should get value", () => {
const s = signal(1);
expect(s.peek()).equal(1);
});
it("should not trigger a read", () => {
const s = signal(1);
const spy = sinon.spy(() => {
// When we trigger a read this would cause an infinite loop
s.peek();
});
effect(spy);
s.value = 2;
expect(spy).to.be.calledOnce;
});
it("should refresh value if stale", () => {
const a = signal(1);
const b = computed(() => a.value);
const dispose = effect(() => {
b.value;
});
dispose();
a.value = 2;
expect(b.peek()).to.equal(2);
});
});
});
describe("effect()", () => {
it("should init with value", () => {
const s = signal(123);
const spy = sinon.spy(() => s.value);
effect(spy);
expect(spy).to.be.called;
expect(spy).to.returned(123);
});
it("should subscribe to signals", () => {
const s = signal(123);
const spy = sinon.spy(() => s.value);
effect(spy);
spy.resetHistory();
s.value = 42;
expect(spy).to.be.called;
expect(spy).to.returned(42);
});
it("should subscribe to multiple signals", () => {
const a = signal("a");
const b = signal("b");
const spy = sinon.spy(() => a.value + " " + b.value);
effect(spy);
spy.resetHistory();
a.value = "aa";
b.value = "bb";
expect(spy).to.returned("aa bb");
});
it("should dispose of subscriptions", () => {
const a = signal("a");
const b = signal("b");
const spy = sinon.spy(() => a.value + " " + b.value);
const dispose = effect(spy);
spy.resetHistory();
dispose();
expect(spy).not.to.be.called;
a.value = "aa";
b.value = "bb";
expect(spy).not.to.be.called;
});
it("should unsubscribe from signal", () => {
const s = signal(123);
const spy = sinon.spy(() => s.value);
const unsub = effect(spy);
spy.resetHistory();
unsub();
s.value = 42;
expect(spy).not.to.be.called;
});
it("should batch writes", () => {
const a = signal("a");
const spy = sinon.spy(() => a.value);
effect(spy);
spy.resetHistory();
effect(() => {
a.value = "aa";
a.value = "aaa";
});
expect(spy).to.be.calledOnce;
});
it("should throw on cycles", () => {
const a = signal(0);
let i = 0;
const fn = () =>
effect(() => {
// Prevent test suite from spinning if limit is not hit
if (i++ > 10) {
throw new Error("test failed");
}
a.value;
a.value = NaN;
});
expect(fn).to.throw(/Cycle detected/);
});
});
describe("computed()", () => {
it("should return value", () => {
const a = signal("a");
const b = signal("b");
const c = computed(() => a.value + b.value);
expect(c.value).to.equal("ab");
});
it("should return updated value", () => {
const a = signal("a");
const b = signal("b");
const c = computed(() => a.value + b.value);
expect(c.value).to.equal("ab");
a.value = "aa";
expect(c.value).to.equal("aab");
});
it("should conditionally unsubscribe from signals", () => {
const a = signal("a");
const b = signal("b");
const cond = signal(true);
const spy = sinon.spy(() => {
return cond.value ? a.value : b.value;
});
const c = computed(spy);
expect(c.value).to.equal("a");
expect(spy).to.be.calledOnce;
b.value = "bb";
expect(c.value).to.equal("a");
expect(spy).to.be.calledOnce;
cond.value = false;
expect(c.value).to.equal("bb");
expect(spy).to.be.calledTwice;
spy.resetHistory();
a.value = "aaa";
expect(c.value).to.equal("bb");
expect(spy).not.to.be.called;
});
describe("graph updates", () => {
it("should run computeds once for multiple dep changes", async () => {
const a = signal("a");
const b = signal("b");
const compute = sinon.spy(() => {
// debugger;
return a.value + b.value;
});
const c = computed(compute);
expect(c.value).to.equal("ab");
expect(compute).to.have.been.calledOnce;
compute.resetHistory();
a.value = "aa";
expect(compute).to.have.been.calledOnce;
});
it("should drop A->B->A updates", async () => {
const a = signal(2);
const b = computed(() => a.value - 1);
const c = computed(() => a.value + 1);
const d = computed(() => a.value + b.value);
const compute = sinon.spy(() => "d: " + d.value);
const e = computed(compute);
// Trigger read
e.value;
expect(compute).to.have.been.calledOnce;
compute.resetHistory();
a.value = 4;
expect(compute).to.have.been.calledOnce;
});
it("should only update every signal once (diamond graph)", () => {
// In this scenario "D" should only update once when "A" receives
// an update. This is sometimes referred to as the "diamond" scenario.
// A
// / \
// B C
// \ /
// D
const a = signal("a");
const b = computed(() => a.value);
const c = computed(() => a.value);
const spy = sinon.spy(() => b.value + " " + c.value);
const d = computed(spy);
expect(d.value).to.equal("a a");
expect(spy).to.be.calledOnce;
a.value = "aa";
expect(d.value).to.equal("aa aa");
expect(spy).to.be.calledTwice;
});
it("should only update every signal once (diamond graph + tail)", () => {
// "E" will be likely updated twice if our mark+sweep logic is buggy.
// A
// / \
// B C
// \ /
// D
// |
// E
const a = signal("a");
const b = computed(() => a.value);
const c = computed(() => a.value);
const d = computed(() => b.value + " " + c.value);
const spy = sinon.spy(() => d.value);
const e = computed(spy);
expect(e.value).to.equal("a a");
expect(spy).to.be.calledOnce;
a.value = "aa";
expect(e.value).to.equal("aa aa");
expect(spy).to.be.calledTwice;
});
it("should bail out if result is the same", () => {
// Bail out if value of "B" never changes
// A->B->C
const a = signal("a");
const b = computed(() => {
a.value;
return "foo";
});
const spy = sinon.spy(() => b.value);
const c = computed(spy);
expect(c.value).to.equal("foo");
expect(spy).to.be.calledOnce;
a.value = "aa";
expect(c.value).to.equal("foo");
expect(spy).to.be.calledOnce;
});
it("should only update every signal once (jagged diamond graph + tails)", () => {
// "F" and "G" will be likely updated twice if our mark+sweep logic is buggy.
// A
// / \
// B C
// | |
// | D
// \ /
// E
// / \
// F G
const a = signal("a");
const b = computed(() => a.value);
const c = computed(() => a.value);
const d = computed(() => c.value);
const eSpy = sinon.spy(() => b.value + " " + d.value);
const e = computed(eSpy);
const fSpy = sinon.spy(() => e.value);
const f = computed(fSpy);
const gSpy = sinon.spy(() => e.value);
const g = computed(gSpy);
expect(f.value).to.equal("a a");
expect(fSpy).to.be.calledOnce;
expect(g.value).to.equal("a a");
expect(gSpy).to.be.calledOnce;
eSpy.resetHistory();
fSpy.resetHistory();
gSpy.resetHistory();
a.value = "b";
expect(e.value).to.equal("b b");
expect(eSpy).to.be.calledOnce;
expect(f.value).to.equal("b b");
expect(fSpy).to.be.calledOnce;
expect(g.value).to.equal("b b");
expect(gSpy).to.be.calledOnce;
eSpy.resetHistory();
fSpy.resetHistory();
gSpy.resetHistory();
a.value = "c";
expect(e.value).to.equal("c c");
expect(eSpy).to.be.calledOnce;
expect(f.value).to.equal("c c");
expect(fSpy).to.be.calledOnce;
expect(g.value).to.equal("c c");
expect(gSpy).to.be.calledOnce;
// top to bottom
expect(eSpy).to.have.been.calledBefore(fSpy);
// right to left
expect(fSpy).to.have.been.calledBefore(gSpy);
});
it("should only subscribe to signals listened to", () => {
// *A
// / \
// *B C <- we don't listen to C
const a = signal("a");
const b = computed(() => a.value);
const spy = sinon.spy(() => a.value);
computed(spy);
expect(b.value).to.equal("a");
expect(spy).not.to.be.called;
a.value = "aa";
expect(b.value).to.equal("aa");
expect(spy).not.to.be.called;
});
it("should only subscribe to signals listened to", () => {
// Here both "B" and "C" are active in the beginnning, but
// "B" becomes inactive later. At that point it should
// not receive any updates anymore.
// *A
// / \
// *B D <- we don't listen to C
// |
// *C
const a = signal("a");
const spyB = sinon.spy(() => a.value);
const b = computed(spyB);
const spyC = sinon.spy(() => b.value);
const c = computed(spyC);
const d = computed(() => a.value);
let result = "";
const unsub = effect(() => (result = c.value));
expect(result).to.equal("a");
expect(d.value).to.equal("a");
spyB.resetHistory();
spyC.resetHistory();
unsub();
a.value = "aa";
expect(spyB).not.to.be.called;
expect(spyC).not.to.be.called;
expect(d.value).to.equal("aa");
});
it("should ensure subs update even if one dep unmarks it", () => {
// In this scenario "C" always returns the same value. When "A"
// changes, "B" will update, then "C" at which point its update
// to "D" will be unmarked. But "D" must still update because
// "B" marked it. If "D" isn't updated, then we have a bug.
// A
// / \
// B *C <- returns same value every time
// \ /
// D
const a = signal("a");
const b = computed(() => a.value);
const c = computed(() => {
a.value;
return "c";
});
const spy = sinon.spy(() => b.value + " " + c.value);
const d = computed(spy);
expect(d.value).to.equal("a c");
spy.resetHistory();
a.value = "aa";
expect(spy).to.returned("aa c");
});
it("should ensure subs update even if two deps unmark it", () => {
// In this scenario both "C" and "D" always return the same
// value. But "E" must still update because "A" marked it.
// If "E" isn't updated, then we have a bug.
// A
// / | \
// B *C *D
// \ | /
// E
const a = signal("a");
const b = computed(() => a.value);
const c = computed(() => {
a.value;
return "c";
});
const d = computed(() => {
a.value;
return "d";
});
const spy = sinon.spy(() => b.value + " " + c.value + " " + d.value);
const e = computed(spy);
expect(e.value).to.equal("a c d");
spy.resetHistory();
a.value = "aa";
expect(spy).to.returned("aa c d");
});
it("should prevent invalid unmark state when called on a source signal", () => {
// Don't allow our internal logic to get in an invalid state, even through
// our own internal API. The bug this tests for is that a source signal
// will be unmarked, leading to all its subscribers `_pending` value to become
// negative. This is invalid and breaks further updates.
const a = signal("a");
const b = computed(() => a.value);
effect(() => b.value);
a._setCurrent()(true, true);
a.value = "aa";
expect(b.value).to.equal("aa");
});
});
describe("error handling", () => {
it("should throw when writing to computeds", () => {
const a = signal("a");
const b = computed(() => a.value);
const fn = () => ((b as any).value = "aa");
expect(fn).to.throw(/readonly/);
});
it("should keep graph consistent on errors during activation", () => {
const a = signal(0);
const b = computed(() => {
throw new Error("fail");
});
const c = computed(() => a.value);
expect(() => b.value).to.throw("fail");
a.value = 1;
expect(c.value).to.equal(1);
});
it("should keep graph consistent on errors in computeds", () => {
const a = signal(0);
let shouldThrow = false;
const b = computed(() => {
if (shouldThrow) throw new Error("fail");
return a.value;
});
const c = computed(() => b.value);
expect(c.value).to.equal(0);
shouldThrow = true;
let error: Error | null = null;
try {
a.value = 1;
} catch (err: any) {
error = err;
}
expect(error?.message).to.equal("fail");
// Now update signal again without throwing an error. If we didn't
// reset the subtree's PENDING counter C's value wouldn't update.
shouldThrow = false;
a.value = 2;
expect(c.value).to.equal(2);
});
it("should revert subscriptions on errors in computeds", () => {
const a = signal(1);
const b = signal(1);
const c = signal(1);
let shouldThrow = false;
const compute = sinon.spy(() => {
if (shouldThrow) {
throw new Error("fail: " + c.value);
}
return a.value + b.value;
});
const d = computed(compute);
expect(d.value).to.equal(2);
shouldThrow = true;
expect(() => {
a.value = 2;
}).to.throw();
expect(d.value).to.equal(2);
// when errors occur, we intentionally over-subscribe.
// This includes retaining subscriptions after the error:
compute.resetHistory();
try {
b.value = 2;
} catch (e) {
// may error, but not in a way we can assert over
}
expect(compute).to.have.been.called;
compute.resetHistory();
shouldThrow = false;
// Note: b.value=2 should probably also update the subgraph.
// ...but its value is already 2 from the errored computation.
// b.value = 2;
c.value = 2;
expect(compute).to.have.been.called;
expect(d.value).to.equal(4);
});
});
});
describe("batch/transaction", () => {
it("should delay writes", () => {
const a = signal("a");
const b = signal("b");
const spy = sinon.spy(() => a.value + " " + b.value);
const c = computed(spy);
spy.resetHistory();
batch(() => {
a.value = "aa";
b.value = "bb";
});
expect(c.value).to.equal("aa bb");
expect(spy).to.be.calledOnce;
});
it("should delay writes until outermost batch is complete", () => {
const a = signal("a");
const b = signal("b");
const spy = sinon.spy(() => a.value + ", " + b.value);
const c = computed(spy);
spy.resetHistory();
batch(() => {
batch(() => {
a.value += " inner";
b.value += " inner";
});
a.value += " outer";
b.value += " outer";
});
expect(c.value).to.equal("a inner outer, b inner outer");
// If the inner batch() would have flushed the update
// this spy would've been called twice.
expect(spy).to.be.calledOnce;
});
it("should read signals written to", () => {
const a = signal("a");
let result = "";
batch(() => {
a.value = "aa";
result = a.value;
});
expect(result).to.equal("aa");
});
it("should read computed signals with updated source signals", () => {
// A->B->C->D->E
const a = signal("a");
const b = computed(() => a.value);
const spyC = sinon.spy(() => b.value);
const c = computed(spyC);
const spyD = sinon.spy(() => c.value);
const d = computed(spyD);
const spyE = sinon.spy(() => b.value);
const e = computed(spyE);
spyC.resetHistory();
spyD.resetHistory();
spyE.resetHistory();
let result = "";
batch(() => {
a.value = "aa";
result = c.value;
// Since "D" isn't accessed during batching, we should not
// update it, only after batching has completed
expect(spyD).not.to.be.called;
});
expect(result).to.equal("aa");
expect(d.value).to.equal("aa");
expect(e.value).to.equal("aa");
expect(spyC).to.be.calledOnce;
expect(spyD).to.be.calledOnce;
expect(spyE).to.be.calledOnce;
});
it("should not block writes after batching completed", () => {
// If no further writes after batch() are possible, than we
// didn't restore state properly. Most likely "pending" still
// holds elements that are already processed.
const a = signal("a");
const b = signal("b");
const c = signal("c");
const d = computed(() => a.value + " " + b.value + " " + c.value);
let result;
effect(() => (result = d.value));
batch(() => {
a.value = "aa";
b.value = "bb";
});
c.value = "cc";
expect(result).to.equal("aa bb cc");
});
});