-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathcomputed.test.ts
74 lines (63 loc) · 1.84 KB
/
computed.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
import { describe, expect, it } from "vitest";
import { Signal } from "../../src/wrapper.js";
describe("Computed", () => {
it("should work", () => {
const stateSignal = new Signal.State(1);
const computedSignal = new Signal.Computed(() => {
const f = stateSignal.get() * 2;
return f;
});
expect(computedSignal.get()).toEqual(2);
stateSignal.set(5);
expect(stateSignal.get()).toEqual(5);
expect(computedSignal.get()).toEqual(10);
});
describe("Comparison semantics", () => {
it("should track Computed by Object.is", () => {
const state = new Signal.State(1);
let value = 5;
let calls = 0;
const computed = new Signal.Computed(() => (state.get(), value));
const c2 = new Signal.Computed(() => (calls++, computed.get()));
expect(calls).toBe(0);
expect(c2.get()).toBe(5);
expect(calls).toBe(1);
state.set(2);
expect(c2.get()).toBe(5);
expect(calls).toBe(1);
value = NaN;
expect(c2.get()).toBe(5);
expect(calls).toBe(1);
state.set(3);
expect(c2.get()).toBe(NaN);
expect(calls).toBe(2);
state.set(4);
expect(c2.get()).toBe(NaN);
expect(calls).toBe(2);
});
it("applies custom equality in Computed", () => {
const s = new Signal.State(5);
let ecalls = 0;
const c1 = new Signal.Computed(() => (s.get(), 1), {
equals() {
ecalls++;
return false;
},
});
let calls = 0;
const c2 = new Signal.Computed(() => {
calls++;
return c1.get();
});
expect(calls).toBe(0);
expect(ecalls).toBe(0);
expect(c2.get()).toBe(1);
expect(ecalls).toBe(0);
expect(calls).toBe(1);
s.set(10);
expect(c2.get()).toBe(1);
expect(ecalls).toBe(1);
expect(calls).toBe(2);
});
});
});