@assistant-ui/tap 0.5.14 → 0.6.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 +9 -8
- package/dist/core/ResourceFiber.d.ts.map +1 -1
- package/dist/core/ResourceFiber.js +3 -2
- package/dist/core/ResourceFiber.js.map +1 -1
- package/dist/core/context.d.ts +13 -6
- package/dist/core/context.d.ts.map +1 -1
- package/dist/core/context.js +19 -6
- package/dist/core/context.js.map +1 -1
- package/dist/core/createResourceRoot.d.ts +2 -1
- package/dist/core/createResourceRoot.d.ts.map +1 -1
- package/dist/core/createResourceRoot.js +2 -2
- package/dist/core/createResourceRoot.js.map +1 -1
- package/dist/core/helpers/execution-context.d.ts +2 -1
- package/dist/core/helpers/execution-context.d.ts.map +1 -1
- package/dist/core/helpers/execution-context.js +4 -1
- package/dist/core/helpers/execution-context.js.map +1 -1
- package/dist/core/react-dispatcher.d.ts +12 -0
- package/dist/core/react-dispatcher.d.ts.map +1 -0
- package/dist/core/react-dispatcher.js +62 -0
- package/dist/core/react-dispatcher.js.map +1 -0
- package/dist/core/scheduler.js +1 -1
- package/dist/core/scheduler.js.map +1 -1
- package/dist/core/types.d.ts +3 -3
- package/dist/hooks/index.d.ts +13 -0
- package/dist/hooks/index.js +13 -0
- package/dist/hooks/use.d.ts +9 -0
- package/dist/hooks/use.d.ts.map +1 -0
- package/dist/hooks/use.js +14 -0
- package/dist/hooks/use.js.map +1 -0
- package/dist/hooks/useCallback.d.ts +5 -0
- package/dist/hooks/useCallback.d.ts.map +1 -0
- package/dist/hooks/useCallback.js +9 -0
- package/dist/hooks/useCallback.js.map +1 -0
- package/dist/hooks/useEffect.d.ts +10 -0
- package/dist/hooks/useEffect.d.ts.map +1 -0
- package/dist/hooks/{tap-effect.js → useEffect.js} +7 -7
- package/dist/hooks/useEffect.js.map +1 -0
- package/dist/hooks/{tap-effect-event.d.ts → useEffectEvent.d.ts} +5 -5
- package/dist/hooks/useEffectEvent.d.ts.map +1 -0
- package/dist/hooks/{tap-effect-event.js → useEffectEvent.js} +12 -12
- package/dist/hooks/useEffectEvent.js.map +1 -0
- package/dist/hooks/useMemo.d.ts +5 -0
- package/dist/hooks/useMemo.d.ts.map +1 -0
- package/dist/hooks/{tap-memo.js → useMemo.js} +6 -6
- package/dist/hooks/useMemo.js.map +1 -0
- package/dist/hooks/useMemoCache.d.ts +10 -0
- package/dist/hooks/useMemoCache.d.ts.map +1 -0
- package/dist/hooks/useMemoCache.js +21 -0
- package/dist/hooks/useMemoCache.js.map +1 -0
- package/dist/hooks/useReducer.d.ts +21 -0
- package/dist/hooks/useReducer.d.ts.map +1 -0
- package/dist/hooks/{tap-reducer.js → useReducer.js} +10 -10
- package/dist/hooks/useReducer.js.map +1 -0
- package/dist/hooks/useRef.d.ts +11 -0
- package/dist/hooks/useRef.d.ts.map +1 -0
- package/dist/hooks/useRef.js +10 -0
- package/dist/hooks/useRef.js.map +1 -0
- package/dist/{react/use-resource.d.ts → hooks/useResource.d.ts} +3 -2
- package/dist/hooks/useResource.d.ts.map +1 -0
- package/dist/hooks/{tap-resource.js → useResource.js} +12 -12
- package/dist/hooks/useResource.js.map +1 -0
- package/dist/hooks/useResourceRoot.d.ts +20 -0
- package/dist/hooks/useResourceRoot.d.ts.map +1 -0
- package/dist/{tapResourceRoot.js → hooks/useResourceRoot.js} +30 -26
- package/dist/hooks/useResourceRoot.js.map +1 -0
- package/dist/hooks/{tap-resources.d.ts → useResources.d.ts} +4 -4
- package/dist/hooks/useResources.d.ts.map +1 -0
- package/dist/hooks/{tap-resources.js → useResources.js} +28 -23
- package/dist/hooks/useResources.js.map +1 -0
- package/dist/hooks/useState.d.ts +9 -0
- package/dist/hooks/useState.d.ts.map +1 -0
- package/dist/hooks/useState.js +11 -0
- package/dist/hooks/useState.js.map +1 -0
- package/dist/hooks/utils/useCell.d.ts +10 -0
- package/dist/hooks/utils/useCell.d.ts.map +1 -0
- package/dist/hooks/utils/{tapHook.js → useCell.js} +4 -4
- package/dist/hooks/utils/{tapHook.js.map → useCell.js.map} +1 -1
- package/dist/index.d.ts +3 -13
- package/dist/index.js +3 -13
- package/dist/react/hooks.d.ts +25 -0
- package/dist/react/hooks.d.ts.map +1 -0
- package/dist/react/hooks.js +69 -0
- package/dist/react/hooks.js.map +1 -0
- package/dist/react-shim/index.d.ts +19 -0
- package/dist/react-shim/index.d.ts.map +1 -0
- package/dist/react-shim/index.js +28 -0
- package/dist/react-shim/index.js.map +1 -0
- package/package.json +13 -16
- package/react-shim/package.json +4 -0
- package/src/__tests__/basic/resourceHandle.test.ts +7 -3
- package/src/__tests__/basic/tapEffect.basic.test.ts +19 -19
- package/src/__tests__/basic/tapReducer.basic.test.ts +14 -14
- package/src/__tests__/basic/tapResources.basic.test.ts +19 -14
- package/src/__tests__/basic/tapState.basic.test.ts +20 -20
- package/src/__tests__/errors/errors.effect-errors.test.ts +21 -21
- package/src/__tests__/errors/errors.render-errors.test.ts +18 -18
- package/src/__tests__/lifecycle/lifecycle.dependencies.test.ts +25 -25
- package/src/__tests__/lifecycle/lifecycle.mount-unmount.test.ts +17 -18
- package/src/__tests__/react/concurrent-mode.test.tsx +7 -7
- package/src/__tests__/react/react-shim.test.tsx +65 -0
- package/src/__tests__/react/useResource.test.tsx +172 -0
- package/src/__tests__/react-dispatcher.test.ts +74 -0
- package/src/__tests__/rules/rules.hook-count.test.ts +30 -29
- package/src/__tests__/rules/rules.hook-order.test.ts +27 -27
- package/src/__tests__/strictmode/react-strictmode-behavior.test.tsx +1 -1
- package/src/__tests__/strictmode/strictmode.test.ts +42 -42
- package/src/__tests__/strictmode/tap-strictmode-rerender-sources.test.ts +55 -55
- package/src/__tests__/test-utils.ts +2 -2
- package/src/core/ResourceFiber.ts +4 -1
- package/src/core/context.ts +31 -9
- package/src/core/createResourceRoot.ts +4 -4
- package/src/core/helpers/execution-context.ts +4 -0
- package/src/core/react-dispatcher.ts +78 -0
- package/src/core/scheduler.ts +1 -1
- package/src/core/types.ts +3 -3
- package/src/hooks/index.ts +12 -0
- package/src/hooks/use.ts +13 -0
- package/src/hooks/useCallback.ts +9 -0
- package/src/hooks/{tap-effect.ts → useEffect.ts} +9 -9
- package/src/hooks/{tap-effect-event.ts → useEffectEvent.ts} +9 -9
- package/src/hooks/{tap-memo.ts → useMemo.ts} +3 -3
- package/src/hooks/useMemoCache.ts +25 -0
- package/src/hooks/{tap-reducer.ts → useReducer.ts} +23 -11
- package/src/hooks/useRef.ts +16 -0
- package/src/hooks/{tap-resource.ts → useResource.ts} +13 -12
- package/src/{tapResourceRoot.ts → hooks/useResourceRoot.ts} +26 -27
- package/src/hooks/{tap-resources.ts → useResources.ts} +21 -22
- package/src/hooks/useState.ts +29 -0
- package/src/hooks/utils/{tapHook.ts → useCell.ts} +1 -1
- package/src/index.ts +4 -24
- package/src/react/hooks.ts +112 -0
- package/src/react-shim/index.ts +64 -0
- package/dist/hooks/tap-callback.d.ts +0 -5
- package/dist/hooks/tap-callback.d.ts.map +0 -1
- package/dist/hooks/tap-callback.js +0 -9
- package/dist/hooks/tap-callback.js.map +0 -1
- package/dist/hooks/tap-const.d.ts +0 -5
- package/dist/hooks/tap-const.d.ts.map +0 -1
- package/dist/hooks/tap-const.js +0 -10
- package/dist/hooks/tap-const.js.map +0 -1
- package/dist/hooks/tap-effect-event.d.ts.map +0 -1
- package/dist/hooks/tap-effect-event.js.map +0 -1
- package/dist/hooks/tap-effect.d.ts +0 -10
- package/dist/hooks/tap-effect.d.ts.map +0 -1
- package/dist/hooks/tap-effect.js.map +0 -1
- package/dist/hooks/tap-memo.d.ts +0 -5
- package/dist/hooks/tap-memo.d.ts.map +0 -1
- package/dist/hooks/tap-memo.js.map +0 -1
- package/dist/hooks/tap-reducer.d.ts +0 -9
- package/dist/hooks/tap-reducer.d.ts.map +0 -1
- package/dist/hooks/tap-reducer.js.map +0 -1
- package/dist/hooks/tap-ref.d.ts +0 -11
- package/dist/hooks/tap-ref.d.ts.map +0 -1
- package/dist/hooks/tap-ref.js +0 -10
- package/dist/hooks/tap-ref.js.map +0 -1
- package/dist/hooks/tap-resource.d.ts +0 -8
- package/dist/hooks/tap-resource.d.ts.map +0 -1
- package/dist/hooks/tap-resource.js.map +0 -1
- package/dist/hooks/tap-resources.d.ts.map +0 -1
- package/dist/hooks/tap-resources.js.map +0 -1
- package/dist/hooks/tap-state.d.ts +0 -9
- package/dist/hooks/tap-state.d.ts.map +0 -1
- package/dist/hooks/tap-state.js +0 -11
- package/dist/hooks/tap-state.js.map +0 -1
- package/dist/hooks/utils/tapHook.d.ts +0 -10
- package/dist/hooks/utils/tapHook.d.ts.map +0 -1
- package/dist/react/index.d.ts +0 -2
- package/dist/react/index.js +0 -2
- package/dist/react/use-resource.d.ts.map +0 -1
- package/dist/react/use-resource.js +0 -46
- package/dist/react/use-resource.js.map +0 -1
- package/dist/tapResourceRoot.d.ts +0 -20
- package/dist/tapResourceRoot.d.ts.map +0 -1
- package/dist/tapResourceRoot.js.map +0 -1
- package/react/package.json +0 -5
- package/src/hooks/tap-callback.ts +0 -9
- package/src/hooks/tap-const.ts +0 -6
- package/src/hooks/tap-ref.ts +0 -16
- package/src/hooks/tap-state.ts +0 -29
- package/src/react/index.ts +0 -1
- package/src/react/use-resource.ts +0 -61
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { describe, it, expect, afterEach } from "vitest";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
2
|
+
import { useResources } from "../../hooks/useResources";
|
|
3
|
+
import { useState } from "../../hooks/useState";
|
|
4
4
|
import { resource } from "../../core/resource";
|
|
5
5
|
import { withKey } from "../../core/withKey";
|
|
6
6
|
import {
|
|
@@ -13,20 +13,25 @@ import {
|
|
|
13
13
|
const SimpleCounter = resource(createCounterResource());
|
|
14
14
|
|
|
15
15
|
// Stateful counter that tracks its own count
|
|
16
|
-
const StatefulCounter = resource((props: {
|
|
17
|
-
|
|
16
|
+
const StatefulCounter = resource(function StatefulCounter(props: {
|
|
17
|
+
initial: number;
|
|
18
|
+
}) {
|
|
19
|
+
const [count] = useState(props.initial);
|
|
18
20
|
return { count };
|
|
19
21
|
});
|
|
20
22
|
|
|
21
23
|
// Display component for testing type changes
|
|
22
|
-
const Display = resource((props: { text: string })
|
|
24
|
+
const Display = resource(function Display(props: { text: string }) {
|
|
23
25
|
return { type: "display", text: props.text };
|
|
24
26
|
});
|
|
25
27
|
|
|
26
28
|
// Counter with render tracking for testing instance preservation
|
|
27
29
|
const renderCounts = new Map<string, number>();
|
|
28
30
|
const instances = new Map<string, object>();
|
|
29
|
-
const TrackingCounter = resource((props: {
|
|
31
|
+
const TrackingCounter = resource(function TrackingCounter(props: {
|
|
32
|
+
value: number;
|
|
33
|
+
id: string;
|
|
34
|
+
}) {
|
|
30
35
|
const currentCount = (renderCounts.get(props.id) || 0) + 1;
|
|
31
36
|
renderCounts.set(props.id, currentCount);
|
|
32
37
|
|
|
@@ -42,7 +47,7 @@ const TrackingCounter = resource((props: { value: number; id: string }) => {
|
|
|
42
47
|
};
|
|
43
48
|
});
|
|
44
49
|
|
|
45
|
-
describe("
|
|
50
|
+
describe("useResources - Basic Functionality", () => {
|
|
46
51
|
afterEach(() => {
|
|
47
52
|
cleanupAllResources();
|
|
48
53
|
});
|
|
@@ -50,7 +55,7 @@ describe("tapResources - Basic Functionality", () => {
|
|
|
50
55
|
describe("Basic Rendering", () => {
|
|
51
56
|
it("should render multiple resources with keys", () => {
|
|
52
57
|
const testFiber = createTestResource(() => {
|
|
53
|
-
const results =
|
|
58
|
+
const results = useResources(
|
|
54
59
|
() => [
|
|
55
60
|
withKey("a", SimpleCounter({ value: 10 })),
|
|
56
61
|
withKey("b", SimpleCounter({ value: 20 })),
|
|
@@ -67,8 +72,8 @@ describe("tapResources - Basic Functionality", () => {
|
|
|
67
72
|
});
|
|
68
73
|
|
|
69
74
|
it("should work with resource constructor syntax", () => {
|
|
70
|
-
const Counter = resource((props: { value: number })
|
|
71
|
-
const [count] =
|
|
75
|
+
const Counter = resource(function Counter(props: { value: number }) {
|
|
76
|
+
const [count] = useState(props.value);
|
|
72
77
|
return { count, double: count * 2 };
|
|
73
78
|
});
|
|
74
79
|
|
|
@@ -79,7 +84,7 @@ describe("tapResources - Basic Functionality", () => {
|
|
|
79
84
|
];
|
|
80
85
|
|
|
81
86
|
const testFiber = createTestResource(() => {
|
|
82
|
-
const results =
|
|
87
|
+
const results = useResources(
|
|
83
88
|
() =>
|
|
84
89
|
items.map((item) =>
|
|
85
90
|
withKey(item.key, Counter({ value: item.value })),
|
|
@@ -105,7 +110,7 @@ describe("tapResources - Basic Functionality", () => {
|
|
|
105
110
|
(props: {
|
|
106
111
|
items: Array<{ key: string; value: number; id: string }>;
|
|
107
112
|
}) => {
|
|
108
|
-
return
|
|
113
|
+
return useResources(() => {
|
|
109
114
|
return props.items.map((item) =>
|
|
110
115
|
withKey(
|
|
111
116
|
item.key,
|
|
@@ -162,7 +167,7 @@ describe("tapResources - Basic Functionality", () => {
|
|
|
162
167
|
it("should handle adding and removing resources", () => {
|
|
163
168
|
const testFiber = createTestResource(
|
|
164
169
|
(props: { items: Array<{ key: string; value: number }> }) => {
|
|
165
|
-
const results =
|
|
170
|
+
const results = useResources(() => {
|
|
166
171
|
return props.items.map((item) =>
|
|
167
172
|
withKey(item.key, SimpleCounter({ value: item.value })),
|
|
168
173
|
);
|
|
@@ -203,7 +208,7 @@ describe("tapResources - Basic Functionality", () => {
|
|
|
203
208
|
|
|
204
209
|
it("should handle changing resource types for the same key", () => {
|
|
205
210
|
const testFiber = createTestResource((props: { useCounter: boolean }) => {
|
|
206
|
-
const results =
|
|
211
|
+
const results = useResources(
|
|
207
212
|
() => [
|
|
208
213
|
withKey(
|
|
209
214
|
"item",
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { describe, it, expect, afterEach } from "vitest";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
2
|
+
import { useState } from "../../hooks/useState";
|
|
3
|
+
import { useEffect } from "../../hooks/useEffect";
|
|
4
4
|
import {
|
|
5
5
|
createTestResource,
|
|
6
6
|
renderTest,
|
|
@@ -9,7 +9,7 @@ import {
|
|
|
9
9
|
getCommittedOutput,
|
|
10
10
|
} from "../test-utils";
|
|
11
11
|
|
|
12
|
-
describe("
|
|
12
|
+
describe("useState - Basic Functionality", () => {
|
|
13
13
|
afterEach(() => {
|
|
14
14
|
cleanupAllResources();
|
|
15
15
|
});
|
|
@@ -17,7 +17,7 @@ describe("tapState - Basic Functionality", () => {
|
|
|
17
17
|
describe("Initialization", () => {
|
|
18
18
|
it("should initialize with direct value", () => {
|
|
19
19
|
const testFiber = createTestResource(() => {
|
|
20
|
-
const [count] =
|
|
20
|
+
const [count] = useState(42);
|
|
21
21
|
return count;
|
|
22
22
|
});
|
|
23
23
|
|
|
@@ -29,7 +29,7 @@ describe("tapState - Basic Functionality", () => {
|
|
|
29
29
|
let initCalled = 0;
|
|
30
30
|
|
|
31
31
|
const testFiber = createTestResource(() => {
|
|
32
|
-
const [count] =
|
|
32
|
+
const [count] = useState(() => {
|
|
33
33
|
initCalled++;
|
|
34
34
|
return 100;
|
|
35
35
|
});
|
|
@@ -48,7 +48,7 @@ describe("tapState - Basic Functionality", () => {
|
|
|
48
48
|
|
|
49
49
|
it("should handle undefined initial state", () => {
|
|
50
50
|
const testFiber = createTestResource(() => {
|
|
51
|
-
const [value] =
|
|
51
|
+
const [value] = useState<string>();
|
|
52
52
|
return value;
|
|
53
53
|
});
|
|
54
54
|
|
|
@@ -64,7 +64,7 @@ describe("tapState - Basic Functionality", () => {
|
|
|
64
64
|
|
|
65
65
|
const testFiber = createTestResource(() => {
|
|
66
66
|
renderCount++;
|
|
67
|
-
const [count, setCount] =
|
|
67
|
+
const [count, setCount] = useState(0);
|
|
68
68
|
|
|
69
69
|
// Capture setter on first render
|
|
70
70
|
if (!setCountFn) {
|
|
@@ -97,9 +97,9 @@ describe("tapState - Basic Functionality", () => {
|
|
|
97
97
|
|
|
98
98
|
const testFiber = createTestResource(() => {
|
|
99
99
|
renderCount++;
|
|
100
|
-
const [count, setCount] =
|
|
100
|
+
const [count, setCount] = useState(42);
|
|
101
101
|
|
|
102
|
-
|
|
102
|
+
useEffect(() => {
|
|
103
103
|
setCountFn = setCount;
|
|
104
104
|
});
|
|
105
105
|
|
|
@@ -125,9 +125,9 @@ describe("tapState - Basic Functionality", () => {
|
|
|
125
125
|
null;
|
|
126
126
|
|
|
127
127
|
const testFiber = createTestResource(() => {
|
|
128
|
-
const [count, setCount] =
|
|
128
|
+
const [count, setCount] = useState(10);
|
|
129
129
|
|
|
130
|
-
|
|
130
|
+
useEffect(() => {
|
|
131
131
|
setCountFn = setCount;
|
|
132
132
|
});
|
|
133
133
|
|
|
@@ -155,9 +155,9 @@ describe("tapState - Basic Functionality", () => {
|
|
|
155
155
|
describe("Multiple States", () => {
|
|
156
156
|
it("should handle multiple state hooks independently", () => {
|
|
157
157
|
const testFiber = createTestResource(() => {
|
|
158
|
-
const [count1, setCount1] =
|
|
159
|
-
const [count2, setCount2] =
|
|
160
|
-
const [text, setText] =
|
|
158
|
+
const [count1, setCount1] = useState(1);
|
|
159
|
+
const [count2, setCount2] = useState(2);
|
|
160
|
+
const [text, setText] = useState("hello");
|
|
161
161
|
|
|
162
162
|
return {
|
|
163
163
|
count1,
|
|
@@ -179,11 +179,11 @@ describe("tapState - Basic Functionality", () => {
|
|
|
179
179
|
let setters: any = null;
|
|
180
180
|
|
|
181
181
|
const testFiber = createTestResource(() => {
|
|
182
|
-
const [a, setA] =
|
|
183
|
-
const [b, setB] =
|
|
184
|
-
const [c, setC] =
|
|
182
|
+
const [a, setA] = useState("a");
|
|
183
|
+
const [b, setB] = useState("b");
|
|
184
|
+
const [c, setC] = useState("c");
|
|
185
185
|
|
|
186
|
-
|
|
186
|
+
useEffect(() => {
|
|
187
187
|
setters = { setA, setB, setC };
|
|
188
188
|
});
|
|
189
189
|
|
|
@@ -212,9 +212,9 @@ describe("tapState - Basic Functionality", () => {
|
|
|
212
212
|
let setCountFn: ((value: number) => void) | null = null;
|
|
213
213
|
|
|
214
214
|
const testFiber = createTestResource((props: { multiplier: number }) => {
|
|
215
|
-
const [count, setCount] =
|
|
215
|
+
const [count, setCount] = useState(10);
|
|
216
216
|
|
|
217
|
-
|
|
217
|
+
useEffect(() => {
|
|
218
218
|
setCountFn = setCount;
|
|
219
219
|
});
|
|
220
220
|
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
/* oxlint-disable
|
|
1
|
+
/* oxlint-disable react/exhaustive-deps -- tests deliberately exercise invalid dep arrays */
|
|
2
2
|
import { describe, it, expect, vi } from "vitest";
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
3
|
+
import { useEffect } from "../../hooks/useEffect";
|
|
4
|
+
import { useState } from "../../hooks/useState";
|
|
5
5
|
import { createTestResource, renderTest, unmountResource } from "../test-utils";
|
|
6
6
|
import {
|
|
7
7
|
renderResourceFiber,
|
|
@@ -13,7 +13,7 @@ describe("Errors - Effect Errors", () => {
|
|
|
13
13
|
const error = new Error("Effect error");
|
|
14
14
|
|
|
15
15
|
const resource = createTestResource(() => {
|
|
16
|
-
|
|
16
|
+
useEffect(() => {
|
|
17
17
|
throw error;
|
|
18
18
|
});
|
|
19
19
|
return null;
|
|
@@ -27,7 +27,7 @@ describe("Errors - Effect Errors", () => {
|
|
|
27
27
|
let dep = 0;
|
|
28
28
|
|
|
29
29
|
const resource = createTestResource(() => {
|
|
30
|
-
|
|
30
|
+
useEffect(() => {
|
|
31
31
|
return () => {
|
|
32
32
|
if (dep > 0) {
|
|
33
33
|
throw error;
|
|
@@ -52,7 +52,7 @@ describe("Errors - Effect Errors", () => {
|
|
|
52
52
|
|
|
53
53
|
it("should throw on invalid effect return value", () => {
|
|
54
54
|
const resource = createTestResource(() => {
|
|
55
|
-
|
|
55
|
+
useEffect(() => {
|
|
56
56
|
return "not a function" as any; // Invalid return
|
|
57
57
|
});
|
|
58
58
|
return null;
|
|
@@ -69,13 +69,13 @@ describe("Errors - Effect Errors", () => {
|
|
|
69
69
|
const goodEffect = vi.fn();
|
|
70
70
|
|
|
71
71
|
const resource = createTestResource(() => {
|
|
72
|
-
|
|
72
|
+
useEffect(() => {
|
|
73
73
|
throw error1;
|
|
74
74
|
});
|
|
75
75
|
|
|
76
|
-
|
|
76
|
+
useEffect(goodEffect); // This won't run
|
|
77
77
|
|
|
78
|
-
|
|
78
|
+
useEffect(() => {
|
|
79
79
|
throw error2;
|
|
80
80
|
});
|
|
81
81
|
|
|
@@ -99,9 +99,9 @@ describe("Errors - Effect Errors", () => {
|
|
|
99
99
|
const cleanup3 = vi.fn();
|
|
100
100
|
|
|
101
101
|
const resource = createTestResource(() => {
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
102
|
+
useEffect(() => cleanup1);
|
|
103
|
+
useEffect(() => cleanup2);
|
|
104
|
+
useEffect(() => cleanup3);
|
|
105
105
|
return null;
|
|
106
106
|
});
|
|
107
107
|
|
|
@@ -119,16 +119,16 @@ describe("Errors - Effect Errors", () => {
|
|
|
119
119
|
let shouldThrow = false;
|
|
120
120
|
|
|
121
121
|
const resource = createTestResource(() => {
|
|
122
|
-
const [dep, setDep] =
|
|
122
|
+
const [dep, setDep] = useState(0);
|
|
123
123
|
|
|
124
|
-
|
|
124
|
+
useEffect(() => {
|
|
125
125
|
if (shouldThrow) {
|
|
126
126
|
throw error;
|
|
127
127
|
}
|
|
128
128
|
}, [dep]);
|
|
129
129
|
|
|
130
130
|
// Use effect to trigger state change
|
|
131
|
-
|
|
131
|
+
useEffect(() => {
|
|
132
132
|
if (dep === 0) {
|
|
133
133
|
shouldThrow = true;
|
|
134
134
|
setDep(1); // Trigger effect re-run
|
|
@@ -148,7 +148,7 @@ describe("Errors - Effect Errors", () => {
|
|
|
148
148
|
let asyncErrorPromise: Promise<void>;
|
|
149
149
|
|
|
150
150
|
const resource = createTestResource(() => {
|
|
151
|
-
|
|
151
|
+
useEffect(() => {
|
|
152
152
|
// Async errors are not caught by the framework
|
|
153
153
|
asyncErrorPromise = new Promise((_, reject) => {
|
|
154
154
|
setTimeout(() => {
|
|
@@ -176,9 +176,9 @@ describe("Errors - Effect Errors", () => {
|
|
|
176
176
|
let effectRan = false;
|
|
177
177
|
|
|
178
178
|
const resource = createTestResource(() => {
|
|
179
|
-
const [value] =
|
|
179
|
+
const [value] = useState("initial");
|
|
180
180
|
|
|
181
|
-
|
|
181
|
+
useEffect(() => {
|
|
182
182
|
effectRan = true;
|
|
183
183
|
throw error;
|
|
184
184
|
});
|
|
@@ -198,9 +198,9 @@ describe("Errors - Effect Errors", () => {
|
|
|
198
198
|
let throwOnCleanup = false;
|
|
199
199
|
|
|
200
200
|
const resource = createTestResource(() => {
|
|
201
|
-
const [count, setCount] =
|
|
201
|
+
const [count, setCount] = useState(0);
|
|
202
202
|
|
|
203
|
-
|
|
203
|
+
useEffect(() => {
|
|
204
204
|
return () => {
|
|
205
205
|
if (throwOnCleanup) {
|
|
206
206
|
throw cleanupError;
|
|
@@ -209,7 +209,7 @@ describe("Errors - Effect Errors", () => {
|
|
|
209
209
|
}, [count]);
|
|
210
210
|
|
|
211
211
|
// Use effect to trigger state change
|
|
212
|
-
|
|
212
|
+
useEffect(() => {
|
|
213
213
|
if (count === 0) {
|
|
214
214
|
throwOnCleanup = true;
|
|
215
215
|
setCount(1);
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { describe, it, expect } from "vitest";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
2
|
+
import { useEffect } from "../../hooks/useEffect";
|
|
3
|
+
import { useState } from "../../hooks/useState";
|
|
4
4
|
import { createTestResource, renderTest } from "../test-utils";
|
|
5
5
|
import {
|
|
6
6
|
renderResourceFiber,
|
|
@@ -22,11 +22,11 @@ describe("Errors - Render Errors", () => {
|
|
|
22
22
|
it("should throw when hooks are called outside render context", () => {
|
|
23
23
|
// Try to call hook outside of resource render
|
|
24
24
|
expect(() => {
|
|
25
|
-
|
|
25
|
+
useState(0);
|
|
26
26
|
}).toThrow("No resource fiber available");
|
|
27
27
|
|
|
28
28
|
expect(() => {
|
|
29
|
-
|
|
29
|
+
useEffect(() => {});
|
|
30
30
|
}).toThrow("No resource fiber available");
|
|
31
31
|
});
|
|
32
32
|
|
|
@@ -34,7 +34,7 @@ describe("Errors - Render Errors", () => {
|
|
|
34
34
|
const error = new Error("Initializer error");
|
|
35
35
|
|
|
36
36
|
const resource = createTestResource(() => {
|
|
37
|
-
const [value] =
|
|
37
|
+
const [value] = useState(() => {
|
|
38
38
|
throw error;
|
|
39
39
|
});
|
|
40
40
|
return value;
|
|
@@ -45,7 +45,7 @@ describe("Errors - Render Errors", () => {
|
|
|
45
45
|
|
|
46
46
|
it("should detect render during render", () => {
|
|
47
47
|
const resource = createTestResource(() => {
|
|
48
|
-
const [count, setCount] =
|
|
48
|
+
const [count, setCount] = useState(0);
|
|
49
49
|
|
|
50
50
|
// This violates the rules - no state updates during render
|
|
51
51
|
if (count < 5) {
|
|
@@ -62,9 +62,9 @@ describe("Errors - Render Errors", () => {
|
|
|
62
62
|
|
|
63
63
|
it("should allow setState during commit (effects)", () => {
|
|
64
64
|
const resource = createTestResource(() => {
|
|
65
|
-
const [count, setCount] =
|
|
65
|
+
const [count, setCount] = useState(0);
|
|
66
66
|
|
|
67
|
-
|
|
67
|
+
useEffect(() => {
|
|
68
68
|
// setState during effects (commit phase) is allowed
|
|
69
69
|
if (count < 5) {
|
|
70
70
|
setCount(count + 1);
|
|
@@ -85,11 +85,11 @@ describe("Errors - Render Errors", () => {
|
|
|
85
85
|
|
|
86
86
|
const resource = createTestResource(() => {
|
|
87
87
|
if (useStateFirst) {
|
|
88
|
-
|
|
89
|
-
|
|
88
|
+
useState(1);
|
|
89
|
+
useEffect(() => {});
|
|
90
90
|
} else {
|
|
91
|
-
|
|
92
|
-
|
|
91
|
+
useEffect(() => {});
|
|
92
|
+
useState(1);
|
|
93
93
|
}
|
|
94
94
|
return null;
|
|
95
95
|
});
|
|
@@ -107,7 +107,7 @@ describe("Errors - Render Errors", () => {
|
|
|
107
107
|
let shouldThrow = false;
|
|
108
108
|
|
|
109
109
|
const resource = createTestResource(() => {
|
|
110
|
-
const [count, _setCount] =
|
|
110
|
+
const [count, _setCount] = useState(42);
|
|
111
111
|
|
|
112
112
|
if (shouldThrow) {
|
|
113
113
|
throw new Error("Render failed");
|
|
@@ -134,11 +134,11 @@ describe("Errors - Render Errors", () => {
|
|
|
134
134
|
const resource = createTestResource(() => {
|
|
135
135
|
if (phase === "hook-order") {
|
|
136
136
|
// Wrong hook order
|
|
137
|
-
|
|
138
|
-
|
|
137
|
+
useEffect(() => {});
|
|
138
|
+
useState(1);
|
|
139
139
|
} else {
|
|
140
|
-
|
|
141
|
-
|
|
140
|
+
useState(1);
|
|
141
|
+
useEffect(() => {
|
|
142
142
|
if (phase === "effect-error") {
|
|
143
143
|
throw new Error("Effect error");
|
|
144
144
|
}
|
|
@@ -171,7 +171,7 @@ describe("Errors - Render Errors", () => {
|
|
|
171
171
|
it("should handle errors in nested hook calls", () => {
|
|
172
172
|
const useFeature = () => {
|
|
173
173
|
// This will fail if called outside render
|
|
174
|
-
const [value] =
|
|
174
|
+
const [value] = useState("feature");
|
|
175
175
|
return value;
|
|
176
176
|
};
|
|
177
177
|
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
/* oxlint-disable
|
|
1
|
+
/* oxlint-disable react/exhaustive-deps -- tests deliberately exercise invalid dep arrays */
|
|
2
2
|
import { describe, it, expect, vi } from "vitest";
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
3
|
+
import { useEffect } from "../../hooks/useEffect";
|
|
4
|
+
import { useState } from "../../hooks/useState";
|
|
5
5
|
import { createTestResource, renderTest, waitForNextTick } from "../test-utils";
|
|
6
6
|
import {
|
|
7
7
|
renderResourceFiber,
|
|
@@ -15,10 +15,10 @@ describe("Lifecycle - Dependencies", () => {
|
|
|
15
15
|
let setDep: any;
|
|
16
16
|
|
|
17
17
|
const resource = createTestResource(() => {
|
|
18
|
-
const [dep, _setDep] =
|
|
18
|
+
const [dep, _setDep] = useState(1);
|
|
19
19
|
setDep = _setDep;
|
|
20
20
|
|
|
21
|
-
|
|
21
|
+
useEffect(effect, [dep]);
|
|
22
22
|
return dep;
|
|
23
23
|
});
|
|
24
24
|
|
|
@@ -38,11 +38,11 @@ describe("Lifecycle - Dependencies", () => {
|
|
|
38
38
|
let triggerRerender: any;
|
|
39
39
|
|
|
40
40
|
const resource = createTestResource(() => {
|
|
41
|
-
const [count, setCount] =
|
|
42
|
-
const [dep] =
|
|
41
|
+
const [count, setCount] = useState(0);
|
|
42
|
+
const [dep] = useState("constant");
|
|
43
43
|
triggerRerender = setCount;
|
|
44
44
|
|
|
45
|
-
|
|
45
|
+
useEffect(effect, [dep]);
|
|
46
46
|
return { count, dep };
|
|
47
47
|
});
|
|
48
48
|
|
|
@@ -62,10 +62,10 @@ describe("Lifecycle - Dependencies", () => {
|
|
|
62
62
|
let setDep: any;
|
|
63
63
|
|
|
64
64
|
const resource = createTestResource(() => {
|
|
65
|
-
const [dep, _setDep] =
|
|
65
|
+
const [dep, _setDep] = useState(1);
|
|
66
66
|
setDep = _setDep;
|
|
67
67
|
|
|
68
|
-
|
|
68
|
+
useEffect(() => {
|
|
69
69
|
log.push(`effect-${dep}`);
|
|
70
70
|
return () => log.push(`cleanup-${dep}`);
|
|
71
71
|
}, [dep]);
|
|
@@ -89,10 +89,10 @@ describe("Lifecycle - Dependencies", () => {
|
|
|
89
89
|
let triggerRerender: any;
|
|
90
90
|
|
|
91
91
|
const resource = createTestResource(() => {
|
|
92
|
-
const [count, setCount] =
|
|
92
|
+
const [count, setCount] = useState(0);
|
|
93
93
|
triggerRerender = setCount;
|
|
94
94
|
|
|
95
|
-
|
|
95
|
+
useEffect(effect); // No deps = always re-run
|
|
96
96
|
return count;
|
|
97
97
|
});
|
|
98
98
|
|
|
@@ -112,10 +112,10 @@ describe("Lifecycle - Dependencies", () => {
|
|
|
112
112
|
let triggerRerender: any;
|
|
113
113
|
|
|
114
114
|
const resource = createTestResource(() => {
|
|
115
|
-
const [count, setCount] =
|
|
115
|
+
const [count, setCount] = useState(0);
|
|
116
116
|
triggerRerender = setCount;
|
|
117
117
|
|
|
118
|
-
|
|
118
|
+
useEffect(effect, []); // Empty deps = run once
|
|
119
119
|
return count;
|
|
120
120
|
});
|
|
121
121
|
|
|
@@ -135,12 +135,12 @@ describe("Lifecycle - Dependencies", () => {
|
|
|
135
135
|
let setDep1: any, setDep2: any;
|
|
136
136
|
|
|
137
137
|
const resource = createTestResource(() => {
|
|
138
|
-
const [dep1, _setDep1] =
|
|
139
|
-
const [dep2, _setDep2] =
|
|
138
|
+
const [dep1, _setDep1] = useState("a");
|
|
139
|
+
const [dep2, _setDep2] = useState(1);
|
|
140
140
|
setDep1 = _setDep1;
|
|
141
141
|
setDep2 = _setDep2;
|
|
142
142
|
|
|
143
|
-
|
|
143
|
+
useEffect(effect, [dep1, dep2]);
|
|
144
144
|
return { dep1, dep2 };
|
|
145
145
|
});
|
|
146
146
|
|
|
@@ -176,10 +176,10 @@ describe("Lifecycle - Dependencies", () => {
|
|
|
176
176
|
let setObj: any;
|
|
177
177
|
|
|
178
178
|
const resource = createTestResource(() => {
|
|
179
|
-
const [obj, _setObj] =
|
|
179
|
+
const [obj, _setObj] = useState({ value: 1 });
|
|
180
180
|
setObj = _setObj;
|
|
181
181
|
|
|
182
|
-
|
|
182
|
+
useEffect(effect, [obj]);
|
|
183
183
|
return obj;
|
|
184
184
|
});
|
|
185
185
|
|
|
@@ -199,10 +199,10 @@ describe("Lifecycle - Dependencies", () => {
|
|
|
199
199
|
let setValue: any;
|
|
200
200
|
|
|
201
201
|
const resource = createTestResource(() => {
|
|
202
|
-
const [value, _setValue] =
|
|
202
|
+
const [value, _setValue] = useState(NaN);
|
|
203
203
|
setValue = _setValue;
|
|
204
204
|
|
|
205
|
-
|
|
205
|
+
useEffect(effect, [value]);
|
|
206
206
|
return value;
|
|
207
207
|
});
|
|
208
208
|
|
|
@@ -222,9 +222,9 @@ describe("Lifecycle - Dependencies", () => {
|
|
|
222
222
|
|
|
223
223
|
const resource = createTestResource(() => {
|
|
224
224
|
if (useDeps) {
|
|
225
|
-
|
|
225
|
+
useEffect(() => {}, [1]);
|
|
226
226
|
} else {
|
|
227
|
-
|
|
227
|
+
useEffect(() => {}); // No deps
|
|
228
228
|
}
|
|
229
229
|
return null;
|
|
230
230
|
});
|
|
@@ -234,9 +234,9 @@ describe("Lifecycle - Dependencies", () => {
|
|
|
234
234
|
// Change to no deps
|
|
235
235
|
useDeps = false;
|
|
236
236
|
|
|
237
|
-
// Error
|
|
237
|
+
// Error throws during render (fail-fast validation)
|
|
238
238
|
expect(() => renderResourceFiber(resource, undefined)).toThrow(
|
|
239
|
-
"
|
|
239
|
+
"useEffect called with and without dependencies across re-renders",
|
|
240
240
|
);
|
|
241
241
|
});
|
|
242
242
|
});
|