-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreact.test.tsx
More file actions
369 lines (298 loc) · 9.34 KB
/
react.test.tsx
File metadata and controls
369 lines (298 loc) · 9.34 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
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
import {afterEach, describe, expect, it, mock} from 'bun:test';
import {act, type ReactNode} from 'react';
import {createRoot} from 'react-dom/client';
import {store} from '../core/core';
import {useStore} from './react';
// ── Test harness ────────────────────────────────────────────────────────────
/** Flush queueMicrotask batching. */
const flush = () => new Promise<void>((resolve) => setTimeout(resolve, 0));
let container: HTMLDivElement;
function setup(): void {
container = document.createElement('div');
document.body.appendChild(container);
}
function teardown(): void {
document.body.removeChild(container);
}
/** Render a component and return a helper to update. */
function render(element: ReactNode): void {
const root = createRoot(container);
act(() => {
root.render(element);
});
}
// ── Selector mode tests ─────────────────────────────────────────────────────
describe('useStore — selector mode', () => {
afterEach(teardown);
it('renders the selected value', () => {
class Counter {
count = 42;
}
const s = store(new Counter());
function Display() {
const count = useStore(s, (snap) => snap.count);
return <div data-testid="count">{count}</div>;
}
setup();
render(<Display />);
expect(container.textContent).toBe('42');
});
it('re-renders when selected value changes', async () => {
class Counter {
count = 0;
increment() {
this.count++;
}
}
const s = store(new Counter());
const renderCount = mock(() => {});
function Display() {
const count = useStore(s, (snap) => snap.count);
renderCount();
return <div>{count}</div>;
}
setup();
render(<Display />);
expect(container.textContent).toBe('0');
expect(renderCount).toHaveBeenCalledTimes(1);
await act(async () => {
s.increment();
await flush();
});
expect(container.textContent).toBe('1');
expect(renderCount).toHaveBeenCalledTimes(2);
});
it('does NOT re-render when unrelated prop changes', async () => {
const s = store({count: 0, name: 'hello'});
const renderCount = mock(() => {});
function CountDisplay() {
const count = useStore(s, (snap) => snap.count);
renderCount();
return <div>{count}</div>;
}
setup();
render(<CountDisplay />);
expect(renderCount).toHaveBeenCalledTimes(1);
// Change `name` which CountDisplay does not select.
await act(async () => {
s.name = 'world';
await flush();
});
// Selector returns same count → Object.is matches → no re-render.
expect(renderCount).toHaveBeenCalledTimes(1);
});
it('handles object selectors with structural equality', async () => {
class Store {
user = {name: 'Alice', age: 30};
theme = 'dark';
updateName(name: string) {
this.user.name = name;
}
}
const s = store(new Store());
const renderCount = mock(() => {});
function UserDisplay() {
const user = useStore(s, (snap) => snap.user);
renderCount();
return <div>{user.name}</div>;
}
setup();
render(<UserDisplay />);
expect(container.textContent).toBe('Alice');
expect(renderCount).toHaveBeenCalledTimes(1);
// Change user → should re-render.
await act(async () => {
s.updateName('Bob');
await flush();
});
expect(container.textContent).toBe('Bob');
expect(renderCount).toHaveBeenCalledTimes(2);
});
it('handles array selectors', async () => {
const s = store({items: ['a', 'b']});
const renderCount = mock(() => {});
function List() {
const items = useStore(s, (snap) => snap.items);
renderCount();
return <div>{items.join(',')}</div>;
}
setup();
render(<List />);
expect(container.textContent).toBe('a,b');
expect(renderCount).toHaveBeenCalledTimes(1);
await act(async () => {
s.items.push('c');
await flush();
});
expect(container.textContent).toBe('a,b,c');
expect(renderCount).toHaveBeenCalledTimes(2);
});
it('supports computed getters in selector', async () => {
class Store {
count = 5;
get doubled() {
return this.count * 2;
}
setCount(value: number) {
this.count = value;
}
}
const s = store(new Store());
function Display() {
const doubled = useStore(s, (snap) => snap.doubled);
return <div>{doubled}</div>;
}
setup();
render(<Display />);
expect(container.textContent).toBe('10');
await act(async () => {
s.setCount(10);
await flush();
});
expect(container.textContent).toBe('20');
});
it('supports custom isEqual for selector', async () => {
const s = store({items: [{id: 1, name: 'a'}]});
const renderCount = mock(() => {});
// Selector always returns a new array reference, but custom isEqual does shallow comparison.
function List() {
const firstItem = useStore(
s,
(snap) => ({name: snap.items[0]?.name}),
(a, b) => a.name === b.name,
);
renderCount();
return <div>{firstItem.name}</div>;
}
setup();
render(<List />);
expect(renderCount).toHaveBeenCalledTimes(1);
// Mutation that doesn't change the selected data.
await act(async () => {
s.items.push({id: 2, name: 'b'});
await flush();
});
// Custom isEqual prevents re-render since first item name is the same.
expect(renderCount).toHaveBeenCalledTimes(1);
});
});
// ── Auto-tracked (selectorless) mode tests ──────────────────────────────────
describe('useStore — auto-tracked mode', () => {
afterEach(teardown);
it('renders accessed properties', () => {
const s = store({count: 42, name: 'hello'});
function Display() {
const snap = useStore(s);
return <div>{snap.count}</div>;
}
setup();
render(<Display />);
expect(container.textContent).toBe('42');
});
it('re-renders when accessed property changes', async () => {
const s = store({count: 0, name: 'hello'});
const renderCount = mock(() => {});
function Display() {
const snap = useStore(s);
renderCount();
return <div>{snap.count}</div>;
}
setup();
render(<Display />);
expect(container.textContent).toBe('0');
expect(renderCount).toHaveBeenCalledTimes(1);
await act(async () => {
s.count = 5;
await flush();
});
expect(container.textContent).toBe('5');
expect(renderCount).toHaveBeenCalledTimes(2);
});
it('does NOT re-render when non-accessed property changes', async () => {
const s = store({count: 0, name: 'hello'});
const renderCount = mock(() => {});
function CountOnly() {
const snap = useStore(s);
renderCount();
return <div>{snap.count}</div>;
}
setup();
render(<CountOnly />);
expect(renderCount).toHaveBeenCalledTimes(1);
// `name` was never accessed by the component.
await act(async () => {
s.name = 'world';
await flush();
});
expect(renderCount).toHaveBeenCalledTimes(1);
});
it('tracks nested object property access', async () => {
const s = store({user: {name: 'Alice', age: 30}, theme: 'dark'});
const renderCount = mock(() => {});
function UserName() {
const snap = useStore(s);
renderCount();
return <div>{snap.user.name}</div>;
}
setup();
render(<UserName />);
expect(container.textContent).toBe('Alice');
expect(renderCount).toHaveBeenCalledTimes(1);
// Change `theme` — should NOT re-render (not accessed).
await act(async () => {
s.theme = 'light';
await flush();
});
expect(renderCount).toHaveBeenCalledTimes(1);
// Change `user.name` — SHOULD re-render (accessed).
await act(async () => {
s.user.name = 'Bob';
await flush();
});
expect(container.textContent).toBe('Bob');
expect(renderCount).toHaveBeenCalledTimes(2);
});
it('tracks array length and element access', async () => {
const s = store({items: ['a', 'b'], other: 'x'});
const renderCount = mock(() => {});
function ItemCount() {
const snap = useStore(s);
renderCount();
return <div>{snap.items.length}</div>;
}
setup();
render(<ItemCount />);
expect(container.textContent).toBe('2');
expect(renderCount).toHaveBeenCalledTimes(1);
await act(async () => {
s.items.push('c');
await flush();
});
expect(container.textContent).toBe('3');
expect(renderCount).toHaveBeenCalledTimes(2);
});
it('computed getters work in auto-tracked mode', async () => {
class Store {
count = 5;
get doubled() {
return this.count * 2;
}
setCount(value: number) {
this.count = value;
}
}
const s = store(new Store());
function Display() {
const snap = useStore(s);
return <div>{snap.doubled}</div>;
}
setup();
render(<Display />);
expect(container.textContent).toBe('10');
await act(async () => {
s.setCount(20);
await flush();
});
expect(container.textContent).toBe('40');
});
});