atomirx 0.0.1
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 +1666 -0
- package/coverage/base.css +224 -0
- package/coverage/block-navigation.js +87 -0
- package/coverage/clover.xml +1440 -0
- package/coverage/coverage-final.json +14 -0
- package/coverage/favicon.png +0 -0
- package/coverage/index.html +131 -0
- package/coverage/prettify.css +1 -0
- package/coverage/prettify.js +2 -0
- package/coverage/sort-arrow-sprite.png +0 -0
- package/coverage/sorter.js +210 -0
- package/coverage/src/core/atom.ts.html +889 -0
- package/coverage/src/core/batch.ts.html +223 -0
- package/coverage/src/core/define.ts.html +805 -0
- package/coverage/src/core/emitter.ts.html +919 -0
- package/coverage/src/core/equality.ts.html +631 -0
- package/coverage/src/core/hook.ts.html +460 -0
- package/coverage/src/core/index.html +281 -0
- package/coverage/src/core/isAtom.ts.html +100 -0
- package/coverage/src/core/isPromiseLike.ts.html +133 -0
- package/coverage/src/core/onCreateHook.ts.html +136 -0
- package/coverage/src/core/scheduleNotifyHook.ts.html +94 -0
- package/coverage/src/core/types.ts.html +523 -0
- package/coverage/src/core/withUse.ts.html +253 -0
- package/coverage/src/index.html +116 -0
- package/coverage/src/index.ts.html +106 -0
- package/dist/core/atom.d.ts +63 -0
- package/dist/core/atom.test.d.ts +1 -0
- package/dist/core/atomState.d.ts +104 -0
- package/dist/core/atomState.test.d.ts +1 -0
- package/dist/core/batch.d.ts +126 -0
- package/dist/core/batch.test.d.ts +1 -0
- package/dist/core/define.d.ts +173 -0
- package/dist/core/define.test.d.ts +1 -0
- package/dist/core/derived.d.ts +102 -0
- package/dist/core/derived.test.d.ts +1 -0
- package/dist/core/effect.d.ts +120 -0
- package/dist/core/effect.test.d.ts +1 -0
- package/dist/core/emitter.d.ts +237 -0
- package/dist/core/emitter.test.d.ts +1 -0
- package/dist/core/equality.d.ts +62 -0
- package/dist/core/equality.test.d.ts +1 -0
- package/dist/core/hook.d.ts +134 -0
- package/dist/core/hook.test.d.ts +1 -0
- package/dist/core/isAtom.d.ts +9 -0
- package/dist/core/isPromiseLike.d.ts +9 -0
- package/dist/core/isPromiseLike.test.d.ts +1 -0
- package/dist/core/onCreateHook.d.ts +79 -0
- package/dist/core/promiseCache.d.ts +134 -0
- package/dist/core/promiseCache.test.d.ts +1 -0
- package/dist/core/scheduleNotifyHook.d.ts +51 -0
- package/dist/core/select.d.ts +151 -0
- package/dist/core/selector.test.d.ts +1 -0
- package/dist/core/types.d.ts +279 -0
- package/dist/core/withUse.d.ts +38 -0
- package/dist/core/withUse.test.d.ts +1 -0
- package/dist/index-2ok7ilik.js +1217 -0
- package/dist/index-B_5SFzfl.cjs +1 -0
- package/dist/index.cjs +1 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +20 -0
- package/dist/index.test.d.ts +1 -0
- package/dist/react/index.cjs +30 -0
- package/dist/react/index.d.ts +7 -0
- package/dist/react/index.js +823 -0
- package/dist/react/rx.d.ts +250 -0
- package/dist/react/rx.test.d.ts +1 -0
- package/dist/react/strictModeTest.d.ts +10 -0
- package/dist/react/useAction.d.ts +381 -0
- package/dist/react/useAction.test.d.ts +1 -0
- package/dist/react/useStable.d.ts +183 -0
- package/dist/react/useStable.test.d.ts +1 -0
- package/dist/react/useValue.d.ts +134 -0
- package/dist/react/useValue.test.d.ts +1 -0
- package/package.json +57 -0
- package/scripts/publish.js +198 -0
- package/src/core/atom.test.ts +369 -0
- package/src/core/atom.ts +189 -0
- package/src/core/atomState.test.ts +342 -0
- package/src/core/atomState.ts +256 -0
- package/src/core/batch.test.ts +257 -0
- package/src/core/batch.ts +172 -0
- package/src/core/define.test.ts +342 -0
- package/src/core/define.ts +243 -0
- package/src/core/derived.test.ts +381 -0
- package/src/core/derived.ts +339 -0
- package/src/core/effect.test.ts +196 -0
- package/src/core/effect.ts +184 -0
- package/src/core/emitter.test.ts +364 -0
- package/src/core/emitter.ts +392 -0
- package/src/core/equality.test.ts +392 -0
- package/src/core/equality.ts +182 -0
- package/src/core/hook.test.ts +227 -0
- package/src/core/hook.ts +177 -0
- package/src/core/isAtom.ts +27 -0
- package/src/core/isPromiseLike.test.ts +72 -0
- package/src/core/isPromiseLike.ts +16 -0
- package/src/core/onCreateHook.ts +92 -0
- package/src/core/promiseCache.test.ts +239 -0
- package/src/core/promiseCache.ts +279 -0
- package/src/core/scheduleNotifyHook.ts +53 -0
- package/src/core/select.ts +454 -0
- package/src/core/selector.test.ts +257 -0
- package/src/core/types.ts +311 -0
- package/src/core/withUse.test.ts +249 -0
- package/src/core/withUse.ts +56 -0
- package/src/index.test.ts +80 -0
- package/src/index.ts +51 -0
- package/src/react/index.ts +20 -0
- package/src/react/rx.test.tsx +416 -0
- package/src/react/rx.tsx +300 -0
- package/src/react/strictModeTest.tsx +71 -0
- package/src/react/useAction.test.ts +989 -0
- package/src/react/useAction.ts +605 -0
- package/src/react/useStable.test.ts +553 -0
- package/src/react/useStable.ts +288 -0
- package/src/react/useValue.test.ts +182 -0
- package/src/react/useValue.ts +261 -0
- package/tsconfig.json +9 -0
- package/v2.md +725 -0
- package/vite.config.ts +39 -0
package/src/index.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
// Core
|
|
2
|
+
export { atom } from "./core/atom";
|
|
3
|
+
export { batch } from "./core/batch";
|
|
4
|
+
export { define } from "./core/define";
|
|
5
|
+
export { derived } from "./core/derived";
|
|
6
|
+
export { effect } from "./core/effect";
|
|
7
|
+
export { emitter } from "./core/emitter";
|
|
8
|
+
export { isAtom, isDerived } from "./core/isAtom";
|
|
9
|
+
export { select, AllAtomsRejectedError } from "./core/select";
|
|
10
|
+
|
|
11
|
+
// Promise utilities
|
|
12
|
+
export {
|
|
13
|
+
getAtomState,
|
|
14
|
+
isPending,
|
|
15
|
+
isFulfilled,
|
|
16
|
+
isRejected,
|
|
17
|
+
trackPromise,
|
|
18
|
+
unwrap,
|
|
19
|
+
} from "./core/promiseCache";
|
|
20
|
+
|
|
21
|
+
// Types
|
|
22
|
+
export type {
|
|
23
|
+
Atom,
|
|
24
|
+
AtomMeta,
|
|
25
|
+
AtomOptions,
|
|
26
|
+
AtomState,
|
|
27
|
+
AtomValue,
|
|
28
|
+
AnyAtom,
|
|
29
|
+
DerivedAtom,
|
|
30
|
+
DerivedAtomMeta,
|
|
31
|
+
DerivedOptions,
|
|
32
|
+
EffectOptions,
|
|
33
|
+
Equality,
|
|
34
|
+
EqualityShorthand,
|
|
35
|
+
Getter,
|
|
36
|
+
MutableAtom,
|
|
37
|
+
MutableAtomMeta,
|
|
38
|
+
Pipeable,
|
|
39
|
+
SettledResult,
|
|
40
|
+
} from "./core/types";
|
|
41
|
+
|
|
42
|
+
export { onCreateHook } from "./core/onCreateHook";
|
|
43
|
+
export type { AtomCreateInfo, ModuleCreateInfo } from "./core/onCreateHook";
|
|
44
|
+
|
|
45
|
+
export type {
|
|
46
|
+
SelectContext,
|
|
47
|
+
SelectResult,
|
|
48
|
+
ContextSelectorFn,
|
|
49
|
+
} from "./core/select";
|
|
50
|
+
|
|
51
|
+
export type { PromiseState } from "./core/promiseCache";
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export { useValue } from "./useValue";
|
|
2
|
+
export { useStable } from "./useStable";
|
|
3
|
+
export type { UseStableResult } from "./useStable";
|
|
4
|
+
export { useAction } from "./useAction";
|
|
5
|
+
export { rx } from "./rx";
|
|
6
|
+
|
|
7
|
+
export type {
|
|
8
|
+
ActionState,
|
|
9
|
+
ActionIdleState,
|
|
10
|
+
ActionLoadingState,
|
|
11
|
+
ActionSuccessState,
|
|
12
|
+
ActionErrorState,
|
|
13
|
+
ActionStateWithoutIdle,
|
|
14
|
+
AbortablePromise,
|
|
15
|
+
UseActionOptions,
|
|
16
|
+
ActionContext,
|
|
17
|
+
ActionApi,
|
|
18
|
+
Action,
|
|
19
|
+
} from "./useAction";
|
|
20
|
+
export * from "../index";
|
|
@@ -0,0 +1,416 @@
|
|
|
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(<div data-testid="result">{rx(({ get }) => get(count) * 2)}</div>);
|
|
28
|
+
|
|
29
|
+
expect(screen.getByTestId("result").textContent).toBe("10");
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it("should render string values", () => {
|
|
33
|
+
const name = atom("John");
|
|
34
|
+
|
|
35
|
+
render(<div data-testid="result">{rx(name)}</div>);
|
|
36
|
+
|
|
37
|
+
expect(screen.getByTestId("result").textContent).toBe("John");
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it("should render null/undefined as empty", () => {
|
|
41
|
+
const value = atom<string | null>(null);
|
|
42
|
+
|
|
43
|
+
render(<div data-testid="result">{rx(value)}</div>);
|
|
44
|
+
|
|
45
|
+
expect(screen.getByTestId("result").textContent).toBe("");
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it("should render undefined as empty", () => {
|
|
49
|
+
const value = atom<string | undefined>(undefined);
|
|
50
|
+
|
|
51
|
+
render(<div data-testid="result">{rx(value)}</div>);
|
|
52
|
+
|
|
53
|
+
expect(screen.getByTestId("result").textContent).toBe("");
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
describe("multiple atoms with context selector", () => {
|
|
58
|
+
it("should render derived value from multiple atoms", () => {
|
|
59
|
+
const firstName = atom("John");
|
|
60
|
+
const lastName = atom("Doe");
|
|
61
|
+
|
|
62
|
+
render(
|
|
63
|
+
<div data-testid="result">
|
|
64
|
+
{rx(({ get }) => `${get(firstName)} ${get(lastName)}`)}
|
|
65
|
+
</div>
|
|
66
|
+
);
|
|
67
|
+
|
|
68
|
+
expect(screen.getByTestId("result").textContent).toBe("John Doe");
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it("should render computed value from multiple numeric atoms", () => {
|
|
72
|
+
const a = atom(1);
|
|
73
|
+
const b = atom(2);
|
|
74
|
+
const c = atom(3);
|
|
75
|
+
|
|
76
|
+
render(
|
|
77
|
+
<div data-testid="result">
|
|
78
|
+
{rx(({ get }) => get(a) + get(b) + get(c))}
|
|
79
|
+
</div>
|
|
80
|
+
);
|
|
81
|
+
|
|
82
|
+
expect(screen.getByTestId("result").textContent).toBe("6");
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
describe("reactivity", () => {
|
|
87
|
+
it("should update when source atom changes (shorthand)", () => {
|
|
88
|
+
const count = atom(5);
|
|
89
|
+
|
|
90
|
+
render(<div data-testid="result">{rx(count)}</div>);
|
|
91
|
+
|
|
92
|
+
expect(screen.getByTestId("result").textContent).toBe("5");
|
|
93
|
+
|
|
94
|
+
act(() => {
|
|
95
|
+
count.set(10);
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
expect(screen.getByTestId("result").textContent).toBe("10");
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it("should update when source atom changes (context selector)", () => {
|
|
102
|
+
const count = atom(5);
|
|
103
|
+
|
|
104
|
+
render(<div data-testid="result">{rx(({ get }) => get(count) * 2)}</div>);
|
|
105
|
+
|
|
106
|
+
expect(screen.getByTestId("result").textContent).toBe("10");
|
|
107
|
+
|
|
108
|
+
act(() => {
|
|
109
|
+
count.set(10);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
expect(screen.getByTestId("result").textContent).toBe("20");
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it("should update when any dependency changes", () => {
|
|
116
|
+
const a = atom(1);
|
|
117
|
+
const b = atom(2);
|
|
118
|
+
|
|
119
|
+
render(
|
|
120
|
+
<div data-testid="result">{rx(({ get }) => get(a) + get(b))}</div>
|
|
121
|
+
);
|
|
122
|
+
|
|
123
|
+
expect(screen.getByTestId("result").textContent).toBe("3");
|
|
124
|
+
|
|
125
|
+
act(() => {
|
|
126
|
+
a.set(5);
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
expect(screen.getByTestId("result").textContent).toBe("7");
|
|
130
|
+
|
|
131
|
+
act(() => {
|
|
132
|
+
b.set(10);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
expect(screen.getByTestId("result").textContent).toBe("15");
|
|
136
|
+
});
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
describe("conditional dependencies", () => {
|
|
140
|
+
it("should only re-render when accessed dependencies change", () => {
|
|
141
|
+
const flag = atom(true);
|
|
142
|
+
const a = atom(1);
|
|
143
|
+
const b = atom(2);
|
|
144
|
+
|
|
145
|
+
const selectorFn = vi.fn(({ get }: SelectContext) =>
|
|
146
|
+
get(flag) ? get(a) : get(b)
|
|
147
|
+
);
|
|
148
|
+
|
|
149
|
+
render(<div data-testid="result">{rx(selectorFn)}</div>);
|
|
150
|
+
|
|
151
|
+
expect(screen.getByTestId("result").textContent).toBe("1");
|
|
152
|
+
const callCount = selectorFn.mock.calls.length;
|
|
153
|
+
|
|
154
|
+
// Change b - should NOT trigger re-render since b is not accessed
|
|
155
|
+
act(() => {
|
|
156
|
+
b.set(20);
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
// Selector should not have been called again
|
|
160
|
+
expect(selectorFn.mock.calls.length).toBe(callCount);
|
|
161
|
+
expect(screen.getByTestId("result").textContent).toBe("1");
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
it("should update subscriptions when dependencies change", () => {
|
|
165
|
+
const flag = atom(true);
|
|
166
|
+
const a = atom(1);
|
|
167
|
+
const b = atom(2);
|
|
168
|
+
|
|
169
|
+
render(
|
|
170
|
+
<div data-testid="result">
|
|
171
|
+
{rx(({ get }) => (get(flag) ? get(a) : get(b)))}
|
|
172
|
+
</div>
|
|
173
|
+
);
|
|
174
|
+
|
|
175
|
+
expect(screen.getByTestId("result").textContent).toBe("1");
|
|
176
|
+
|
|
177
|
+
// Change flag to false - now b should be accessed
|
|
178
|
+
act(() => {
|
|
179
|
+
flag.set(false);
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
expect(screen.getByTestId("result").textContent).toBe("2");
|
|
183
|
+
|
|
184
|
+
// Now change b - should trigger re-render
|
|
185
|
+
act(() => {
|
|
186
|
+
b.set(20);
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
expect(screen.getByTestId("result").textContent).toBe("20");
|
|
190
|
+
});
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
describe("equals option", () => {
|
|
194
|
+
it("should use shallow equality by default", () => {
|
|
195
|
+
const user = atom({ name: "John", age: 30 });
|
|
196
|
+
const renderCount = { current: 0 };
|
|
197
|
+
|
|
198
|
+
const TestComponent = () => {
|
|
199
|
+
renderCount.current++;
|
|
200
|
+
return (
|
|
201
|
+
<div data-testid="result">
|
|
202
|
+
{rx(({ get }) => JSON.stringify(get(user)))}
|
|
203
|
+
</div>
|
|
204
|
+
);
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
render(<TestComponent />);
|
|
208
|
+
|
|
209
|
+
expect(screen.getByTestId("result").textContent).toBe(
|
|
210
|
+
'{"name":"John","age":30}'
|
|
211
|
+
);
|
|
212
|
+
const initialRenderCount = renderCount.current;
|
|
213
|
+
|
|
214
|
+
// Set same content but different reference - should NOT re-render with shallow equality
|
|
215
|
+
act(() => {
|
|
216
|
+
user.set({ name: "John", age: 30 });
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
expect(renderCount.current).toBe(initialRenderCount);
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
it("should use strict equality when specified", () => {
|
|
223
|
+
const user = atom({ name: "John", age: 30 });
|
|
224
|
+
const selectorCallCount = { current: 0 };
|
|
225
|
+
|
|
226
|
+
const selector = ({ get }: SelectContext) => {
|
|
227
|
+
selectorCallCount.current++;
|
|
228
|
+
return JSON.stringify(get(user));
|
|
229
|
+
};
|
|
230
|
+
|
|
231
|
+
render(<div data-testid="result">{rx(selector, "strict")}</div>);
|
|
232
|
+
|
|
233
|
+
expect(screen.getByTestId("result").textContent).toBe(
|
|
234
|
+
'{"name":"John","age":30}'
|
|
235
|
+
);
|
|
236
|
+
const initialCallCount = selectorCallCount.current;
|
|
237
|
+
|
|
238
|
+
// Set same content but different reference - with strict equality,
|
|
239
|
+
// the selector result (string) will be different reference, causing re-render
|
|
240
|
+
act(() => {
|
|
241
|
+
user.set({ name: "John", age: 30 });
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
// Selector should have been called again due to atom change
|
|
245
|
+
expect(selectorCallCount.current).toBeGreaterThan(initialCallCount);
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
it("should use custom equality function", () => {
|
|
249
|
+
const user = atom({ id: 1, name: "John" });
|
|
250
|
+
const renderCount = { current: 0 };
|
|
251
|
+
|
|
252
|
+
const TestComponent = () => {
|
|
253
|
+
renderCount.current++;
|
|
254
|
+
return (
|
|
255
|
+
<div data-testid="result">
|
|
256
|
+
{rx(
|
|
257
|
+
({ get }) => JSON.stringify(get(user)),
|
|
258
|
+
(a, b) => a === b // Compare stringified values
|
|
259
|
+
)}
|
|
260
|
+
</div>
|
|
261
|
+
);
|
|
262
|
+
};
|
|
263
|
+
|
|
264
|
+
render(<TestComponent />);
|
|
265
|
+
|
|
266
|
+
expect(screen.getByTestId("result").textContent).toBe(
|
|
267
|
+
'{"id":1,"name":"John"}'
|
|
268
|
+
);
|
|
269
|
+
const initialRenderCount = renderCount.current;
|
|
270
|
+
|
|
271
|
+
// Same stringified value - should NOT re-render
|
|
272
|
+
act(() => {
|
|
273
|
+
user.set({ id: 1, name: "John" });
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
expect(renderCount.current).toBe(initialRenderCount);
|
|
277
|
+
});
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
describe("memoization", () => {
|
|
281
|
+
it("should not re-render parent when only rx value changes", () => {
|
|
282
|
+
const count = atom(5);
|
|
283
|
+
const parentRenderCount = { current: 0 };
|
|
284
|
+
|
|
285
|
+
const Parent = () => {
|
|
286
|
+
parentRenderCount.current++;
|
|
287
|
+
return (
|
|
288
|
+
<div data-testid="result">{rx(({ get }) => get(count) * 2)}</div>
|
|
289
|
+
);
|
|
290
|
+
};
|
|
291
|
+
|
|
292
|
+
render(<Parent />);
|
|
293
|
+
|
|
294
|
+
expect(screen.getByTestId("result").textContent).toBe("10");
|
|
295
|
+
const initialParentRenderCount = parentRenderCount.current;
|
|
296
|
+
|
|
297
|
+
act(() => {
|
|
298
|
+
count.set(10);
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
expect(screen.getByTestId("result").textContent).toBe("20");
|
|
302
|
+
// Parent should not re-render - only the memoized Rx component should
|
|
303
|
+
expect(parentRenderCount.current).toBe(initialParentRenderCount);
|
|
304
|
+
});
|
|
305
|
+
});
|
|
306
|
+
|
|
307
|
+
describe("edge cases", () => {
|
|
308
|
+
it("should handle zero value", () => {
|
|
309
|
+
const count = atom(0);
|
|
310
|
+
|
|
311
|
+
render(<div data-testid="result">{rx(count)}</div>);
|
|
312
|
+
|
|
313
|
+
expect(screen.getByTestId("result").textContent).toBe("0");
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
it("should handle false value", () => {
|
|
317
|
+
const flag = atom(false);
|
|
318
|
+
|
|
319
|
+
render(
|
|
320
|
+
<div data-testid="result">{rx(({ get }) => String(get(flag)))}</div>
|
|
321
|
+
);
|
|
322
|
+
|
|
323
|
+
expect(screen.getByTestId("result").textContent).toBe("false");
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
it("should handle empty string", () => {
|
|
327
|
+
const text = atom("");
|
|
328
|
+
|
|
329
|
+
render(<div data-testid="result">{rx(text)}</div>);
|
|
330
|
+
|
|
331
|
+
expect(screen.getByTestId("result").textContent).toBe("");
|
|
332
|
+
});
|
|
333
|
+
|
|
334
|
+
it("should handle rapid updates", () => {
|
|
335
|
+
const count = atom(0);
|
|
336
|
+
|
|
337
|
+
render(<div data-testid="result">{rx(count)}</div>);
|
|
338
|
+
|
|
339
|
+
act(() => {
|
|
340
|
+
for (let i = 1; i <= 100; i++) {
|
|
341
|
+
count.set(i);
|
|
342
|
+
}
|
|
343
|
+
});
|
|
344
|
+
|
|
345
|
+
expect(screen.getByTestId("result").textContent).toBe("100");
|
|
346
|
+
});
|
|
347
|
+
});
|
|
348
|
+
|
|
349
|
+
describe("async atoms", () => {
|
|
350
|
+
it("should handle selector that catches loading state", () => {
|
|
351
|
+
const asyncAtom = atom(Promise.resolve(10));
|
|
352
|
+
|
|
353
|
+
render(
|
|
354
|
+
<div data-testid="result">
|
|
355
|
+
{rx(({ get }) => {
|
|
356
|
+
try {
|
|
357
|
+
return get(asyncAtom) * 2;
|
|
358
|
+
} catch {
|
|
359
|
+
return "loading";
|
|
360
|
+
}
|
|
361
|
+
})}
|
|
362
|
+
</div>
|
|
363
|
+
);
|
|
364
|
+
|
|
365
|
+
// Initially loading - selector catches and returns "loading"
|
|
366
|
+
expect(screen.getByTestId("result").textContent).toBe("loading");
|
|
367
|
+
});
|
|
368
|
+
|
|
369
|
+
it("should update when source atom changes", async () => {
|
|
370
|
+
// v2: Test with sync atoms - derived atoms have async behavior
|
|
371
|
+
// For simpler reactivity testing, use sync atoms directly
|
|
372
|
+
const sourceAtom = atom(5);
|
|
373
|
+
|
|
374
|
+
render(
|
|
375
|
+
<div data-testid="result">{rx(({ get }) => get(sourceAtom) * 2)}</div>
|
|
376
|
+
);
|
|
377
|
+
|
|
378
|
+
expect(screen.getByTestId("result").textContent).toBe("10");
|
|
379
|
+
|
|
380
|
+
// Update source and verify updates
|
|
381
|
+
await act(async () => {
|
|
382
|
+
sourceAtom.set(10);
|
|
383
|
+
await new Promise((r) => setTimeout(r, 10));
|
|
384
|
+
});
|
|
385
|
+
|
|
386
|
+
expect(screen.getByTestId("result").textContent).toBe("20");
|
|
387
|
+
});
|
|
388
|
+
});
|
|
389
|
+
|
|
390
|
+
describe("async utilities", () => {
|
|
391
|
+
it("should support all() for multiple atoms", async () => {
|
|
392
|
+
// v2: Use sync atoms or derived atoms for proper reactivity
|
|
393
|
+
const a = atom(1);
|
|
394
|
+
const b = atom(2);
|
|
395
|
+
|
|
396
|
+
render(
|
|
397
|
+
<div data-testid="result">
|
|
398
|
+
{rx(({ all }) => {
|
|
399
|
+
const [valA, valB] = all(a, b);
|
|
400
|
+
return valA + valB;
|
|
401
|
+
})}
|
|
402
|
+
</div>
|
|
403
|
+
);
|
|
404
|
+
|
|
405
|
+
expect(screen.getByTestId("result").textContent).toBe("3");
|
|
406
|
+
|
|
407
|
+
// Update and verify
|
|
408
|
+
await act(async () => {
|
|
409
|
+
a.set(10);
|
|
410
|
+
await new Promise((r) => setTimeout(r, 10));
|
|
411
|
+
});
|
|
412
|
+
|
|
413
|
+
expect(screen.getByTestId("result").textContent).toBe("12");
|
|
414
|
+
});
|
|
415
|
+
});
|
|
416
|
+
});
|