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
|
@@ -0,0 +1,989 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
|
|
2
|
+
import { useAction } from "./useAction";
|
|
3
|
+
import { atom } from "../core/atom";
|
|
4
|
+
import { act } from "react";
|
|
5
|
+
import { wrappers } from "./strictModeTest";
|
|
6
|
+
|
|
7
|
+
describe.each(wrappers)("useAction - $mode", ({ mode, renderHook }) => {
|
|
8
|
+
beforeEach(() => {
|
|
9
|
+
vi.useFakeTimers();
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
afterEach(() => {
|
|
13
|
+
vi.useRealTimers();
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
describe("initial state", () => {
|
|
17
|
+
it("should start with idle status when lazy is true (default)", () => {
|
|
18
|
+
const fn = vi.fn(() => "result");
|
|
19
|
+
|
|
20
|
+
const { result } = renderHook(() => useAction(fn));
|
|
21
|
+
|
|
22
|
+
expect(result.current.status).toBe("idle");
|
|
23
|
+
expect(result.current.result).toBeUndefined();
|
|
24
|
+
expect(result.current.error).toBeUndefined();
|
|
25
|
+
expect(fn).not.toHaveBeenCalled();
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it("should execute on mount when lazy is false", () => {
|
|
29
|
+
const fn = vi.fn(() => "result");
|
|
30
|
+
|
|
31
|
+
const { result } = renderHook(() =>
|
|
32
|
+
useAction(fn, { lazy: false })
|
|
33
|
+
);
|
|
34
|
+
|
|
35
|
+
// Sync function completes immediately, so status is success
|
|
36
|
+
expect(result.current.status).toBe("success");
|
|
37
|
+
expect(result.current.result).toBe("result");
|
|
38
|
+
// In strict mode, effects run twice
|
|
39
|
+
expect(fn).toHaveBeenCalledTimes(mode === "strict" ? 2 : 1);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it("should start with loading status when lazy is false with async fn", () => {
|
|
43
|
+
const fn = vi.fn(
|
|
44
|
+
() =>
|
|
45
|
+
new Promise<string>((resolve) => {
|
|
46
|
+
setTimeout(() => resolve("result"), 1000);
|
|
47
|
+
})
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
const { result } = renderHook(() =>
|
|
51
|
+
useAction(fn, { lazy: false })
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
expect(result.current.status).toBe("loading");
|
|
55
|
+
// In strict mode, effects run twice
|
|
56
|
+
expect(fn).toHaveBeenCalledTimes(mode === "strict" ? 2 : 1);
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
describe("return shape", () => {
|
|
61
|
+
it("should return callable function with status, result, error, abort, reset properties", () => {
|
|
62
|
+
const fn = vi.fn(() => "result");
|
|
63
|
+
|
|
64
|
+
const { result } = renderHook(() => useAction(fn));
|
|
65
|
+
|
|
66
|
+
// Should be callable
|
|
67
|
+
expect(typeof result.current).toBe("function");
|
|
68
|
+
// Should have state properties
|
|
69
|
+
expect(result.current).toHaveProperty("status");
|
|
70
|
+
expect(result.current).toHaveProperty("result");
|
|
71
|
+
expect(result.current).toHaveProperty("error");
|
|
72
|
+
// Should have API methods
|
|
73
|
+
expect(result.current).toHaveProperty("abort");
|
|
74
|
+
expect(typeof result.current.abort).toBe("function");
|
|
75
|
+
expect(result.current).toHaveProperty("reset");
|
|
76
|
+
expect(typeof result.current.reset).toBe("function");
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
describe("synchronous actions", () => {
|
|
81
|
+
it("should transition to success on sync function", () => {
|
|
82
|
+
const fn = vi.fn(() => "sync-result");
|
|
83
|
+
|
|
84
|
+
const { result } = renderHook(() => useAction(fn));
|
|
85
|
+
|
|
86
|
+
act(() => {
|
|
87
|
+
result.current();
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
expect(result.current.status).toBe("success");
|
|
91
|
+
expect(result.current.result).toBe("sync-result");
|
|
92
|
+
expect(result.current.error).toBeUndefined();
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it("should transition to error when sync function throws", async () => {
|
|
96
|
+
const error = new Error("sync-error");
|
|
97
|
+
const fn = vi.fn((): void => {
|
|
98
|
+
throw error;
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
const { result } = renderHook(() => useAction(fn));
|
|
102
|
+
|
|
103
|
+
await act(async () => {
|
|
104
|
+
// calling action returns rejected promise, doesn't throw
|
|
105
|
+
const promise = result.current();
|
|
106
|
+
await expect(promise).rejects.toBe(error);
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
expect(result.current.status).toBe("error");
|
|
110
|
+
expect(result.current.result).toBeUndefined();
|
|
111
|
+
expect(result.current.error).toBe(error);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it("should return rejected promise for sync errors (no re-throw)", async () => {
|
|
115
|
+
const error = new Error("sync-error");
|
|
116
|
+
const fn = vi.fn((): string => {
|
|
117
|
+
throw error;
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
const { result } = renderHook(() => useAction(fn));
|
|
121
|
+
|
|
122
|
+
await act(async () => {
|
|
123
|
+
// Does not throw synchronously
|
|
124
|
+
const promise = result.current();
|
|
125
|
+
// But the promise rejects
|
|
126
|
+
await expect(promise).rejects.toBe(error);
|
|
127
|
+
});
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
it("should return promise that resolves with sync result", async () => {
|
|
131
|
+
const fn = vi.fn(() => 42);
|
|
132
|
+
|
|
133
|
+
const { result } = renderHook(() => useAction(fn));
|
|
134
|
+
|
|
135
|
+
let returnValue: number | undefined;
|
|
136
|
+
await act(async () => {
|
|
137
|
+
returnValue = await result.current();
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
expect(returnValue).toBe(42);
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
it("should return abortable promise even for sync functions", () => {
|
|
144
|
+
const fn = vi.fn(() => 42);
|
|
145
|
+
|
|
146
|
+
const { result } = renderHook(() => useAction(fn));
|
|
147
|
+
|
|
148
|
+
let promise: PromiseLike<number> & { abort: () => void };
|
|
149
|
+
act(() => {
|
|
150
|
+
promise = result.current();
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
expect(promise!).toHaveProperty("abort");
|
|
154
|
+
expect(typeof promise!.abort).toBe("function");
|
|
155
|
+
expect(typeof promise!.then).toBe("function");
|
|
156
|
+
});
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
describe("asynchronous actions", () => {
|
|
160
|
+
it("should transition through loading to success", async () => {
|
|
161
|
+
let resolve: (value: string) => void;
|
|
162
|
+
const promise = new Promise<string>((r) => {
|
|
163
|
+
resolve = r;
|
|
164
|
+
});
|
|
165
|
+
const fn = vi.fn(() => promise);
|
|
166
|
+
|
|
167
|
+
const { result } = renderHook(() => useAction(fn));
|
|
168
|
+
|
|
169
|
+
act(() => {
|
|
170
|
+
result.current();
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
expect(result.current.status).toBe("loading");
|
|
174
|
+
expect(result.current.result).toBeUndefined();
|
|
175
|
+
expect(result.current.error).toBeUndefined();
|
|
176
|
+
|
|
177
|
+
await act(async () => {
|
|
178
|
+
resolve!("async-result");
|
|
179
|
+
await promise;
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
expect(result.current.status).toBe("success");
|
|
183
|
+
expect(result.current.result).toBe("async-result");
|
|
184
|
+
expect(result.current.error).toBeUndefined();
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
it("should transition through loading to error on rejection", async () => {
|
|
188
|
+
const error = new Error("async-error");
|
|
189
|
+
let reject: (error: Error) => void;
|
|
190
|
+
const promise = new Promise<string>((_, r) => {
|
|
191
|
+
reject = r;
|
|
192
|
+
});
|
|
193
|
+
const fn = vi.fn(() => promise);
|
|
194
|
+
|
|
195
|
+
const { result } = renderHook(() => useAction(fn));
|
|
196
|
+
|
|
197
|
+
act(() => {
|
|
198
|
+
result.current();
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
expect(result.current.status).toBe("loading");
|
|
202
|
+
|
|
203
|
+
await act(async () => {
|
|
204
|
+
reject!(error);
|
|
205
|
+
await promise.catch(() => {});
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
expect(result.current.status).toBe("error");
|
|
209
|
+
expect(result.current.result).toBeUndefined();
|
|
210
|
+
expect(result.current.error).toBe(error);
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
it("should return abortable promise from action call", async () => {
|
|
214
|
+
let resolve: (value: string) => void;
|
|
215
|
+
const promise = new Promise<string>((r) => {
|
|
216
|
+
resolve = r;
|
|
217
|
+
});
|
|
218
|
+
const fn = vi.fn(() => promise);
|
|
219
|
+
|
|
220
|
+
const { result } = renderHook(() => useAction(fn));
|
|
221
|
+
|
|
222
|
+
let returnValue: PromiseLike<string> & { abort: () => void };
|
|
223
|
+
act(() => {
|
|
224
|
+
returnValue = result.current() as any;
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
expect(returnValue!).toHaveProperty("abort");
|
|
228
|
+
expect(typeof returnValue!.abort).toBe("function");
|
|
229
|
+
expect(typeof returnValue!.then).toBe("function");
|
|
230
|
+
|
|
231
|
+
await act(async () => {
|
|
232
|
+
resolve!("result");
|
|
233
|
+
await returnValue;
|
|
234
|
+
});
|
|
235
|
+
});
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
describe("AbortSignal", () => {
|
|
239
|
+
it("should pass AbortSignal to the function", () => {
|
|
240
|
+
const fn = vi.fn(({ signal }: { signal: AbortSignal }) => {
|
|
241
|
+
expect(signal).toBeInstanceOf(AbortSignal);
|
|
242
|
+
return "result";
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
const { result } = renderHook(() => useAction(fn));
|
|
246
|
+
|
|
247
|
+
act(() => {
|
|
248
|
+
result.current();
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
expect(fn).toHaveBeenCalledWith({ signal: expect.any(AbortSignal) });
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
it("should create new AbortSignal per call", () => {
|
|
255
|
+
const signals: AbortSignal[] = [];
|
|
256
|
+
const fn = vi.fn(({ signal }: { signal: AbortSignal }) => {
|
|
257
|
+
signals.push(signal);
|
|
258
|
+
return "result";
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
const { result } = renderHook(() => useAction(fn, { exclusive: false }));
|
|
262
|
+
|
|
263
|
+
act(() => {
|
|
264
|
+
result.current();
|
|
265
|
+
result.current();
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
expect(signals).toHaveLength(2);
|
|
269
|
+
expect(signals[0]).not.toBe(signals[1]);
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
it("should abort previous signal when exclusive is true (default)", async () => {
|
|
273
|
+
const signals: AbortSignal[] = [];
|
|
274
|
+
const fn = vi.fn(({ signal }: { signal: AbortSignal }) => {
|
|
275
|
+
signals.push(signal);
|
|
276
|
+
return new Promise<string>((resolve) => {
|
|
277
|
+
setTimeout(() => resolve("result"), 1000);
|
|
278
|
+
});
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
const { result } = renderHook(() => useAction(fn));
|
|
282
|
+
|
|
283
|
+
act(() => {
|
|
284
|
+
result.current();
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
expect(signals[0].aborted).toBe(false);
|
|
288
|
+
|
|
289
|
+
act(() => {
|
|
290
|
+
result.current();
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
expect(signals[0].aborted).toBe(true);
|
|
294
|
+
expect(signals[1].aborted).toBe(false);
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
it("should NOT abort previous signal when exclusive is false", () => {
|
|
298
|
+
const signals: AbortSignal[] = [];
|
|
299
|
+
const fn = vi.fn(({ signal }: { signal: AbortSignal }) => {
|
|
300
|
+
signals.push(signal);
|
|
301
|
+
return new Promise<string>((resolve) => {
|
|
302
|
+
setTimeout(() => resolve("result"), 1000);
|
|
303
|
+
});
|
|
304
|
+
});
|
|
305
|
+
|
|
306
|
+
const { result } = renderHook(() => useAction(fn, { exclusive: false }));
|
|
307
|
+
|
|
308
|
+
act(() => {
|
|
309
|
+
result.current();
|
|
310
|
+
result.current();
|
|
311
|
+
});
|
|
312
|
+
|
|
313
|
+
expect(signals[0].aborted).toBe(false);
|
|
314
|
+
expect(signals[1].aborted).toBe(false);
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
it("should abort on unmount when exclusive is true", () => {
|
|
318
|
+
let capturedSignal: AbortSignal;
|
|
319
|
+
const fn = vi.fn(({ signal }: { signal: AbortSignal }) => {
|
|
320
|
+
capturedSignal = signal;
|
|
321
|
+
return new Promise<string>((resolve) => {
|
|
322
|
+
setTimeout(() => resolve("result"), 1000);
|
|
323
|
+
});
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
const { result, unmount } = renderHook(() => useAction(fn));
|
|
327
|
+
|
|
328
|
+
act(() => {
|
|
329
|
+
result.current();
|
|
330
|
+
});
|
|
331
|
+
|
|
332
|
+
expect(capturedSignal!.aborted).toBe(false);
|
|
333
|
+
|
|
334
|
+
unmount();
|
|
335
|
+
|
|
336
|
+
expect(capturedSignal!.aborted).toBe(true);
|
|
337
|
+
});
|
|
338
|
+
|
|
339
|
+
it("should NOT abort on unmount when exclusive is false", () => {
|
|
340
|
+
let capturedSignal: AbortSignal;
|
|
341
|
+
const fn = vi.fn(({ signal }: { signal: AbortSignal }) => {
|
|
342
|
+
capturedSignal = signal;
|
|
343
|
+
return new Promise<string>((resolve) => {
|
|
344
|
+
setTimeout(() => resolve("result"), 1000);
|
|
345
|
+
});
|
|
346
|
+
});
|
|
347
|
+
|
|
348
|
+
const { result, unmount } = renderHook(() =>
|
|
349
|
+
useAction(fn, { exclusive: false })
|
|
350
|
+
);
|
|
351
|
+
|
|
352
|
+
act(() => {
|
|
353
|
+
result.current();
|
|
354
|
+
});
|
|
355
|
+
|
|
356
|
+
expect(capturedSignal!.aborted).toBe(false);
|
|
357
|
+
|
|
358
|
+
unmount();
|
|
359
|
+
|
|
360
|
+
expect(capturedSignal!.aborted).toBe(false);
|
|
361
|
+
});
|
|
362
|
+
|
|
363
|
+
it("should allow manual abort via abort() method", () => {
|
|
364
|
+
let capturedSignal: AbortSignal;
|
|
365
|
+
const fn = vi.fn(({ signal }: { signal: AbortSignal }) => {
|
|
366
|
+
capturedSignal = signal;
|
|
367
|
+
return new Promise<string>((resolve) => {
|
|
368
|
+
setTimeout(() => resolve("result"), 1000);
|
|
369
|
+
});
|
|
370
|
+
});
|
|
371
|
+
|
|
372
|
+
const { result } = renderHook(() => useAction(fn, { exclusive: false }));
|
|
373
|
+
|
|
374
|
+
act(() => {
|
|
375
|
+
result.current();
|
|
376
|
+
});
|
|
377
|
+
|
|
378
|
+
expect(capturedSignal!.aborted).toBe(false);
|
|
379
|
+
|
|
380
|
+
act(() => {
|
|
381
|
+
result.current.abort();
|
|
382
|
+
});
|
|
383
|
+
|
|
384
|
+
expect(capturedSignal!.aborted).toBe(true);
|
|
385
|
+
});
|
|
386
|
+
|
|
387
|
+
it("should allow manual abort via returned promise.abort()", () => {
|
|
388
|
+
let capturedSignal: AbortSignal;
|
|
389
|
+
const fn = vi.fn(({ signal }: { signal: AbortSignal }) => {
|
|
390
|
+
capturedSignal = signal;
|
|
391
|
+
return new Promise<string>((resolve) => {
|
|
392
|
+
setTimeout(() => resolve("result"), 1000);
|
|
393
|
+
});
|
|
394
|
+
});
|
|
395
|
+
|
|
396
|
+
const { result } = renderHook(() => useAction(fn, { exclusive: false }));
|
|
397
|
+
|
|
398
|
+
let returnedPromise: PromiseLike<string> & { abort: () => void };
|
|
399
|
+
act(() => {
|
|
400
|
+
returnedPromise = result.current() as any;
|
|
401
|
+
});
|
|
402
|
+
|
|
403
|
+
expect(capturedSignal!.aborted).toBe(false);
|
|
404
|
+
|
|
405
|
+
act(() => {
|
|
406
|
+
returnedPromise.abort();
|
|
407
|
+
});
|
|
408
|
+
|
|
409
|
+
expect(capturedSignal!.aborted).toBe(true);
|
|
410
|
+
});
|
|
411
|
+
});
|
|
412
|
+
|
|
413
|
+
describe("reset", () => {
|
|
414
|
+
it("should reset state to idle after success", async () => {
|
|
415
|
+
const fn = vi.fn(() => "result");
|
|
416
|
+
|
|
417
|
+
const { result } = renderHook(() => useAction(fn));
|
|
418
|
+
|
|
419
|
+
await act(async () => {
|
|
420
|
+
await result.current();
|
|
421
|
+
});
|
|
422
|
+
|
|
423
|
+
expect(result.current.status).toBe("success");
|
|
424
|
+
expect(result.current.result).toBe("result");
|
|
425
|
+
|
|
426
|
+
act(() => {
|
|
427
|
+
result.current.reset();
|
|
428
|
+
});
|
|
429
|
+
|
|
430
|
+
expect(result.current.status).toBe("idle");
|
|
431
|
+
expect(result.current.result).toBeUndefined();
|
|
432
|
+
expect(result.current.error).toBeUndefined();
|
|
433
|
+
});
|
|
434
|
+
|
|
435
|
+
it("should reset state to idle after error", async () => {
|
|
436
|
+
const error = new Error("test-error");
|
|
437
|
+
const fn = vi.fn(() => {
|
|
438
|
+
throw error;
|
|
439
|
+
});
|
|
440
|
+
|
|
441
|
+
const { result } = renderHook(() => useAction(fn));
|
|
442
|
+
|
|
443
|
+
await act(async () => {
|
|
444
|
+
await result.current().then(
|
|
445
|
+
() => {},
|
|
446
|
+
() => {}
|
|
447
|
+
);
|
|
448
|
+
});
|
|
449
|
+
|
|
450
|
+
expect(result.current.status).toBe("error");
|
|
451
|
+
expect(result.current.error).toBe(error);
|
|
452
|
+
|
|
453
|
+
act(() => {
|
|
454
|
+
result.current.reset();
|
|
455
|
+
});
|
|
456
|
+
|
|
457
|
+
expect(result.current.status).toBe("idle");
|
|
458
|
+
expect(result.current.result).toBeUndefined();
|
|
459
|
+
expect(result.current.error).toBeUndefined();
|
|
460
|
+
});
|
|
461
|
+
|
|
462
|
+
it("should abort in-flight request when reset is called with exclusive: true", () => {
|
|
463
|
+
let capturedSignal: AbortSignal;
|
|
464
|
+
const fn = vi.fn(({ signal }: { signal: AbortSignal }) => {
|
|
465
|
+
capturedSignal = signal;
|
|
466
|
+
return new Promise<string>((resolve) => {
|
|
467
|
+
setTimeout(() => resolve("result"), 1000);
|
|
468
|
+
});
|
|
469
|
+
});
|
|
470
|
+
|
|
471
|
+
const { result } = renderHook(() => useAction(fn));
|
|
472
|
+
|
|
473
|
+
act(() => {
|
|
474
|
+
result.current();
|
|
475
|
+
});
|
|
476
|
+
|
|
477
|
+
expect(result.current.status).toBe("loading");
|
|
478
|
+
expect(capturedSignal!.aborted).toBe(false);
|
|
479
|
+
|
|
480
|
+
act(() => {
|
|
481
|
+
result.current.reset();
|
|
482
|
+
});
|
|
483
|
+
|
|
484
|
+
expect(result.current.status).toBe("idle");
|
|
485
|
+
expect(capturedSignal!.aborted).toBe(true);
|
|
486
|
+
});
|
|
487
|
+
|
|
488
|
+
it("should NOT abort in-flight request when reset is called with exclusive: false", () => {
|
|
489
|
+
let capturedSignal: AbortSignal;
|
|
490
|
+
const fn = vi.fn(({ signal }: { signal: AbortSignal }) => {
|
|
491
|
+
capturedSignal = signal;
|
|
492
|
+
return new Promise<string>((resolve) => {
|
|
493
|
+
setTimeout(() => resolve("result"), 1000);
|
|
494
|
+
});
|
|
495
|
+
});
|
|
496
|
+
|
|
497
|
+
const { result } = renderHook(() => useAction(fn, { exclusive: false }));
|
|
498
|
+
|
|
499
|
+
act(() => {
|
|
500
|
+
result.current();
|
|
501
|
+
});
|
|
502
|
+
|
|
503
|
+
expect(result.current.status).toBe("loading");
|
|
504
|
+
expect(capturedSignal!.aborted).toBe(false);
|
|
505
|
+
|
|
506
|
+
act(() => {
|
|
507
|
+
result.current.reset();
|
|
508
|
+
});
|
|
509
|
+
|
|
510
|
+
// State is reset but request continues
|
|
511
|
+
expect(result.current.status).toBe("idle");
|
|
512
|
+
expect(capturedSignal!.aborted).toBe(false);
|
|
513
|
+
});
|
|
514
|
+
|
|
515
|
+
it("should allow re-dispatch after reset", async () => {
|
|
516
|
+
let counter = 0;
|
|
517
|
+
const fn = vi.fn(() => ++counter);
|
|
518
|
+
|
|
519
|
+
const { result } = renderHook(() => useAction(fn));
|
|
520
|
+
|
|
521
|
+
await act(async () => {
|
|
522
|
+
await result.current();
|
|
523
|
+
});
|
|
524
|
+
|
|
525
|
+
expect(result.current.result).toBe(1);
|
|
526
|
+
|
|
527
|
+
act(() => {
|
|
528
|
+
result.current.reset();
|
|
529
|
+
});
|
|
530
|
+
|
|
531
|
+
expect(result.current.status).toBe("idle");
|
|
532
|
+
|
|
533
|
+
await act(async () => {
|
|
534
|
+
await result.current();
|
|
535
|
+
});
|
|
536
|
+
|
|
537
|
+
expect(result.current.status).toBe("success");
|
|
538
|
+
expect(result.current.result).toBe(2);
|
|
539
|
+
});
|
|
540
|
+
});
|
|
541
|
+
|
|
542
|
+
describe("eager execution (lazy: false)", () => {
|
|
543
|
+
it("should execute on mount when lazy is false", () => {
|
|
544
|
+
const fn = vi.fn(() => "result");
|
|
545
|
+
|
|
546
|
+
renderHook(() => useAction(fn, { lazy: false }));
|
|
547
|
+
|
|
548
|
+
// In strict mode, effects run twice
|
|
549
|
+
expect(fn).toHaveBeenCalledTimes(mode === "strict" ? 2 : 1);
|
|
550
|
+
});
|
|
551
|
+
|
|
552
|
+
it("should re-execute when deps change", () => {
|
|
553
|
+
const fn = vi.fn(() => "result");
|
|
554
|
+
|
|
555
|
+
const { rerender } = renderHook(
|
|
556
|
+
({ dep }) => useAction(fn, { lazy: false, deps: [dep] }),
|
|
557
|
+
{ initialProps: { dep: 1 } }
|
|
558
|
+
);
|
|
559
|
+
|
|
560
|
+
// In strict mode, effects run twice on mount
|
|
561
|
+
const initialCalls = mode === "strict" ? 2 : 1;
|
|
562
|
+
expect(fn).toHaveBeenCalledTimes(initialCalls);
|
|
563
|
+
|
|
564
|
+
rerender({ dep: 2 });
|
|
565
|
+
|
|
566
|
+
// +1 for dep change
|
|
567
|
+
expect(fn).toHaveBeenCalledTimes(initialCalls + 1);
|
|
568
|
+
});
|
|
569
|
+
|
|
570
|
+
it("should NOT re-execute when deps are the same", () => {
|
|
571
|
+
const fn = vi.fn(() => "result");
|
|
572
|
+
|
|
573
|
+
const { rerender } = renderHook(
|
|
574
|
+
({ dep }) => useAction(fn, { lazy: false, deps: [dep] }),
|
|
575
|
+
{ initialProps: { dep: 1 } }
|
|
576
|
+
);
|
|
577
|
+
|
|
578
|
+
// In strict mode, effects run twice on mount
|
|
579
|
+
const initialCalls = mode === "strict" ? 2 : 1;
|
|
580
|
+
expect(fn).toHaveBeenCalledTimes(initialCalls);
|
|
581
|
+
|
|
582
|
+
rerender({ dep: 1 });
|
|
583
|
+
|
|
584
|
+
// No additional calls when deps are the same
|
|
585
|
+
expect(fn).toHaveBeenCalledTimes(initialCalls);
|
|
586
|
+
});
|
|
587
|
+
|
|
588
|
+
it("should abort previous when deps change and exclusive is true", async () => {
|
|
589
|
+
const signals: AbortSignal[] = [];
|
|
590
|
+
const fn = vi.fn(({ signal }: { signal: AbortSignal }) => {
|
|
591
|
+
signals.push(signal);
|
|
592
|
+
return new Promise<string>((resolve) => {
|
|
593
|
+
setTimeout(() => resolve("result"), 1000);
|
|
594
|
+
});
|
|
595
|
+
});
|
|
596
|
+
|
|
597
|
+
const { rerender } = renderHook(
|
|
598
|
+
({ dep }) => useAction(fn, { lazy: false, deps: [dep] }),
|
|
599
|
+
{ initialProps: { dep: 1 } }
|
|
600
|
+
);
|
|
601
|
+
|
|
602
|
+
// In strict mode, first signal is aborted by second mount effect
|
|
603
|
+
const firstSignalIndex = mode === "strict" ? 1 : 0;
|
|
604
|
+
expect(signals[firstSignalIndex].aborted).toBe(false);
|
|
605
|
+
|
|
606
|
+
rerender({ dep: 2 });
|
|
607
|
+
|
|
608
|
+
// Previous signal should be aborted
|
|
609
|
+
expect(signals[firstSignalIndex].aborted).toBe(true);
|
|
610
|
+
// New signal should not be aborted
|
|
611
|
+
expect(signals[signals.length - 1].aborted).toBe(false);
|
|
612
|
+
});
|
|
613
|
+
});
|
|
614
|
+
|
|
615
|
+
describe("stale closure prevention", () => {
|
|
616
|
+
it("should ignore stale async results", async () => {
|
|
617
|
+
let resolvers: Array<(value: string) => void> = [];
|
|
618
|
+
const fn = vi.fn(
|
|
619
|
+
() =>
|
|
620
|
+
new Promise<string>((resolve) => {
|
|
621
|
+
resolvers.push(resolve);
|
|
622
|
+
})
|
|
623
|
+
);
|
|
624
|
+
|
|
625
|
+
const { result } = renderHook(() => useAction(fn));
|
|
626
|
+
|
|
627
|
+
// First call
|
|
628
|
+
act(() => {
|
|
629
|
+
result.current();
|
|
630
|
+
});
|
|
631
|
+
|
|
632
|
+
// Second call (should abort first)
|
|
633
|
+
act(() => {
|
|
634
|
+
result.current();
|
|
635
|
+
});
|
|
636
|
+
|
|
637
|
+
// Resolve first (stale) - should be ignored
|
|
638
|
+
await act(async () => {
|
|
639
|
+
resolvers[0]("first-result");
|
|
640
|
+
await Promise.resolve();
|
|
641
|
+
});
|
|
642
|
+
|
|
643
|
+
// State should still be loading (waiting for second)
|
|
644
|
+
expect(result.current.status).toBe("loading");
|
|
645
|
+
|
|
646
|
+
// Resolve second
|
|
647
|
+
await act(async () => {
|
|
648
|
+
resolvers[1]("second-result");
|
|
649
|
+
await Promise.resolve();
|
|
650
|
+
});
|
|
651
|
+
|
|
652
|
+
expect(result.current.status).toBe("success");
|
|
653
|
+
expect(result.current.result).toBe("second-result");
|
|
654
|
+
});
|
|
655
|
+
});
|
|
656
|
+
|
|
657
|
+
describe("edge cases", () => {
|
|
658
|
+
it("should handle rapid calls", async () => {
|
|
659
|
+
let counter = 0;
|
|
660
|
+
const fn = vi.fn(
|
|
661
|
+
() =>
|
|
662
|
+
new Promise<number>((resolve) => {
|
|
663
|
+
const current = ++counter;
|
|
664
|
+
setTimeout(() => resolve(current), 100);
|
|
665
|
+
})
|
|
666
|
+
);
|
|
667
|
+
|
|
668
|
+
const { result } = renderHook(() => useAction(fn));
|
|
669
|
+
|
|
670
|
+
// Rapid fire calls
|
|
671
|
+
act(() => {
|
|
672
|
+
result.current();
|
|
673
|
+
result.current();
|
|
674
|
+
result.current();
|
|
675
|
+
});
|
|
676
|
+
|
|
677
|
+
await act(async () => {
|
|
678
|
+
vi.advanceTimersByTime(100);
|
|
679
|
+
await Promise.resolve();
|
|
680
|
+
});
|
|
681
|
+
|
|
682
|
+
// Only the last result should be used
|
|
683
|
+
expect(result.current.status).toBe("success");
|
|
684
|
+
expect(result.current.result).toBe(3);
|
|
685
|
+
});
|
|
686
|
+
|
|
687
|
+
it("should handle re-call after error", async () => {
|
|
688
|
+
let shouldFail = true;
|
|
689
|
+
const fn = vi.fn(() => {
|
|
690
|
+
if (shouldFail) {
|
|
691
|
+
throw new Error("fail");
|
|
692
|
+
}
|
|
693
|
+
return "success";
|
|
694
|
+
});
|
|
695
|
+
|
|
696
|
+
const { result } = renderHook(() => useAction(fn));
|
|
697
|
+
|
|
698
|
+
await act(async () => {
|
|
699
|
+
// First call returns rejected promise
|
|
700
|
+
await result.current().then(
|
|
701
|
+
() => {},
|
|
702
|
+
() => {}
|
|
703
|
+
);
|
|
704
|
+
});
|
|
705
|
+
|
|
706
|
+
expect(result.current.status).toBe("error");
|
|
707
|
+
|
|
708
|
+
shouldFail = false;
|
|
709
|
+
|
|
710
|
+
await act(async () => {
|
|
711
|
+
await result.current();
|
|
712
|
+
});
|
|
713
|
+
|
|
714
|
+
expect(result.current.status).toBe("success");
|
|
715
|
+
expect(result.current.result).toBe("success");
|
|
716
|
+
});
|
|
717
|
+
|
|
718
|
+
it("should handle re-call after success", async () => {
|
|
719
|
+
let counter = 0;
|
|
720
|
+
const fn = vi.fn(() => ++counter);
|
|
721
|
+
|
|
722
|
+
const { result } = renderHook(() => useAction(fn));
|
|
723
|
+
|
|
724
|
+
await act(async () => {
|
|
725
|
+
await result.current();
|
|
726
|
+
});
|
|
727
|
+
|
|
728
|
+
expect(result.current.result).toBe(1);
|
|
729
|
+
|
|
730
|
+
await act(async () => {
|
|
731
|
+
await result.current();
|
|
732
|
+
});
|
|
733
|
+
|
|
734
|
+
expect(result.current.result).toBe(2);
|
|
735
|
+
});
|
|
736
|
+
});
|
|
737
|
+
|
|
738
|
+
describe("chaining operations", () => {
|
|
739
|
+
it("should support async/await chaining with sync functions", async () => {
|
|
740
|
+
const fn1 = vi.fn(() => 1);
|
|
741
|
+
const fn2 = vi.fn(() => 2);
|
|
742
|
+
|
|
743
|
+
const { result: result1 } = renderHook(() => useAction(fn1));
|
|
744
|
+
const { result: result2 } = renderHook(() => useAction(fn2));
|
|
745
|
+
|
|
746
|
+
let values: number[] = [];
|
|
747
|
+
await act(async () => {
|
|
748
|
+
const value1 = await result1.current();
|
|
749
|
+
values.push(value1);
|
|
750
|
+
const value2 = await result2.current();
|
|
751
|
+
values.push(value2);
|
|
752
|
+
});
|
|
753
|
+
|
|
754
|
+
expect(values).toEqual([1, 2]);
|
|
755
|
+
});
|
|
756
|
+
|
|
757
|
+
it("should support async/await chaining with async functions", async () => {
|
|
758
|
+
const fn1 = vi.fn(async () => {
|
|
759
|
+
return 1;
|
|
760
|
+
});
|
|
761
|
+
const fn2 = vi.fn(async () => {
|
|
762
|
+
return 2;
|
|
763
|
+
});
|
|
764
|
+
|
|
765
|
+
const { result: result1 } = renderHook(() => useAction(fn1));
|
|
766
|
+
const { result: result2 } = renderHook(() => useAction(fn2));
|
|
767
|
+
|
|
768
|
+
let values: number[] = [];
|
|
769
|
+
await act(async () => {
|
|
770
|
+
values.push(await result1.current());
|
|
771
|
+
values.push(await result2.current());
|
|
772
|
+
});
|
|
773
|
+
|
|
774
|
+
expect(values).toEqual([1, 2]);
|
|
775
|
+
});
|
|
776
|
+
|
|
777
|
+
it("should stop chain on error with try/catch", async () => {
|
|
778
|
+
const error = new Error("chain-error");
|
|
779
|
+
const fn1 = vi.fn(() => {
|
|
780
|
+
throw error;
|
|
781
|
+
});
|
|
782
|
+
const fn2 = vi.fn(() => "should not run");
|
|
783
|
+
|
|
784
|
+
const { result: result1 } = renderHook(() => useAction(fn1));
|
|
785
|
+
const { result: result2 } = renderHook(() => useAction(fn2));
|
|
786
|
+
|
|
787
|
+
let caughtError: unknown;
|
|
788
|
+
await act(async () => {
|
|
789
|
+
try {
|
|
790
|
+
await result1.current();
|
|
791
|
+
await result2.current();
|
|
792
|
+
} catch (e) {
|
|
793
|
+
caughtError = e;
|
|
794
|
+
}
|
|
795
|
+
});
|
|
796
|
+
|
|
797
|
+
expect(caughtError).toBe(error);
|
|
798
|
+
expect(fn2).not.toHaveBeenCalled();
|
|
799
|
+
});
|
|
800
|
+
});
|
|
801
|
+
|
|
802
|
+
describe("atom deps", () => {
|
|
803
|
+
it("should execute when atom in deps changes", () => {
|
|
804
|
+
const userId = atom(1);
|
|
805
|
+
const fn = vi.fn(() => `user-${userId.value}`);
|
|
806
|
+
|
|
807
|
+
const { result } = renderHook(() =>
|
|
808
|
+
useAction(fn, { lazy: false, deps: [userId] })
|
|
809
|
+
);
|
|
810
|
+
|
|
811
|
+
// In strict mode, effects run twice on mount
|
|
812
|
+
const initialCalls = mode === "strict" ? 2 : 1;
|
|
813
|
+
expect(fn).toHaveBeenCalledTimes(initialCalls);
|
|
814
|
+
expect(result.current.result).toBe("user-1");
|
|
815
|
+
|
|
816
|
+
// Change atom value
|
|
817
|
+
act(() => {
|
|
818
|
+
userId.set(2);
|
|
819
|
+
});
|
|
820
|
+
|
|
821
|
+
expect(fn).toHaveBeenCalledTimes(initialCalls + 1);
|
|
822
|
+
expect(result.current.result).toBe("user-2");
|
|
823
|
+
});
|
|
824
|
+
|
|
825
|
+
it("should NOT re-execute when atom value is shallowly equal", () => {
|
|
826
|
+
// Use atom with shallow equals so it doesn't notify on shallow equal values
|
|
827
|
+
const config = atom({ page: 1 }, { equals: "shallow" });
|
|
828
|
+
const fn = vi.fn(() => `page-${config.value?.page}`);
|
|
829
|
+
|
|
830
|
+
renderHook(() => useAction(fn, { lazy: false, deps: [config] }));
|
|
831
|
+
|
|
832
|
+
// In strict mode, effects run twice on mount
|
|
833
|
+
const initialCalls = mode === "strict" ? 2 : 1;
|
|
834
|
+
expect(fn).toHaveBeenCalledTimes(initialCalls);
|
|
835
|
+
|
|
836
|
+
// Set same value (different reference but shallow equal)
|
|
837
|
+
// Atom with equals: "shallow" won't notify
|
|
838
|
+
act(() => {
|
|
839
|
+
config.set({ page: 1 });
|
|
840
|
+
});
|
|
841
|
+
|
|
842
|
+
// Should NOT re-execute because atom didn't notify (shallow equal)
|
|
843
|
+
expect(fn).toHaveBeenCalledTimes(initialCalls);
|
|
844
|
+
});
|
|
845
|
+
|
|
846
|
+
it("should NOT re-execute when selected atom values are shallowly equal", () => {
|
|
847
|
+
// Even if atom notifies, if the selected values are shallow equal,
|
|
848
|
+
// the effect should not re-run
|
|
849
|
+
const userId = atom(1);
|
|
850
|
+
const fn = vi.fn(() => `user-${userId.value}`);
|
|
851
|
+
|
|
852
|
+
renderHook(() => useAction(fn, { lazy: false, deps: [userId] }));
|
|
853
|
+
|
|
854
|
+
// In strict mode, effects run twice on mount
|
|
855
|
+
const initialCalls = mode === "strict" ? 2 : 1;
|
|
856
|
+
expect(fn).toHaveBeenCalledTimes(initialCalls);
|
|
857
|
+
|
|
858
|
+
// Set same primitive value - atom won't notify (strict equal)
|
|
859
|
+
act(() => {
|
|
860
|
+
userId.set(1);
|
|
861
|
+
});
|
|
862
|
+
|
|
863
|
+
// Should NOT re-execute
|
|
864
|
+
expect(fn).toHaveBeenCalledTimes(initialCalls);
|
|
865
|
+
});
|
|
866
|
+
|
|
867
|
+
it("should re-execute when atom value changes (not shallow equal)", () => {
|
|
868
|
+
const config = atom({ page: 1 });
|
|
869
|
+
const fn = vi.fn(() => `page-${config.value?.page}`);
|
|
870
|
+
|
|
871
|
+
const { result } = renderHook(() =>
|
|
872
|
+
useAction(fn, { lazy: false, deps: [config] })
|
|
873
|
+
);
|
|
874
|
+
|
|
875
|
+
// In strict mode, effects run twice on mount
|
|
876
|
+
const initialCalls = mode === "strict" ? 2 : 1;
|
|
877
|
+
expect(fn).toHaveBeenCalledTimes(initialCalls);
|
|
878
|
+
expect(result.current.result).toBe("page-1");
|
|
879
|
+
|
|
880
|
+
// Set different value
|
|
881
|
+
act(() => {
|
|
882
|
+
config.set({ page: 2 });
|
|
883
|
+
});
|
|
884
|
+
|
|
885
|
+
expect(fn).toHaveBeenCalledTimes(initialCalls + 1);
|
|
886
|
+
expect(result.current.result).toBe("page-2");
|
|
887
|
+
});
|
|
888
|
+
|
|
889
|
+
it("should work with mixed deps (atoms and regular values)", () => {
|
|
890
|
+
const userId = atom(1);
|
|
891
|
+
const fn = vi.fn(() => "result");
|
|
892
|
+
|
|
893
|
+
const { rerender } = renderHook(
|
|
894
|
+
({ extraDep }) =>
|
|
895
|
+
useAction(fn, { lazy: false, deps: [userId, extraDep] }),
|
|
896
|
+
{ initialProps: { extraDep: "a" } }
|
|
897
|
+
);
|
|
898
|
+
|
|
899
|
+
// In strict mode, effects run twice on mount
|
|
900
|
+
const initialCalls = mode === "strict" ? 2 : 1;
|
|
901
|
+
expect(fn).toHaveBeenCalledTimes(initialCalls);
|
|
902
|
+
|
|
903
|
+
// Change atom
|
|
904
|
+
act(() => {
|
|
905
|
+
userId.set(2);
|
|
906
|
+
});
|
|
907
|
+
|
|
908
|
+
expect(fn).toHaveBeenCalledTimes(initialCalls + 1);
|
|
909
|
+
|
|
910
|
+
// Change regular dep
|
|
911
|
+
rerender({ extraDep: "b" });
|
|
912
|
+
|
|
913
|
+
expect(fn).toHaveBeenCalledTimes(initialCalls + 2);
|
|
914
|
+
});
|
|
915
|
+
|
|
916
|
+
it("should NOT track atoms when lazy is true (default)", () => {
|
|
917
|
+
const userId = atom(1);
|
|
918
|
+
const fn = vi.fn(() => `user-${userId.value}`);
|
|
919
|
+
|
|
920
|
+
renderHook(() => useAction(fn, { lazy: true, deps: [userId] }));
|
|
921
|
+
|
|
922
|
+
// Should not execute at all
|
|
923
|
+
expect(fn).not.toHaveBeenCalled();
|
|
924
|
+
|
|
925
|
+
// Change atom - should still not execute
|
|
926
|
+
act(() => {
|
|
927
|
+
userId.set(2);
|
|
928
|
+
});
|
|
929
|
+
|
|
930
|
+
expect(fn).not.toHaveBeenCalled();
|
|
931
|
+
});
|
|
932
|
+
|
|
933
|
+
it("should abort previous request when atom changes and exclusive is true", () => {
|
|
934
|
+
const userId = atom(1);
|
|
935
|
+
const signals: AbortSignal[] = [];
|
|
936
|
+
const fn = vi.fn(({ signal }: { signal: AbortSignal }) => {
|
|
937
|
+
signals.push(signal);
|
|
938
|
+
return new Promise<string>((resolve) => {
|
|
939
|
+
setTimeout(() => resolve(`user-${userId.value}`), 1000);
|
|
940
|
+
});
|
|
941
|
+
});
|
|
942
|
+
|
|
943
|
+
renderHook(() => useAction(fn, { lazy: false, deps: [userId] }));
|
|
944
|
+
|
|
945
|
+
// In strict mode, first signal is aborted by second mount effect
|
|
946
|
+
const firstSignalIndex = mode === "strict" ? 1 : 0;
|
|
947
|
+
expect(signals[firstSignalIndex].aborted).toBe(false);
|
|
948
|
+
|
|
949
|
+
// Change atom - should abort previous
|
|
950
|
+
act(() => {
|
|
951
|
+
userId.set(2);
|
|
952
|
+
});
|
|
953
|
+
|
|
954
|
+
expect(signals[firstSignalIndex].aborted).toBe(true);
|
|
955
|
+
expect(signals[signals.length - 1].aborted).toBe(false);
|
|
956
|
+
});
|
|
957
|
+
|
|
958
|
+
it("should work with multiple atoms in deps", () => {
|
|
959
|
+
const userId = atom(1);
|
|
960
|
+
const orgId = atom(100);
|
|
961
|
+
const fn = vi.fn(() => `user-${userId.value}-org-${orgId.value}`);
|
|
962
|
+
|
|
963
|
+
const { result } = renderHook(() =>
|
|
964
|
+
useAction(fn, { lazy: false, deps: [userId, orgId] })
|
|
965
|
+
);
|
|
966
|
+
|
|
967
|
+
// In strict mode, effects run twice on mount
|
|
968
|
+
const initialCalls = mode === "strict" ? 2 : 1;
|
|
969
|
+
expect(fn).toHaveBeenCalledTimes(initialCalls);
|
|
970
|
+
expect(result.current.result).toBe("user-1-org-100");
|
|
971
|
+
|
|
972
|
+
// Change first atom
|
|
973
|
+
act(() => {
|
|
974
|
+
userId.set(2);
|
|
975
|
+
});
|
|
976
|
+
|
|
977
|
+
expect(fn).toHaveBeenCalledTimes(initialCalls + 1);
|
|
978
|
+
expect(result.current.result).toBe("user-2-org-100");
|
|
979
|
+
|
|
980
|
+
// Change second atom
|
|
981
|
+
act(() => {
|
|
982
|
+
orgId.set(200);
|
|
983
|
+
});
|
|
984
|
+
|
|
985
|
+
expect(fn).toHaveBeenCalledTimes(initialCalls + 2);
|
|
986
|
+
expect(result.current.result).toBe("user-2-org-200");
|
|
987
|
+
});
|
|
988
|
+
});
|
|
989
|
+
});
|