rask-ui 0.1.0 → 0.2.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/component.d.ts +17 -0
- package/dist/component.d.ts.map +1 -1
- package/dist/component.js +4 -2
- package/dist/createAsync.d.ts +23 -0
- package/dist/createAsync.d.ts.map +1 -1
- package/dist/createAsync.js +23 -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 +26 -2
- package/dist/createContext.d.ts.map +1 -1
- package/dist/createContext.js +31 -5
- package/dist/createContext.test.d.ts +2 -0
- package/dist/createContext.test.d.ts.map +1 -0
- package/dist/createContext.test.js +136 -0
- package/dist/createMutation.d.ts +23 -0
- package/dist/createMutation.d.ts.map +1 -1
- package/dist/createMutation.js +23 -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.d.ts +23 -0
- package/dist/createQuery.d.ts.map +1 -1
- package/dist/createQuery.js +23 -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.test.d.ts +2 -0
- package/dist/createRef.test.d.ts.map +1 -0
- package/dist/createRef.test.js +80 -0
- package/dist/createState.d.ts +24 -0
- package/dist/createState.d.ts.map +1 -1
- package/dist/createState.js +24 -0
- package/dist/createState.test.d.ts +2 -0
- package/dist/createState.test.d.ts.map +1 -0
- package/dist/createState.test.js +111 -0
- package/dist/createView.d.ts +54 -0
- package/dist/createView.d.ts.map +1 -0
- package/dist/createView.js +68 -0
- package/dist/createView.test.d.ts +2 -0
- package/dist/createView.test.d.ts.map +1 -0
- package/dist/createView.test.js +203 -0
- package/dist/error.d.ts.map +1 -1
- package/dist/error.js +15 -2
- package/dist/error.test.d.ts +2 -0
- package/dist/error.test.d.ts.map +1 -0
- package/dist/error.test.js +144 -0
- package/dist/index.d.ts +3 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -2
- package/dist/integration.test.d.ts +2 -0
- package/dist/integration.test.d.ts.map +1 -0
- package/dist/integration.test.js +155 -0
- package/dist/jsx-dev-runtime.d.ts +3 -3
- package/dist/jsx-dev-runtime.d.ts.map +1 -1
- package/dist/jsx-dev-runtime.js +2 -2
- package/dist/jsx-runtime.d.ts +1 -4
- package/dist/jsx-runtime.d.ts.map +1 -1
- package/dist/jsx-runtime.js +3 -14
- package/dist/observation.d.ts +1 -0
- package/dist/observation.d.ts.map +1 -1
- package/dist/observation.js +5 -0
- package/dist/observation.test.d.ts +2 -0
- package/dist/observation.test.d.ts.map +1 -0
- package/dist/observation.test.js +150 -0
- package/dist/render-test.d.ts +2 -0
- package/dist/render-test.d.ts.map +1 -0
- package/dist/render-test.js +21 -0
- package/dist/render.d.ts +1 -1
- package/dist/render.d.ts.map +1 -1
- package/dist/render.js +13 -1
- package/dist/test-setup.d.ts +16 -0
- package/dist/test-setup.d.ts.map +1 -0
- package/dist/test-setup.js +39 -0
- package/dist/tests/class.test.d.ts +2 -0
- package/dist/tests/class.test.d.ts.map +1 -0
- package/dist/tests/class.test.js +143 -0
- package/dist/tests/complex-rendering.test.d.ts +2 -0
- package/dist/tests/complex-rendering.test.d.ts.map +1 -0
- package/dist/tests/complex-rendering.test.js +400 -0
- package/dist/tests/component.cleanup.test.d.ts +2 -0
- package/dist/tests/component.cleanup.test.d.ts.map +1 -0
- package/dist/tests/component.cleanup.test.js +325 -0
- package/dist/tests/component.counter.test.d.ts +2 -0
- package/dist/tests/component.counter.test.d.ts.map +1 -0
- package/dist/tests/component.counter.test.js +124 -0
- package/dist/tests/component.interaction.test.d.ts +2 -0
- package/dist/tests/component.interaction.test.d.ts.map +1 -0
- package/dist/tests/component.interaction.test.js +73 -0
- package/dist/tests/component.props.test.d.ts +2 -0
- package/dist/tests/component.props.test.d.ts.map +1 -0
- package/dist/tests/component.props.test.js +88 -0
- package/dist/tests/component.return-types.test.d.ts +2 -0
- package/dist/tests/component.return-types.test.d.ts.map +1 -0
- package/dist/tests/component.return-types.test.js +357 -0
- package/dist/tests/component.state.test.d.ts +2 -0
- package/dist/tests/component.state.test.d.ts.map +1 -0
- package/dist/tests/component.state.test.js +129 -0
- package/dist/tests/component.test.d.ts +2 -0
- package/dist/tests/component.test.d.ts.map +1 -0
- package/dist/tests/component.test.js +63 -0
- package/dist/tests/createAsync.test.d.ts +2 -0
- package/dist/tests/createAsync.test.d.ts.map +1 -0
- package/dist/tests/createAsync.test.js +110 -0
- package/dist/tests/createContext.test.d.ts +2 -0
- package/dist/tests/createContext.test.d.ts.map +1 -0
- package/dist/tests/createContext.test.js +141 -0
- package/dist/tests/createMutation.test.d.ts +2 -0
- package/dist/tests/createMutation.test.d.ts.map +1 -0
- package/dist/tests/createMutation.test.js +168 -0
- package/dist/tests/createQuery.test.d.ts +2 -0
- package/dist/tests/createQuery.test.d.ts.map +1 -0
- package/dist/tests/createQuery.test.js +156 -0
- package/dist/tests/createRef.test.d.ts +2 -0
- package/dist/tests/createRef.test.d.ts.map +1 -0
- package/dist/tests/createRef.test.js +84 -0
- package/dist/tests/createState.test.d.ts +2 -0
- package/dist/tests/createState.test.d.ts.map +1 -0
- package/dist/tests/createState.test.js +111 -0
- package/dist/tests/createView.test.d.ts +2 -0
- package/dist/tests/createView.test.d.ts.map +1 -0
- package/dist/tests/createView.test.js +203 -0
- package/dist/tests/edge-cases.test.d.ts +2 -0
- package/dist/tests/edge-cases.test.d.ts.map +1 -0
- package/dist/tests/edge-cases.test.js +637 -0
- package/dist/tests/error-no-boundary.test.d.ts +2 -0
- package/dist/tests/error-no-boundary.test.d.ts.map +1 -0
- package/dist/tests/error-no-boundary.test.js +174 -0
- package/dist/tests/error.test.d.ts +2 -0
- package/dist/tests/error.test.d.ts.map +1 -0
- package/dist/tests/error.test.js +199 -0
- package/dist/tests/fragment.test.d.ts +2 -0
- package/dist/tests/fragment.test.d.ts.map +1 -0
- package/dist/tests/fragment.test.js +618 -0
- package/dist/tests/integration.test.d.ts +2 -0
- package/dist/tests/integration.test.d.ts.map +1 -0
- package/dist/tests/integration.test.js +192 -0
- package/dist/tests/keys.test.d.ts +2 -0
- package/dist/tests/keys.test.d.ts.map +1 -0
- package/dist/tests/keys.test.js +293 -0
- package/dist/tests/mount.test.d.ts +2 -0
- package/dist/tests/mount.test.d.ts.map +1 -0
- package/dist/tests/mount.test.js +91 -0
- package/dist/tests/observation.test.d.ts +2 -0
- package/dist/tests/observation.test.d.ts.map +1 -0
- package/dist/tests/observation.test.js +150 -0
- package/dist/tests/patch.test.d.ts +2 -0
- package/dist/tests/patch.test.d.ts.map +1 -0
- package/dist/tests/patch.test.js +498 -0
- package/dist/tests/patchChildren.test.d.ts +2 -0
- package/dist/tests/patchChildren.test.d.ts.map +1 -0
- package/dist/tests/patchChildren.test.js +387 -0
- package/dist/tests/primitives.test.d.ts +2 -0
- package/dist/tests/primitives.test.d.ts.map +1 -0
- package/dist/tests/primitives.test.js +132 -0
- package/dist/vdom/AbstractVNode.d.ts +22 -0
- package/dist/vdom/AbstractVNode.d.ts.map +1 -0
- package/dist/vdom/AbstractVNode.js +106 -0
- package/dist/vdom/ComponentVNode.d.ts +48 -0
- package/dist/vdom/ComponentVNode.d.ts.map +1 -0
- package/dist/vdom/ComponentVNode.js +209 -0
- package/dist/vdom/ElementVNode.d.ts +24 -0
- package/dist/vdom/ElementVNode.d.ts.map +1 -0
- package/dist/vdom/ElementVNode.js +126 -0
- package/dist/vdom/FragmentVNode.d.ts +13 -0
- package/dist/vdom/FragmentVNode.d.ts.map +1 -0
- package/dist/vdom/FragmentVNode.js +34 -0
- package/dist/vdom/RootVNode.d.ts +22 -0
- package/dist/vdom/RootVNode.d.ts.map +1 -0
- package/dist/vdom/RootVNode.js +55 -0
- package/dist/vdom/TextVNode.d.ts +11 -0
- package/dist/vdom/TextVNode.d.ts.map +1 -0
- package/dist/vdom/TextVNode.js +32 -0
- package/dist/vdom/class.test.d.ts +2 -0
- package/dist/vdom/class.test.d.ts.map +1 -0
- package/dist/vdom/class.test.js +143 -0
- package/dist/vdom/complex-rendering.test.d.ts +2 -0
- package/dist/vdom/complex-rendering.test.d.ts.map +1 -0
- package/dist/vdom/complex-rendering.test.js +400 -0
- package/dist/vdom/component.cleanup.test.d.ts +2 -0
- package/dist/vdom/component.cleanup.test.d.ts.map +1 -0
- package/dist/vdom/component.cleanup.test.js +323 -0
- package/dist/vdom/component.counter.test.d.ts +2 -0
- package/dist/vdom/component.counter.test.d.ts.map +1 -0
- package/dist/vdom/component.counter.test.js +124 -0
- package/dist/vdom/component.interaction.test.d.ts +2 -0
- package/dist/vdom/component.interaction.test.d.ts.map +1 -0
- package/dist/vdom/component.interaction.test.js +73 -0
- package/dist/vdom/component.props.test.d.ts +2 -0
- package/dist/vdom/component.props.test.d.ts.map +1 -0
- package/dist/vdom/component.props.test.js +88 -0
- package/dist/vdom/component.return-types.test.d.ts +2 -0
- package/dist/vdom/component.return-types.test.d.ts.map +1 -0
- package/dist/vdom/component.return-types.test.js +357 -0
- package/dist/vdom/component.state.test.d.ts +2 -0
- package/dist/vdom/component.state.test.d.ts.map +1 -0
- package/dist/vdom/component.state.test.js +129 -0
- package/dist/vdom/component.test.d.ts +2 -0
- package/dist/vdom/component.test.d.ts.map +1 -0
- package/dist/vdom/component.test.js +63 -0
- package/dist/vdom/dom-utils.d.ts +9 -0
- package/dist/vdom/dom-utils.d.ts.map +1 -0
- package/dist/vdom/dom-utils.js +74 -0
- package/dist/vdom/edge-cases.test.d.ts +2 -0
- package/dist/vdom/edge-cases.test.d.ts.map +1 -0
- package/dist/vdom/edge-cases.test.js +637 -0
- package/dist/vdom/fragment.test.d.ts +2 -0
- package/dist/vdom/fragment.test.d.ts.map +1 -0
- package/dist/vdom/fragment.test.js +618 -0
- package/dist/vdom/index.d.ts +10 -0
- package/dist/vdom/index.d.ts.map +1 -0
- package/dist/vdom/index.js +26 -0
- package/dist/vdom/keys.test.d.ts +2 -0
- package/dist/vdom/keys.test.d.ts.map +1 -0
- package/dist/vdom/keys.test.js +293 -0
- package/dist/vdom/mount.test.d.ts +2 -0
- package/dist/vdom/mount.test.d.ts.map +1 -0
- package/dist/vdom/mount.test.js +91 -0
- package/dist/vdom/patch.test.d.ts +2 -0
- package/dist/vdom/patch.test.d.ts.map +1 -0
- package/dist/vdom/patch.test.js +498 -0
- package/dist/vdom/patchChildren.test.d.ts +2 -0
- package/dist/vdom/patchChildren.test.d.ts.map +1 -0
- package/dist/vdom/patchChildren.test.js +392 -0
- package/dist/vdom/primitives.test.d.ts +2 -0
- package/dist/vdom/primitives.test.d.ts.map +1 -0
- package/dist/vdom/primitives.test.js +132 -0
- package/dist/vdom/types.d.ts +8 -0
- package/dist/vdom/types.d.ts.map +1 -0
- package/dist/vdom/types.js +1 -0
- package/dist/vdom/utils.d.ts +6 -0
- package/dist/vdom/utils.d.ts.map +1 -0
- package/dist/vdom/utils.js +63 -0
- package/package.json +1 -4
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import { describe, it, expect, vi } from 'vitest';
|
|
2
|
+
import { createMutation } from './createMutation';
|
|
3
|
+
describe('createMutation', () => {
|
|
4
|
+
it('should start in idle state', () => {
|
|
5
|
+
const mutator = vi.fn(() => Promise.resolve(null));
|
|
6
|
+
const mutation = createMutation(mutator);
|
|
7
|
+
expect(mutation.isPending).toBe(false);
|
|
8
|
+
expect(mutation.params).toBeNull();
|
|
9
|
+
expect(mutation.error).toBeNull();
|
|
10
|
+
expect(mutator).not.toHaveBeenCalled();
|
|
11
|
+
});
|
|
12
|
+
it('should execute mutator when mutate is called', async () => {
|
|
13
|
+
const mutator = vi.fn((params) => Promise.resolve(params));
|
|
14
|
+
const mutation = createMutation(mutator);
|
|
15
|
+
mutation.mutate('test');
|
|
16
|
+
expect(mutation.isPending).toBe(true);
|
|
17
|
+
expect(mutation.params).toBe('test');
|
|
18
|
+
expect(mutator).toHaveBeenCalledWith('test');
|
|
19
|
+
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
20
|
+
expect(mutation.isPending).toBe(false);
|
|
21
|
+
expect(mutation.params).toBeNull();
|
|
22
|
+
});
|
|
23
|
+
it('should handle successful mutations', async () => {
|
|
24
|
+
const mutator = (params) => Promise.resolve(params);
|
|
25
|
+
const mutation = createMutation(mutator);
|
|
26
|
+
mutation.mutate({ id: 1 });
|
|
27
|
+
expect(mutation.isPending).toBe(true);
|
|
28
|
+
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
29
|
+
expect(mutation.isPending).toBe(false);
|
|
30
|
+
expect(mutation.error).toBeNull();
|
|
31
|
+
expect(mutation.params).toBeNull();
|
|
32
|
+
});
|
|
33
|
+
it('should handle mutation errors', async () => {
|
|
34
|
+
const mutator = (params) => Promise.reject(new Error('Mutation failed'));
|
|
35
|
+
const mutation = createMutation(mutator);
|
|
36
|
+
mutation.mutate('test');
|
|
37
|
+
expect(mutation.isPending).toBe(true);
|
|
38
|
+
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
39
|
+
expect(mutation.isPending).toBe(false);
|
|
40
|
+
expect(mutation.error).toContain('Mutation failed');
|
|
41
|
+
expect(mutation.params).toBeNull();
|
|
42
|
+
});
|
|
43
|
+
it('should cancel previous mutation on new mutate call', async () => {
|
|
44
|
+
let resolveFirst;
|
|
45
|
+
let resolveSecond;
|
|
46
|
+
const firstPromise = new Promise((resolve) => {
|
|
47
|
+
resolveFirst = resolve;
|
|
48
|
+
});
|
|
49
|
+
const secondPromise = new Promise((resolve) => {
|
|
50
|
+
resolveSecond = resolve;
|
|
51
|
+
});
|
|
52
|
+
const mutator = vi
|
|
53
|
+
.fn()
|
|
54
|
+
.mockReturnValueOnce(firstPromise)
|
|
55
|
+
.mockReturnValueOnce(secondPromise);
|
|
56
|
+
const mutation = createMutation(mutator);
|
|
57
|
+
mutation.mutate('first');
|
|
58
|
+
expect(mutation.params).toBe('first');
|
|
59
|
+
// Trigger second mutation before first completes
|
|
60
|
+
mutation.mutate('second');
|
|
61
|
+
expect(mutation.params).toBe('second');
|
|
62
|
+
// Resolve first (should be ignored due to cancellation)
|
|
63
|
+
resolveFirst('first');
|
|
64
|
+
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
65
|
+
expect(mutation.isPending).toBe(true); // Still pending second
|
|
66
|
+
// Resolve second
|
|
67
|
+
resolveSecond('second');
|
|
68
|
+
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
69
|
+
expect(mutation.isPending).toBe(false);
|
|
70
|
+
expect(mutation.params).toBeNull();
|
|
71
|
+
});
|
|
72
|
+
it('should handle rapid successive mutations', async () => {
|
|
73
|
+
let counter = 0;
|
|
74
|
+
const mutator = vi.fn(() => Promise.resolve(++counter));
|
|
75
|
+
const mutation = createMutation(mutator);
|
|
76
|
+
// Rapid mutations
|
|
77
|
+
mutation.mutate('1');
|
|
78
|
+
mutation.mutate('2');
|
|
79
|
+
mutation.mutate('3');
|
|
80
|
+
await new Promise((resolve) => setTimeout(resolve, 20));
|
|
81
|
+
// Only the last mutation should complete
|
|
82
|
+
expect(mutator).toHaveBeenCalledTimes(3);
|
|
83
|
+
expect(mutation.isPending).toBe(false);
|
|
84
|
+
expect(mutation.params).toBeNull();
|
|
85
|
+
});
|
|
86
|
+
it('should clear error on successful retry', async () => {
|
|
87
|
+
const mutator = vi
|
|
88
|
+
.fn()
|
|
89
|
+
.mockRejectedValueOnce(new Error('First error'))
|
|
90
|
+
.mockResolvedValueOnce('success');
|
|
91
|
+
const mutation = createMutation(mutator);
|
|
92
|
+
mutation.mutate('attempt1');
|
|
93
|
+
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
94
|
+
expect(mutation.error).toContain('First error');
|
|
95
|
+
mutation.mutate('attempt2');
|
|
96
|
+
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
97
|
+
expect(mutation.error).toBeNull();
|
|
98
|
+
expect(mutation.isPending).toBe(false);
|
|
99
|
+
});
|
|
100
|
+
it('should handle different parameter types', async () => {
|
|
101
|
+
const mutator = vi.fn((params) => Promise.resolve(params));
|
|
102
|
+
const mutation = createMutation(mutator);
|
|
103
|
+
// Object params
|
|
104
|
+
mutation.mutate({ id: 1, name: 'test' });
|
|
105
|
+
expect(mutation.params).toEqual({ id: 1, name: 'test' });
|
|
106
|
+
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
107
|
+
// Array params
|
|
108
|
+
const mutation2 = createMutation(mutator);
|
|
109
|
+
mutation2.mutate([1, 2, 3]);
|
|
110
|
+
expect(mutation2.params).toEqual([1, 2, 3]);
|
|
111
|
+
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
112
|
+
// String params
|
|
113
|
+
const mutation3 = createMutation(mutator);
|
|
114
|
+
mutation3.mutate('string');
|
|
115
|
+
expect(mutation3.params).toBe('string');
|
|
116
|
+
});
|
|
117
|
+
it('should convert errors to strings', async () => {
|
|
118
|
+
const mutator = (params) => Promise.reject('string error');
|
|
119
|
+
const mutation = createMutation(mutator);
|
|
120
|
+
mutation.mutate('test');
|
|
121
|
+
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
122
|
+
expect(typeof mutation.error).toBe('string');
|
|
123
|
+
expect(mutation.error).toBe('string error');
|
|
124
|
+
});
|
|
125
|
+
it('should handle AbortController cancellation correctly', async () => {
|
|
126
|
+
const abortedPromise = new Promise((_, reject) => {
|
|
127
|
+
const error = new Error('Aborted');
|
|
128
|
+
error.name = 'AbortError';
|
|
129
|
+
setTimeout(() => reject(error), 5);
|
|
130
|
+
});
|
|
131
|
+
const successPromise = Promise.resolve('success');
|
|
132
|
+
const mutator = vi
|
|
133
|
+
.fn()
|
|
134
|
+
.mockReturnValueOnce(abortedPromise)
|
|
135
|
+
.mockReturnValueOnce(successPromise);
|
|
136
|
+
const mutation = createMutation(mutator);
|
|
137
|
+
mutation.mutate('first');
|
|
138
|
+
// Immediately trigger second mutation to abort first
|
|
139
|
+
mutation.mutate('second');
|
|
140
|
+
await new Promise((resolve) => setTimeout(resolve, 20));
|
|
141
|
+
expect(mutation.isPending).toBe(false);
|
|
142
|
+
expect(mutation.error).toBeNull();
|
|
143
|
+
});
|
|
144
|
+
it('should track params during pending state', () => {
|
|
145
|
+
const mutator = () => new Promise((resolve) => setTimeout(() => resolve(null), 100));
|
|
146
|
+
const mutation = createMutation(mutator);
|
|
147
|
+
const params = { id: 123, action: 'update' };
|
|
148
|
+
mutation.mutate(params);
|
|
149
|
+
expect(mutation.isPending).toBe(true);
|
|
150
|
+
expect(mutation.params).toEqual(params);
|
|
151
|
+
});
|
|
152
|
+
it('should expose reactive getters', async () => {
|
|
153
|
+
const mutator = () => Promise.resolve('data');
|
|
154
|
+
const mutation = createMutation(mutator);
|
|
155
|
+
// Access getters before mutation
|
|
156
|
+
expect(mutation.isPending).toBe(false);
|
|
157
|
+
expect(mutation.params).toBeNull();
|
|
158
|
+
expect(mutation.error).toBeNull();
|
|
159
|
+
mutation.mutate('test');
|
|
160
|
+
// Access getters during mutation
|
|
161
|
+
expect(mutation.isPending).toBe(true);
|
|
162
|
+
expect(mutation.params).toBe('test');
|
|
163
|
+
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
164
|
+
// Access getters after mutation
|
|
165
|
+
expect(mutation.isPending).toBe(false);
|
|
166
|
+
expect(mutation.params).toBeNull();
|
|
167
|
+
});
|
|
168
|
+
});
|
package/dist/createQuery.d.ts
CHANGED
|
@@ -14,6 +14,29 @@ type QueryState<T> = {
|
|
|
14
14
|
export type Query<T> = QueryState<T> & {
|
|
15
15
|
fetch(force?: boolean): void;
|
|
16
16
|
};
|
|
17
|
+
/**
|
|
18
|
+
* Creates a reactive query that manages data fetching with loading and error states.
|
|
19
|
+
*
|
|
20
|
+
* @warning **Do not destructure the returned reactive object!** Destructuring breaks reactivity.
|
|
21
|
+
* Access properties directly in your render function instead.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* // ❌ Bad - destructuring loses reactivity
|
|
25
|
+
* function Component() {
|
|
26
|
+
* const query = createQuery(() => fetchUsers());
|
|
27
|
+
* const { isPending, data, error } = query; // Don't do this!
|
|
28
|
+
* return () => <div>{isPending ? "Loading..." : data.length}</div>; // Won't update!
|
|
29
|
+
* }
|
|
30
|
+
*
|
|
31
|
+
* // ✅ Good - access properties directly
|
|
32
|
+
* function Component() {
|
|
33
|
+
* const query = createQuery(() => fetchUsers());
|
|
34
|
+
* return () => <div>{query.isPending ? "Loading..." : query.data.length}</div>;
|
|
35
|
+
* }
|
|
36
|
+
*
|
|
37
|
+
* @param fetcher - Function that returns a promise with the data to fetch
|
|
38
|
+
* @returns Reactive query object with isPending, data, error properties and fetch method
|
|
39
|
+
*/
|
|
17
40
|
export declare function createQuery<T>(fetcher: () => Promise<T>): Query<T>;
|
|
18
41
|
export {};
|
|
19
42
|
//# sourceMappingURL=createQuery.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createQuery.d.ts","sourceRoot":"","sources":["../src/createQuery.ts"],"names":[],"mappings":"AAEA,KAAK,UAAU,CAAC,CAAC,IACb;IACE,SAAS,EAAE,IAAI,CAAC;IAChB,IAAI,EAAE,CAAC,GAAG,IAAI,CAAC;IACf,KAAK,EAAE,IAAI,CAAC;CACb,GACD;IACE,SAAS,EAAE,KAAK,CAAC;IACjB,IAAI,EAAE,CAAC,CAAC;IACR,KAAK,EAAE,IAAI,CAAC;CACb,GACD;IACE,SAAS,EAAE,KAAK,CAAC;IACjB,IAAI,EAAE,IAAI,CAAC;IACX,KAAK,EAAE,MAAM,CAAC;CACf,CAAC;AAEN,MAAM,MAAM,KAAK,CAAC,CAAC,IAAI,UAAU,CAAC,CAAC,CAAC,GAAG;IACrC,KAAK,CAAC,KAAK,CAAC,EAAE,OAAO,GAAG,IAAI,CAAC;CAC9B,CAAC;AAEF,wBAAgB,WAAW,CAAC,CAAC,EAAE,OAAO,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC,CA+DlE"}
|
|
1
|
+
{"version":3,"file":"createQuery.d.ts","sourceRoot":"","sources":["../src/createQuery.ts"],"names":[],"mappings":"AAEA,KAAK,UAAU,CAAC,CAAC,IACb;IACE,SAAS,EAAE,IAAI,CAAC;IAChB,IAAI,EAAE,CAAC,GAAG,IAAI,CAAC;IACf,KAAK,EAAE,IAAI,CAAC;CACb,GACD;IACE,SAAS,EAAE,KAAK,CAAC;IACjB,IAAI,EAAE,CAAC,CAAC;IACR,KAAK,EAAE,IAAI,CAAC;CACb,GACD;IACE,SAAS,EAAE,KAAK,CAAC;IACjB,IAAI,EAAE,IAAI,CAAC;IACX,KAAK,EAAE,MAAM,CAAC;CACf,CAAC;AAEN,MAAM,MAAM,KAAK,CAAC,CAAC,IAAI,UAAU,CAAC,CAAC,CAAC,GAAG;IACrC,KAAK,CAAC,KAAK,CAAC,EAAE,OAAO,GAAG,IAAI,CAAC;CAC9B,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,WAAW,CAAC,CAAC,EAAE,OAAO,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC,CA+DlE"}
|
package/dist/createQuery.js
CHANGED
|
@@ -1,4 +1,27 @@
|
|
|
1
1
|
import { createState } from "./createState";
|
|
2
|
+
/**
|
|
3
|
+
* Creates a reactive query that manages data fetching with loading and error states.
|
|
4
|
+
*
|
|
5
|
+
* @warning **Do not destructure the returned reactive object!** Destructuring breaks reactivity.
|
|
6
|
+
* Access properties directly in your render function instead.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* // ❌ Bad - destructuring loses reactivity
|
|
10
|
+
* function Component() {
|
|
11
|
+
* const query = createQuery(() => fetchUsers());
|
|
12
|
+
* const { isPending, data, error } = query; // Don't do this!
|
|
13
|
+
* return () => <div>{isPending ? "Loading..." : data.length}</div>; // Won't update!
|
|
14
|
+
* }
|
|
15
|
+
*
|
|
16
|
+
* // ✅ Good - access properties directly
|
|
17
|
+
* function Component() {
|
|
18
|
+
* const query = createQuery(() => fetchUsers());
|
|
19
|
+
* return () => <div>{query.isPending ? "Loading..." : query.data.length}</div>;
|
|
20
|
+
* }
|
|
21
|
+
*
|
|
22
|
+
* @param fetcher - Function that returns a promise with the data to fetch
|
|
23
|
+
* @returns Reactive query object with isPending, data, error properties and fetch method
|
|
24
|
+
*/
|
|
2
25
|
export function createQuery(fetcher) {
|
|
3
26
|
const state = createState({
|
|
4
27
|
isPending: true,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createQuery.test.d.ts","sourceRoot":"","sources":["../src/createQuery.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { describe, it, expect, vi } from 'vitest';
|
|
2
|
+
import { createQuery } from './createQuery';
|
|
3
|
+
describe('createQuery', () => {
|
|
4
|
+
it('should start in pending state and fetch immediately', async () => {
|
|
5
|
+
const fetcher = vi.fn(() => Promise.resolve('data'));
|
|
6
|
+
const query = createQuery(fetcher);
|
|
7
|
+
expect(query.isPending).toBe(true);
|
|
8
|
+
expect(query.data).toBeNull();
|
|
9
|
+
expect(query.error).toBeNull();
|
|
10
|
+
expect(fetcher).toHaveBeenCalledTimes(1);
|
|
11
|
+
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
12
|
+
expect(query.isPending).toBe(false);
|
|
13
|
+
expect(query.data).toBe('data');
|
|
14
|
+
});
|
|
15
|
+
it('should resolve to data state on success', async () => {
|
|
16
|
+
const fetcher = () => Promise.resolve({ id: 1, name: 'Test' });
|
|
17
|
+
const query = createQuery(fetcher);
|
|
18
|
+
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
19
|
+
expect(query.isPending).toBe(false);
|
|
20
|
+
expect(query.data).toEqual({ id: 1, name: 'Test' });
|
|
21
|
+
expect(query.error).toBeNull();
|
|
22
|
+
});
|
|
23
|
+
it('should resolve to error state on failure', async () => {
|
|
24
|
+
const fetcher = () => Promise.reject(new Error('Network error'));
|
|
25
|
+
const query = createQuery(fetcher);
|
|
26
|
+
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
27
|
+
expect(query.isPending).toBe(false);
|
|
28
|
+
expect(query.data).toBeNull();
|
|
29
|
+
expect(query.error).toContain('Network error');
|
|
30
|
+
});
|
|
31
|
+
it('should allow manual refetch', async () => {
|
|
32
|
+
const fetcher = vi.fn(() => Promise.resolve('data'));
|
|
33
|
+
const query = createQuery(fetcher);
|
|
34
|
+
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
35
|
+
expect(fetcher).toHaveBeenCalledTimes(1);
|
|
36
|
+
query.fetch();
|
|
37
|
+
expect(query.isPending).toBe(true);
|
|
38
|
+
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
39
|
+
expect(fetcher).toHaveBeenCalledTimes(2);
|
|
40
|
+
expect(query.isPending).toBe(false);
|
|
41
|
+
});
|
|
42
|
+
it('should retain old data during refetch by default', async () => {
|
|
43
|
+
const fetcher = vi
|
|
44
|
+
.fn()
|
|
45
|
+
.mockResolvedValueOnce('data1')
|
|
46
|
+
.mockResolvedValueOnce('data2');
|
|
47
|
+
const query = createQuery(fetcher);
|
|
48
|
+
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
49
|
+
expect(query.data).toBe('data1');
|
|
50
|
+
query.fetch();
|
|
51
|
+
expect(query.isPending).toBe(true);
|
|
52
|
+
expect(query.data).toBe('data1'); // Old data retained
|
|
53
|
+
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
54
|
+
expect(query.data).toBe('data2');
|
|
55
|
+
});
|
|
56
|
+
it('should clear old data when force refetch is used', async () => {
|
|
57
|
+
const fetcher = vi
|
|
58
|
+
.fn()
|
|
59
|
+
.mockResolvedValueOnce('data1')
|
|
60
|
+
.mockResolvedValueOnce('data2');
|
|
61
|
+
const query = createQuery(fetcher);
|
|
62
|
+
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
63
|
+
expect(query.data).toBe('data1');
|
|
64
|
+
query.fetch(true); // Force refresh
|
|
65
|
+
expect(query.isPending).toBe(true);
|
|
66
|
+
expect(query.data).toBeNull(); // Old data cleared
|
|
67
|
+
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
68
|
+
expect(query.data).toBe('data2');
|
|
69
|
+
});
|
|
70
|
+
it('should cancel previous request on new fetch', async () => {
|
|
71
|
+
let resolveFirst;
|
|
72
|
+
let resolveSecond;
|
|
73
|
+
const firstPromise = new Promise((resolve) => {
|
|
74
|
+
resolveFirst = resolve;
|
|
75
|
+
});
|
|
76
|
+
const secondPromise = new Promise((resolve) => {
|
|
77
|
+
resolveSecond = resolve;
|
|
78
|
+
});
|
|
79
|
+
const fetcher = vi
|
|
80
|
+
.fn()
|
|
81
|
+
.mockReturnValueOnce(firstPromise)
|
|
82
|
+
.mockReturnValueOnce(secondPromise);
|
|
83
|
+
const query = createQuery(fetcher);
|
|
84
|
+
expect(query.isPending).toBe(true);
|
|
85
|
+
// Trigger second fetch before first completes
|
|
86
|
+
query.fetch();
|
|
87
|
+
// Resolve first (should be ignored due to cancellation)
|
|
88
|
+
resolveFirst('first');
|
|
89
|
+
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
90
|
+
expect(query.data).toBeNull(); // First result ignored
|
|
91
|
+
// Resolve second
|
|
92
|
+
resolveSecond('second');
|
|
93
|
+
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
94
|
+
expect(query.data).toBe('second');
|
|
95
|
+
});
|
|
96
|
+
it('should handle rapid successive fetches', async () => {
|
|
97
|
+
let counter = 0;
|
|
98
|
+
const fetcher = vi.fn(() => Promise.resolve(`data-${++counter}`));
|
|
99
|
+
const query = createQuery(fetcher);
|
|
100
|
+
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
101
|
+
// Rapid fetches
|
|
102
|
+
query.fetch();
|
|
103
|
+
query.fetch();
|
|
104
|
+
query.fetch();
|
|
105
|
+
await new Promise((resolve) => setTimeout(resolve, 20));
|
|
106
|
+
// Only the last fetch should matter
|
|
107
|
+
expect(fetcher).toHaveBeenCalledTimes(4); // Initial + 3 fetches
|
|
108
|
+
expect(query.data).toBe('data-4');
|
|
109
|
+
});
|
|
110
|
+
it('should cancel on error and allow refetch', async () => {
|
|
111
|
+
const fetcher = vi
|
|
112
|
+
.fn()
|
|
113
|
+
.mockRejectedValueOnce(new Error('First error'))
|
|
114
|
+
.mockResolvedValueOnce('success');
|
|
115
|
+
const query = createQuery(fetcher);
|
|
116
|
+
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
117
|
+
expect(query.error).toContain('First error');
|
|
118
|
+
expect(query.data).toBeNull();
|
|
119
|
+
query.fetch();
|
|
120
|
+
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
121
|
+
expect(query.error).toBeNull();
|
|
122
|
+
expect(query.data).toBe('success');
|
|
123
|
+
});
|
|
124
|
+
it('should handle AbortController cancellation correctly', async () => {
|
|
125
|
+
const abortedPromise = new Promise((_, reject) => {
|
|
126
|
+
const error = new Error('Aborted');
|
|
127
|
+
error.name = 'AbortError';
|
|
128
|
+
setTimeout(() => reject(error), 5);
|
|
129
|
+
});
|
|
130
|
+
const successPromise = Promise.resolve('success');
|
|
131
|
+
const fetcher = vi
|
|
132
|
+
.fn()
|
|
133
|
+
.mockReturnValueOnce(abortedPromise)
|
|
134
|
+
.mockReturnValueOnce(successPromise);
|
|
135
|
+
const query = createQuery(fetcher);
|
|
136
|
+
// Immediately trigger second fetch to abort first
|
|
137
|
+
query.fetch();
|
|
138
|
+
await new Promise((resolve) => setTimeout(resolve, 20));
|
|
139
|
+
expect(query.data).toBe('success');
|
|
140
|
+
expect(query.error).toBeNull();
|
|
141
|
+
});
|
|
142
|
+
it('should expose reactive getters', async () => {
|
|
143
|
+
const fetcher = () => Promise.resolve('data');
|
|
144
|
+
const query = createQuery(fetcher);
|
|
145
|
+
// Access getters
|
|
146
|
+
const pending1 = query.isPending;
|
|
147
|
+
const data1 = query.data;
|
|
148
|
+
const error1 = query.error;
|
|
149
|
+
expect(pending1).toBe(true);
|
|
150
|
+
expect(data1).toBeNull();
|
|
151
|
+
expect(error1).toBeNull();
|
|
152
|
+
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
153
|
+
expect(query.isPending).toBe(false);
|
|
154
|
+
expect(query.data).toBe('data');
|
|
155
|
+
});
|
|
156
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createRef.test.d.ts","sourceRoot":"","sources":["../src/createRef.test.tsx"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "./jsx-runtime";
|
|
2
|
+
import { describe, it, expect } from "vitest";
|
|
3
|
+
import { createRef } from "./createRef";
|
|
4
|
+
import { render } from "./vdom";
|
|
5
|
+
describe("createRef", () => {
|
|
6
|
+
it("should create a ref with null initial value", () => {
|
|
7
|
+
const ref = createRef();
|
|
8
|
+
expect(ref.current).toBeNull();
|
|
9
|
+
});
|
|
10
|
+
it("should be callable as a function", () => {
|
|
11
|
+
const ref = createRef();
|
|
12
|
+
expect(typeof ref).toBe("function");
|
|
13
|
+
});
|
|
14
|
+
it("should update current when called with a node", () => {
|
|
15
|
+
const ref = createRef();
|
|
16
|
+
const element = document.createElement("div");
|
|
17
|
+
ref(element);
|
|
18
|
+
expect(ref.current).toBe(element);
|
|
19
|
+
});
|
|
20
|
+
it("should update current when called with null", () => {
|
|
21
|
+
const ref = createRef();
|
|
22
|
+
const element = document.createElement("div");
|
|
23
|
+
ref(element);
|
|
24
|
+
expect(ref.current).toBe(element);
|
|
25
|
+
ref(null);
|
|
26
|
+
expect(ref.current).toBeNull();
|
|
27
|
+
});
|
|
28
|
+
it("should work with JSX ref attribute", async () => {
|
|
29
|
+
const ref = createRef();
|
|
30
|
+
function TestComponent() {
|
|
31
|
+
return () => _jsx("div", { ref: ref, children: "Hello" });
|
|
32
|
+
}
|
|
33
|
+
const container = document.createElement("div");
|
|
34
|
+
document.body.appendChild(container);
|
|
35
|
+
const vnode = render(_jsx(TestComponent, {}), container);
|
|
36
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
37
|
+
expect(ref.current).not.toBeNull();
|
|
38
|
+
expect(ref.current?.tagName).toBe("DIV");
|
|
39
|
+
expect(ref.current?.textContent).toBe("Hello");
|
|
40
|
+
document.body.removeChild(vnode.elm);
|
|
41
|
+
});
|
|
42
|
+
it("should handle multiple ref updates", () => {
|
|
43
|
+
const ref = createRef();
|
|
44
|
+
const div = document.createElement("div");
|
|
45
|
+
const span = document.createElement("span");
|
|
46
|
+
ref(div);
|
|
47
|
+
expect(ref.current).toBe(div);
|
|
48
|
+
ref(span);
|
|
49
|
+
expect(ref.current).toBe(span);
|
|
50
|
+
ref(null);
|
|
51
|
+
expect(ref.current).toBeNull();
|
|
52
|
+
});
|
|
53
|
+
it("should work with different element types", async () => {
|
|
54
|
+
const inputRef = createRef();
|
|
55
|
+
const buttonRef = createRef();
|
|
56
|
+
function TestComponent() {
|
|
57
|
+
return () => (_jsxs("div", { children: [_jsx("input", { ref: inputRef, type: "text" }), _jsx("button", { ref: buttonRef, children: "Click" })] }));
|
|
58
|
+
}
|
|
59
|
+
const container = document.createElement("div");
|
|
60
|
+
document.body.appendChild(container);
|
|
61
|
+
const vnode = render(_jsx(TestComponent, {}), container);
|
|
62
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
63
|
+
expect(inputRef.current?.tagName).toBe("INPUT");
|
|
64
|
+
expect(buttonRef.current?.tagName).toBe("BUTTON");
|
|
65
|
+
document.body.removeChild(vnode.elm);
|
|
66
|
+
});
|
|
67
|
+
it("should allow accessing DOM properties", async () => {
|
|
68
|
+
const ref = createRef();
|
|
69
|
+
function TestComponent() {
|
|
70
|
+
return () => _jsx("input", { ref: ref, type: "text", value: "test" });
|
|
71
|
+
}
|
|
72
|
+
const container = document.createElement("div");
|
|
73
|
+
document.body.appendChild(container);
|
|
74
|
+
const vnode = render(_jsx(TestComponent, {}), container);
|
|
75
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
76
|
+
expect(ref.current?.value).toBe("test");
|
|
77
|
+
expect(ref.current?.type).toBe("text");
|
|
78
|
+
document.body.removeChild(vnode.elm);
|
|
79
|
+
});
|
|
80
|
+
});
|
package/dist/createState.d.ts
CHANGED
|
@@ -1,2 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a reactive state object that tracks property access and notifies observers on changes.
|
|
3
|
+
*
|
|
4
|
+
* @warning **Do not destructure the returned reactive object!** Destructuring breaks reactivity
|
|
5
|
+
* because it extracts plain values instead of maintaining proxy access. This is the same rule
|
|
6
|
+
* as Solid.js signals.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* // ❌ Bad - destructuring loses reactivity
|
|
10
|
+
* function Component(props) {
|
|
11
|
+
* const state = createState({ count: 0, name: "foo" });
|
|
12
|
+
* const { count, name } = state; // Don't do this!
|
|
13
|
+
* return () => <div>{count} {name}</div>; // Won't update!
|
|
14
|
+
* }
|
|
15
|
+
*
|
|
16
|
+
* // ✅ Good - access properties directly in render
|
|
17
|
+
* function Component(props) {
|
|
18
|
+
* const state = createState({ count: 0, name: "foo" });
|
|
19
|
+
* return () => <div>{state.count} {state.name}</div>; // Reactive!
|
|
20
|
+
* }
|
|
21
|
+
*
|
|
22
|
+
* @param state - The initial state object to make reactive
|
|
23
|
+
* @returns A reactive proxy of the state object
|
|
24
|
+
*/
|
|
1
25
|
export declare function createState<T extends object>(state: T): T;
|
|
2
26
|
//# sourceMappingURL=createState.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createState.d.ts","sourceRoot":"","sources":["../src/createState.ts"],"names":[],"mappings":"AAEA,wBAAgB,WAAW,CAAC,CAAC,SAAS,MAAM,EAAE,KAAK,EAAE,CAAC,GAAG,CAAC,CAEzD"}
|
|
1
|
+
{"version":3,"file":"createState.d.ts","sourceRoot":"","sources":["../src/createState.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAgB,WAAW,CAAC,CAAC,SAAS,MAAM,EAAE,KAAK,EAAE,CAAC,GAAG,CAAC,CAEzD"}
|
package/dist/createState.js
CHANGED
|
@@ -1,4 +1,28 @@
|
|
|
1
1
|
import { getCurrentObserver, Signal } from "./observation";
|
|
2
|
+
/**
|
|
3
|
+
* Creates a reactive state object that tracks property access and notifies observers on changes.
|
|
4
|
+
*
|
|
5
|
+
* @warning **Do not destructure the returned reactive object!** Destructuring breaks reactivity
|
|
6
|
+
* because it extracts plain values instead of maintaining proxy access. This is the same rule
|
|
7
|
+
* as Solid.js signals.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* // ❌ Bad - destructuring loses reactivity
|
|
11
|
+
* function Component(props) {
|
|
12
|
+
* const state = createState({ count: 0, name: "foo" });
|
|
13
|
+
* const { count, name } = state; // Don't do this!
|
|
14
|
+
* return () => <div>{count} {name}</div>; // Won't update!
|
|
15
|
+
* }
|
|
16
|
+
*
|
|
17
|
+
* // ✅ Good - access properties directly in render
|
|
18
|
+
* function Component(props) {
|
|
19
|
+
* const state = createState({ count: 0, name: "foo" });
|
|
20
|
+
* return () => <div>{state.count} {state.name}</div>; // Reactive!
|
|
21
|
+
* }
|
|
22
|
+
*
|
|
23
|
+
* @param state - The initial state object to make reactive
|
|
24
|
+
* @returns A reactive proxy of the state object
|
|
25
|
+
*/
|
|
2
26
|
export function createState(state) {
|
|
3
27
|
return getProxy(state);
|
|
4
28
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createState.test.d.ts","sourceRoot":"","sources":["../src/createState.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import { createState } from './createState';
|
|
3
|
+
import { Observer } from './observation';
|
|
4
|
+
describe('createState', () => {
|
|
5
|
+
it('should create a reactive proxy from an object', () => {
|
|
6
|
+
const state = createState({ count: 0 });
|
|
7
|
+
expect(state.count).toBe(0);
|
|
8
|
+
});
|
|
9
|
+
it('should allow mutations', () => {
|
|
10
|
+
const state = createState({ count: 0 });
|
|
11
|
+
state.count = 5;
|
|
12
|
+
expect(state.count).toBe(5);
|
|
13
|
+
});
|
|
14
|
+
it('should return the same proxy for the same object', () => {
|
|
15
|
+
const obj = { count: 0 };
|
|
16
|
+
const proxy1 = createState(obj);
|
|
17
|
+
const proxy2 = createState(obj);
|
|
18
|
+
expect(proxy1).toBe(proxy2);
|
|
19
|
+
});
|
|
20
|
+
it('should create nested proxies for nested objects', () => {
|
|
21
|
+
const state = createState({ user: { name: 'Alice', age: 30 } });
|
|
22
|
+
state.user.name = 'Bob';
|
|
23
|
+
expect(state.user.name).toBe('Bob');
|
|
24
|
+
});
|
|
25
|
+
it('should handle arrays reactively', () => {
|
|
26
|
+
const state = createState({ items: [1, 2, 3] });
|
|
27
|
+
state.items.push(4);
|
|
28
|
+
expect(state.items).toEqual([1, 2, 3, 4]);
|
|
29
|
+
});
|
|
30
|
+
it('should track property access in observers', () => {
|
|
31
|
+
const state = createState({ count: 0 });
|
|
32
|
+
let renderCount = 0;
|
|
33
|
+
const observer = new Observer(() => {
|
|
34
|
+
renderCount++;
|
|
35
|
+
});
|
|
36
|
+
const dispose = observer.observe();
|
|
37
|
+
state.count; // Access property to track it
|
|
38
|
+
dispose();
|
|
39
|
+
expect(renderCount).toBe(0);
|
|
40
|
+
// Mutate after observation setup
|
|
41
|
+
const dispose2 = observer.observe();
|
|
42
|
+
const value = state.count; // Track
|
|
43
|
+
dispose2(); // Stop observing, subscriptions are now active
|
|
44
|
+
state.count = 1;
|
|
45
|
+
// Wait for microtask
|
|
46
|
+
return new Promise((resolve) => {
|
|
47
|
+
queueMicrotask(() => {
|
|
48
|
+
expect(renderCount).toBeGreaterThan(0);
|
|
49
|
+
resolve(undefined);
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
it('should handle property deletion', () => {
|
|
54
|
+
const state = createState({ count: 0, temp: 'value' });
|
|
55
|
+
delete state.temp;
|
|
56
|
+
expect(state.temp).toBeUndefined();
|
|
57
|
+
expect('temp' in state).toBe(false);
|
|
58
|
+
});
|
|
59
|
+
it('should not create proxies for functions', () => {
|
|
60
|
+
const fn = () => 'hello';
|
|
61
|
+
const state = createState({ method: fn });
|
|
62
|
+
expect(state.method).toBe(fn);
|
|
63
|
+
expect(state.method()).toBe('hello');
|
|
64
|
+
});
|
|
65
|
+
it('should handle symbol properties', () => {
|
|
66
|
+
const sym = Symbol('test');
|
|
67
|
+
const state = createState({ [sym]: 'value' });
|
|
68
|
+
expect(state[sym]).toBe('value');
|
|
69
|
+
});
|
|
70
|
+
it('should notify observers only on actual changes', () => {
|
|
71
|
+
const state = createState({ count: 0 });
|
|
72
|
+
let notifyCount = 0;
|
|
73
|
+
const observer = new Observer(() => {
|
|
74
|
+
notifyCount++;
|
|
75
|
+
});
|
|
76
|
+
const dispose = observer.observe();
|
|
77
|
+
state.count; // Track
|
|
78
|
+
dispose();
|
|
79
|
+
state.count = 0; // Same value - should still notify per current implementation
|
|
80
|
+
state.count = 0;
|
|
81
|
+
return new Promise((resolve) => {
|
|
82
|
+
queueMicrotask(() => {
|
|
83
|
+
// The implementation notifies even for same value, except for optimization cases
|
|
84
|
+
observer.dispose();
|
|
85
|
+
resolve(undefined);
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
it('should handle deeply nested objects', () => {
|
|
90
|
+
const state = createState({
|
|
91
|
+
level1: {
|
|
92
|
+
level2: {
|
|
93
|
+
level3: {
|
|
94
|
+
value: 'deep',
|
|
95
|
+
},
|
|
96
|
+
},
|
|
97
|
+
},
|
|
98
|
+
});
|
|
99
|
+
state.level1.level2.level3.value = 'modified';
|
|
100
|
+
expect(state.level1.level2.level3.value).toBe('modified');
|
|
101
|
+
});
|
|
102
|
+
it('should handle array mutations correctly', () => {
|
|
103
|
+
const state = createState({ items: [1, 2, 3] });
|
|
104
|
+
state.items.pop();
|
|
105
|
+
expect(state.items).toEqual([1, 2]);
|
|
106
|
+
state.items.unshift(0);
|
|
107
|
+
expect(state.items).toEqual([0, 1, 2]);
|
|
108
|
+
state.items.splice(1, 1, 99);
|
|
109
|
+
expect(state.items).toEqual([0, 99, 2]);
|
|
110
|
+
});
|
|
111
|
+
});
|