zero-query 0.6.3 → 0.8.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +39 -29
- package/cli/commands/build.js +113 -4
- package/cli/commands/bundle.js +392 -29
- package/cli/commands/create.js +1 -1
- package/cli/commands/dev/devtools/index.js +56 -0
- package/cli/commands/dev/devtools/js/components.js +49 -0
- package/cli/commands/dev/devtools/js/core.js +409 -0
- package/cli/commands/dev/devtools/js/elements.js +413 -0
- package/cli/commands/dev/devtools/js/network.js +166 -0
- package/cli/commands/dev/devtools/js/performance.js +73 -0
- package/cli/commands/dev/devtools/js/router.js +105 -0
- package/cli/commands/dev/devtools/js/source.js +132 -0
- package/cli/commands/dev/devtools/js/stats.js +35 -0
- package/cli/commands/dev/devtools/js/tabs.js +79 -0
- package/cli/commands/dev/devtools/panel.html +95 -0
- package/cli/commands/dev/devtools/styles.css +244 -0
- package/cli/commands/dev/index.js +29 -4
- package/cli/commands/dev/logger.js +6 -1
- package/cli/commands/dev/overlay.js +428 -2
- package/cli/commands/dev/server.js +42 -5
- package/cli/commands/dev/watcher.js +59 -1
- package/cli/help.js +8 -5
- package/cli/scaffold/{scripts → app}/app.js +16 -23
- package/cli/scaffold/{scripts → app}/components/about.js +4 -4
- package/cli/scaffold/{scripts → app}/components/api-demo.js +1 -1
- package/cli/scaffold/{scripts → app}/components/contacts/contacts.css +0 -7
- package/cli/scaffold/{scripts → app}/components/contacts/contacts.html +3 -3
- package/cli/scaffold/app/components/home.js +137 -0
- package/cli/scaffold/{scripts → app}/routes.js +1 -1
- package/cli/scaffold/{scripts → app}/store.js +6 -6
- package/cli/scaffold/assets/.gitkeep +0 -0
- package/cli/scaffold/{styles/styles.css → global.css} +4 -2
- package/cli/scaffold/index.html +12 -11
- package/cli/utils.js +111 -6
- package/dist/zquery.dist.zip +0 -0
- package/dist/zquery.js +1122 -158
- package/dist/zquery.min.js +3 -16
- package/index.d.ts +129 -1290
- package/index.js +15 -10
- package/package.json +7 -6
- package/src/component.js +172 -49
- package/src/core.js +359 -18
- package/src/diff.js +256 -58
- package/src/expression.js +33 -3
- package/src/reactive.js +37 -5
- package/src/router.js +243 -7
- package/tests/component.test.js +886 -0
- package/tests/core.test.js +977 -0
- package/tests/diff.test.js +525 -0
- package/tests/errors.test.js +162 -0
- package/tests/expression.test.js +482 -0
- package/tests/http.test.js +289 -0
- package/tests/reactive.test.js +339 -0
- package/tests/router.test.js +649 -0
- package/tests/store.test.js +379 -0
- package/tests/utils.test.js +512 -0
- package/types/collection.d.ts +383 -0
- package/types/component.d.ts +217 -0
- package/types/errors.d.ts +103 -0
- package/types/http.d.ts +81 -0
- package/types/misc.d.ts +179 -0
- package/types/reactive.d.ts +76 -0
- package/types/router.d.ts +161 -0
- package/types/ssr.d.ts +49 -0
- package/types/store.d.ts +107 -0
- package/types/utils.d.ts +142 -0
- package/cli/commands/dev.old.js +0 -520
- package/cli/scaffold/scripts/components/home.js +0 -137
- /package/cli/scaffold/{scripts → app}/components/contacts/contacts.js +0 -0
- /package/cli/scaffold/{scripts → app}/components/counter.js +0 -0
- /package/cli/scaffold/{scripts → app}/components/not-found.js +0 -0
- /package/cli/scaffold/{scripts → app}/components/todos.js +0 -0
|
@@ -0,0 +1,379 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
2
|
+
import { createStore, getStore } from '../src/store.js';
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
// ---------------------------------------------------------------------------
|
|
6
|
+
// Store creation
|
|
7
|
+
// ---------------------------------------------------------------------------
|
|
8
|
+
|
|
9
|
+
describe('Store — creation', () => {
|
|
10
|
+
it('creates a store with initial state', () => {
|
|
11
|
+
const store = createStore('test-create', {
|
|
12
|
+
state: { count: 0, name: 'Tony' },
|
|
13
|
+
});
|
|
14
|
+
expect(store.state.count).toBe(0);
|
|
15
|
+
expect(store.state.name).toBe('Tony');
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it('supports state as a factory function', () => {
|
|
19
|
+
const store = createStore('test-fn', {
|
|
20
|
+
state: () => ({ items: [] }),
|
|
21
|
+
});
|
|
22
|
+
expect(store.state.items).toEqual([]);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it('getStore retrieves by name', () => {
|
|
26
|
+
const store = createStore('named-store', { state: { x: 1 } });
|
|
27
|
+
expect(getStore('named-store')).toBe(store);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it('getStore returns null for unknown stores', () => {
|
|
31
|
+
expect(getStore('nonexistent')).toBeNull();
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it('defaults to "default" when no name is provided', () => {
|
|
35
|
+
const store = createStore({ state: { val: 42 } });
|
|
36
|
+
expect(getStore('default')).toBe(store);
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
// ---------------------------------------------------------------------------
|
|
42
|
+
// Dispatch & actions
|
|
43
|
+
// ---------------------------------------------------------------------------
|
|
44
|
+
|
|
45
|
+
describe('Store — dispatch', () => {
|
|
46
|
+
it('dispatches a named action', () => {
|
|
47
|
+
const store = createStore('dispatch-1', {
|
|
48
|
+
state: { count: 0 },
|
|
49
|
+
actions: {
|
|
50
|
+
increment(state) { state.count++; },
|
|
51
|
+
},
|
|
52
|
+
});
|
|
53
|
+
store.dispatch('increment');
|
|
54
|
+
expect(store.state.count).toBe(1);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('passes payload to action', () => {
|
|
58
|
+
const store = createStore('dispatch-2', {
|
|
59
|
+
state: { count: 0 },
|
|
60
|
+
actions: {
|
|
61
|
+
add(state, amount) { state.count += amount; },
|
|
62
|
+
},
|
|
63
|
+
});
|
|
64
|
+
store.dispatch('add', 5);
|
|
65
|
+
expect(store.state.count).toBe(5);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it('reports error for unknown actions', () => {
|
|
69
|
+
const store = createStore('dispatch-unknown', {
|
|
70
|
+
state: {},
|
|
71
|
+
actions: {},
|
|
72
|
+
});
|
|
73
|
+
// Should not throw if action doesn't exist
|
|
74
|
+
expect(() => store.dispatch('nonexistent')).not.toThrow();
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it('records action in history', () => {
|
|
78
|
+
const store = createStore('dispatch-hist', {
|
|
79
|
+
state: { x: 0 },
|
|
80
|
+
actions: { inc(state) { state.x++; } },
|
|
81
|
+
});
|
|
82
|
+
store.dispatch('inc');
|
|
83
|
+
store.dispatch('inc');
|
|
84
|
+
expect(store.history.length).toBe(2);
|
|
85
|
+
expect(store.history[0].action).toBe('inc');
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it('does not crash when action throws', () => {
|
|
89
|
+
const store = createStore('dispatch-throw', {
|
|
90
|
+
state: {},
|
|
91
|
+
actions: {
|
|
92
|
+
bad() { throw new Error('action error'); },
|
|
93
|
+
},
|
|
94
|
+
});
|
|
95
|
+
expect(() => store.dispatch('bad')).not.toThrow();
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
// ---------------------------------------------------------------------------
|
|
101
|
+
// Subscriptions
|
|
102
|
+
// ---------------------------------------------------------------------------
|
|
103
|
+
|
|
104
|
+
describe('Store — subscribe', () => {
|
|
105
|
+
it('notifies key-specific subscribers', () => {
|
|
106
|
+
const store = createStore('sub-1', {
|
|
107
|
+
state: { count: 0 },
|
|
108
|
+
actions: { inc(state) { state.count++; } },
|
|
109
|
+
});
|
|
110
|
+
const fn = vi.fn();
|
|
111
|
+
store.subscribe('count', fn);
|
|
112
|
+
store.dispatch('inc');
|
|
113
|
+
expect(fn).toHaveBeenCalledWith(1, 0, 'count');
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it('wildcard subscriber gets all changes', () => {
|
|
117
|
+
const store = createStore('sub-2', {
|
|
118
|
+
state: { a: 0, b: 0 },
|
|
119
|
+
actions: {
|
|
120
|
+
setA(state, v) { state.a = v; },
|
|
121
|
+
setB(state, v) { state.b = v; },
|
|
122
|
+
},
|
|
123
|
+
});
|
|
124
|
+
const fn = vi.fn();
|
|
125
|
+
store.subscribe(fn);
|
|
126
|
+
store.dispatch('setA', 1);
|
|
127
|
+
store.dispatch('setB', 2);
|
|
128
|
+
expect(fn).toHaveBeenCalledTimes(2);
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
it('unsubscribe stops notifications', () => {
|
|
132
|
+
const store = createStore('sub-3', {
|
|
133
|
+
state: { x: 0 },
|
|
134
|
+
actions: { inc(state) { state.x++; } },
|
|
135
|
+
});
|
|
136
|
+
const fn = vi.fn();
|
|
137
|
+
const unsub = store.subscribe('x', fn);
|
|
138
|
+
store.dispatch('inc');
|
|
139
|
+
expect(fn).toHaveBeenCalledOnce();
|
|
140
|
+
unsub();
|
|
141
|
+
store.dispatch('inc');
|
|
142
|
+
expect(fn).toHaveBeenCalledOnce();
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
it('does not crash when subscriber throws', () => {
|
|
146
|
+
const store = createStore('sub-throw', {
|
|
147
|
+
state: { x: 0 },
|
|
148
|
+
actions: { inc(state) { state.x++; } },
|
|
149
|
+
});
|
|
150
|
+
store.subscribe('x', () => { throw new Error('subscriber error'); });
|
|
151
|
+
expect(() => store.dispatch('inc')).not.toThrow();
|
|
152
|
+
});
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
// ---------------------------------------------------------------------------
|
|
157
|
+
// Getters
|
|
158
|
+
// ---------------------------------------------------------------------------
|
|
159
|
+
|
|
160
|
+
describe('Store — getters', () => {
|
|
161
|
+
it('computes values from state', () => {
|
|
162
|
+
const store = createStore('getters-1', {
|
|
163
|
+
state: { count: 5 },
|
|
164
|
+
getters: {
|
|
165
|
+
doubled: (state) => state.count * 2,
|
|
166
|
+
},
|
|
167
|
+
});
|
|
168
|
+
expect(store.getters.doubled).toBe(10);
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
it('updates when state changes', () => {
|
|
172
|
+
const store = createStore('getters-2', {
|
|
173
|
+
state: { count: 1 },
|
|
174
|
+
actions: { inc(state) { state.count++; } },
|
|
175
|
+
getters: { doubled: (state) => state.count * 2 },
|
|
176
|
+
});
|
|
177
|
+
store.dispatch('inc');
|
|
178
|
+
expect(store.getters.doubled).toBe(4);
|
|
179
|
+
});
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
// ---------------------------------------------------------------------------
|
|
184
|
+
// Middleware
|
|
185
|
+
// ---------------------------------------------------------------------------
|
|
186
|
+
|
|
187
|
+
describe('Store — middleware', () => {
|
|
188
|
+
it('calls middleware before action', () => {
|
|
189
|
+
const log = vi.fn();
|
|
190
|
+
const store = createStore('mw-1', {
|
|
191
|
+
state: { x: 0 },
|
|
192
|
+
actions: { inc(state) { state.x++; } },
|
|
193
|
+
});
|
|
194
|
+
store.use((name, args, state) => { log(name); });
|
|
195
|
+
store.dispatch('inc');
|
|
196
|
+
expect(log).toHaveBeenCalledWith('inc');
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
it('blocks action when middleware returns false', () => {
|
|
200
|
+
const store = createStore('mw-block', {
|
|
201
|
+
state: { x: 0 },
|
|
202
|
+
actions: { inc(state) { state.x++; } },
|
|
203
|
+
});
|
|
204
|
+
store.use(() => false);
|
|
205
|
+
store.dispatch('inc');
|
|
206
|
+
expect(store.state.x).toBe(0);
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
it('does not crash when middleware throws', () => {
|
|
210
|
+
const store = createStore('mw-throw', {
|
|
211
|
+
state: { x: 0 },
|
|
212
|
+
actions: { inc(state) { state.x++; } },
|
|
213
|
+
});
|
|
214
|
+
store.use(() => { throw new Error('middleware error'); });
|
|
215
|
+
expect(() => store.dispatch('inc')).not.toThrow();
|
|
216
|
+
// Action should NOT have run (middleware threw → dispatch returns)
|
|
217
|
+
expect(store.state.x).toBe(0);
|
|
218
|
+
});
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
// ---------------------------------------------------------------------------
|
|
223
|
+
// Snapshot & replaceState
|
|
224
|
+
// ---------------------------------------------------------------------------
|
|
225
|
+
|
|
226
|
+
describe('Store — snapshot & replaceState', () => {
|
|
227
|
+
it('snapshot returns plain object copy', () => {
|
|
228
|
+
const store = createStore('snap-1', { state: { a: 1, b: { c: 2 } } });
|
|
229
|
+
const snap = store.snapshot();
|
|
230
|
+
expect(snap).toEqual({ a: 1, b: { c: 2 } });
|
|
231
|
+
snap.a = 99;
|
|
232
|
+
expect(store.state.a).toBe(1); // original unchanged
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
it('replaceState replaces entire state', () => {
|
|
236
|
+
const store = createStore('replace-1', { state: { x: 1, y: 2 } });
|
|
237
|
+
store.replaceState({ x: 10, z: 30 });
|
|
238
|
+
expect(store.state.x).toBe(10);
|
|
239
|
+
expect(store.state.z).toBe(30);
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
it('reset replaces state and clears history', () => {
|
|
243
|
+
const store = createStore('reset-1', {
|
|
244
|
+
state: { count: 0 },
|
|
245
|
+
actions: { inc(state) { state.count++; } },
|
|
246
|
+
});
|
|
247
|
+
store.dispatch('inc');
|
|
248
|
+
store.dispatch('inc');
|
|
249
|
+
store.reset({ count: 0 });
|
|
250
|
+
expect(store.state.count).toBe(0);
|
|
251
|
+
expect(store.history.length).toBe(0);
|
|
252
|
+
});
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
// ---------------------------------------------------------------------------
|
|
257
|
+
// Multiple middleware
|
|
258
|
+
// ---------------------------------------------------------------------------
|
|
259
|
+
|
|
260
|
+
describe('Store — multiple middleware', () => {
|
|
261
|
+
it('runs middleware in order', () => {
|
|
262
|
+
const order = [];
|
|
263
|
+
const store = createStore('mw-multi', {
|
|
264
|
+
state: { x: 0 },
|
|
265
|
+
actions: { inc(state) { state.x++; } },
|
|
266
|
+
});
|
|
267
|
+
store.use(() => { order.push('a'); });
|
|
268
|
+
store.use(() => { order.push('b'); });
|
|
269
|
+
store.dispatch('inc');
|
|
270
|
+
expect(order).toEqual(['a', 'b']);
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
it('second middleware can block even if first passes', () => {
|
|
274
|
+
const store = createStore('mw-multi-block', {
|
|
275
|
+
state: { x: 0 },
|
|
276
|
+
actions: { inc(state) { state.x++; } },
|
|
277
|
+
});
|
|
278
|
+
store.use(() => true);
|
|
279
|
+
store.use(() => false);
|
|
280
|
+
store.dispatch('inc');
|
|
281
|
+
expect(store.state.x).toBe(0);
|
|
282
|
+
});
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
|
|
286
|
+
// ---------------------------------------------------------------------------
|
|
287
|
+
// Async actions
|
|
288
|
+
// ---------------------------------------------------------------------------
|
|
289
|
+
|
|
290
|
+
describe('Store — async actions', () => {
|
|
291
|
+
it('supports async action returning promise', async () => {
|
|
292
|
+
const store = createStore('async-1', {
|
|
293
|
+
state: { data: null },
|
|
294
|
+
actions: {
|
|
295
|
+
async fetchData(state) {
|
|
296
|
+
state.data = await Promise.resolve('loaded');
|
|
297
|
+
},
|
|
298
|
+
},
|
|
299
|
+
});
|
|
300
|
+
await store.dispatch('fetchData');
|
|
301
|
+
expect(store.state.data).toBe('loaded');
|
|
302
|
+
});
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
|
|
306
|
+
// ---------------------------------------------------------------------------
|
|
307
|
+
// Subscriber deduplication
|
|
308
|
+
// ---------------------------------------------------------------------------
|
|
309
|
+
|
|
310
|
+
describe('Store — subscriber edge cases', () => {
|
|
311
|
+
it('same function subscribed twice fires twice', () => {
|
|
312
|
+
const store = createStore('sub-dedup', {
|
|
313
|
+
state: { x: 0 },
|
|
314
|
+
actions: { inc(state) { state.x++; } },
|
|
315
|
+
});
|
|
316
|
+
const fn = vi.fn();
|
|
317
|
+
store.subscribe('x', fn);
|
|
318
|
+
store.subscribe('x', fn); // Set deduplicates
|
|
319
|
+
store.dispatch('inc');
|
|
320
|
+
expect(fn).toHaveBeenCalledOnce(); // Set prevents duplicates
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
it('wildcard and key subscriber both fire', () => {
|
|
324
|
+
const store = createStore('sub-both', {
|
|
325
|
+
state: { x: 0 },
|
|
326
|
+
actions: { inc(state) { state.x++; } },
|
|
327
|
+
});
|
|
328
|
+
const keyFn = vi.fn();
|
|
329
|
+
const wildFn = vi.fn();
|
|
330
|
+
store.subscribe('x', keyFn);
|
|
331
|
+
store.subscribe(wildFn);
|
|
332
|
+
store.dispatch('inc');
|
|
333
|
+
expect(keyFn).toHaveBeenCalledOnce();
|
|
334
|
+
expect(wildFn).toHaveBeenCalledOnce();
|
|
335
|
+
});
|
|
336
|
+
});
|
|
337
|
+
|
|
338
|
+
|
|
339
|
+
// ---------------------------------------------------------------------------
|
|
340
|
+
// Action return value
|
|
341
|
+
// ---------------------------------------------------------------------------
|
|
342
|
+
|
|
343
|
+
describe('Store — action return value', () => {
|
|
344
|
+
it('dispatch returns action result', () => {
|
|
345
|
+
const store = createStore('ret-1', {
|
|
346
|
+
state: { x: 0 },
|
|
347
|
+
actions: { compute(state) { return state.x + 10; } },
|
|
348
|
+
});
|
|
349
|
+
expect(store.dispatch('compute')).toBe(10);
|
|
350
|
+
});
|
|
351
|
+
});
|
|
352
|
+
|
|
353
|
+
|
|
354
|
+
// ---------------------------------------------------------------------------
|
|
355
|
+
// Getters with multiple state keys
|
|
356
|
+
// ---------------------------------------------------------------------------
|
|
357
|
+
|
|
358
|
+
describe('Store — complex getters', () => {
|
|
359
|
+
it('getter uses multiple state keys', () => {
|
|
360
|
+
const store = createStore('getter-multi', {
|
|
361
|
+
state: { firstName: 'Tony', lastName: 'W' },
|
|
362
|
+
getters: {
|
|
363
|
+
fullName: (state) => `${state.firstName} ${state.lastName}`,
|
|
364
|
+
},
|
|
365
|
+
});
|
|
366
|
+
expect(store.getters.fullName).toBe('Tony W');
|
|
367
|
+
});
|
|
368
|
+
|
|
369
|
+
it('getter recalculates after state change', () => {
|
|
370
|
+
const store = createStore('getter-recalc', {
|
|
371
|
+
state: { count: 2 },
|
|
372
|
+
actions: { set(state, v) { state.count = v; } },
|
|
373
|
+
getters: { doubled: (s) => s.count * 2 },
|
|
374
|
+
});
|
|
375
|
+
expect(store.getters.doubled).toBe(4);
|
|
376
|
+
store.dispatch('set', 10);
|
|
377
|
+
expect(store.getters.doubled).toBe(20);
|
|
378
|
+
});
|
|
379
|
+
});
|