rask-ui 0.21.0 → 0.23.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/dist/asyncState.d.ts +16 -0
- package/dist/asyncState.d.ts.map +1 -0
- package/dist/asyncState.js +24 -0
- package/dist/component.d.ts.map +1 -1
- package/dist/component.js +6 -4
- package/dist/context.d.ts +5 -0
- package/dist/context.d.ts.map +1 -0
- package/dist/context.js +29 -0
- package/dist/createAsync.test.d.ts +2 -0
- package/dist/createAsync.test.d.ts.map +1 -0
- package/dist/createAsync.test.js +110 -0
- package/dist/createContext.d.ts +36 -20
- package/dist/createContext.d.ts.map +1 -1
- package/dist/createContext.js +58 -37
- package/dist/createMutation.test.d.ts +2 -0
- package/dist/createMutation.test.d.ts.map +1 -0
- package/dist/createMutation.test.js +168 -0
- package/dist/createQuery.test.d.ts +2 -0
- package/dist/createQuery.test.d.ts.map +1 -0
- package/dist/createQuery.test.js +156 -0
- package/dist/createRef.d.ts +6 -0
- package/dist/createRef.d.ts.map +1 -0
- package/dist/createRef.js +8 -0
- package/dist/createState.d.ts +0 -2
- package/dist/createState.d.ts.map +1 -1
- package/dist/createState.js +5 -40
- package/dist/createState.test.d.ts.map +1 -0
- package/dist/createState.test.js +111 -0
- package/dist/createView.d.ts +44 -18
- package/dist/createView.d.ts.map +1 -1
- package/dist/createView.js +48 -57
- package/dist/createView.test.d.ts.map +1 -0
- package/dist/{tests/createView.test.js → createView.test.js} +40 -40
- package/dist/error.d.ts +14 -3
- package/dist/error.d.ts.map +1 -1
- package/dist/error.js +15 -14
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/jsx.d.ts +256 -10
- package/dist/observation.test.d.ts.map +1 -0
- package/dist/observation.test.js +150 -0
- package/dist/suspense.d.ts +25 -0
- package/dist/suspense.d.ts.map +1 -0
- package/dist/suspense.js +97 -0
- package/dist/test-setup.d.ts +16 -0
- package/dist/test-setup.d.ts.map +1 -0
- package/dist/test-setup.js +40 -0
- package/dist/test.d.ts +2 -0
- package/dist/test.d.ts.map +1 -0
- package/dist/test.js +24 -0
- package/dist/useCatchError.d.ts +6 -1
- package/dist/useCatchError.d.ts.map +1 -1
- package/dist/useCatchError.js +5 -4
- package/package.json +2 -2
- package/swc-plugin/target/wasm32-wasip1/release/swc_plugin_rask_component.wasm +0 -0
- package/dist/createComputed.d.ts +0 -4
- package/dist/createComputed.d.ts.map +0 -1
- package/dist/createComputed.js +0 -69
- package/dist/createEffect.d.ts +0 -2
- package/dist/createEffect.d.ts.map +0 -1
- package/dist/createEffect.js +0 -29
- package/dist/createRouter.d.ts +0 -8
- package/dist/createRouter.d.ts.map +0 -1
- package/dist/createRouter.js +0 -27
- package/dist/createTask.d.ts +0 -31
- package/dist/createTask.d.ts.map +0 -1
- package/dist/createTask.js +0 -79
- package/dist/patchInferno.d.ts +0 -6
- package/dist/patchInferno.d.ts.map +0 -1
- package/dist/patchInferno.js +0 -53
- package/dist/scheduler.d.ts +0 -4
- package/dist/scheduler.d.ts.map +0 -1
- package/dist/scheduler.js +0 -107
- package/dist/tests/batch.test.d.ts +0 -2
- package/dist/tests/batch.test.d.ts.map +0 -1
- package/dist/tests/batch.test.js +0 -244
- package/dist/tests/createComputed.test.d.ts +0 -2
- package/dist/tests/createComputed.test.d.ts.map +0 -1
- package/dist/tests/createComputed.test.js +0 -257
- package/dist/tests/createContext.test.d.ts +0 -2
- package/dist/tests/createContext.test.d.ts.map +0 -1
- package/dist/tests/createContext.test.js +0 -136
- package/dist/tests/createEffect.test.d.ts +0 -2
- package/dist/tests/createEffect.test.d.ts.map +0 -1
- package/dist/tests/createEffect.test.js +0 -467
- package/dist/tests/createState.test.d.ts.map +0 -1
- package/dist/tests/createState.test.js +0 -144
- package/dist/tests/createTask.test.d.ts +0 -2
- package/dist/tests/createTask.test.d.ts.map +0 -1
- package/dist/tests/createTask.test.js +0 -322
- package/dist/tests/createView.test.d.ts.map +0 -1
- package/dist/tests/error.test.d.ts +0 -2
- package/dist/tests/error.test.d.ts.map +0 -1
- package/dist/tests/error.test.js +0 -168
- package/dist/tests/observation.test.d.ts.map +0 -1
- package/dist/tests/observation.test.js +0 -341
- package/dist/useComputed.d.ts +0 -5
- package/dist/useComputed.d.ts.map +0 -1
- package/dist/useComputed.js +0 -69
- package/dist/useQuery.d.ts +0 -25
- package/dist/useQuery.d.ts.map +0 -1
- package/dist/useQuery.js +0 -25
- package/dist/useSuspendAsync.d.ts +0 -18
- package/dist/useSuspendAsync.d.ts.map +0 -1
- package/dist/useSuspendAsync.js +0 -37
- package/dist/useTask.d.ts +0 -25
- package/dist/useTask.d.ts.map +0 -1
- package/dist/useTask.js +0 -70
- /package/dist/{tests/createState.test.d.ts → createState.test.d.ts} +0 -0
- /package/dist/{tests/createView.test.d.ts → createView.test.d.ts} +0 -0
- /package/dist/{tests/observation.test.d.ts → observation.test.d.ts} +0 -0
|
@@ -1,257 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect, vi } from "vitest";
|
|
2
|
-
import { createComputed } from "../createComputed";
|
|
3
|
-
import { createState } from "../createState";
|
|
4
|
-
import { Observer } from "../observation";
|
|
5
|
-
describe("createComputed", () => {
|
|
6
|
-
it("should compute values lazily", () => {
|
|
7
|
-
const state = createState({ count: 5 });
|
|
8
|
-
const computeFn = vi.fn(() => state.count * 2);
|
|
9
|
-
const computed = createComputed({
|
|
10
|
-
doubled: computeFn,
|
|
11
|
-
});
|
|
12
|
-
// Should not compute until accessed
|
|
13
|
-
expect(computeFn).not.toHaveBeenCalled();
|
|
14
|
-
const result = computed.doubled;
|
|
15
|
-
expect(computeFn).toHaveBeenCalledTimes(1);
|
|
16
|
-
expect(result).toBe(10);
|
|
17
|
-
});
|
|
18
|
-
it("should cache computed values", () => {
|
|
19
|
-
const state = createState({ count: 5 });
|
|
20
|
-
const computeFn = vi.fn(() => state.count * 2);
|
|
21
|
-
const computed = createComputed({
|
|
22
|
-
doubled: computeFn,
|
|
23
|
-
});
|
|
24
|
-
// Access multiple times
|
|
25
|
-
computed.doubled;
|
|
26
|
-
computed.doubled;
|
|
27
|
-
computed.doubled;
|
|
28
|
-
// Should only compute once due to caching
|
|
29
|
-
expect(computeFn).toHaveBeenCalledTimes(1);
|
|
30
|
-
});
|
|
31
|
-
it("should invalidate cache when dependencies change", async () => {
|
|
32
|
-
const state = createState({ count: 5 });
|
|
33
|
-
const computeFn = vi.fn(() => state.count * 2);
|
|
34
|
-
const computed = createComputed({
|
|
35
|
-
doubled: computeFn,
|
|
36
|
-
});
|
|
37
|
-
expect(computed.doubled).toBe(10);
|
|
38
|
-
expect(computeFn).toHaveBeenCalledTimes(1);
|
|
39
|
-
// Change dependency
|
|
40
|
-
state.count = 10;
|
|
41
|
-
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
42
|
-
// Should recompute on next access
|
|
43
|
-
expect(computed.doubled).toBe(20);
|
|
44
|
-
expect(computeFn).toHaveBeenCalledTimes(2);
|
|
45
|
-
});
|
|
46
|
-
it("should handle multiple computed properties", async () => {
|
|
47
|
-
const state = createState({ width: 10, height: 5 });
|
|
48
|
-
const computed = createComputed({
|
|
49
|
-
area: () => state.width * state.height,
|
|
50
|
-
perimeter: () => 2 * (state.width + state.height),
|
|
51
|
-
});
|
|
52
|
-
expect(computed.area).toBe(50);
|
|
53
|
-
expect(computed.perimeter).toBe(30);
|
|
54
|
-
state.width = 20;
|
|
55
|
-
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
56
|
-
expect(computed.area).toBe(100);
|
|
57
|
-
expect(computed.perimeter).toBe(50);
|
|
58
|
-
});
|
|
59
|
-
it("should support computed properties depending on other computed properties", async () => {
|
|
60
|
-
const state = createState({ count: 5 });
|
|
61
|
-
const computed = createComputed({
|
|
62
|
-
doubled: () => state.count * 2,
|
|
63
|
-
quadrupled: () => computed.doubled * 2,
|
|
64
|
-
});
|
|
65
|
-
expect(computed.doubled).toBe(10);
|
|
66
|
-
expect(computed.quadrupled).toBe(20);
|
|
67
|
-
state.count = 10;
|
|
68
|
-
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
69
|
-
expect(computed.doubled).toBe(20);
|
|
70
|
-
expect(computed.quadrupled).toBe(40);
|
|
71
|
-
});
|
|
72
|
-
it("should be reactive when observed", async () => {
|
|
73
|
-
const state = createState({ count: 5 });
|
|
74
|
-
const computed = createComputed({
|
|
75
|
-
doubled: () => state.count * 2,
|
|
76
|
-
});
|
|
77
|
-
let observedValue = null;
|
|
78
|
-
const observer = new Observer(() => {
|
|
79
|
-
observedValue = computed.doubled;
|
|
80
|
-
});
|
|
81
|
-
const dispose = observer.observe();
|
|
82
|
-
computed.doubled; // Track the computed
|
|
83
|
-
dispose();
|
|
84
|
-
expect(observedValue).toBe(null);
|
|
85
|
-
// Change state
|
|
86
|
-
state.count = 10;
|
|
87
|
-
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
88
|
-
expect(observedValue).toBe(20);
|
|
89
|
-
observer.dispose();
|
|
90
|
-
});
|
|
91
|
-
it("should only recompute when actual dependencies change", () => {
|
|
92
|
-
const state = createState({ a: 1, b: 2 });
|
|
93
|
-
const computeFn = vi.fn(() => state.a * 2);
|
|
94
|
-
const computed = createComputed({
|
|
95
|
-
result: computeFn,
|
|
96
|
-
});
|
|
97
|
-
expect(computed.result).toBe(2);
|
|
98
|
-
expect(computeFn).toHaveBeenCalledTimes(1);
|
|
99
|
-
// Change unrelated property
|
|
100
|
-
state.b = 100;
|
|
101
|
-
// Should still return cached value
|
|
102
|
-
expect(computed.result).toBe(2);
|
|
103
|
-
expect(computeFn).toHaveBeenCalledTimes(1);
|
|
104
|
-
});
|
|
105
|
-
it("should handle complex dependency chains", async () => {
|
|
106
|
-
const state = createState({
|
|
107
|
-
items: [1, 2, 3, 4, 5],
|
|
108
|
-
multiplier: 2,
|
|
109
|
-
});
|
|
110
|
-
const computed = createComputed({
|
|
111
|
-
total: () => state.items.reduce((sum, item) => sum + item, 0),
|
|
112
|
-
multipliedTotal: () => computed.total * state.multiplier,
|
|
113
|
-
average: () => computed.total / state.items.length,
|
|
114
|
-
});
|
|
115
|
-
expect(computed.total).toBe(15);
|
|
116
|
-
expect(computed.multipliedTotal).toBe(30);
|
|
117
|
-
expect(computed.average).toBe(3);
|
|
118
|
-
state.items.push(6);
|
|
119
|
-
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
120
|
-
expect(computed.total).toBe(21);
|
|
121
|
-
expect(computed.multipliedTotal).toBe(42);
|
|
122
|
-
expect(computed.average).toBe(3.5);
|
|
123
|
-
state.multiplier = 3;
|
|
124
|
-
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
125
|
-
expect(computed.multipliedTotal).toBe(63);
|
|
126
|
-
});
|
|
127
|
-
it("should handle array operations", async () => {
|
|
128
|
-
const state = createState({ items: [1, 2, 3] });
|
|
129
|
-
const computed = createComputed({
|
|
130
|
-
sum: () => state.items.reduce((sum, item) => sum + item, 0),
|
|
131
|
-
count: () => state.items.length,
|
|
132
|
-
});
|
|
133
|
-
expect(computed.sum).toBe(6);
|
|
134
|
-
expect(computed.count).toBe(3);
|
|
135
|
-
state.items.push(4);
|
|
136
|
-
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
137
|
-
expect(computed.sum).toBe(10);
|
|
138
|
-
expect(computed.count).toBe(4);
|
|
139
|
-
state.items.pop();
|
|
140
|
-
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
141
|
-
expect(computed.sum).toBe(6);
|
|
142
|
-
expect(computed.count).toBe(3);
|
|
143
|
-
});
|
|
144
|
-
it("should handle deeply nested state", async () => {
|
|
145
|
-
const state = createState({
|
|
146
|
-
user: {
|
|
147
|
-
profile: {
|
|
148
|
-
name: "Alice",
|
|
149
|
-
age: 30,
|
|
150
|
-
},
|
|
151
|
-
},
|
|
152
|
-
});
|
|
153
|
-
const computed = createComputed({
|
|
154
|
-
displayName: () => `${state.user.profile.name} (${state.user.profile.age})`,
|
|
155
|
-
});
|
|
156
|
-
expect(computed.displayName).toBe("Alice (30)");
|
|
157
|
-
state.user.profile.name = "Bob";
|
|
158
|
-
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
159
|
-
expect(computed.displayName).toBe("Bob (30)");
|
|
160
|
-
state.user.profile.age = 25;
|
|
161
|
-
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
162
|
-
expect(computed.displayName).toBe("Bob (25)");
|
|
163
|
-
});
|
|
164
|
-
it("should not recompute unnecessarily with nested computed", async () => {
|
|
165
|
-
const state = createState({ count: 5 });
|
|
166
|
-
const innerFn = vi.fn(() => state.count * 2);
|
|
167
|
-
const outerFn = vi.fn(() => computed.inner + 10);
|
|
168
|
-
const computed = createComputed({
|
|
169
|
-
inner: innerFn,
|
|
170
|
-
outer: outerFn,
|
|
171
|
-
});
|
|
172
|
-
// Access outer (should compute both)
|
|
173
|
-
expect(computed.outer).toBe(20);
|
|
174
|
-
expect(innerFn).toHaveBeenCalledTimes(1);
|
|
175
|
-
expect(outerFn).toHaveBeenCalledTimes(1);
|
|
176
|
-
// Access outer again (should use cache)
|
|
177
|
-
expect(computed.outer).toBe(20);
|
|
178
|
-
expect(innerFn).toHaveBeenCalledTimes(1);
|
|
179
|
-
expect(outerFn).toHaveBeenCalledTimes(1);
|
|
180
|
-
// Change state
|
|
181
|
-
state.count = 10;
|
|
182
|
-
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
183
|
-
// Access outer (should recompute both)
|
|
184
|
-
expect(computed.outer).toBe(30);
|
|
185
|
-
expect(innerFn).toHaveBeenCalledTimes(2);
|
|
186
|
-
expect(outerFn).toHaveBeenCalledTimes(2);
|
|
187
|
-
});
|
|
188
|
-
it("should handle conditional dependencies", async () => {
|
|
189
|
-
const state = createState({ useA: true, a: 10, b: 20 });
|
|
190
|
-
const computeFn = vi.fn(() => (state.useA ? state.a : state.b));
|
|
191
|
-
const computed = createComputed({
|
|
192
|
-
value: computeFn,
|
|
193
|
-
});
|
|
194
|
-
expect(computed.value).toBe(10);
|
|
195
|
-
expect(computeFn).toHaveBeenCalledTimes(1);
|
|
196
|
-
// Change b (not currently tracked)
|
|
197
|
-
state.b = 30;
|
|
198
|
-
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
199
|
-
expect(computed.value).toBe(10); // Should not recompute
|
|
200
|
-
expect(computeFn).toHaveBeenCalledTimes(1);
|
|
201
|
-
// Change a (currently tracked)
|
|
202
|
-
state.a = 15;
|
|
203
|
-
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
204
|
-
expect(computed.value).toBe(15); // Should recompute
|
|
205
|
-
expect(computeFn).toHaveBeenCalledTimes(2);
|
|
206
|
-
// Switch to using b
|
|
207
|
-
state.useA = false;
|
|
208
|
-
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
209
|
-
expect(computed.value).toBe(30); // Should recompute and now track b
|
|
210
|
-
expect(computeFn).toHaveBeenCalledTimes(3);
|
|
211
|
-
// Change a (no longer tracked)
|
|
212
|
-
state.a = 100;
|
|
213
|
-
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
214
|
-
expect(computed.value).toBe(30); // Should not recompute
|
|
215
|
-
expect(computeFn).toHaveBeenCalledTimes(3);
|
|
216
|
-
// Change b (now tracked)
|
|
217
|
-
state.b = 40;
|
|
218
|
-
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
219
|
-
expect(computed.value).toBe(40); // Should recompute
|
|
220
|
-
expect(computeFn).toHaveBeenCalledTimes(4);
|
|
221
|
-
});
|
|
222
|
-
it("should return consistent values during same synchronous execution", () => {
|
|
223
|
-
const state = createState({ count: 5 });
|
|
224
|
-
const computed = createComputed({
|
|
225
|
-
doubled: () => state.count * 2,
|
|
226
|
-
});
|
|
227
|
-
const first = computed.doubled;
|
|
228
|
-
const second = computed.doubled;
|
|
229
|
-
const third = computed.doubled;
|
|
230
|
-
expect(first).toBe(10);
|
|
231
|
-
expect(second).toBe(10);
|
|
232
|
-
expect(third).toBe(10);
|
|
233
|
-
});
|
|
234
|
-
it("should handle empty computed object", () => {
|
|
235
|
-
const computed = createComputed({});
|
|
236
|
-
expect(Object.keys(computed).length).toBe(0);
|
|
237
|
-
});
|
|
238
|
-
it("should properly track changes in computed used by observers", async () => {
|
|
239
|
-
const state = createState({ x: 1, y: 2 });
|
|
240
|
-
const computed = createComputed({
|
|
241
|
-
sum: () => state.x + state.y,
|
|
242
|
-
});
|
|
243
|
-
const results = [];
|
|
244
|
-
const observer = new Observer(() => {
|
|
245
|
-
results.push(computed.sum);
|
|
246
|
-
});
|
|
247
|
-
const dispose = observer.observe();
|
|
248
|
-
computed.sum; // Track
|
|
249
|
-
dispose();
|
|
250
|
-
state.x = 10;
|
|
251
|
-
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
252
|
-
state.y = 20;
|
|
253
|
-
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
254
|
-
expect(results).toEqual([12, 30]);
|
|
255
|
-
observer.dispose();
|
|
256
|
-
});
|
|
257
|
-
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"createContext.test.d.ts","sourceRoot":"","sources":["../../src/tests/createContext.test.tsx"],"names":[],"mappings":""}
|
|
@@ -1,136 +0,0 @@
|
|
|
1
|
-
import { jsx as _jsx, jsxs as _jsxs } from "./jsx-runtime";
|
|
2
|
-
import { describe, it, expect } from "vitest";
|
|
3
|
-
import { createContext } from "../createContext";
|
|
4
|
-
import { render } from "../";
|
|
5
|
-
describe("createContext", () => {
|
|
6
|
-
it("should create a context object", () => {
|
|
7
|
-
const context = createContext();
|
|
8
|
-
expect(context).toHaveProperty("inject");
|
|
9
|
-
expect(context).toHaveProperty("get");
|
|
10
|
-
});
|
|
11
|
-
it("should allow setting and getting context values", () => {
|
|
12
|
-
const ThemeContext = createContext();
|
|
13
|
-
function Parent() {
|
|
14
|
-
ThemeContext.inject({ theme: "dark" });
|
|
15
|
-
return () => _jsx(Child, {});
|
|
16
|
-
}
|
|
17
|
-
function Child() {
|
|
18
|
-
const theme = ThemeContext.get();
|
|
19
|
-
return () => _jsx("div", { children: theme.theme });
|
|
20
|
-
}
|
|
21
|
-
const container = document.createElement("div");
|
|
22
|
-
document.body.appendChild(container);
|
|
23
|
-
render(_jsx(Parent, {}), container);
|
|
24
|
-
expect(container.textContent).toContain("dark");
|
|
25
|
-
document.body.removeChild(container);
|
|
26
|
-
});
|
|
27
|
-
it("should traverse parent components to find context", () => {
|
|
28
|
-
const ThemeContext = createContext();
|
|
29
|
-
function GrandParent() {
|
|
30
|
-
ThemeContext.inject({ theme: "light" });
|
|
31
|
-
return () => _jsx(Parent, {});
|
|
32
|
-
}
|
|
33
|
-
function Parent() {
|
|
34
|
-
return () => _jsx(Child, {});
|
|
35
|
-
}
|
|
36
|
-
function Child() {
|
|
37
|
-
const theme = ThemeContext.get();
|
|
38
|
-
return () => _jsx("div", { children: theme.theme });
|
|
39
|
-
}
|
|
40
|
-
const container = document.createElement("div");
|
|
41
|
-
document.body.appendChild(container);
|
|
42
|
-
render(_jsx(GrandParent, {}), container);
|
|
43
|
-
expect(container.textContent).toContain("light");
|
|
44
|
-
document.body.removeChild(container);
|
|
45
|
-
});
|
|
46
|
-
it("should throw error when context is not found", () => {
|
|
47
|
-
const ThemeContext = createContext();
|
|
48
|
-
function Child() {
|
|
49
|
-
expect(() => {
|
|
50
|
-
ThemeContext.get();
|
|
51
|
-
}).toThrow("There is no parent context");
|
|
52
|
-
return () => _jsx("div", { children: "Child" });
|
|
53
|
-
}
|
|
54
|
-
const container = document.createElement("div");
|
|
55
|
-
document.body.appendChild(container);
|
|
56
|
-
render(_jsx(Child, {}), container);
|
|
57
|
-
document.body.removeChild(container);
|
|
58
|
-
});
|
|
59
|
-
it("should throw error when setting context outside component", () => {
|
|
60
|
-
const ThemeContext = createContext();
|
|
61
|
-
expect(() => {
|
|
62
|
-
ThemeContext.inject({ theme: "dark" });
|
|
63
|
-
}).toThrow("No current component");
|
|
64
|
-
});
|
|
65
|
-
it("should throw error when getting context outside component", () => {
|
|
66
|
-
const ThemeContext = createContext();
|
|
67
|
-
expect(() => {
|
|
68
|
-
ThemeContext.get();
|
|
69
|
-
}).toThrow("No current component");
|
|
70
|
-
});
|
|
71
|
-
it("should allow overriding context in nested components", () => {
|
|
72
|
-
const ThemeContext = createContext();
|
|
73
|
-
function GrandParent() {
|
|
74
|
-
ThemeContext.inject({ theme: "light" });
|
|
75
|
-
return () => (_jsxs("div", { children: [_jsx(Parent, {}), _jsx(ChildOfGrandParent, {})] }));
|
|
76
|
-
}
|
|
77
|
-
function Parent() {
|
|
78
|
-
ThemeContext.inject({ theme: "dark" });
|
|
79
|
-
return () => _jsx(ChildOfParent, {});
|
|
80
|
-
}
|
|
81
|
-
function ChildOfParent() {
|
|
82
|
-
const theme = ThemeContext.get();
|
|
83
|
-
return () => _jsx("div", { class: "child-of-parent", children: theme.theme });
|
|
84
|
-
}
|
|
85
|
-
function ChildOfGrandParent() {
|
|
86
|
-
const theme = ThemeContext.get();
|
|
87
|
-
return () => _jsx("div", { class: "child-of-grandparent", children: theme.theme });
|
|
88
|
-
}
|
|
89
|
-
const container = document.createElement("div");
|
|
90
|
-
document.body.appendChild(container);
|
|
91
|
-
render(_jsx(GrandParent, {}), container);
|
|
92
|
-
const childOfParent = document.querySelector(".child-of-parent");
|
|
93
|
-
const childOfGrandParent = document.querySelector(".child-of-grandparent");
|
|
94
|
-
expect(childOfParent?.textContent).toBe("dark");
|
|
95
|
-
expect(childOfGrandParent?.textContent).toBe("light");
|
|
96
|
-
document.body.removeChild(container);
|
|
97
|
-
});
|
|
98
|
-
it("should support multiple different contexts", () => {
|
|
99
|
-
const ThemeContext = createContext();
|
|
100
|
-
const UserContext = createContext();
|
|
101
|
-
function Parent() {
|
|
102
|
-
ThemeContext.inject({ theme: "dark" });
|
|
103
|
-
UserContext.inject({ name: "Alice" });
|
|
104
|
-
return () => _jsx(Child, {});
|
|
105
|
-
}
|
|
106
|
-
function Child() {
|
|
107
|
-
const theme = ThemeContext.get();
|
|
108
|
-
const user = UserContext.get();
|
|
109
|
-
return () => _jsx("div", { children: `${theme.theme} - ${user.name}` });
|
|
110
|
-
}
|
|
111
|
-
const container = document.createElement("div");
|
|
112
|
-
document.body.appendChild(container);
|
|
113
|
-
render(_jsx(Parent, {}), container);
|
|
114
|
-
expect(container.textContent).toContain("dark - Alice");
|
|
115
|
-
document.body.removeChild(container);
|
|
116
|
-
});
|
|
117
|
-
it("should handle context values of different types", () => {
|
|
118
|
-
const NumberContext = createContext();
|
|
119
|
-
const ArrayContext = createContext();
|
|
120
|
-
function Parent() {
|
|
121
|
-
NumberContext.inject(42);
|
|
122
|
-
ArrayContext.inject(["a", "b", "c"]);
|
|
123
|
-
return () => _jsx(Child, {});
|
|
124
|
-
}
|
|
125
|
-
function Child() {
|
|
126
|
-
const num = NumberContext.get();
|
|
127
|
-
const arr = ArrayContext.get();
|
|
128
|
-
return () => _jsx("div", { children: `${num} - ${arr.join(",")}` });
|
|
129
|
-
}
|
|
130
|
-
const container = document.createElement("div");
|
|
131
|
-
document.body.appendChild(container);
|
|
132
|
-
render(_jsx(Parent, {}), container);
|
|
133
|
-
expect(container.textContent).toContain("42 - a,b,c");
|
|
134
|
-
document.body.removeChild(container);
|
|
135
|
-
});
|
|
136
|
-
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"createEffect.test.d.ts","sourceRoot":"","sources":["../../src/tests/createEffect.test.tsx"],"names":[],"mappings":""}
|