rask-ui 0.10.5 → 0.12.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.
Files changed (52) hide show
  1. package/README.md +257 -5
  2. package/dist/batch.d.ts.map +1 -1
  3. package/dist/batch.js +36 -55
  4. package/dist/component.d.ts +2 -1
  5. package/dist/component.d.ts.map +1 -1
  6. package/dist/component.js +37 -22
  7. package/dist/createContext.d.ts +1 -0
  8. package/dist/createContext.d.ts.map +1 -1
  9. package/dist/createContext.js +11 -0
  10. package/dist/createEffect.d.ts +1 -1
  11. package/dist/createEffect.d.ts.map +1 -1
  12. package/dist/createEffect.js +15 -5
  13. package/dist/createRouter.d.ts +8 -0
  14. package/dist/createRouter.d.ts.map +1 -0
  15. package/dist/createRouter.js +24 -0
  16. package/dist/index.d.ts +1 -0
  17. package/dist/index.d.ts.map +1 -1
  18. package/dist/index.js +1 -0
  19. package/dist/jsx-runtime.d.ts +2 -2
  20. package/dist/jsx-runtime.js +2 -2
  21. package/dist/plugin.d.ts +9 -1
  22. package/dist/plugin.d.ts.map +1 -1
  23. package/dist/plugin.js +4 -3
  24. package/dist/tests/batch.test.d.ts +2 -0
  25. package/dist/tests/batch.test.d.ts.map +1 -0
  26. package/dist/tests/batch.test.js +244 -0
  27. package/dist/tests/createComputed.test.d.ts +2 -0
  28. package/dist/tests/createComputed.test.d.ts.map +1 -0
  29. package/dist/tests/createComputed.test.js +257 -0
  30. package/dist/tests/createContext.test.d.ts +2 -0
  31. package/dist/tests/createContext.test.d.ts.map +1 -0
  32. package/dist/tests/createContext.test.js +136 -0
  33. package/dist/tests/createEffect.test.d.ts +2 -0
  34. package/dist/tests/createEffect.test.d.ts.map +1 -0
  35. package/dist/tests/createEffect.test.js +454 -0
  36. package/dist/tests/createState.test.d.ts +2 -0
  37. package/dist/tests/createState.test.d.ts.map +1 -0
  38. package/dist/tests/createState.test.js +144 -0
  39. package/dist/tests/createTask.test.d.ts +2 -0
  40. package/dist/tests/createTask.test.d.ts.map +1 -0
  41. package/dist/tests/createTask.test.js +322 -0
  42. package/dist/tests/createView.test.d.ts +2 -0
  43. package/dist/tests/createView.test.d.ts.map +1 -0
  44. package/dist/tests/createView.test.js +203 -0
  45. package/dist/tests/error.test.d.ts +2 -0
  46. package/dist/tests/error.test.d.ts.map +1 -0
  47. package/dist/tests/error.test.js +168 -0
  48. package/dist/tests/observation.test.d.ts +2 -0
  49. package/dist/tests/observation.test.d.ts.map +1 -0
  50. package/dist/tests/observation.test.js +341 -0
  51. package/package.json +4 -3
  52. package/swc-plugin/target/wasm32-wasip1/release/swc_plugin_rask_component.wasm +0 -0
