atomirx 0.0.8 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +198 -2234
- package/bin/cli.js +90 -0
- package/dist/core/derived.d.ts +2 -2
- package/dist/core/effect.d.ts +3 -2
- package/dist/core/onCreateHook.d.ts +15 -2
- package/dist/core/onErrorHook.d.ts +4 -1
- package/dist/core/pool.d.ts +78 -0
- package/dist/core/pool.test.d.ts +1 -0
- package/dist/core/select-boolean.test.d.ts +1 -0
- package/dist/core/select-pool.test.d.ts +1 -0
- package/dist/core/select.d.ts +278 -86
- package/dist/core/types.d.ts +233 -1
- package/dist/core/withAbort.d.ts +95 -0
- package/dist/core/withReady.d.ts +3 -3
- package/dist/devtools/constants.d.ts +41 -0
- package/dist/devtools/index.cjs +1 -0
- package/dist/devtools/index.d.ts +29 -0
- package/dist/devtools/index.js +429 -0
- package/dist/devtools/registry.d.ts +98 -0
- package/dist/devtools/registry.test.d.ts +1 -0
- package/dist/devtools/setup.d.ts +61 -0
- package/dist/devtools/types.d.ts +311 -0
- package/dist/index-BZEnfIcB.cjs +1 -0
- package/dist/index-BbPZhsDl.js +1653 -0
- package/dist/index.cjs +1 -1
- package/dist/index.d.ts +4 -3
- package/dist/index.js +18 -14
- package/dist/onDispatchHook-C8yLzr-o.cjs +1 -0
- package/dist/onDispatchHook-SKbiIUaJ.js +5 -0
- package/dist/onErrorHook-BGGy3tqK.js +38 -0
- package/dist/onErrorHook-DHBASmYw.cjs +1 -0
- package/dist/react/index.cjs +1 -1
- package/dist/react/index.js +191 -151
- package/dist/react/onDispatchHook.d.ts +106 -0
- package/dist/react/useAction.d.ts +4 -1
- package/dist/react-devtools/DevToolsPanel.d.ts +93 -0
- package/dist/react-devtools/EntityDetails.d.ts +10 -0
- package/dist/react-devtools/EntityList.d.ts +15 -0
- package/dist/react-devtools/LogList.d.ts +12 -0
- package/dist/react-devtools/hooks.d.ts +50 -0
- package/dist/react-devtools/index.cjs +1 -0
- package/dist/react-devtools/index.d.ts +31 -0
- package/dist/react-devtools/index.js +1589 -0
- package/dist/react-devtools/styles.d.ts +148 -0
- package/package.json +26 -2
- package/skills/atomirx/SKILL.md +456 -0
- package/skills/atomirx/references/async-patterns.md +188 -0
- package/skills/atomirx/references/atom-patterns.md +238 -0
- package/skills/atomirx/references/deferred-loading.md +191 -0
- package/skills/atomirx/references/derived-patterns.md +428 -0
- package/skills/atomirx/references/effect-patterns.md +426 -0
- package/skills/atomirx/references/error-handling.md +140 -0
- package/skills/atomirx/references/hooks.md +322 -0
- package/skills/atomirx/references/pool-patterns.md +229 -0
- package/skills/atomirx/references/react-integration.md +411 -0
- package/skills/atomirx/references/rules.md +407 -0
- package/skills/atomirx/references/select-context.md +309 -0
- package/skills/atomirx/references/service-template.md +172 -0
- package/skills/atomirx/references/store-template.md +205 -0
- package/skills/atomirx/references/testing-patterns.md +431 -0
- package/coverage/base.css +0 -224
- package/coverage/block-navigation.js +0 -87
- package/coverage/clover.xml +0 -1440
- package/coverage/coverage-final.json +0 -14
- package/coverage/favicon.png +0 -0
- package/coverage/index.html +0 -131
- package/coverage/prettify.css +0 -1
- package/coverage/prettify.js +0 -2
- package/coverage/sort-arrow-sprite.png +0 -0
- package/coverage/sorter.js +0 -210
- package/coverage/src/core/atom.ts.html +0 -889
- package/coverage/src/core/batch.ts.html +0 -223
- package/coverage/src/core/define.ts.html +0 -805
- package/coverage/src/core/emitter.ts.html +0 -919
- package/coverage/src/core/equality.ts.html +0 -631
- package/coverage/src/core/hook.ts.html +0 -460
- package/coverage/src/core/index.html +0 -281
- package/coverage/src/core/isAtom.ts.html +0 -100
- package/coverage/src/core/isPromiseLike.ts.html +0 -133
- package/coverage/src/core/onCreateHook.ts.html +0 -138
- package/coverage/src/core/scheduleNotifyHook.ts.html +0 -94
- package/coverage/src/core/types.ts.html +0 -523
- package/coverage/src/core/withUse.ts.html +0 -253
- package/coverage/src/index.html +0 -116
- package/coverage/src/index.ts.html +0 -106
- package/dist/index-CBVj1kSj.js +0 -1350
- package/dist/index-Cxk9v0um.cjs +0 -1
- package/scripts/publish.js +0 -198
- package/src/core/atom.test.ts +0 -633
- package/src/core/atom.ts +0 -311
- package/src/core/atomState.test.ts +0 -342
- package/src/core/atomState.ts +0 -256
- package/src/core/batch.test.ts +0 -257
- package/src/core/batch.ts +0 -172
- package/src/core/define.test.ts +0 -343
- package/src/core/define.ts +0 -243
- package/src/core/derived.test.ts +0 -1215
- package/src/core/derived.ts +0 -450
- package/src/core/effect.test.ts +0 -802
- package/src/core/effect.ts +0 -188
- package/src/core/emitter.test.ts +0 -364
- package/src/core/emitter.ts +0 -392
- package/src/core/equality.test.ts +0 -392
- package/src/core/equality.ts +0 -182
- package/src/core/getAtomState.ts +0 -69
- package/src/core/hook.test.ts +0 -227
- package/src/core/hook.ts +0 -177
- package/src/core/isAtom.ts +0 -27
- package/src/core/isPromiseLike.test.ts +0 -72
- package/src/core/isPromiseLike.ts +0 -16
- package/src/core/onCreateHook.ts +0 -107
- package/src/core/onErrorHook.test.ts +0 -350
- package/src/core/onErrorHook.ts +0 -52
- package/src/core/promiseCache.test.ts +0 -241
- package/src/core/promiseCache.ts +0 -284
- package/src/core/scheduleNotifyHook.ts +0 -53
- package/src/core/select.ts +0 -729
- package/src/core/selector.test.ts +0 -799
- package/src/core/types.ts +0 -389
- package/src/core/withReady.test.ts +0 -534
- package/src/core/withReady.ts +0 -191
- package/src/core/withUse.test.ts +0 -249
- package/src/core/withUse.ts +0 -56
- package/src/index.test.ts +0 -80
- package/src/index.ts +0 -65
- package/src/react/index.ts +0 -21
- package/src/react/rx.test.tsx +0 -571
- package/src/react/rx.tsx +0 -531
- package/src/react/strictModeTest.tsx +0 -71
- package/src/react/useAction.test.ts +0 -987
- package/src/react/useAction.ts +0 -607
- package/src/react/useSelector.test.ts +0 -182
- package/src/react/useSelector.ts +0 -292
- package/src/react/useStable.test.ts +0 -553
- package/src/react/useStable.ts +0 -288
- package/tsconfig.json +0 -9
- package/v2.md +0 -725
- package/vite.config.ts +0 -42
package/src/react/rx.test.tsx
DELETED
|
@@ -1,571 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
2
|
-
import { act, screen } from "@testing-library/react";
|
|
3
|
-
import { rx } from "./rx";
|
|
4
|
-
import { atom } from "../core/atom";
|
|
5
|
-
import { scheduleNotifyHook } from "../core/scheduleNotifyHook";
|
|
6
|
-
import { wrappers } from "./strictModeTest";
|
|
7
|
-
import { SelectContext } from "../core/select";
|
|
8
|
-
|
|
9
|
-
describe.each(wrappers)("rx - $mode", ({ render }) => {
|
|
10
|
-
beforeEach(() => {
|
|
11
|
-
// Reset to synchronous notification for predictable tests
|
|
12
|
-
scheduleNotifyHook.reset();
|
|
13
|
-
});
|
|
14
|
-
|
|
15
|
-
describe("basic usage", () => {
|
|
16
|
-
it("should render value from single atom (shorthand)", () => {
|
|
17
|
-
const count = atom(42);
|
|
18
|
-
|
|
19
|
-
render(<div data-testid="result">{rx(count)}</div>);
|
|
20
|
-
|
|
21
|
-
expect(screen.getByTestId("result").textContent).toBe("42");
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
it("should render derived value with context selector", () => {
|
|
25
|
-
const count = atom(5);
|
|
26
|
-
|
|
27
|
-
render(
|
|
28
|
-
<div data-testid="result">{rx(({ read }) => read(count) * 2)}</div>
|
|
29
|
-
);
|
|
30
|
-
|
|
31
|
-
expect(screen.getByTestId("result").textContent).toBe("10");
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
it("should render string values", () => {
|
|
35
|
-
const name = atom("John");
|
|
36
|
-
|
|
37
|
-
render(<div data-testid="result">{rx(name)}</div>);
|
|
38
|
-
|
|
39
|
-
expect(screen.getByTestId("result").textContent).toBe("John");
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
it("should render null/undefined as empty", () => {
|
|
43
|
-
const value = atom<string | null>(null);
|
|
44
|
-
|
|
45
|
-
render(<div data-testid="result">{rx(value)}</div>);
|
|
46
|
-
|
|
47
|
-
expect(screen.getByTestId("result").textContent).toBe("");
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
it("should render undefined as empty", () => {
|
|
51
|
-
const value = atom<string | undefined>(undefined);
|
|
52
|
-
|
|
53
|
-
render(<div data-testid="result">{rx(value)}</div>);
|
|
54
|
-
|
|
55
|
-
expect(screen.getByTestId("result").textContent).toBe("");
|
|
56
|
-
});
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
describe("multiple atoms with context selector", () => {
|
|
60
|
-
it("should render derived value from multiple atoms", () => {
|
|
61
|
-
const firstName = atom("John");
|
|
62
|
-
const lastName = atom("Doe");
|
|
63
|
-
|
|
64
|
-
render(
|
|
65
|
-
<div data-testid="result">
|
|
66
|
-
{rx(({ read }) => `${read(firstName)} ${read(lastName)}`)}
|
|
67
|
-
</div>
|
|
68
|
-
);
|
|
69
|
-
|
|
70
|
-
expect(screen.getByTestId("result").textContent).toBe("John Doe");
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
it("should render computed value from multiple numeric atoms", () => {
|
|
74
|
-
const a = atom(1);
|
|
75
|
-
const b = atom(2);
|
|
76
|
-
const c = atom(3);
|
|
77
|
-
|
|
78
|
-
render(
|
|
79
|
-
<div data-testid="result">
|
|
80
|
-
{rx(({ read }) => read(a) + read(b) + read(c))}
|
|
81
|
-
</div>
|
|
82
|
-
);
|
|
83
|
-
|
|
84
|
-
expect(screen.getByTestId("result").textContent).toBe("6");
|
|
85
|
-
});
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
describe("reactivity", () => {
|
|
89
|
-
it("should update when source atom changes (shorthand)", () => {
|
|
90
|
-
const count = atom(5);
|
|
91
|
-
|
|
92
|
-
render(<div data-testid="result">{rx(count)}</div>);
|
|
93
|
-
|
|
94
|
-
expect(screen.getByTestId("result").textContent).toBe("5");
|
|
95
|
-
|
|
96
|
-
act(() => {
|
|
97
|
-
count.set(10);
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
expect(screen.getByTestId("result").textContent).toBe("10");
|
|
101
|
-
});
|
|
102
|
-
|
|
103
|
-
it("should update when source atom changes (context selector)", () => {
|
|
104
|
-
const count = atom(5);
|
|
105
|
-
|
|
106
|
-
render(
|
|
107
|
-
<div data-testid="result">{rx(({ read }) => read(count) * 2)}</div>
|
|
108
|
-
);
|
|
109
|
-
|
|
110
|
-
expect(screen.getByTestId("result").textContent).toBe("10");
|
|
111
|
-
|
|
112
|
-
act(() => {
|
|
113
|
-
count.set(10);
|
|
114
|
-
});
|
|
115
|
-
|
|
116
|
-
expect(screen.getByTestId("result").textContent).toBe("20");
|
|
117
|
-
});
|
|
118
|
-
|
|
119
|
-
it("should update when any dependency changes", () => {
|
|
120
|
-
const a = atom(1);
|
|
121
|
-
const b = atom(2);
|
|
122
|
-
|
|
123
|
-
render(
|
|
124
|
-
<div data-testid="result">{rx(({ read }) => read(a) + read(b))}</div>
|
|
125
|
-
);
|
|
126
|
-
|
|
127
|
-
expect(screen.getByTestId("result").textContent).toBe("3");
|
|
128
|
-
|
|
129
|
-
act(() => {
|
|
130
|
-
a.set(5);
|
|
131
|
-
});
|
|
132
|
-
|
|
133
|
-
expect(screen.getByTestId("result").textContent).toBe("7");
|
|
134
|
-
|
|
135
|
-
act(() => {
|
|
136
|
-
b.set(10);
|
|
137
|
-
});
|
|
138
|
-
|
|
139
|
-
expect(screen.getByTestId("result").textContent).toBe("15");
|
|
140
|
-
});
|
|
141
|
-
});
|
|
142
|
-
|
|
143
|
-
describe("conditional dependencies", () => {
|
|
144
|
-
it("should only re-render when accessed dependencies change", () => {
|
|
145
|
-
const flag = atom(true);
|
|
146
|
-
const a = atom(1);
|
|
147
|
-
const b = atom(2);
|
|
148
|
-
|
|
149
|
-
const selectorFn = vi.fn(({ read }: SelectContext) =>
|
|
150
|
-
read(flag) ? read(a) : read(b)
|
|
151
|
-
);
|
|
152
|
-
|
|
153
|
-
render(<div data-testid="result">{rx(selectorFn)}</div>);
|
|
154
|
-
|
|
155
|
-
expect(screen.getByTestId("result").textContent).toBe("1");
|
|
156
|
-
const callCount = selectorFn.mock.calls.length;
|
|
157
|
-
|
|
158
|
-
// Change b - should NOT trigger re-render since b is not accessed
|
|
159
|
-
act(() => {
|
|
160
|
-
b.set(20);
|
|
161
|
-
});
|
|
162
|
-
|
|
163
|
-
// Selector should not have been called again
|
|
164
|
-
expect(selectorFn.mock.calls.length).toBe(callCount);
|
|
165
|
-
expect(screen.getByTestId("result").textContent).toBe("1");
|
|
166
|
-
});
|
|
167
|
-
|
|
168
|
-
it("should update subscriptions when dependencies change", () => {
|
|
169
|
-
const flag = atom(true);
|
|
170
|
-
const a = atom(1);
|
|
171
|
-
const b = atom(2);
|
|
172
|
-
|
|
173
|
-
render(
|
|
174
|
-
<div data-testid="result">
|
|
175
|
-
{rx(({ read }) => (read(flag) ? read(a) : read(b)))}
|
|
176
|
-
</div>
|
|
177
|
-
);
|
|
178
|
-
|
|
179
|
-
expect(screen.getByTestId("result").textContent).toBe("1");
|
|
180
|
-
|
|
181
|
-
// Change flag to false - now b should be accessed
|
|
182
|
-
act(() => {
|
|
183
|
-
flag.set(false);
|
|
184
|
-
});
|
|
185
|
-
|
|
186
|
-
expect(screen.getByTestId("result").textContent).toBe("2");
|
|
187
|
-
|
|
188
|
-
// Now change b - should trigger re-render
|
|
189
|
-
act(() => {
|
|
190
|
-
b.set(20);
|
|
191
|
-
});
|
|
192
|
-
|
|
193
|
-
expect(screen.getByTestId("result").textContent).toBe("20");
|
|
194
|
-
});
|
|
195
|
-
});
|
|
196
|
-
|
|
197
|
-
describe("equals option", () => {
|
|
198
|
-
it("should use shallow equality by default", () => {
|
|
199
|
-
const user = atom({ name: "John", age: 30 });
|
|
200
|
-
const renderCount = { current: 0 };
|
|
201
|
-
|
|
202
|
-
const TestComponent = () => {
|
|
203
|
-
renderCount.current++;
|
|
204
|
-
return (
|
|
205
|
-
<div data-testid="result">
|
|
206
|
-
{rx(({ read }) => JSON.stringify(read(user)))}
|
|
207
|
-
</div>
|
|
208
|
-
);
|
|
209
|
-
};
|
|
210
|
-
|
|
211
|
-
render(<TestComponent />);
|
|
212
|
-
|
|
213
|
-
expect(screen.getByTestId("result").textContent).toBe(
|
|
214
|
-
'{"name":"John","age":30}'
|
|
215
|
-
);
|
|
216
|
-
const initialRenderCount = renderCount.current;
|
|
217
|
-
|
|
218
|
-
// Set same content but different reference - should NOT re-render with shallow equality
|
|
219
|
-
act(() => {
|
|
220
|
-
user.set({ name: "John", age: 30 });
|
|
221
|
-
});
|
|
222
|
-
|
|
223
|
-
expect(renderCount.current).toBe(initialRenderCount);
|
|
224
|
-
});
|
|
225
|
-
|
|
226
|
-
it("should use strict equality when specified", () => {
|
|
227
|
-
const user = atom({ name: "John", age: 30 });
|
|
228
|
-
const selectorCallCount = { current: 0 };
|
|
229
|
-
|
|
230
|
-
const selector = ({ read }: SelectContext) => {
|
|
231
|
-
selectorCallCount.current++;
|
|
232
|
-
return JSON.stringify(read(user));
|
|
233
|
-
};
|
|
234
|
-
|
|
235
|
-
render(<div data-testid="result">{rx(selector, "strict")}</div>);
|
|
236
|
-
|
|
237
|
-
expect(screen.getByTestId("result").textContent).toBe(
|
|
238
|
-
'{"name":"John","age":30}'
|
|
239
|
-
);
|
|
240
|
-
const initialCallCount = selectorCallCount.current;
|
|
241
|
-
|
|
242
|
-
// Set same content but different reference - with strict equality,
|
|
243
|
-
// the selector result (string) will be different reference, causing re-render
|
|
244
|
-
act(() => {
|
|
245
|
-
user.set({ name: "John", age: 30 });
|
|
246
|
-
});
|
|
247
|
-
|
|
248
|
-
// Selector should have been called again due to atom change
|
|
249
|
-
expect(selectorCallCount.current).toBeGreaterThan(initialCallCount);
|
|
250
|
-
});
|
|
251
|
-
|
|
252
|
-
it("should use custom equality function", () => {
|
|
253
|
-
const user = atom({ id: 1, name: "John" });
|
|
254
|
-
const renderCount = { current: 0 };
|
|
255
|
-
|
|
256
|
-
const TestComponent = () => {
|
|
257
|
-
renderCount.current++;
|
|
258
|
-
return (
|
|
259
|
-
<div data-testid="result">
|
|
260
|
-
{rx(
|
|
261
|
-
({ read }) => JSON.stringify(read(user)),
|
|
262
|
-
(a, b) => a === b // Compare stringified values
|
|
263
|
-
)}
|
|
264
|
-
</div>
|
|
265
|
-
);
|
|
266
|
-
};
|
|
267
|
-
|
|
268
|
-
render(<TestComponent />);
|
|
269
|
-
|
|
270
|
-
expect(screen.getByTestId("result").textContent).toBe(
|
|
271
|
-
'{"id":1,"name":"John"}'
|
|
272
|
-
);
|
|
273
|
-
const initialRenderCount = renderCount.current;
|
|
274
|
-
|
|
275
|
-
// Same stringified value - should NOT re-render
|
|
276
|
-
act(() => {
|
|
277
|
-
user.set({ id: 1, name: "John" });
|
|
278
|
-
});
|
|
279
|
-
|
|
280
|
-
expect(renderCount.current).toBe(initialRenderCount);
|
|
281
|
-
});
|
|
282
|
-
});
|
|
283
|
-
|
|
284
|
-
describe("memoization", () => {
|
|
285
|
-
it("should not re-render parent when only rx value changes", () => {
|
|
286
|
-
const count = atom(5);
|
|
287
|
-
const parentRenderCount = { current: 0 };
|
|
288
|
-
|
|
289
|
-
const Parent = () => {
|
|
290
|
-
parentRenderCount.current++;
|
|
291
|
-
return (
|
|
292
|
-
<div data-testid="result">{rx(({ read }) => read(count) * 2)}</div>
|
|
293
|
-
);
|
|
294
|
-
};
|
|
295
|
-
|
|
296
|
-
render(<Parent />);
|
|
297
|
-
|
|
298
|
-
expect(screen.getByTestId("result").textContent).toBe("10");
|
|
299
|
-
const initialParentRenderCount = parentRenderCount.current;
|
|
300
|
-
|
|
301
|
-
act(() => {
|
|
302
|
-
count.set(10);
|
|
303
|
-
});
|
|
304
|
-
|
|
305
|
-
expect(screen.getByTestId("result").textContent).toBe("20");
|
|
306
|
-
// Parent should not re-render - only the memoized Rx component should
|
|
307
|
-
expect(parentRenderCount.current).toBe(initialParentRenderCount);
|
|
308
|
-
});
|
|
309
|
-
});
|
|
310
|
-
|
|
311
|
-
describe("edge cases", () => {
|
|
312
|
-
it("should handle zero value", () => {
|
|
313
|
-
const count = atom(0);
|
|
314
|
-
|
|
315
|
-
render(<div data-testid="result">{rx(count)}</div>);
|
|
316
|
-
|
|
317
|
-
expect(screen.getByTestId("result").textContent).toBe("0");
|
|
318
|
-
});
|
|
319
|
-
|
|
320
|
-
it("should handle false value", () => {
|
|
321
|
-
const flag = atom(false);
|
|
322
|
-
|
|
323
|
-
render(
|
|
324
|
-
<div data-testid="result">{rx(({ read }) => String(read(flag)))}</div>
|
|
325
|
-
);
|
|
326
|
-
|
|
327
|
-
expect(screen.getByTestId("result").textContent).toBe("false");
|
|
328
|
-
});
|
|
329
|
-
|
|
330
|
-
it("should handle empty string", () => {
|
|
331
|
-
const text = atom("");
|
|
332
|
-
|
|
333
|
-
render(<div data-testid="result">{rx(text)}</div>);
|
|
334
|
-
|
|
335
|
-
expect(screen.getByTestId("result").textContent).toBe("");
|
|
336
|
-
});
|
|
337
|
-
|
|
338
|
-
it("should handle rapid updates", () => {
|
|
339
|
-
const count = atom(0);
|
|
340
|
-
|
|
341
|
-
render(<div data-testid="result">{rx(count)}</div>);
|
|
342
|
-
|
|
343
|
-
act(() => {
|
|
344
|
-
for (let i = 1; i <= 100; i++) {
|
|
345
|
-
count.set(i);
|
|
346
|
-
}
|
|
347
|
-
});
|
|
348
|
-
|
|
349
|
-
expect(screen.getByTestId("result").textContent).toBe("100");
|
|
350
|
-
});
|
|
351
|
-
});
|
|
352
|
-
|
|
353
|
-
describe("async atoms", () => {
|
|
354
|
-
it("should handle selector that catches loading state", () => {
|
|
355
|
-
const asyncAtom = atom(Promise.resolve(10));
|
|
356
|
-
|
|
357
|
-
render(
|
|
358
|
-
<div data-testid="result">
|
|
359
|
-
{rx(({ read }) => {
|
|
360
|
-
try {
|
|
361
|
-
return read(asyncAtom) * 2;
|
|
362
|
-
} catch {
|
|
363
|
-
return "loading";
|
|
364
|
-
}
|
|
365
|
-
})}
|
|
366
|
-
</div>
|
|
367
|
-
);
|
|
368
|
-
|
|
369
|
-
// Initially loading - selector catches and returns "loading"
|
|
370
|
-
expect(screen.getByTestId("result").textContent).toBe("loading");
|
|
371
|
-
});
|
|
372
|
-
|
|
373
|
-
it("should update when source atom changes", async () => {
|
|
374
|
-
// v2: Test with sync atoms - derived atoms have async behavior
|
|
375
|
-
// For simpler reactivity testing, use sync atoms directly
|
|
376
|
-
const sourceAtom = atom(5);
|
|
377
|
-
|
|
378
|
-
render(
|
|
379
|
-
<div data-testid="result">{rx(({ read }) => read(sourceAtom) * 2)}</div>
|
|
380
|
-
);
|
|
381
|
-
|
|
382
|
-
expect(screen.getByTestId("result").textContent).toBe("10");
|
|
383
|
-
|
|
384
|
-
// Update source and verify updates
|
|
385
|
-
await act(async () => {
|
|
386
|
-
sourceAtom.set(10);
|
|
387
|
-
await new Promise((r) => setTimeout(r, 10));
|
|
388
|
-
});
|
|
389
|
-
|
|
390
|
-
expect(screen.getByTestId("result").textContent).toBe("20");
|
|
391
|
-
});
|
|
392
|
-
});
|
|
393
|
-
|
|
394
|
-
describe("async utilities", () => {
|
|
395
|
-
it("should support all() for multiple atoms", async () => {
|
|
396
|
-
// v2: Use sync atoms or derived atoms for proper reactivity
|
|
397
|
-
const a = atom(1);
|
|
398
|
-
const b = atom(2);
|
|
399
|
-
|
|
400
|
-
render(
|
|
401
|
-
<div data-testid="result">
|
|
402
|
-
{rx(({ all }) => {
|
|
403
|
-
const [valA, valB] = all([a, b]);
|
|
404
|
-
return valA + valB;
|
|
405
|
-
})}
|
|
406
|
-
</div>
|
|
407
|
-
);
|
|
408
|
-
|
|
409
|
-
expect(screen.getByTestId("result").textContent).toBe("3");
|
|
410
|
-
|
|
411
|
-
// Update and verify
|
|
412
|
-
await act(async () => {
|
|
413
|
-
a.set(10);
|
|
414
|
-
await new Promise((r) => setTimeout(r, 10));
|
|
415
|
-
});
|
|
416
|
-
|
|
417
|
-
expect(screen.getByTestId("result").textContent).toBe("12");
|
|
418
|
-
});
|
|
419
|
-
});
|
|
420
|
-
|
|
421
|
-
describe("loading/error options", () => {
|
|
422
|
-
it("should render loading fallback when atom is pending", () => {
|
|
423
|
-
const asyncAtom = atom(new Promise<string>(() => {}));
|
|
424
|
-
|
|
425
|
-
render(
|
|
426
|
-
<div data-testid="result">
|
|
427
|
-
{rx(asyncAtom, { loading: () => <span>Loading...</span> })}
|
|
428
|
-
</div>
|
|
429
|
-
);
|
|
430
|
-
|
|
431
|
-
expect(screen.getByTestId("result").textContent).toBe("Loading...");
|
|
432
|
-
});
|
|
433
|
-
|
|
434
|
-
it("should render error fallback when atom has error", async () => {
|
|
435
|
-
const error = new Error("Test error");
|
|
436
|
-
const rejectedPromise = Promise.reject(error);
|
|
437
|
-
rejectedPromise.catch(() => {}); // Prevent unhandled rejection
|
|
438
|
-
const asyncAtom = atom(rejectedPromise);
|
|
439
|
-
|
|
440
|
-
// Wait for promise to be tracked
|
|
441
|
-
await act(async () => {
|
|
442
|
-
await Promise.resolve();
|
|
443
|
-
await Promise.resolve();
|
|
444
|
-
});
|
|
445
|
-
|
|
446
|
-
render(
|
|
447
|
-
<div data-testid="result">
|
|
448
|
-
{rx(asyncAtom, {
|
|
449
|
-
error: ({ error: e }) => <span>Error: {(e as Error).message}</span>,
|
|
450
|
-
})}
|
|
451
|
-
</div>
|
|
452
|
-
);
|
|
453
|
-
|
|
454
|
-
expect(screen.getByTestId("result").textContent).toBe(
|
|
455
|
-
"Error: Test error"
|
|
456
|
-
);
|
|
457
|
-
});
|
|
458
|
-
|
|
459
|
-
it("should render value when atom resolves with loading option", async () => {
|
|
460
|
-
let resolve: (value: string) => void;
|
|
461
|
-
const promise = new Promise<string>((r) => {
|
|
462
|
-
resolve = r;
|
|
463
|
-
});
|
|
464
|
-
const asyncAtom = atom(promise);
|
|
465
|
-
|
|
466
|
-
const { rerender } = render(
|
|
467
|
-
<div data-testid="result">
|
|
468
|
-
{rx(asyncAtom, {
|
|
469
|
-
loading: () => <span>Loading...</span>,
|
|
470
|
-
})}
|
|
471
|
-
</div>
|
|
472
|
-
);
|
|
473
|
-
|
|
474
|
-
expect(screen.getByTestId("result").textContent).toBe("Loading...");
|
|
475
|
-
|
|
476
|
-
await act(async () => {
|
|
477
|
-
resolve!("Hello");
|
|
478
|
-
await Promise.resolve();
|
|
479
|
-
await Promise.resolve();
|
|
480
|
-
});
|
|
481
|
-
|
|
482
|
-
rerender(
|
|
483
|
-
<div data-testid="result">
|
|
484
|
-
{rx(asyncAtom, {
|
|
485
|
-
loading: () => <span>Loading...</span>,
|
|
486
|
-
})}
|
|
487
|
-
</div>
|
|
488
|
-
);
|
|
489
|
-
|
|
490
|
-
expect(screen.getByTestId("result").textContent).toBe("Hello");
|
|
491
|
-
});
|
|
492
|
-
|
|
493
|
-
it("should work with selector function and loading option", () => {
|
|
494
|
-
const asyncAtom = atom(new Promise<number>(() => {}));
|
|
495
|
-
|
|
496
|
-
render(
|
|
497
|
-
<div data-testid="result">
|
|
498
|
-
{rx(({ read }) => read(asyncAtom) * 2, {
|
|
499
|
-
loading: () => <span>Computing...</span>,
|
|
500
|
-
})}
|
|
501
|
-
</div>
|
|
502
|
-
);
|
|
503
|
-
|
|
504
|
-
expect(screen.getByTestId("result").textContent).toBe("Computing...");
|
|
505
|
-
});
|
|
506
|
-
|
|
507
|
-
it("should support both loading and error options", async () => {
|
|
508
|
-
const error = new Error("Failed");
|
|
509
|
-
const rejectedPromise = Promise.reject(error);
|
|
510
|
-
rejectedPromise.catch(() => {});
|
|
511
|
-
const asyncAtom = atom(rejectedPromise);
|
|
512
|
-
|
|
513
|
-
await act(async () => {
|
|
514
|
-
await Promise.resolve();
|
|
515
|
-
await Promise.resolve();
|
|
516
|
-
});
|
|
517
|
-
|
|
518
|
-
render(
|
|
519
|
-
<div data-testid="result">
|
|
520
|
-
{rx(asyncAtom, {
|
|
521
|
-
loading: () => <span>Loading...</span>,
|
|
522
|
-
error: ({ error: e }) => (
|
|
523
|
-
<span>Failed: {(e as Error).message}</span>
|
|
524
|
-
),
|
|
525
|
-
})}
|
|
526
|
-
</div>
|
|
527
|
-
);
|
|
528
|
-
|
|
529
|
-
expect(screen.getByTestId("result").textContent).toBe("Failed: Failed");
|
|
530
|
-
});
|
|
531
|
-
|
|
532
|
-
it("should pass equality in options object", () => {
|
|
533
|
-
const user = atom({ id: 1, name: "John" });
|
|
534
|
-
const renderSpy = vi.fn();
|
|
535
|
-
|
|
536
|
-
function TestComponent() {
|
|
537
|
-
renderSpy();
|
|
538
|
-
return (
|
|
539
|
-
<div data-testid="result">
|
|
540
|
-
{rx(({ read }) => read(user).name, {
|
|
541
|
-
equals: (a, b) => a === b,
|
|
542
|
-
})}
|
|
543
|
-
</div>
|
|
544
|
-
);
|
|
545
|
-
}
|
|
546
|
-
|
|
547
|
-
render(<TestComponent />);
|
|
548
|
-
expect(screen.getByTestId("result").textContent).toBe("John");
|
|
549
|
-
|
|
550
|
-
// Update with same name
|
|
551
|
-
act(() => {
|
|
552
|
-
user.set({ id: 2, name: "John" });
|
|
553
|
-
});
|
|
554
|
-
|
|
555
|
-
// Name didn't change, so rx content should be same
|
|
556
|
-
expect(screen.getByTestId("result").textContent).toBe("John");
|
|
557
|
-
});
|
|
558
|
-
|
|
559
|
-
it("should still work with legacy equality parameter", () => {
|
|
560
|
-
const count = atom(5);
|
|
561
|
-
|
|
562
|
-
render(
|
|
563
|
-
<div data-testid="result">
|
|
564
|
-
{rx(({ read }) => read(count) * 2, "strict")}
|
|
565
|
-
</div>
|
|
566
|
-
);
|
|
567
|
-
|
|
568
|
-
expect(screen.getByTestId("result").textContent).toBe("10");
|
|
569
|
-
});
|
|
570
|
-
});
|
|
571
|
-
});
|