rask-ui 0.20.1 → 0.20.2
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/compiler.d.ts +2 -0
- package/dist/compiler.d.ts.map +1 -0
- package/dist/compiler.js +1 -0
- 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/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 +2 -2
- 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/plugin.js +1 -1
- 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/types.d.ts +20 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +1 -0
- package/package.json +5 -1
- 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
package/dist/tests/batch.test.js
DELETED
|
@@ -1,244 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect } from "vitest";
|
|
2
|
-
import { syncBatch } from "../batch";
|
|
3
|
-
import { createState } from "../createState";
|
|
4
|
-
import { Observer } from "../observation";
|
|
5
|
-
describe("syncBatch", () => {
|
|
6
|
-
it("should batch multiple state changes into a single notification", () => {
|
|
7
|
-
const state = createState({ count: 0, name: "Alice" });
|
|
8
|
-
let notifyCount = 0;
|
|
9
|
-
const observer = new Observer(() => {
|
|
10
|
-
notifyCount++;
|
|
11
|
-
});
|
|
12
|
-
const dispose = observer.observe();
|
|
13
|
-
state.count; // Track count
|
|
14
|
-
state.name; // Track name
|
|
15
|
-
dispose();
|
|
16
|
-
// Make multiple changes in a batch
|
|
17
|
-
syncBatch(() => {
|
|
18
|
-
state.count = 1;
|
|
19
|
-
state.name = "Bob";
|
|
20
|
-
state.count = 2;
|
|
21
|
-
});
|
|
22
|
-
// Should only notify once despite multiple changes, and synchronously
|
|
23
|
-
expect(notifyCount).toBe(1);
|
|
24
|
-
expect(state.count).toBe(2);
|
|
25
|
-
expect(state.name).toBe("Bob");
|
|
26
|
-
observer.dispose();
|
|
27
|
-
});
|
|
28
|
-
it("should handle nested batches correctly", () => {
|
|
29
|
-
const state = createState({ count: 0 });
|
|
30
|
-
let notifyCount = 0;
|
|
31
|
-
const observer = new Observer(() => {
|
|
32
|
-
notifyCount++;
|
|
33
|
-
});
|
|
34
|
-
const dispose = observer.observe();
|
|
35
|
-
state.count; // Track
|
|
36
|
-
dispose();
|
|
37
|
-
syncBatch(() => {
|
|
38
|
-
state.count = 1;
|
|
39
|
-
syncBatch(() => {
|
|
40
|
-
state.count = 2;
|
|
41
|
-
});
|
|
42
|
-
state.count = 3;
|
|
43
|
-
});
|
|
44
|
-
// Should still only notify once for nested batches
|
|
45
|
-
expect(notifyCount).toBe(1);
|
|
46
|
-
expect(state.count).toBe(3);
|
|
47
|
-
observer.dispose();
|
|
48
|
-
});
|
|
49
|
-
it("should handle multiple observers with syncBatch", () => {
|
|
50
|
-
const state = createState({ count: 0 });
|
|
51
|
-
let notifyCount1 = 0;
|
|
52
|
-
let notifyCount2 = 0;
|
|
53
|
-
const observer1 = new Observer(() => {
|
|
54
|
-
notifyCount1++;
|
|
55
|
-
});
|
|
56
|
-
const observer2 = new Observer(() => {
|
|
57
|
-
notifyCount2++;
|
|
58
|
-
});
|
|
59
|
-
const dispose1 = observer1.observe();
|
|
60
|
-
state.count; // Track in observer1
|
|
61
|
-
dispose1();
|
|
62
|
-
const dispose2 = observer2.observe();
|
|
63
|
-
state.count; // Track in observer2
|
|
64
|
-
dispose2();
|
|
65
|
-
syncBatch(() => {
|
|
66
|
-
state.count = 1;
|
|
67
|
-
state.count = 2;
|
|
68
|
-
state.count = 3;
|
|
69
|
-
});
|
|
70
|
-
// Both observers should be notified exactly once
|
|
71
|
-
expect(notifyCount1).toBe(1);
|
|
72
|
-
expect(notifyCount2).toBe(1);
|
|
73
|
-
observer1.dispose();
|
|
74
|
-
observer2.dispose();
|
|
75
|
-
});
|
|
76
|
-
it("should maintain correct state values after syncBatch", () => {
|
|
77
|
-
const state = createState({
|
|
78
|
-
count: 0,
|
|
79
|
-
name: "Alice",
|
|
80
|
-
items: [1, 2, 3],
|
|
81
|
-
});
|
|
82
|
-
syncBatch(() => {
|
|
83
|
-
state.count = 10;
|
|
84
|
-
state.name = "Bob";
|
|
85
|
-
state.items.push(4);
|
|
86
|
-
state.items[0] = 100;
|
|
87
|
-
});
|
|
88
|
-
expect(state.count).toBe(10);
|
|
89
|
-
expect(state.name).toBe("Bob");
|
|
90
|
-
expect(state.items).toEqual([100, 2, 3, 4]);
|
|
91
|
-
});
|
|
92
|
-
it("should not flush if exception thrown within syncBatch", () => {
|
|
93
|
-
const state = createState({ count: 0 });
|
|
94
|
-
let notifyCount = 0;
|
|
95
|
-
const observer = new Observer(() => {
|
|
96
|
-
notifyCount++;
|
|
97
|
-
});
|
|
98
|
-
const dispose = observer.observe();
|
|
99
|
-
state.count; // Track
|
|
100
|
-
dispose();
|
|
101
|
-
try {
|
|
102
|
-
syncBatch(() => {
|
|
103
|
-
state.count = 1;
|
|
104
|
-
throw new Error("Test error");
|
|
105
|
-
});
|
|
106
|
-
}
|
|
107
|
-
catch (e) {
|
|
108
|
-
// Expected error
|
|
109
|
-
}
|
|
110
|
-
// Should NOT have flushed since the batch was interrupted
|
|
111
|
-
expect(notifyCount).toBe(0);
|
|
112
|
-
// But state change still occurred
|
|
113
|
-
expect(state.count).toBe(1);
|
|
114
|
-
observer.dispose();
|
|
115
|
-
});
|
|
116
|
-
it("should deduplicate notifications for the same observer", () => {
|
|
117
|
-
const state = createState({ count: 0, name: "Alice" });
|
|
118
|
-
let notifyCount = 0;
|
|
119
|
-
const observer = new Observer(() => {
|
|
120
|
-
notifyCount++;
|
|
121
|
-
});
|
|
122
|
-
const dispose = observer.observe();
|
|
123
|
-
state.count; // Track
|
|
124
|
-
state.name; // Track
|
|
125
|
-
dispose();
|
|
126
|
-
syncBatch(() => {
|
|
127
|
-
state.count = 1; // Triggers observer
|
|
128
|
-
state.name = "Bob"; // Triggers same observer again
|
|
129
|
-
state.count = 2; // Triggers observer yet again
|
|
130
|
-
});
|
|
131
|
-
// Should deduplicate and only notify once
|
|
132
|
-
expect(notifyCount).toBe(1);
|
|
133
|
-
observer.dispose();
|
|
134
|
-
});
|
|
135
|
-
});
|
|
136
|
-
describe("queue (async batching)", () => {
|
|
137
|
-
it("should queue updates and flush on microtask", async () => {
|
|
138
|
-
const state = createState({ count: 0 });
|
|
139
|
-
let notifyCount = 0;
|
|
140
|
-
const observer = new Observer(() => {
|
|
141
|
-
notifyCount++;
|
|
142
|
-
});
|
|
143
|
-
const dispose = observer.observe();
|
|
144
|
-
state.count; // Track
|
|
145
|
-
dispose();
|
|
146
|
-
// Make changes that will be queued
|
|
147
|
-
state.count = 1;
|
|
148
|
-
state.count = 2;
|
|
149
|
-
state.count = 3;
|
|
150
|
-
// Not yet notified (queued)
|
|
151
|
-
expect(notifyCount).toBe(0);
|
|
152
|
-
// Wait for microtask flush
|
|
153
|
-
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
154
|
-
// Should have notified once after flush
|
|
155
|
-
expect(notifyCount).toBe(1);
|
|
156
|
-
expect(state.count).toBe(3);
|
|
157
|
-
observer.dispose();
|
|
158
|
-
});
|
|
159
|
-
it("should batch multiple async updates into one notification", async () => {
|
|
160
|
-
const state = createState({ count: 0, name: "Alice" });
|
|
161
|
-
let notifyCount = 0;
|
|
162
|
-
const observer = new Observer(() => {
|
|
163
|
-
notifyCount++;
|
|
164
|
-
});
|
|
165
|
-
const dispose = observer.observe();
|
|
166
|
-
state.count;
|
|
167
|
-
state.name;
|
|
168
|
-
dispose();
|
|
169
|
-
state.count = 1;
|
|
170
|
-
state.name = "Bob";
|
|
171
|
-
state.count = 2;
|
|
172
|
-
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
173
|
-
// Should batch all updates into single notification
|
|
174
|
-
expect(notifyCount).toBe(1);
|
|
175
|
-
observer.dispose();
|
|
176
|
-
});
|
|
177
|
-
it("should handle separate async batches", async () => {
|
|
178
|
-
const state = createState({ count: 0 });
|
|
179
|
-
let notifyCount = 0;
|
|
180
|
-
const observer = new Observer(() => {
|
|
181
|
-
notifyCount++;
|
|
182
|
-
});
|
|
183
|
-
const dispose = observer.observe();
|
|
184
|
-
state.count;
|
|
185
|
-
dispose();
|
|
186
|
-
state.count = 1;
|
|
187
|
-
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
188
|
-
const afterFirst = notifyCount;
|
|
189
|
-
state.count = 2;
|
|
190
|
-
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
191
|
-
const afterSecond = notifyCount;
|
|
192
|
-
expect(afterFirst).toBe(1);
|
|
193
|
-
expect(afterSecond).toBe(2);
|
|
194
|
-
observer.dispose();
|
|
195
|
-
});
|
|
196
|
-
});
|
|
197
|
-
describe("syncBatch with nested async updates", () => {
|
|
198
|
-
it("should handle syncBatch inside async context", async () => {
|
|
199
|
-
const state = createState({ count: 0 });
|
|
200
|
-
let notifyCount = 0;
|
|
201
|
-
const observer = new Observer(() => {
|
|
202
|
-
notifyCount++;
|
|
203
|
-
});
|
|
204
|
-
const dispose = observer.observe();
|
|
205
|
-
state.count;
|
|
206
|
-
dispose();
|
|
207
|
-
// Async update
|
|
208
|
-
state.count = 1;
|
|
209
|
-
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
210
|
-
expect(notifyCount).toBe(1);
|
|
211
|
-
// Sync batch after async
|
|
212
|
-
syncBatch(() => {
|
|
213
|
-
state.count = 2;
|
|
214
|
-
state.count = 3;
|
|
215
|
-
});
|
|
216
|
-
expect(notifyCount).toBe(2); // +1 from sync batch
|
|
217
|
-
observer.dispose();
|
|
218
|
-
});
|
|
219
|
-
it("should handle async updates inside syncBatch callback", async () => {
|
|
220
|
-
const state = createState({ count: 0 });
|
|
221
|
-
let notifyCount = 0;
|
|
222
|
-
const observer = new Observer(() => {
|
|
223
|
-
notifyCount++;
|
|
224
|
-
});
|
|
225
|
-
const dispose = observer.observe();
|
|
226
|
-
state.count;
|
|
227
|
-
dispose();
|
|
228
|
-
syncBatch(() => {
|
|
229
|
-
state.count = 1;
|
|
230
|
-
// Trigger an async update from within syncBatch
|
|
231
|
-
setTimeout(() => {
|
|
232
|
-
state.count = 2;
|
|
233
|
-
}, 0);
|
|
234
|
-
});
|
|
235
|
-
// Sync batch should flush immediately
|
|
236
|
-
expect(notifyCount).toBe(1);
|
|
237
|
-
expect(state.count).toBe(1);
|
|
238
|
-
// Wait for async update
|
|
239
|
-
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
240
|
-
expect(notifyCount).toBe(2);
|
|
241
|
-
expect(state.count).toBe(2);
|
|
242
|
-
observer.dispose();
|
|
243
|
-
});
|
|
244
|
-
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"createComputed.test.d.ts","sourceRoot":"","sources":["../../src/tests/createComputed.test.ts"],"names":[],"mappings":""}
|
|
@@ -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":""}
|