-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.test.tsx
310 lines (265 loc) · 8.74 KB
/
index.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
// SPDX-FileCopyrightText: 2023 Open Pioneer project (https://github.com/open-pioneer)
// SPDX-License-Identifier: Apache-2.0
import { Reactive, batch, computed, isReactive, reactive } from "@conterra/reactivity-core";
import { act, render, renderHook, screen } from "@testing-library/react";
import { describe, expect, it } from "vitest";
import { useComputed, useReactive, useReactiveSnapshot, useReactiveValue } from "./index";
import { Model } from "./examples/Model";
import { YourComponent } from "./examples/YourComponent";
describe("useReactive", () => {
it("creates a new signal", () => {
const hook = renderHook(() => {
const signal = useReactive();
return signal;
});
const signal = hook.result.current;
expect(isReactive(signal)).toBe(true);
expect(signal.value).toBeUndefined();
});
it("supports initial values", () => {
const hook = renderHook(() => {
const signal = useReactive(123);
return signal;
});
const signal = hook.result.current;
expect(signal.value).toBe(123);
});
it("does not change the signal instance if the initial value changes", () => {
const hook = renderHook(
(init: number) => {
// only the very first value of `init` is used, like in useState()
const signal = useReactive(init);
return signal;
},
{
initialProps: 42
}
);
const signal1 = hook.result.current;
expect(signal1.value).toBe(42);
hook.rerender(43);
const signal2 = hook.result.current;
expect(signal1).toBe(signal2);
expect(signal2.value).toBe(42);
});
});
describe("useComputed", () => {
it("creates a computed signal", () => {
const hook = renderHook(() => {
const computed = useComputed(() => 1, []);
return computed;
});
expect(String(hook.result.current)).toMatchInlineSnapshot(`"Reactive[value=1]"`);
});
it("evaluates the function body", () => {
const count = reactive(1);
let calls = 0;
const hook = renderHook(() => {
const computed = useComputed(() => {
calls += 1;
return count.value * 2;
}, []);
return computed;
});
const computed = hook.result.current;
hook.rerender();
expect(hook.result.current).toBe(computed); // cached instance
expect(calls).toBe(0); // never called
expect(computed.value).toBe(2);
expect(calls).toBe(1);
count.value += 1;
expect(computed.value).toBe(4);
expect(calls).toBe(2);
});
it("re-creates the computed signal if deps change", () => {
const hook = renderHook(
(props: { a: number }) => {
const computed = useComputed(() => props.a, [props.a]);
return computed;
},
{
initialProps: {
a: 1
}
}
);
const initialComputed = hook.result.current;
// no change
hook.rerender({ a: 1 });
expect(hook.result.current).toBe(initialComputed);
// new instance
hook.rerender({ a: 2 });
expect(hook.result.current).not.toBe(initialComputed);
});
});
describe("useReactiveValue", () => {
it("watches signals", async () => {
const count = reactive(1);
const hook = renderHook(() => {
const computed = useComputed(() => count.value * 2, []);
const value = useReactiveValue(computed);
return value;
});
expect(hook.result.current).toBe(2);
await act(async () => {
count.value = 10;
await waitForUpdate();
});
expect(hook.result.current).toBe(20);
});
});
describe("useReactiveSnapshot", () => {
it("watches reactive values", async () => {
const a = reactive(1);
const b = reactive(2);
const product = computed(() => a.value * b.value);
let calls = 0;
const hook = renderHook(() =>
useReactiveSnapshot(() => {
calls += 1;
return {
a: a.value,
b: b.value,
product: product.value,
sum: a.value + b.value
};
}, [])
);
expect(calls).toBe(1);
expect(hook.result.current).toMatchInlineSnapshot(`
{
"a": 1,
"b": 2,
"product": 2,
"sum": 3,
}
`);
await act(async () => {
batch(() => {
a.value = 1000;
b.value = 3000;
});
waitForUpdate();
});
expect(calls).toBe(2);
expect(hook.result.current).toMatchInlineSnapshot(`
{
"a": 1000,
"b": 3000,
"product": 3000000,
"sum": 4000,
}
`);
});
it("updates when react values change", () => {
const a = reactive(10);
const b = reactive(20);
let calls = 0;
const hook = renderHook(
(props: { additional: number; reactive: Reactive<number> }) => {
return useReactiveSnapshot(() => {
calls += 1;
return {
value: props.reactive.value + props.additional
};
}, [props.additional, props.reactive]);
},
{
initialProps: {
additional: 1,
reactive: a
}
}
);
expect(calls).toBe(1);
expect(hook.result.current).toMatchInlineSnapshot(`
{
"value": 11,
}
`);
// change plain react value
hook.rerender({
additional: 2,
reactive: a
});
expect(calls).toBe(2);
expect(hook.result.current).toMatchInlineSnapshot(`
{
"value": 12,
}
`);
// change to a different reactive value
hook.rerender({
additional: 2,
reactive: b
});
expect(calls).toBe(3);
expect(hook.result.current).toMatchInlineSnapshot(`
{
"value": 22,
}
`);
// no change -> no call
hook.rerender({
additional: 2,
reactive: b
});
expect(calls).toBe(3);
});
});
describe("rendering components", () => {
interface Model {
currentCount: number;
}
class ModelImpl implements Model {
_currentCount: Reactive<number>;
constructor(initialValue = 3) {
this._currentCount = reactive(initialValue);
}
get currentCount() {
return this._currentCount.value;
}
set currentCount(newValue: number) {
this._currentCount.value = newValue;
}
}
function Component(props: { model: Model }) {
const { model } = props;
const currentCount = useReactiveSnapshot(() => model.currentCount, [model]);
return <div data-testid="content">The current count is {currentCount}</div>;
}
it("renders a component that uses reactive signals", async () => {
const model = new ModelImpl();
render(<Component model={model} />);
const div = await screen.findByTestId("content");
expect(div.textContent).toMatch(/current count is 3/);
await act(async () => {
model.currentCount = 4;
await waitForUpdate();
});
expect(div.textContent).toMatch(/current count is 4/);
});
it("rerenders the component if the model property changes", async () => {
const model1 = new ModelImpl(1);
const model2 = new ModelImpl(2);
const { rerender } = render(<Component model={model1} />);
const div = await screen.findByTestId("content");
expect(div.textContent).toMatch(/current count is 1/);
rerender(<Component model={model2} />);
expect(div.textContent).toMatch(/current count is 2/);
});
});
it("renders the example correctly", async () => {
const model = new Model();
render(<YourComponent model={model} />);
const div = await screen.findByText("Hello John Doe");
await act(async () => {
model.updateName("Jane", "Doe");
await waitForUpdate();
});
expect(div.textContent).toBe("Hello Jane Doe");
});
// Watch callbacks are executed with a small delay (in a new microtask).
async function waitForUpdate() {
await new Promise((resolve) => setTimeout(resolve, 1));
}