@@ -0,0 +1,136 @@
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
+ });
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=createEffect.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"createEffect.test.d.ts","sourceRoot":"","sources":["../../src/tests/createEffect.test.tsx"],"names":[],"mappings":""}
@@ -0,0 +1,454 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "./jsx-runtime";
2
+ import { describe, it, expect, vi } from "vitest";
3
+ import { createEffect } from "../createEffect";
4
+ import { createState } from "../createState";
5
+ import { render } from "../index";
6
+ describe("createEffect", () => {
7
+ it("should run immediately on creation", () => {
8
+ const effectFn = vi.fn();
9
+ createEffect(effectFn);
10
+ expect(effectFn).toHaveBeenCalledTimes(1);
11
+ });
12
+ it("should track reactive dependencies", async () => {
13
+ const state = createState({ count: 0 });
14
+ const effectFn = vi.fn(() => {
15
+ state.count; // Access to track
16
+ });
17
+ createEffect(effectFn);
18
+ expect(effectFn).toHaveBeenCalledTimes(1);
19
+ state.count = 1;
20
+ await new Promise((resolve) => setTimeout(resolve, 0));
21
+ expect(effectFn).toHaveBeenCalledTimes(2);
22
+ });
23
+ it("should re-run when dependencies change", async () => {
24
+ const state = createState({ count: 0 });
25
+ const results = [];
26
+ createEffect(() => {
27
+ results.push(state.count);
28
+ });
29
+ expect(results).toEqual([0]);
30
+ state.count = 1;
31
+ await new Promise((resolve) => setTimeout(resolve, 0));
32
+ expect(results).toEqual([0, 1]);
33
+ state.count = 2;
34
+ await new Promise((resolve) => setTimeout(resolve, 0));
35
+ expect(results).toEqual([0, 1, 2]);
36
+ });
37
+ it("should run on microtask, not synchronously", () => {
38
+ const state = createState({ count: 0 });
39
+ const results = [];
40
+ createEffect(() => {
41
+ results.push(state.count);
42
+ });
43
+ expect(results).toEqual([0]); // Initial run is synchronous
44
+ state.count = 1;
45
+ // Should not have run yet (microtask not flushed)
46
+ expect(results).toEqual([0]);
47
+ });
48
+ it("should handle multiple effects on same state", async () => {
49
+ const state = createState({ count: 0 });
50
+ const results1 = [];
51
+ const results2 = [];
52
+ createEffect(() => {
53
+ results1.push(state.count);
54
+ });
55
+ createEffect(() => {
56
+ results2.push(state.count * 2);
57
+ });
58
+ expect(results1).toEqual([0]);
59
+ expect(results2).toEqual([0]);
60
+ state.count = 5;
61
+ await new Promise((resolve) => setTimeout(resolve, 0));
62
+ expect(results1).toEqual([0, 5]);
63
+ expect(results2).toEqual([0, 10]);
64
+ });
65
+ it("should only track dependencies accessed during execution", async () => {
66
+ const state = createState({ a: 1, b: 2 });
67
+ const effectFn = vi.fn(() => {
68
+ state.a; // Only track 'a'
69
+ });
70
+ createEffect(effectFn);
71
+ expect(effectFn).toHaveBeenCalledTimes(1);
72
+ // Change 'b' (not tracked)
73
+ state.b = 100;
74
+ await new Promise((resolve) => setTimeout(resolve, 0));
75
+ expect(effectFn).toHaveBeenCalledTimes(1); // Should not re-run
76
+ // Change 'a' (tracked)
77
+ state.a = 10;
78
+ await new Promise((resolve) => setTimeout(resolve, 0));
79
+ expect(effectFn).toHaveBeenCalledTimes(2); // Should re-run
80
+ });
81
+ it("should re-track dependencies on each run", async () => {
82
+ const state = createState({ useA: true, a: 1, b: 2 });
83
+ const effectFn = vi.fn(() => {
84
+ if (state.useA) {
85
+ state.a;
86
+ }
87
+ else {
88
+ state.b;
89
+ }
90
+ });
91
+ createEffect(effectFn);
92
+ expect(effectFn).toHaveBeenCalledTimes(1);
93
+ // Change 'a' (currently tracked)
94
+ state.a = 10;
95
+ await new Promise((resolve) => setTimeout(resolve, 0));
96
+ expect(effectFn).toHaveBeenCalledTimes(2);
97
+ // Change 'b' (not tracked)
98
+ state.b = 20;
99
+ await new Promise((resolve) => setTimeout(resolve, 0));
100
+ expect(effectFn).toHaveBeenCalledTimes(2); // No change
101
+ // Switch to using 'b'
102
+ state.useA = false;
103
+ await new Promise((resolve) => setTimeout(resolve, 0));
104
+ expect(effectFn).toHaveBeenCalledTimes(3);
105
+ // Change 'a' (no longer tracked)
106
+ state.a = 100;
107
+ await new Promise((resolve) => setTimeout(resolve, 0));
108
+ expect(effectFn).toHaveBeenCalledTimes(3); // No change
109
+ // Change 'b' (now tracked)
110
+ state.b = 200;
111
+ await new Promise((resolve) => setTimeout(resolve, 0));
112
+ expect(effectFn).toHaveBeenCalledTimes(4);
113
+ });
114
+ it("should handle effects that modify state", async () => {
115
+ const state = createState({ input: 1, output: 0 });
116
+ createEffect(() => {
117
+ state.output = state.input * 2;
118
+ });
119
+ expect(state.output).toBe(2);
120
+ state.input = 5;
121
+ await new Promise((resolve) => setTimeout(resolve, 0));
122
+ expect(state.output).toBe(10);
123
+ });
124
+ it("should handle nested state access", async () => {
125
+ const state = createState({
126
+ user: {
127
+ profile: {
128
+ name: "Alice",
129
+ },
130
+ },
131
+ });
132
+ const results = [];
133
+ createEffect(() => {
134
+ results.push(state.user.profile.name);
135
+ });
136
+ expect(results).toEqual(["Alice"]);
137
+ state.user.profile.name = "Bob";
138
+ await new Promise((resolve) => setTimeout(resolve, 0));
139
+ expect(results).toEqual(["Alice", "Bob"]);
140
+ });
141
+ it("should handle array access", async () => {
142
+ const state = createState({ items: [1, 2, 3] });
143
+ const results = [];
144
+ createEffect(() => {
145
+ results.push(state.items.length);
146
+ });
147
+ expect(results).toEqual([3]);
148
+ state.items.push(4);
149
+ await new Promise((resolve) => setTimeout(resolve, 0));
150
+ expect(results).toEqual([3, 4]);
151
+ state.items.pop();
152
+ await new Promise((resolve) => setTimeout(resolve, 0));
153
+ expect(results).toEqual([3, 4, 3]);
154
+ });
155
+ it("should handle effects accessing array elements", async () => {
156
+ const state = createState({ items: [1, 2, 3] });
157
+ const results = [];
158
+ createEffect(() => {
159
+ const sum = state.items.reduce((acc, val) => acc + val, 0);
160
+ results.push(sum);
161
+ });
162
+ expect(results).toEqual([6]);
163
+ state.items.push(4);
164
+ await new Promise((resolve) => setTimeout(resolve, 0));
165
+ expect(results).toEqual([6, 10]);
166
+ state.items[0] = 10;
167
+ await new Promise((resolve) => setTimeout(resolve, 0));
168
+ expect(results).toEqual([6, 10, 19]);
169
+ });
170
+ it("should batch multiple state changes before re-running", async () => {
171
+ const state = createState({ a: 1, b: 2 });
172
+ const effectFn = vi.fn(() => {
173
+ state.a;
174
+ state.b;
175
+ });
176
+ createEffect(effectFn);
177
+ expect(effectFn).toHaveBeenCalledTimes(1);
178
+ // Multiple changes in same turn
179
+ state.a = 10;
180
+ state.b = 20;
181
+ state.a = 15;
182
+ await new Promise((resolve) => setTimeout(resolve, 0));
183
+ // Should only run once for all changes
184
+ expect(effectFn).toHaveBeenCalledTimes(2);
185
+ });
186
+ it("should handle effects with no dependencies", async () => {
187
+ let runCount = 0;
188
+ createEffect(() => {
189
+ runCount++;
190
+ // No reactive state accessed
191
+ });
192
+ expect(runCount).toBe(1);
193
+ // Wait to ensure it doesn't run again
194
+ await new Promise((resolve) => setTimeout(resolve, 10));
195
+ expect(runCount).toBe(1);
196
+ });
197
+ it("should handle effects that conditionally access state", async () => {
198
+ const state = createState({ enabled: true, value: 5 });
199
+ const effectFn = vi.fn(() => {
200
+ if (state.enabled) {
201
+ state.value;
202
+ }
203
+ });
204
+ createEffect(effectFn);
205
+ expect(effectFn).toHaveBeenCalledTimes(1);
206
+ // Change value (tracked because enabled is true)
207
+ state.value = 10;
208
+ await new Promise((resolve) => setTimeout(resolve, 0));
209
+ expect(effectFn).toHaveBeenCalledTimes(2);
210
+ // Disable
211
+ state.enabled = false;
212
+ await new Promise((resolve) => setTimeout(resolve, 0));
213
+ expect(effectFn).toHaveBeenCalledTimes(3);
214
+ // Change value (not tracked because enabled is false)
215
+ state.value = 20;
216
+ await new Promise((resolve) => setTimeout(resolve, 0));
217
+ expect(effectFn).toHaveBeenCalledTimes(3); // No change
218
+ });
219
+ it("should handle complex dependency graphs", async () => {
220
+ const state = createState({
221
+ multiplier: 2,
222
+ values: [1, 2, 3],
223
+ });
224
+ const results = [];
225
+ createEffect(() => {
226
+ const sum = state.values.reduce((acc, val) => acc + val, 0);
227
+ results.push(sum * state.multiplier);
228
+ });
229
+ expect(results).toEqual([12]); // (1+2+3) * 2
230
+ state.multiplier = 3;
231
+ await new Promise((resolve) => setTimeout(resolve, 0));
232
+ expect(results).toEqual([12, 18]); // (1+2+3) * 3
233
+ state.values.push(4);
234
+ await new Promise((resolve) => setTimeout(resolve, 0));
235
+ expect(results).toEqual([12, 18, 30]); // (1+2+3+4) * 3
236
+ });
237
+ it("should handle effects that run other synchronous code", async () => {
238
+ const state = createState({ count: 0 });
239
+ const sideEffects = [];
240
+ createEffect(() => {
241
+ sideEffects.push("effect-start");
242
+ const value = state.count;
243
+ sideEffects.push(`value-${value}`);
244
+ sideEffects.push("effect-end");
245
+ });
246
+ expect(sideEffects).toEqual(["effect-start", "value-0", "effect-end"]);
247
+ state.count = 1;
248
+ await new Promise((resolve) => setTimeout(resolve, 0));
249
+ expect(sideEffects).toEqual([
250
+ "effect-start",
251
+ "value-0",
252
+ "effect-end",
253
+ "effect-start",
254
+ "value-1",
255
+ "effect-end",
256
+ ]);
257
+ });
258
+ it("should handle rapid state changes", async () => {
259
+ const state = createState({ count: 0 });
260
+ const effectFn = vi.fn(() => {
261
+ state.count;
262
+ });
263
+ createEffect(effectFn);
264
+ expect(effectFn).toHaveBeenCalledTimes(1);
265
+ // Rapid changes
266
+ for (let i = 1; i <= 10; i++) {
267
+ state.count = i;
268
+ }
269
+ await new Promise((resolve) => setTimeout(resolve, 0));
270
+ // Should batch all changes into one effect run
271
+ expect(effectFn).toHaveBeenCalledTimes(2);
272
+ });
273
+ it("should access latest state values when effect runs", async () => {
274
+ const state = createState({ count: 0 });
275
+ const results = [];
276
+ createEffect(() => {
277
+ results.push(state.count);
278
+ });
279
+ state.count = 1;
280
+ state.count = 2;
281
+ state.count = 3;
282
+ await new Promise((resolve) => setTimeout(resolve, 0));
283
+ // Should see latest value when effect runs
284
+ expect(results).toEqual([0, 3]);
285
+ });
286
+ it("should call dispose function before re-executing", async () => {
287
+ const state = createState({ count: 0 });
288
+ const disposeCalls = [];
289
+ const effectCalls = [];
290
+ createEffect(() => {
291
+ effectCalls.push(state.count);
292
+ return () => {
293
+ // Dispose sees the current state at the time it's called
294
+ disposeCalls.push(state.count);
295
+ };
296
+ });
297
+ expect(effectCalls).toEqual([0]);
298
+ expect(disposeCalls).toEqual([]);
299
+ state.count = 1;
300
+ await new Promise((resolve) => setTimeout(resolve, 0));
301
+ // Dispose is called after state change, before effect re-runs
302
+ expect(disposeCalls).toEqual([1]);
303
+ expect(effectCalls).toEqual([0, 1]);
304
+ state.count = 2;
305
+ await new Promise((resolve) => setTimeout(resolve, 0));
306
+ expect(disposeCalls).toEqual([1, 2]);
307
+ expect(effectCalls).toEqual([0, 1, 2]);
308
+ });
309
+ it("should handle dispose function with cleanup logic", async () => {
310
+ const state = createState({ url: "/api/data" });
311
+ const subscriptions = [];
312
+ createEffect(() => {
313
+ const currentUrl = state.url;
314
+ subscriptions.push(`subscribe:${currentUrl}`);
315
+ return () => {
316
+ subscriptions.push(`unsubscribe:${currentUrl}`);
317
+ };
318
+ });
319
+ expect(subscriptions).toEqual(["subscribe:/api/data"]);
320
+ state.url = "/api/users";
321
+ await new Promise((resolve) => setTimeout(resolve, 0));
322
+ expect(subscriptions).toEqual([
323
+ "subscribe:/api/data",
324
+ "unsubscribe:/api/data",
325
+ "subscribe:/api/users",
326
+ ]);
327
+ state.url = "/api/posts";
328
+ await new Promise((resolve) => setTimeout(resolve, 0));
329
+ expect(subscriptions).toEqual([
330
+ "subscribe:/api/data",
331
+ "unsubscribe:/api/data",
332
+ "subscribe:/api/users",
333
+ "unsubscribe:/api/users",
334
+ "subscribe:/api/posts",
335
+ ]);
336
+ });
337
+ it("should handle effects without dispose function", async () => {
338
+ const state = createState({ count: 0 });
339
+ const effectCalls = [];
340
+ createEffect(() => {
341
+ effectCalls.push(state.count);
342
+ // No dispose function returned
343
+ });
344
+ expect(effectCalls).toEqual([0]);
345
+ state.count = 1;
346
+ await new Promise((resolve) => setTimeout(resolve, 0));
347
+ expect(effectCalls).toEqual([0, 1]);
348
+ state.count = 2;
349
+ await new Promise((resolve) => setTimeout(resolve, 0));
350
+ expect(effectCalls).toEqual([0, 1, 2]);
351
+ });
352
+ it("should handle dispose function that throws error", async () => {
353
+ const state = createState({ count: 0 });
354
+ const effectCalls = [];
355
+ const consoleErrorSpy = vi
356
+ .spyOn(console, "error")
357
+ .mockImplementation(() => { });
358
+ createEffect(() => {
359
+ effectCalls.push(state.count);
360
+ return () => {
361
+ throw new Error("Dispose error");
362
+ };
363
+ });
364
+ expect(effectCalls).toEqual([0]);
365
+ state.count = 1;
366
+ await new Promise((resolve) => setTimeout(resolve, 0));
367
+ // Effect should still have run despite dispose throwing
368
+ expect(effectCalls).toEqual([0, 1]);
369
+ expect(consoleErrorSpy).toHaveBeenCalledWith("Error in effect dispose function:", expect.any(Error));
370
+ consoleErrorSpy.mockRestore();
371
+ });
372
+ it("should call dispose with latest closure values", async () => {
373
+ const state = createState({ count: 0 });
374
+ const disposeValues = [];
375
+ createEffect(() => {
376
+ const capturedCount = state.count;
377
+ return () => {
378
+ disposeValues.push(capturedCount);
379
+ };
380
+ });
381
+ state.count = 1;
382
+ await new Promise((resolve) => setTimeout(resolve, 0));
383
+ expect(disposeValues).toEqual([0]);
384
+ state.count = 5;
385
+ await new Promise((resolve) => setTimeout(resolve, 0));
386
+ expect(disposeValues).toEqual([0, 1]);
387
+ state.count = 10;
388
+ await new Promise((resolve) => setTimeout(resolve, 0));
389
+ expect(disposeValues).toEqual([0, 1, 5]);
390
+ });
391
+ it("should handle rapid state changes with dispose", async () => {
392
+ const state = createState({ count: 0 });
393
+ const effectFn = vi.fn(() => {
394
+ state.count;
395
+ });
396
+ const disposeFn = vi.fn();
397
+ createEffect(() => {
398
+ effectFn();
399
+ return disposeFn;
400
+ });
401
+ expect(effectFn).toHaveBeenCalledTimes(1);
402
+ expect(disposeFn).toHaveBeenCalledTimes(0);
403
+ // Rapid changes should batch
404
+ state.count = 1;
405
+ state.count = 2;
406
+ state.count = 3;
407
+ await new Promise((resolve) => setTimeout(resolve, 0));
408
+ // Effect and dispose should each be called once more
409
+ expect(effectFn).toHaveBeenCalledTimes(2);
410
+ expect(disposeFn).toHaveBeenCalledTimes(1);
411
+ });
412
+ it("should run effects synchronously before render when props change", async () => {
413
+ const renderLog = [];
414
+ function Child(props) {
415
+ const state = createState({ internalValue: 0 });
416
+ createEffect(() => {
417
+ // Update internal state based on prop
418
+ state.internalValue = props.value * 2;
419
+ });
420
+ return () => {
421
+ renderLog.push(`render:${state.internalValue}`);
422
+ return _jsx("div", { class: "child-component", children: state.internalValue });
423
+ };
424
+ }
425
+ function Parent() {
426
+ const state = createState({ count: 1 });
427
+ return () => {
428
+ renderLog.push(`parent-render:${state.count}`);
429
+ return (_jsxs("div", { children: [_jsx(Child, { value: state.count }), _jsx("button", { onClick: () => {
430
+ state.count = 2;
431
+ }, children: "Increment" })] }));
432
+ };
433
+ }
434
+ const container = document.createElement("div");
435
+ document.body.appendChild(container);
436
+ render(_jsx(Parent, {}), container);
437
+ await new Promise((resolve) => setTimeout(resolve, 10));
438
+ // Initial render: parent renders, then child renders with effect-updated state
439
+ expect(renderLog).toEqual(["parent-render:1", "render:2"]);
440
+ const childDiv = container.querySelector(".child-component");
441
+ expect(childDiv?.textContent).toBe("2");
442
+ // Clear log and trigger update
443
+ renderLog.length = 0;
444
+ const button = container.querySelector("button");
445
+ button.click();
446
+ await new Promise((resolve) => setTimeout(resolve, 10));
447
+ console.log(renderLog);
448
+ // After prop update: parent renders, child's effect runs synchronously updating state,
449
+ // then child renders once with the updated state
450
+ expect(renderLog).toEqual(["parent-render:2", "render:4"]);
451
+ expect(childDiv?.textContent).toBe("4");
452
+ document.body.removeChild(container);
453
+ });
454
+ });
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=createState.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"createState.test.d.ts","sourceRoot":"","sources":["../../src/tests/createState.test.ts"],"names":[],"mappings":""}