-
Notifications
You must be signed in to change notification settings - Fork 150
Expand file tree
/
Copy pathlazy.test.ts
More file actions
278 lines (223 loc) · 6.54 KB
/
lazy.test.ts
File metadata and controls
278 lines (223 loc) · 6.54 KB
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
import { describe, it, expect } from "vitest";
import { createLazyMemo } from "../src/index.js";
import { createComputed, createEffect, createMemo, createRoot, createSignal } from "solid-js";
describe("createLazyMemo", () => {
it("won't run if not accessed", () =>
createRoot(dispose => {
const [count, setCount] = createSignal(0);
let runs = 0;
createLazyMemo(() => {
runs++;
return count();
});
setCount(1);
expect(runs, "0 in setup").toBe(0);
setTimeout(() => {
expect(runs, "0 after timeout").toBe(0);
setCount(2);
expect(runs, "0 after set in timeout").toBe(0);
dispose();
}, 0);
}));
it("runs after being accessed", () =>
createRoot(dispose => {
const [count, setCount] = createSignal(0);
let runs = 0;
const memo = createLazyMemo(() => {
runs++;
return count();
});
setCount(1);
expect(runs, "0 in setup").toBe(0);
expect(memo(), "memo matches the signal on the first access").toBe(1);
expect(runs, "ran once").toBe(1);
dispose();
}));
it("runs only once, even if accessed multiple times", () =>
createRoot(dispose => {
const [count, setCount] = createSignal(0);
let runs = 0;
const memo = createLazyMemo(() => {
runs++;
return count();
});
setCount(1);
expect(runs, "0 in setup").toBe(0);
expect(memo(), "memo matches the signal on the first access").toBe(1);
expect(memo(), "memo matches the signal on the second access").toBe(1);
expect(memo(), "memo matches the signal on the third access").toBe(1);
expect(runs, "ran once").toBe(1);
dispose();
}));
it("runs once until invalidated", () =>
createRoot(dispose => {
const [count, setCount] = createSignal(0);
let runs = 0;
const memo = createLazyMemo(() => {
runs++;
return count();
});
createComputed(memo);
expect(runs, "1-1.").toBe(1);
createComputed(memo);
expect(runs, "1-2.").toBe(1);
memo();
expect(runs, "1-3.").toBe(1);
setCount(1);
expect(runs, "2-1.").toBe(2);
createComputed(memo);
expect(runs, "2-2.").toBe(2);
dispose();
}));
it("won't run if the root of where it was accessed is gone", () =>
createRoot(dispose => {
const [count, setCount] = createSignal(0);
let runs = 0;
const memo = createLazyMemo(() => {
runs++;
return count();
});
createRoot(dispose => {
createComputed(memo);
dispose();
});
expect(runs, "1").toBe(1);
setCount(1);
expect(runs, "2").toBe(1);
dispose();
}));
it("will be running even if some of the reading roots are disposed", () =>
createRoot(dispose => {
const [count, setCount] = createSignal(0);
let runs = 0;
const memo = createLazyMemo(() => {
runs++;
return count();
});
const dispose1 = createRoot(dispose => {
createComputed(memo);
return dispose;
});
const dispose2 = createRoot(dispose => {
createComputed(memo);
return dispose;
});
expect(runs, "ran once").toBe(1);
setCount(1);
expect(runs, "ran twice").toBe(2);
dispose1();
setCount(2);
expect(runs, "ran 3 times").toBe(3);
dispose2();
setCount(3);
expect(runs, "ran 3 times; (not changed)").toBe(3);
dispose();
}));
it("initial value if NOT set in options", () =>
createRoot(dispose => {
const [count, setCount] = createSignal(0);
let capturedPrev: any;
const memo = createLazyMemo(prev => {
capturedPrev = prev;
return count();
});
const captured: any[] = [];
createComputed(() => captured.push(memo()));
expect(captured).toEqual([0]);
expect(capturedPrev).toEqual(undefined);
setCount(1);
expect(captured).toEqual([0, 1]);
expect(capturedPrev).toEqual(0);
dispose();
}));
it("initial value if set", () =>
createRoot(dispose => {
const [count, setCount] = createSignal(0);
let capturedPrev: any;
const memo = createLazyMemo(prev => {
capturedPrev = prev;
return count();
}, 123);
const captured: any[] = [];
createComputed(() => captured.push(memo()));
expect(captured).toEqual([0]);
expect(capturedPrev).toEqual(123);
setCount(1);
expect(captured).toEqual([0, 1]);
expect(capturedPrev).toEqual(0);
dispose();
}));
it("handles prev value properly", () =>
createRoot(dispose => {
const [count, setCount] = createSignal(0);
let capturedPrev: any;
const memo = createLazyMemo(prev => {
capturedPrev = prev;
return count();
});
const dis1 = createRoot(dis => {
createComputed(memo);
return dis;
});
expect(capturedPrev).toBe(undefined);
setCount(1);
expect(capturedPrev).toBe(0);
dis1();
setCount(2);
expect(capturedPrev).toBe(0);
expect(memo()).toBe(2);
expect(capturedPrev).toBe(1);
dispose();
}));
it("works in an effect", () =>
createRoot(dispose => {
const [count, setCount] = createSignal(0);
const memo = createLazyMemo(count);
const captured: number[] = [];
createEffect(() => captured.push(memo()));
queueMicrotask(() => {
expect(captured).toEqual([0]);
setCount(1);
queueMicrotask(() => {
expect(captured).toEqual([0, 1]);
dispose();
});
});
}));
it("computation will last until the source changes", () =>
createRoot(dispose => {
const [count, setCount] = createSignal(0);
let runs = 0;
const memo = createLazyMemo(() => {
runs++;
return count();
});
createRoot(dispose => {
createComputed(memo);
dispose();
});
expect(runs).toBe(1);
createRoot(dispose => {
createComputed(memo);
dispose();
});
expect(runs).toBe(1);
setCount(1);
expect(runs).toBe(1);
dispose();
}));
it("stays in sync when read in a memo", () => {
const { dispose, setA, memo } = createRoot(dispose => {
const [a, setA] = createSignal(1);
const b = createLazyMemo(() => a());
const memo = createMemo(() => {
expect(a()).toBe(b());
});
return { dispose, setA, memo };
});
memo();
setA(2);
memo();
dispose();
});
});