zero-query 1.1.1 → 1.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/LICENSE +21 -21
- package/README.md +2 -0
- package/cli/args.js +33 -33
- package/cli/commands/build-api.js +443 -442
- package/cli/commands/build.js +254 -247
- package/cli/commands/bundle.js +1228 -1224
- package/cli/commands/create.js +137 -121
- package/cli/commands/dev/devtools/index.js +56 -56
- package/cli/commands/dev/devtools/js/components.js +49 -49
- package/cli/commands/dev/devtools/js/core.js +423 -423
- package/cli/commands/dev/devtools/js/elements.js +421 -421
- package/cli/commands/dev/devtools/js/network.js +166 -166
- package/cli/commands/dev/devtools/js/performance.js +73 -73
- package/cli/commands/dev/devtools/js/router.js +105 -105
- package/cli/commands/dev/devtools/js/source.js +132 -132
- package/cli/commands/dev/devtools/js/stats.js +35 -35
- package/cli/commands/dev/devtools/js/tabs.js +79 -79
- package/cli/commands/dev/devtools/panel.html +95 -95
- package/cli/commands/dev/devtools/styles.css +244 -244
- package/cli/commands/dev/index.js +107 -107
- package/cli/commands/dev/logger.js +75 -75
- package/cli/commands/dev/overlay.js +858 -858
- package/cli/commands/dev/server.js +220 -220
- package/cli/commands/dev/validator.js +94 -94
- package/cli/commands/dev/watcher.js +172 -172
- package/cli/help.js +114 -112
- package/cli/index.js +52 -52
- package/cli/scaffold/default/LICENSE +21 -21
- package/cli/scaffold/default/app/app.js +207 -207
- package/cli/scaffold/default/app/components/about.js +201 -201
- package/cli/scaffold/default/app/components/api-demo.js +143 -143
- package/cli/scaffold/default/app/components/contact-card.js +231 -231
- package/cli/scaffold/default/app/components/contacts/contacts.css +706 -706
- package/cli/scaffold/default/app/components/contacts/contacts.html +200 -200
- package/cli/scaffold/default/app/components/contacts/contacts.js +196 -196
- package/cli/scaffold/default/app/components/counter.js +127 -127
- package/cli/scaffold/default/app/components/home.js +249 -249
- package/cli/scaffold/default/app/components/not-found.js +16 -16
- package/cli/scaffold/default/app/components/playground/playground.css +115 -115
- package/cli/scaffold/default/app/components/playground/playground.html +161 -161
- package/cli/scaffold/default/app/components/playground/playground.js +116 -116
- package/cli/scaffold/default/app/components/todos.js +225 -225
- package/cli/scaffold/default/app/components/toolkit/toolkit.css +97 -97
- package/cli/scaffold/default/app/components/toolkit/toolkit.html +146 -146
- package/cli/scaffold/default/app/components/toolkit/toolkit.js +280 -280
- package/cli/scaffold/default/app/routes.js +15 -15
- package/cli/scaffold/default/app/store.js +101 -101
- package/cli/scaffold/default/global.css +552 -552
- package/cli/scaffold/default/index.html +99 -99
- package/cli/scaffold/minimal/app/app.js +85 -85
- package/cli/scaffold/minimal/app/components/about.js +68 -68
- package/cli/scaffold/minimal/app/components/counter.js +122 -122
- package/cli/scaffold/minimal/app/components/home.js +68 -68
- package/cli/scaffold/minimal/app/components/not-found.js +16 -16
- package/cli/scaffold/minimal/app/routes.js +9 -9
- package/cli/scaffold/minimal/app/store.js +36 -36
- package/cli/scaffold/minimal/global.css +300 -300
- package/cli/scaffold/minimal/index.html +44 -44
- package/cli/scaffold/ssr/app/app.js +41 -41
- package/cli/scaffold/ssr/app/components/about.js +55 -55
- package/cli/scaffold/ssr/app/components/blog/index.js +65 -65
- package/cli/scaffold/ssr/app/components/blog/post.js +86 -86
- package/cli/scaffold/ssr/app/components/home.js +37 -37
- package/cli/scaffold/ssr/app/components/not-found.js +15 -15
- package/cli/scaffold/ssr/app/routes.js +8 -8
- package/cli/scaffold/ssr/global.css +228 -228
- package/cli/scaffold/ssr/index.html +37 -37
- package/cli/scaffold/ssr/package.json +8 -8
- package/cli/scaffold/ssr/server/data/posts.js +144 -144
- package/cli/scaffold/ssr/server/index.js +213 -213
- package/cli/scaffold/webrtc/app/app.js +11 -0
- package/cli/scaffold/webrtc/app/components/video-room.js +295 -0
- package/cli/scaffold/webrtc/app/lib/room.js +252 -0
- package/cli/scaffold/webrtc/assets/.gitkeep +0 -0
- package/cli/scaffold/webrtc/global.css +250 -0
- package/cli/scaffold/webrtc/index.html +21 -0
- package/cli/utils.js +305 -287
- package/dist/API.md +661 -0
- package/dist/zquery.dist.zip +0 -0
- package/dist/zquery.js +10313 -6614
- package/dist/zquery.min.js +8 -631
- package/index.d.ts +570 -371
- package/index.js +311 -240
- package/package.json +76 -70
- package/src/component.js +1709 -1691
- package/src/core.js +921 -921
- package/src/diff.js +497 -497
- package/src/errors.js +209 -209
- package/src/expression.js +922 -922
- package/src/http.js +242 -242
- package/src/package.json +1 -1
- package/src/reactive.js +255 -255
- package/src/router.js +843 -843
- package/src/ssr.js +418 -418
- package/src/store.js +318 -318
- package/src/utils.js +515 -515
- package/src/webrtc/e2ee.js +351 -0
- package/src/webrtc/errors.js +116 -0
- package/src/webrtc/ice.js +301 -0
- package/src/webrtc/index.js +131 -0
- package/src/webrtc/joinToken.js +119 -0
- package/src/webrtc/observe.js +172 -0
- package/src/webrtc/peer.js +351 -0
- package/src/webrtc/reactive.js +268 -0
- package/src/webrtc/room.js +625 -0
- package/src/webrtc/sdp.js +302 -0
- package/src/webrtc/sfu/index.js +43 -0
- package/src/webrtc/sfu/livekit.js +131 -0
- package/src/webrtc/sfu/mediasoup.js +150 -0
- package/src/webrtc/signaling.js +373 -0
- package/src/webrtc/turn.js +237 -0
- package/tests/_helpers/webrtcFakes.js +289 -0
- package/tests/audit.test.js +4158 -4158
- package/tests/cli.test.js +1136 -1103
- package/tests/compare.test.js +497 -486
- package/tests/component.test.js +3969 -3938
- package/tests/core.test.js +1910 -1910
- package/tests/dev-server.test.js +489 -489
- package/tests/diff.test.js +1416 -1416
- package/tests/docs.test.js +1664 -1650
- package/tests/electron-features.test.js +864 -864
- package/tests/errors.test.js +619 -619
- package/tests/expression.test.js +1056 -1056
- package/tests/http.test.js +648 -648
- package/tests/reactive.test.js +819 -819
- package/tests/router.test.js +2327 -2327
- package/tests/ssr.test.js +870 -870
- package/tests/store.test.js +830 -830
- package/tests/test-minifier.js +153 -153
- package/tests/test-ssr.js +27 -27
- package/tests/utils.test.js +1377 -1377
- package/tests/webrtc/e2ee.test.js +283 -0
- package/tests/webrtc/ice.test.js +202 -0
- package/tests/webrtc/joinToken.test.js +89 -0
- package/tests/webrtc/observe.test.js +111 -0
- package/tests/webrtc/peer.test.js +373 -0
- package/tests/webrtc/reactive.test.js +235 -0
- package/tests/webrtc/room.test.js +406 -0
- package/tests/webrtc/sdp.test.js +151 -0
- package/tests/webrtc/sfu-livekit.test.js +119 -0
- package/tests/webrtc/sfu.test.js +160 -0
- package/tests/webrtc/signaling.test.js +251 -0
- package/tests/webrtc/turn.test.js +256 -0
- package/types/collection.d.ts +383 -383
- package/types/component.d.ts +186 -186
- package/types/errors.d.ts +135 -135
- package/types/http.d.ts +92 -92
- package/types/misc.d.ts +201 -201
- package/types/reactive.d.ts +98 -98
- package/types/router.d.ts +190 -190
- package/types/ssr.d.ts +102 -102
- package/types/store.d.ts +146 -146
- package/types/utils.d.ts +245 -245
- package/types/webrtc.d.ts +653 -0
package/tests/store.test.js
CHANGED
|
@@ -1,830 +1,830 @@
|
|
|
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('count', 1, 0);
|
|
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
|
-
});
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
// ---------------------------------------------------------------------------
|
|
383
|
-
// PERF: history trim uses splice (in-place) instead of slice (copy)
|
|
384
|
-
// ---------------------------------------------------------------------------
|
|
385
|
-
|
|
386
|
-
describe('Store - history trim in-place', () => {
|
|
387
|
-
it('trims history to maxHistory without exceeding', () => {
|
|
388
|
-
const store = createStore('hist-trim', {
|
|
389
|
-
state: { n: 0 },
|
|
390
|
-
actions: { inc(s) { s.n++; } },
|
|
391
|
-
maxHistory: 5,
|
|
392
|
-
});
|
|
393
|
-
for (let i = 0; i < 10; i++) store.dispatch('inc');
|
|
394
|
-
expect(store.history.length).toBe(5);
|
|
395
|
-
// Newest actions should survive
|
|
396
|
-
expect(store.state.n).toBe(10);
|
|
397
|
-
});
|
|
398
|
-
|
|
399
|
-
it('maintains same array identity (splice, not slice)', () => {
|
|
400
|
-
const store = createStore('hist-identity', {
|
|
401
|
-
state: { n: 0 },
|
|
402
|
-
actions: { inc(s) { s.n++; } },
|
|
403
|
-
maxHistory: 3,
|
|
404
|
-
});
|
|
405
|
-
const ref = store._history;
|
|
406
|
-
for (let i = 0; i < 10; i++) store.dispatch('inc');
|
|
407
|
-
// splice modifies in-place so the array reference stays the same
|
|
408
|
-
expect(store._history).toBe(ref);
|
|
409
|
-
expect(store._history.length).toBe(3);
|
|
410
|
-
});
|
|
411
|
-
});
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
// ===========================================================================
|
|
415
|
-
// use() - middleware chaining
|
|
416
|
-
// ===========================================================================
|
|
417
|
-
|
|
418
|
-
describe('Store - use() chaining', () => {
|
|
419
|
-
it('returns the store for chaining', () => {
|
|
420
|
-
const store = createStore({
|
|
421
|
-
state: { x: 0 },
|
|
422
|
-
actions: { inc(state) { state.x++; } }
|
|
423
|
-
});
|
|
424
|
-
const result = store.use(() => {}).use(() => {});
|
|
425
|
-
expect(result).toBe(store);
|
|
426
|
-
});
|
|
427
|
-
|
|
428
|
-
it('multiple middleware run in order', () => {
|
|
429
|
-
const order = [];
|
|
430
|
-
const store = createStore({
|
|
431
|
-
state: { x: 0 },
|
|
432
|
-
actions: { inc(state) { state.x++; } }
|
|
433
|
-
});
|
|
434
|
-
store.use(() => { order.push('a'); });
|
|
435
|
-
store.use(() => { order.push('b'); });
|
|
436
|
-
store.dispatch('inc');
|
|
437
|
-
expect(order).toEqual(['a', 'b']);
|
|
438
|
-
});
|
|
439
|
-
|
|
440
|
-
it('middleware returning false blocks action', () => {
|
|
441
|
-
const store = createStore({
|
|
442
|
-
state: { x: 0 },
|
|
443
|
-
actions: { inc(state) { state.x++; } }
|
|
444
|
-
});
|
|
445
|
-
store.use(() => false);
|
|
446
|
-
store.dispatch('inc');
|
|
447
|
-
expect(store.state.x).toBe(0);
|
|
448
|
-
});
|
|
449
|
-
});
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
// ===========================================================================
|
|
453
|
-
// debug mode
|
|
454
|
-
// ===========================================================================
|
|
455
|
-
|
|
456
|
-
describe('Store - debug mode', () => {
|
|
457
|
-
it('logs when debug is true', () => {
|
|
458
|
-
const spy = vi.spyOn(console, 'log').mockImplementation(() => {});
|
|
459
|
-
const store = createStore({
|
|
460
|
-
state: { x: 0 },
|
|
461
|
-
actions: { inc(state) { state.x++; } },
|
|
462
|
-
debug: true
|
|
463
|
-
});
|
|
464
|
-
store.dispatch('inc');
|
|
465
|
-
expect(spy).toHaveBeenCalled();
|
|
466
|
-
const logStr = spy.mock.calls[0].join(' ');
|
|
467
|
-
expect(logStr).toContain('inc');
|
|
468
|
-
spy.mockRestore();
|
|
469
|
-
});
|
|
470
|
-
});
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
// ===========================================================================
|
|
474
|
-
// replaceState
|
|
475
|
-
// ===========================================================================
|
|
476
|
-
|
|
477
|
-
describe('Store - replaceState', () => {
|
|
478
|
-
it('replaces all keys', () => {
|
|
479
|
-
const store = createStore({
|
|
480
|
-
state: { a: 1, b: 2 }
|
|
481
|
-
});
|
|
482
|
-
store.replaceState({ c: 3 });
|
|
483
|
-
const snap = store.snapshot();
|
|
484
|
-
expect(snap).not.toHaveProperty('a');
|
|
485
|
-
expect(snap).not.toHaveProperty('b');
|
|
486
|
-
expect(snap.c).toBe(3);
|
|
487
|
-
});
|
|
488
|
-
});
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
// ===========================================================================
|
|
492
|
-
// wildcard subscription
|
|
493
|
-
// ===========================================================================
|
|
494
|
-
|
|
495
|
-
describe('Store - wildcard subscription', () => {
|
|
496
|
-
it('fires on any state change', () => {
|
|
497
|
-
const store = createStore({
|
|
498
|
-
state: { a: 1, b: 2 },
|
|
499
|
-
actions: {
|
|
500
|
-
setA(state, v) { state.a = v; },
|
|
501
|
-
setB(state, v) { state.b = v; }
|
|
502
|
-
}
|
|
503
|
-
});
|
|
504
|
-
const calls = [];
|
|
505
|
-
store.subscribe((key, val, old) => calls.push([key, val, old]));
|
|
506
|
-
store.dispatch('setA', 10);
|
|
507
|
-
store.dispatch('setB', 20);
|
|
508
|
-
expect(calls).toEqual([['a', 10, 1], ['b', 20, 2]]);
|
|
509
|
-
});
|
|
510
|
-
|
|
511
|
-
it('unsubscribes wildcard', () => {
|
|
512
|
-
const store = createStore({
|
|
513
|
-
state: { a: 1 },
|
|
514
|
-
actions: { setA(state, v) { state.a = v; } }
|
|
515
|
-
});
|
|
516
|
-
const fn = vi.fn();
|
|
517
|
-
const unsub = store.subscribe(fn);
|
|
518
|
-
store.dispatch('setA', 2);
|
|
519
|
-
expect(fn).toHaveBeenCalledTimes(1);
|
|
520
|
-
unsub();
|
|
521
|
-
store.dispatch('setA', 3);
|
|
522
|
-
expect(fn).toHaveBeenCalledTimes(1);
|
|
523
|
-
});
|
|
524
|
-
});
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
// ===========================================================================
|
|
528
|
-
// state as factory function
|
|
529
|
-
// ===========================================================================
|
|
530
|
-
|
|
531
|
-
describe('Store - state factory', () => {
|
|
532
|
-
it('calls state function for initial state', () => {
|
|
533
|
-
const store = createStore({
|
|
534
|
-
state: () => ({ count: 0 })
|
|
535
|
-
});
|
|
536
|
-
expect(store.state.count).toBe(0);
|
|
537
|
-
});
|
|
538
|
-
});
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
// ===========================================================================
|
|
542
|
-
// createStore - named stores
|
|
543
|
-
// ===========================================================================
|
|
544
|
-
|
|
545
|
-
describe('createStore - named stores', () => {
|
|
546
|
-
it('creates default store when no name given', () => {
|
|
547
|
-
const store = createStore({ state: { x: 1 } });
|
|
548
|
-
expect(store.state.x).toBe(1);
|
|
549
|
-
});
|
|
550
|
-
|
|
551
|
-
it('dispatch unknown action does not throw', () => {
|
|
552
|
-
const store = createStore({ state: { x: 1 }, actions: {} });
|
|
553
|
-
expect(() => store.dispatch('nope')).not.toThrow();
|
|
554
|
-
});
|
|
555
|
-
});
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
// ===========================================================================
|
|
559
|
-
// reset
|
|
560
|
-
// ===========================================================================
|
|
561
|
-
|
|
562
|
-
describe('Store - reset', () => {
|
|
563
|
-
it('resets state and clears history', () => {
|
|
564
|
-
const store = createStore({
|
|
565
|
-
state: { x: 0 },
|
|
566
|
-
actions: { inc(state) { state.x++; } }
|
|
567
|
-
});
|
|
568
|
-
store.dispatch('inc');
|
|
569
|
-
store.dispatch('inc');
|
|
570
|
-
expect(store.state.x).toBe(2);
|
|
571
|
-
expect(store.history.length).toBe(2);
|
|
572
|
-
store.reset({ x: 0 });
|
|
573
|
-
expect(store.state.x).toBe(0);
|
|
574
|
-
expect(store.history.length).toBe(0);
|
|
575
|
-
});
|
|
576
|
-
});
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
// ===========================================================================
|
|
580
|
-
// empty config
|
|
581
|
-
// ===========================================================================
|
|
582
|
-
|
|
583
|
-
describe('Store - empty config', () => {
|
|
584
|
-
it('creates store with no config', () => {
|
|
585
|
-
const store = createStore({});
|
|
586
|
-
expect(store.snapshot()).toEqual({});
|
|
587
|
-
expect(store.history).toEqual([]);
|
|
588
|
-
});
|
|
589
|
-
});
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
// ===========================================================================
|
|
593
|
-
// Store - batch
|
|
594
|
-
// ===========================================================================
|
|
595
|
-
|
|
596
|
-
describe('Store - batch', () => {
|
|
597
|
-
it('fires subscribers once per key, not per mutation', () => {
|
|
598
|
-
const store = createStore({
|
|
599
|
-
state: { x: 0, y: 0 },
|
|
600
|
-
actions: {
|
|
601
|
-
setX(state, v) { state.x = v; },
|
|
602
|
-
setY(state, v) { state.y = v; },
|
|
603
|
-
}
|
|
604
|
-
});
|
|
605
|
-
const fn = vi.fn();
|
|
606
|
-
store.subscribe('x', fn);
|
|
607
|
-
|
|
608
|
-
store.batch(state => {
|
|
609
|
-
state.x = 1;
|
|
610
|
-
state.x = 2;
|
|
611
|
-
state.x = 3;
|
|
612
|
-
});
|
|
613
|
-
|
|
614
|
-
// Should fire once with the final value
|
|
615
|
-
expect(fn).toHaveBeenCalledTimes(1);
|
|
616
|
-
expect(store.state.x).toBe(3);
|
|
617
|
-
});
|
|
618
|
-
|
|
619
|
-
it('batches changes across multiple keys', () => {
|
|
620
|
-
const store = createStore({
|
|
621
|
-
state: { a: 0, b: 0 }
|
|
622
|
-
});
|
|
623
|
-
const fnA = vi.fn();
|
|
624
|
-
const fnB = vi.fn();
|
|
625
|
-
store.subscribe('a', fnA);
|
|
626
|
-
store.subscribe('b', fnB);
|
|
627
|
-
|
|
628
|
-
store.batch(state => {
|
|
629
|
-
state.a = 10;
|
|
630
|
-
state.b = 20;
|
|
631
|
-
});
|
|
632
|
-
|
|
633
|
-
expect(fnA).toHaveBeenCalledTimes(1);
|
|
634
|
-
expect(fnB).toHaveBeenCalledTimes(1);
|
|
635
|
-
expect(store.state.a).toBe(10);
|
|
636
|
-
expect(store.state.b).toBe(20);
|
|
637
|
-
});
|
|
638
|
-
|
|
639
|
-
it('does not fire subscribers during the batch', () => {
|
|
640
|
-
const store = createStore({ state: { x: 0 } });
|
|
641
|
-
const calls = [];
|
|
642
|
-
store.subscribe('x', (val) => calls.push(val));
|
|
643
|
-
|
|
644
|
-
store.batch(state => {
|
|
645
|
-
state.x = 1;
|
|
646
|
-
// Subscriber should not have been called yet
|
|
647
|
-
expect(calls.length).toBe(0);
|
|
648
|
-
state.x = 2;
|
|
649
|
-
expect(calls.length).toBe(0);
|
|
650
|
-
});
|
|
651
|
-
|
|
652
|
-
// Now it fires
|
|
653
|
-
expect(calls.length).toBe(1);
|
|
654
|
-
});
|
|
655
|
-
});
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
// ===========================================================================
|
|
659
|
-
// Store - checkpoint / undo / redo
|
|
660
|
-
// ===========================================================================
|
|
661
|
-
|
|
662
|
-
describe('Store - checkpoint / undo / redo', () => {
|
|
663
|
-
it('undo restores to checkpointed state', () => {
|
|
664
|
-
const store = createStore({
|
|
665
|
-
state: { count: 0 },
|
|
666
|
-
actions: { inc(state) { state.count++; } }
|
|
667
|
-
});
|
|
668
|
-
|
|
669
|
-
store.checkpoint();
|
|
670
|
-
store.dispatch('inc');
|
|
671
|
-
store.dispatch('inc');
|
|
672
|
-
expect(store.state.count).toBe(2);
|
|
673
|
-
|
|
674
|
-
const didUndo = store.undo();
|
|
675
|
-
expect(didUndo).toBe(true);
|
|
676
|
-
expect(store.state.count).toBe(0);
|
|
677
|
-
});
|
|
678
|
-
|
|
679
|
-
it('redo restores the undone state', () => {
|
|
680
|
-
const store = createStore({
|
|
681
|
-
state: { count: 0 },
|
|
682
|
-
actions: { inc(state) { state.count++; } }
|
|
683
|
-
});
|
|
684
|
-
|
|
685
|
-
store.checkpoint();
|
|
686
|
-
store.dispatch('inc');
|
|
687
|
-
store.dispatch('inc');
|
|
688
|
-
store.undo();
|
|
689
|
-
expect(store.state.count).toBe(0);
|
|
690
|
-
|
|
691
|
-
const didRedo = store.redo();
|
|
692
|
-
expect(didRedo).toBe(true);
|
|
693
|
-
expect(store.state.count).toBe(2);
|
|
694
|
-
});
|
|
695
|
-
|
|
696
|
-
it('undo returns false when no checkpoints', () => {
|
|
697
|
-
const store = createStore({ state: { x: 1 } });
|
|
698
|
-
expect(store.undo()).toBe(false);
|
|
699
|
-
expect(store.state.x).toBe(1);
|
|
700
|
-
});
|
|
701
|
-
|
|
702
|
-
it('redo returns false when nothing to redo', () => {
|
|
703
|
-
const store = createStore({ state: { x: 1 } });
|
|
704
|
-
expect(store.redo()).toBe(false);
|
|
705
|
-
});
|
|
706
|
-
|
|
707
|
-
it('canUndo and canRedo reflect stack state', () => {
|
|
708
|
-
const store = createStore({
|
|
709
|
-
state: { v: 'a' },
|
|
710
|
-
actions: { set(state, v) { state.v = v; } }
|
|
711
|
-
});
|
|
712
|
-
|
|
713
|
-
expect(store.canUndo).toBe(false);
|
|
714
|
-
expect(store.canRedo).toBe(false);
|
|
715
|
-
|
|
716
|
-
store.checkpoint();
|
|
717
|
-
expect(store.canUndo).toBe(true);
|
|
718
|
-
|
|
719
|
-
store.dispatch('set', 'b');
|
|
720
|
-
store.undo();
|
|
721
|
-
expect(store.canRedo).toBe(true);
|
|
722
|
-
|
|
723
|
-
store.redo();
|
|
724
|
-
expect(store.canRedo).toBe(false);
|
|
725
|
-
});
|
|
726
|
-
|
|
727
|
-
it('new checkpoint clears redo stack', () => {
|
|
728
|
-
const store = createStore({
|
|
729
|
-
state: { x: 0 },
|
|
730
|
-
actions: { set(state, v) { state.x = v; } }
|
|
731
|
-
});
|
|
732
|
-
|
|
733
|
-
store.checkpoint();
|
|
734
|
-
store.dispatch('set', 1);
|
|
735
|
-
store.undo();
|
|
736
|
-
expect(store.canRedo).toBe(true);
|
|
737
|
-
|
|
738
|
-
// New checkpoint clears redo
|
|
739
|
-
store.checkpoint();
|
|
740
|
-
expect(store.canRedo).toBe(false);
|
|
741
|
-
});
|
|
742
|
-
|
|
743
|
-
it('respects maxUndo limit', () => {
|
|
744
|
-
const store = createStore({
|
|
745
|
-
state: { x: 0 },
|
|
746
|
-
maxUndo: 3,
|
|
747
|
-
actions: { set(state, v) { state.x = v; } }
|
|
748
|
-
});
|
|
749
|
-
|
|
750
|
-
store.checkpoint(); // save x=0
|
|
751
|
-
store.dispatch('set', 1);
|
|
752
|
-
store.checkpoint(); // save x=1
|
|
753
|
-
store.dispatch('set', 2);
|
|
754
|
-
store.checkpoint(); // save x=2
|
|
755
|
-
store.dispatch('set', 3);
|
|
756
|
-
store.checkpoint(); // save x=3 -> should trim oldest (x=0)
|
|
757
|
-
store.dispatch('set', 4);
|
|
758
|
-
|
|
759
|
-
// Should have at most 3 entries
|
|
760
|
-
store.undo(); // -> x=3
|
|
761
|
-
store.undo(); // -> x=2
|
|
762
|
-
store.undo(); // -> x=1
|
|
763
|
-
expect(store.undo()).toBe(false); // oldest was trimmed
|
|
764
|
-
});
|
|
765
|
-
|
|
766
|
-
it('multiple undo/redo cycles', () => {
|
|
767
|
-
const store = createStore({
|
|
768
|
-
state: { n: 0 },
|
|
769
|
-
actions: { set(state, v) { state.n = v; } }
|
|
770
|
-
});
|
|
771
|
-
|
|
772
|
-
store.checkpoint();
|
|
773
|
-
store.dispatch('set', 1);
|
|
774
|
-
store.checkpoint();
|
|
775
|
-
store.dispatch('set', 2);
|
|
776
|
-
store.checkpoint();
|
|
777
|
-
store.dispatch('set', 3);
|
|
778
|
-
|
|
779
|
-
store.undo(); // -> 2
|
|
780
|
-
expect(store.state.n).toBe(2);
|
|
781
|
-
store.undo(); // -> 1
|
|
782
|
-
expect(store.state.n).toBe(1);
|
|
783
|
-
store.redo(); // -> 2
|
|
784
|
-
expect(store.state.n).toBe(2);
|
|
785
|
-
store.redo(); // -> 3
|
|
786
|
-
expect(store.state.n).toBe(3);
|
|
787
|
-
});
|
|
788
|
-
});
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
// ===========================================================================
|
|
792
|
-
// Store - reset with no args
|
|
793
|
-
// ===========================================================================
|
|
794
|
-
|
|
795
|
-
describe('Store - reset defaults to initial state', () => {
|
|
796
|
-
it('resets to the original initial state when called with no arguments', () => {
|
|
797
|
-
const store = createStore({
|
|
798
|
-
state: { count: 0, name: 'test' },
|
|
799
|
-
actions: {
|
|
800
|
-
inc(state) { state.count++; },
|
|
801
|
-
rename(state, n) { state.name = n; }
|
|
802
|
-
}
|
|
803
|
-
});
|
|
804
|
-
|
|
805
|
-
store.dispatch('inc');
|
|
806
|
-
store.dispatch('inc');
|
|
807
|
-
store.dispatch('rename', 'changed');
|
|
808
|
-
expect(store.state.count).toBe(2);
|
|
809
|
-
expect(store.state.name).toBe('changed');
|
|
810
|
-
|
|
811
|
-
store.reset();
|
|
812
|
-
expect(store.state.count).toBe(0);
|
|
813
|
-
expect(store.state.name).toBe('test');
|
|
814
|
-
});
|
|
815
|
-
|
|
816
|
-
it('clears undo/redo stacks on reset', () => {
|
|
817
|
-
const store = createStore({
|
|
818
|
-
state: { x: 0 },
|
|
819
|
-
actions: { set(state, v) { state.x = v; } }
|
|
820
|
-
});
|
|
821
|
-
|
|
822
|
-
store.checkpoint();
|
|
823
|
-
store.dispatch('set', 5);
|
|
824
|
-
expect(store.canUndo).toBe(true);
|
|
825
|
-
|
|
826
|
-
store.reset();
|
|
827
|
-
expect(store.canUndo).toBe(false);
|
|
828
|
-
expect(store.canRedo).toBe(false);
|
|
829
|
-
});
|
|
830
|
-
});
|
|
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('count', 1, 0);
|
|
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
|
+
});
|
|
380
|
+
|
|
381
|
+
|
|
382
|
+
// ---------------------------------------------------------------------------
|
|
383
|
+
// PERF: history trim uses splice (in-place) instead of slice (copy)
|
|
384
|
+
// ---------------------------------------------------------------------------
|
|
385
|
+
|
|
386
|
+
describe('Store - history trim in-place', () => {
|
|
387
|
+
it('trims history to maxHistory without exceeding', () => {
|
|
388
|
+
const store = createStore('hist-trim', {
|
|
389
|
+
state: { n: 0 },
|
|
390
|
+
actions: { inc(s) { s.n++; } },
|
|
391
|
+
maxHistory: 5,
|
|
392
|
+
});
|
|
393
|
+
for (let i = 0; i < 10; i++) store.dispatch('inc');
|
|
394
|
+
expect(store.history.length).toBe(5);
|
|
395
|
+
// Newest actions should survive
|
|
396
|
+
expect(store.state.n).toBe(10);
|
|
397
|
+
});
|
|
398
|
+
|
|
399
|
+
it('maintains same array identity (splice, not slice)', () => {
|
|
400
|
+
const store = createStore('hist-identity', {
|
|
401
|
+
state: { n: 0 },
|
|
402
|
+
actions: { inc(s) { s.n++; } },
|
|
403
|
+
maxHistory: 3,
|
|
404
|
+
});
|
|
405
|
+
const ref = store._history;
|
|
406
|
+
for (let i = 0; i < 10; i++) store.dispatch('inc');
|
|
407
|
+
// splice modifies in-place so the array reference stays the same
|
|
408
|
+
expect(store._history).toBe(ref);
|
|
409
|
+
expect(store._history.length).toBe(3);
|
|
410
|
+
});
|
|
411
|
+
});
|
|
412
|
+
|
|
413
|
+
|
|
414
|
+
// ===========================================================================
|
|
415
|
+
// use() - middleware chaining
|
|
416
|
+
// ===========================================================================
|
|
417
|
+
|
|
418
|
+
describe('Store - use() chaining', () => {
|
|
419
|
+
it('returns the store for chaining', () => {
|
|
420
|
+
const store = createStore({
|
|
421
|
+
state: { x: 0 },
|
|
422
|
+
actions: { inc(state) { state.x++; } }
|
|
423
|
+
});
|
|
424
|
+
const result = store.use(() => {}).use(() => {});
|
|
425
|
+
expect(result).toBe(store);
|
|
426
|
+
});
|
|
427
|
+
|
|
428
|
+
it('multiple middleware run in order', () => {
|
|
429
|
+
const order = [];
|
|
430
|
+
const store = createStore({
|
|
431
|
+
state: { x: 0 },
|
|
432
|
+
actions: { inc(state) { state.x++; } }
|
|
433
|
+
});
|
|
434
|
+
store.use(() => { order.push('a'); });
|
|
435
|
+
store.use(() => { order.push('b'); });
|
|
436
|
+
store.dispatch('inc');
|
|
437
|
+
expect(order).toEqual(['a', 'b']);
|
|
438
|
+
});
|
|
439
|
+
|
|
440
|
+
it('middleware returning false blocks action', () => {
|
|
441
|
+
const store = createStore({
|
|
442
|
+
state: { x: 0 },
|
|
443
|
+
actions: { inc(state) { state.x++; } }
|
|
444
|
+
});
|
|
445
|
+
store.use(() => false);
|
|
446
|
+
store.dispatch('inc');
|
|
447
|
+
expect(store.state.x).toBe(0);
|
|
448
|
+
});
|
|
449
|
+
});
|
|
450
|
+
|
|
451
|
+
|
|
452
|
+
// ===========================================================================
|
|
453
|
+
// debug mode
|
|
454
|
+
// ===========================================================================
|
|
455
|
+
|
|
456
|
+
describe('Store - debug mode', () => {
|
|
457
|
+
it('logs when debug is true', () => {
|
|
458
|
+
const spy = vi.spyOn(console, 'log').mockImplementation(() => {});
|
|
459
|
+
const store = createStore({
|
|
460
|
+
state: { x: 0 },
|
|
461
|
+
actions: { inc(state) { state.x++; } },
|
|
462
|
+
debug: true
|
|
463
|
+
});
|
|
464
|
+
store.dispatch('inc');
|
|
465
|
+
expect(spy).toHaveBeenCalled();
|
|
466
|
+
const logStr = spy.mock.calls[0].join(' ');
|
|
467
|
+
expect(logStr).toContain('inc');
|
|
468
|
+
spy.mockRestore();
|
|
469
|
+
});
|
|
470
|
+
});
|
|
471
|
+
|
|
472
|
+
|
|
473
|
+
// ===========================================================================
|
|
474
|
+
// replaceState
|
|
475
|
+
// ===========================================================================
|
|
476
|
+
|
|
477
|
+
describe('Store - replaceState', () => {
|
|
478
|
+
it('replaces all keys', () => {
|
|
479
|
+
const store = createStore({
|
|
480
|
+
state: { a: 1, b: 2 }
|
|
481
|
+
});
|
|
482
|
+
store.replaceState({ c: 3 });
|
|
483
|
+
const snap = store.snapshot();
|
|
484
|
+
expect(snap).not.toHaveProperty('a');
|
|
485
|
+
expect(snap).not.toHaveProperty('b');
|
|
486
|
+
expect(snap.c).toBe(3);
|
|
487
|
+
});
|
|
488
|
+
});
|
|
489
|
+
|
|
490
|
+
|
|
491
|
+
// ===========================================================================
|
|
492
|
+
// wildcard subscription
|
|
493
|
+
// ===========================================================================
|
|
494
|
+
|
|
495
|
+
describe('Store - wildcard subscription', () => {
|
|
496
|
+
it('fires on any state change', () => {
|
|
497
|
+
const store = createStore({
|
|
498
|
+
state: { a: 1, b: 2 },
|
|
499
|
+
actions: {
|
|
500
|
+
setA(state, v) { state.a = v; },
|
|
501
|
+
setB(state, v) { state.b = v; }
|
|
502
|
+
}
|
|
503
|
+
});
|
|
504
|
+
const calls = [];
|
|
505
|
+
store.subscribe((key, val, old) => calls.push([key, val, old]));
|
|
506
|
+
store.dispatch('setA', 10);
|
|
507
|
+
store.dispatch('setB', 20);
|
|
508
|
+
expect(calls).toEqual([['a', 10, 1], ['b', 20, 2]]);
|
|
509
|
+
});
|
|
510
|
+
|
|
511
|
+
it('unsubscribes wildcard', () => {
|
|
512
|
+
const store = createStore({
|
|
513
|
+
state: { a: 1 },
|
|
514
|
+
actions: { setA(state, v) { state.a = v; } }
|
|
515
|
+
});
|
|
516
|
+
const fn = vi.fn();
|
|
517
|
+
const unsub = store.subscribe(fn);
|
|
518
|
+
store.dispatch('setA', 2);
|
|
519
|
+
expect(fn).toHaveBeenCalledTimes(1);
|
|
520
|
+
unsub();
|
|
521
|
+
store.dispatch('setA', 3);
|
|
522
|
+
expect(fn).toHaveBeenCalledTimes(1);
|
|
523
|
+
});
|
|
524
|
+
});
|
|
525
|
+
|
|
526
|
+
|
|
527
|
+
// ===========================================================================
|
|
528
|
+
// state as factory function
|
|
529
|
+
// ===========================================================================
|
|
530
|
+
|
|
531
|
+
describe('Store - state factory', () => {
|
|
532
|
+
it('calls state function for initial state', () => {
|
|
533
|
+
const store = createStore({
|
|
534
|
+
state: () => ({ count: 0 })
|
|
535
|
+
});
|
|
536
|
+
expect(store.state.count).toBe(0);
|
|
537
|
+
});
|
|
538
|
+
});
|
|
539
|
+
|
|
540
|
+
|
|
541
|
+
// ===========================================================================
|
|
542
|
+
// createStore - named stores
|
|
543
|
+
// ===========================================================================
|
|
544
|
+
|
|
545
|
+
describe('createStore - named stores', () => {
|
|
546
|
+
it('creates default store when no name given', () => {
|
|
547
|
+
const store = createStore({ state: { x: 1 } });
|
|
548
|
+
expect(store.state.x).toBe(1);
|
|
549
|
+
});
|
|
550
|
+
|
|
551
|
+
it('dispatch unknown action does not throw', () => {
|
|
552
|
+
const store = createStore({ state: { x: 1 }, actions: {} });
|
|
553
|
+
expect(() => store.dispatch('nope')).not.toThrow();
|
|
554
|
+
});
|
|
555
|
+
});
|
|
556
|
+
|
|
557
|
+
|
|
558
|
+
// ===========================================================================
|
|
559
|
+
// reset
|
|
560
|
+
// ===========================================================================
|
|
561
|
+
|
|
562
|
+
describe('Store - reset', () => {
|
|
563
|
+
it('resets state and clears history', () => {
|
|
564
|
+
const store = createStore({
|
|
565
|
+
state: { x: 0 },
|
|
566
|
+
actions: { inc(state) { state.x++; } }
|
|
567
|
+
});
|
|
568
|
+
store.dispatch('inc');
|
|
569
|
+
store.dispatch('inc');
|
|
570
|
+
expect(store.state.x).toBe(2);
|
|
571
|
+
expect(store.history.length).toBe(2);
|
|
572
|
+
store.reset({ x: 0 });
|
|
573
|
+
expect(store.state.x).toBe(0);
|
|
574
|
+
expect(store.history.length).toBe(0);
|
|
575
|
+
});
|
|
576
|
+
});
|
|
577
|
+
|
|
578
|
+
|
|
579
|
+
// ===========================================================================
|
|
580
|
+
// empty config
|
|
581
|
+
// ===========================================================================
|
|
582
|
+
|
|
583
|
+
describe('Store - empty config', () => {
|
|
584
|
+
it('creates store with no config', () => {
|
|
585
|
+
const store = createStore({});
|
|
586
|
+
expect(store.snapshot()).toEqual({});
|
|
587
|
+
expect(store.history).toEqual([]);
|
|
588
|
+
});
|
|
589
|
+
});
|
|
590
|
+
|
|
591
|
+
|
|
592
|
+
// ===========================================================================
|
|
593
|
+
// Store - batch
|
|
594
|
+
// ===========================================================================
|
|
595
|
+
|
|
596
|
+
describe('Store - batch', () => {
|
|
597
|
+
it('fires subscribers once per key, not per mutation', () => {
|
|
598
|
+
const store = createStore({
|
|
599
|
+
state: { x: 0, y: 0 },
|
|
600
|
+
actions: {
|
|
601
|
+
setX(state, v) { state.x = v; },
|
|
602
|
+
setY(state, v) { state.y = v; },
|
|
603
|
+
}
|
|
604
|
+
});
|
|
605
|
+
const fn = vi.fn();
|
|
606
|
+
store.subscribe('x', fn);
|
|
607
|
+
|
|
608
|
+
store.batch(state => {
|
|
609
|
+
state.x = 1;
|
|
610
|
+
state.x = 2;
|
|
611
|
+
state.x = 3;
|
|
612
|
+
});
|
|
613
|
+
|
|
614
|
+
// Should fire once with the final value
|
|
615
|
+
expect(fn).toHaveBeenCalledTimes(1);
|
|
616
|
+
expect(store.state.x).toBe(3);
|
|
617
|
+
});
|
|
618
|
+
|
|
619
|
+
it('batches changes across multiple keys', () => {
|
|
620
|
+
const store = createStore({
|
|
621
|
+
state: { a: 0, b: 0 }
|
|
622
|
+
});
|
|
623
|
+
const fnA = vi.fn();
|
|
624
|
+
const fnB = vi.fn();
|
|
625
|
+
store.subscribe('a', fnA);
|
|
626
|
+
store.subscribe('b', fnB);
|
|
627
|
+
|
|
628
|
+
store.batch(state => {
|
|
629
|
+
state.a = 10;
|
|
630
|
+
state.b = 20;
|
|
631
|
+
});
|
|
632
|
+
|
|
633
|
+
expect(fnA).toHaveBeenCalledTimes(1);
|
|
634
|
+
expect(fnB).toHaveBeenCalledTimes(1);
|
|
635
|
+
expect(store.state.a).toBe(10);
|
|
636
|
+
expect(store.state.b).toBe(20);
|
|
637
|
+
});
|
|
638
|
+
|
|
639
|
+
it('does not fire subscribers during the batch', () => {
|
|
640
|
+
const store = createStore({ state: { x: 0 } });
|
|
641
|
+
const calls = [];
|
|
642
|
+
store.subscribe('x', (val) => calls.push(val));
|
|
643
|
+
|
|
644
|
+
store.batch(state => {
|
|
645
|
+
state.x = 1;
|
|
646
|
+
// Subscriber should not have been called yet
|
|
647
|
+
expect(calls.length).toBe(0);
|
|
648
|
+
state.x = 2;
|
|
649
|
+
expect(calls.length).toBe(0);
|
|
650
|
+
});
|
|
651
|
+
|
|
652
|
+
// Now it fires
|
|
653
|
+
expect(calls.length).toBe(1);
|
|
654
|
+
});
|
|
655
|
+
});
|
|
656
|
+
|
|
657
|
+
|
|
658
|
+
// ===========================================================================
|
|
659
|
+
// Store - checkpoint / undo / redo
|
|
660
|
+
// ===========================================================================
|
|
661
|
+
|
|
662
|
+
describe('Store - checkpoint / undo / redo', () => {
|
|
663
|
+
it('undo restores to checkpointed state', () => {
|
|
664
|
+
const store = createStore({
|
|
665
|
+
state: { count: 0 },
|
|
666
|
+
actions: { inc(state) { state.count++; } }
|
|
667
|
+
});
|
|
668
|
+
|
|
669
|
+
store.checkpoint();
|
|
670
|
+
store.dispatch('inc');
|
|
671
|
+
store.dispatch('inc');
|
|
672
|
+
expect(store.state.count).toBe(2);
|
|
673
|
+
|
|
674
|
+
const didUndo = store.undo();
|
|
675
|
+
expect(didUndo).toBe(true);
|
|
676
|
+
expect(store.state.count).toBe(0);
|
|
677
|
+
});
|
|
678
|
+
|
|
679
|
+
it('redo restores the undone state', () => {
|
|
680
|
+
const store = createStore({
|
|
681
|
+
state: { count: 0 },
|
|
682
|
+
actions: { inc(state) { state.count++; } }
|
|
683
|
+
});
|
|
684
|
+
|
|
685
|
+
store.checkpoint();
|
|
686
|
+
store.dispatch('inc');
|
|
687
|
+
store.dispatch('inc');
|
|
688
|
+
store.undo();
|
|
689
|
+
expect(store.state.count).toBe(0);
|
|
690
|
+
|
|
691
|
+
const didRedo = store.redo();
|
|
692
|
+
expect(didRedo).toBe(true);
|
|
693
|
+
expect(store.state.count).toBe(2);
|
|
694
|
+
});
|
|
695
|
+
|
|
696
|
+
it('undo returns false when no checkpoints', () => {
|
|
697
|
+
const store = createStore({ state: { x: 1 } });
|
|
698
|
+
expect(store.undo()).toBe(false);
|
|
699
|
+
expect(store.state.x).toBe(1);
|
|
700
|
+
});
|
|
701
|
+
|
|
702
|
+
it('redo returns false when nothing to redo', () => {
|
|
703
|
+
const store = createStore({ state: { x: 1 } });
|
|
704
|
+
expect(store.redo()).toBe(false);
|
|
705
|
+
});
|
|
706
|
+
|
|
707
|
+
it('canUndo and canRedo reflect stack state', () => {
|
|
708
|
+
const store = createStore({
|
|
709
|
+
state: { v: 'a' },
|
|
710
|
+
actions: { set(state, v) { state.v = v; } }
|
|
711
|
+
});
|
|
712
|
+
|
|
713
|
+
expect(store.canUndo).toBe(false);
|
|
714
|
+
expect(store.canRedo).toBe(false);
|
|
715
|
+
|
|
716
|
+
store.checkpoint();
|
|
717
|
+
expect(store.canUndo).toBe(true);
|
|
718
|
+
|
|
719
|
+
store.dispatch('set', 'b');
|
|
720
|
+
store.undo();
|
|
721
|
+
expect(store.canRedo).toBe(true);
|
|
722
|
+
|
|
723
|
+
store.redo();
|
|
724
|
+
expect(store.canRedo).toBe(false);
|
|
725
|
+
});
|
|
726
|
+
|
|
727
|
+
it('new checkpoint clears redo stack', () => {
|
|
728
|
+
const store = createStore({
|
|
729
|
+
state: { x: 0 },
|
|
730
|
+
actions: { set(state, v) { state.x = v; } }
|
|
731
|
+
});
|
|
732
|
+
|
|
733
|
+
store.checkpoint();
|
|
734
|
+
store.dispatch('set', 1);
|
|
735
|
+
store.undo();
|
|
736
|
+
expect(store.canRedo).toBe(true);
|
|
737
|
+
|
|
738
|
+
// New checkpoint clears redo
|
|
739
|
+
store.checkpoint();
|
|
740
|
+
expect(store.canRedo).toBe(false);
|
|
741
|
+
});
|
|
742
|
+
|
|
743
|
+
it('respects maxUndo limit', () => {
|
|
744
|
+
const store = createStore({
|
|
745
|
+
state: { x: 0 },
|
|
746
|
+
maxUndo: 3,
|
|
747
|
+
actions: { set(state, v) { state.x = v; } }
|
|
748
|
+
});
|
|
749
|
+
|
|
750
|
+
store.checkpoint(); // save x=0
|
|
751
|
+
store.dispatch('set', 1);
|
|
752
|
+
store.checkpoint(); // save x=1
|
|
753
|
+
store.dispatch('set', 2);
|
|
754
|
+
store.checkpoint(); // save x=2
|
|
755
|
+
store.dispatch('set', 3);
|
|
756
|
+
store.checkpoint(); // save x=3 -> should trim oldest (x=0)
|
|
757
|
+
store.dispatch('set', 4);
|
|
758
|
+
|
|
759
|
+
// Should have at most 3 entries
|
|
760
|
+
store.undo(); // -> x=3
|
|
761
|
+
store.undo(); // -> x=2
|
|
762
|
+
store.undo(); // -> x=1
|
|
763
|
+
expect(store.undo()).toBe(false); // oldest was trimmed
|
|
764
|
+
});
|
|
765
|
+
|
|
766
|
+
it('multiple undo/redo cycles', () => {
|
|
767
|
+
const store = createStore({
|
|
768
|
+
state: { n: 0 },
|
|
769
|
+
actions: { set(state, v) { state.n = v; } }
|
|
770
|
+
});
|
|
771
|
+
|
|
772
|
+
store.checkpoint();
|
|
773
|
+
store.dispatch('set', 1);
|
|
774
|
+
store.checkpoint();
|
|
775
|
+
store.dispatch('set', 2);
|
|
776
|
+
store.checkpoint();
|
|
777
|
+
store.dispatch('set', 3);
|
|
778
|
+
|
|
779
|
+
store.undo(); // -> 2
|
|
780
|
+
expect(store.state.n).toBe(2);
|
|
781
|
+
store.undo(); // -> 1
|
|
782
|
+
expect(store.state.n).toBe(1);
|
|
783
|
+
store.redo(); // -> 2
|
|
784
|
+
expect(store.state.n).toBe(2);
|
|
785
|
+
store.redo(); // -> 3
|
|
786
|
+
expect(store.state.n).toBe(3);
|
|
787
|
+
});
|
|
788
|
+
});
|
|
789
|
+
|
|
790
|
+
|
|
791
|
+
// ===========================================================================
|
|
792
|
+
// Store - reset with no args
|
|
793
|
+
// ===========================================================================
|
|
794
|
+
|
|
795
|
+
describe('Store - reset defaults to initial state', () => {
|
|
796
|
+
it('resets to the original initial state when called with no arguments', () => {
|
|
797
|
+
const store = createStore({
|
|
798
|
+
state: { count: 0, name: 'test' },
|
|
799
|
+
actions: {
|
|
800
|
+
inc(state) { state.count++; },
|
|
801
|
+
rename(state, n) { state.name = n; }
|
|
802
|
+
}
|
|
803
|
+
});
|
|
804
|
+
|
|
805
|
+
store.dispatch('inc');
|
|
806
|
+
store.dispatch('inc');
|
|
807
|
+
store.dispatch('rename', 'changed');
|
|
808
|
+
expect(store.state.count).toBe(2);
|
|
809
|
+
expect(store.state.name).toBe('changed');
|
|
810
|
+
|
|
811
|
+
store.reset();
|
|
812
|
+
expect(store.state.count).toBe(0);
|
|
813
|
+
expect(store.state.name).toBe('test');
|
|
814
|
+
});
|
|
815
|
+
|
|
816
|
+
it('clears undo/redo stacks on reset', () => {
|
|
817
|
+
const store = createStore({
|
|
818
|
+
state: { x: 0 },
|
|
819
|
+
actions: { set(state, v) { state.x = v; } }
|
|
820
|
+
});
|
|
821
|
+
|
|
822
|
+
store.checkpoint();
|
|
823
|
+
store.dispatch('set', 5);
|
|
824
|
+
expect(store.canUndo).toBe(true);
|
|
825
|
+
|
|
826
|
+
store.reset();
|
|
827
|
+
expect(store.canUndo).toBe(false);
|
|
828
|
+
expect(store.canRedo).toBe(false);
|
|
829
|
+
});
|
|
830
|
+
});
|