@ersbeth/picoflow 1.0.1 → 1.1.1
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/.cursor/plans/unifier-flowresource-avec-flowderivation-c9506e24.plan.md +372 -0
- package/README.md +17 -1
- package/biome.json +4 -1
- package/dist/picoflow.js +1155 -582
- package/dist/types/flow/base/flowDisposable.d.ts +67 -0
- package/dist/types/flow/base/flowDisposable.d.ts.map +1 -0
- package/dist/types/flow/base/flowEffect.d.ts +127 -0
- package/dist/types/flow/base/flowEffect.d.ts.map +1 -0
- package/dist/types/flow/base/flowGraph.d.ts +97 -0
- package/dist/types/flow/base/flowGraph.d.ts.map +1 -0
- package/dist/types/flow/base/flowSignal.d.ts +134 -0
- package/dist/types/flow/base/flowSignal.d.ts.map +1 -0
- package/dist/types/flow/base/flowTracker.d.ts +15 -0
- package/dist/types/flow/base/flowTracker.d.ts.map +1 -0
- package/dist/types/flow/base/index.d.ts +7 -0
- package/dist/types/flow/base/index.d.ts.map +1 -0
- package/dist/types/flow/base/utils.d.ts +20 -0
- package/dist/types/flow/base/utils.d.ts.map +1 -0
- package/dist/types/{advanced/array.d.ts → flow/collections/flowArray.d.ts} +50 -12
- package/dist/types/flow/collections/flowArray.d.ts.map +1 -0
- package/dist/types/flow/collections/flowMap.d.ts +224 -0
- package/dist/types/flow/collections/flowMap.d.ts.map +1 -0
- package/dist/types/flow/collections/index.d.ts +3 -0
- package/dist/types/flow/collections/index.d.ts.map +1 -0
- package/dist/types/flow/index.d.ts +4 -0
- package/dist/types/flow/index.d.ts.map +1 -0
- package/dist/types/flow/nodes/async/flowConstantAsync.d.ts +137 -0
- package/dist/types/flow/nodes/async/flowConstantAsync.d.ts.map +1 -0
- package/dist/types/flow/nodes/async/flowDerivationAsync.d.ts +137 -0
- package/dist/types/flow/nodes/async/flowDerivationAsync.d.ts.map +1 -0
- package/dist/types/flow/nodes/async/flowNodeAsync.d.ts +343 -0
- package/dist/types/flow/nodes/async/flowNodeAsync.d.ts.map +1 -0
- package/dist/types/flow/nodes/async/flowReadonlyAsync.d.ts +81 -0
- package/dist/types/flow/nodes/async/flowReadonlyAsync.d.ts.map +1 -0
- package/dist/types/flow/nodes/async/flowStateAsync.d.ts +111 -0
- package/dist/types/flow/nodes/async/flowStateAsync.d.ts.map +1 -0
- package/dist/types/flow/nodes/async/index.d.ts +6 -0
- package/dist/types/flow/nodes/async/index.d.ts.map +1 -0
- package/dist/types/flow/nodes/index.d.ts +3 -0
- package/dist/types/flow/nodes/index.d.ts.map +1 -0
- package/dist/types/flow/nodes/sync/flowConstant.d.ts +108 -0
- package/dist/types/flow/nodes/sync/flowConstant.d.ts.map +1 -0
- package/dist/types/flow/nodes/sync/flowDerivation.d.ts +100 -0
- package/dist/types/flow/nodes/sync/flowDerivation.d.ts.map +1 -0
- package/dist/types/flow/nodes/sync/flowNode.d.ts +314 -0
- package/dist/types/flow/nodes/sync/flowNode.d.ts.map +1 -0
- package/dist/types/flow/nodes/sync/flowReadonly.d.ts +57 -0
- package/dist/types/flow/nodes/sync/flowReadonly.d.ts.map +1 -0
- package/dist/types/flow/nodes/sync/flowState.d.ts +96 -0
- package/dist/types/flow/nodes/sync/flowState.d.ts.map +1 -0
- package/dist/types/flow/nodes/sync/index.d.ts +6 -0
- package/dist/types/flow/nodes/sync/index.d.ts.map +1 -0
- package/dist/types/index.d.ts +2 -4
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/solid/converters.d.ts +34 -45
- package/dist/types/solid/converters.d.ts.map +1 -1
- package/dist/types/solid/index.d.ts +2 -2
- package/dist/types/solid/index.d.ts.map +1 -1
- package/dist/types/solid/primitives.d.ts +1 -0
- package/dist/types/solid/primitives.d.ts.map +1 -1
- package/docs/.vitepress/config.mts +1 -1
- package/docs/api/typedoc-sidebar.json +81 -1
- package/package.json +60 -58
- package/src/flow/base/flowDisposable.ts +71 -0
- package/src/flow/base/flowEffect.ts +171 -0
- package/src/flow/base/flowGraph.ts +288 -0
- package/src/flow/base/flowSignal.ts +207 -0
- package/src/flow/base/flowTracker.ts +17 -0
- package/src/flow/base/index.ts +6 -0
- package/src/flow/base/utils.ts +19 -0
- package/src/flow/collections/flowArray.ts +409 -0
- package/src/flow/collections/flowMap.ts +398 -0
- package/src/flow/collections/index.ts +2 -0
- package/src/flow/index.ts +3 -0
- package/src/flow/nodes/async/flowConstantAsync.ts +142 -0
- package/src/flow/nodes/async/flowDerivationAsync.ts +143 -0
- package/src/flow/nodes/async/flowNodeAsync.ts +474 -0
- package/src/flow/nodes/async/flowReadonlyAsync.ts +81 -0
- package/src/flow/nodes/async/flowStateAsync.ts +116 -0
- package/src/flow/nodes/async/index.ts +5 -0
- package/src/flow/nodes/await/advanced/index.ts +5 -0
- package/src/{advanced → flow/nodes/await/advanced}/resource.ts +37 -3
- package/src/{advanced → flow/nodes/await/advanced}/resourceAsync.ts +35 -3
- package/src/{advanced → flow/nodes/await/advanced}/stream.ts +40 -2
- package/src/{advanced → flow/nodes/await/advanced}/streamAsync.ts +38 -3
- package/src/flow/nodes/await/flowConstantAwait.ts +154 -0
- package/src/flow/nodes/await/flowDerivationAwait.ts +154 -0
- package/src/flow/nodes/await/flowNodeAwait.ts +508 -0
- package/src/flow/nodes/await/flowReadonlyAwait.ts +89 -0
- package/src/flow/nodes/await/flowStateAwait.ts +130 -0
- package/src/flow/nodes/await/index.ts +5 -0
- package/src/flow/nodes/index.ts +3 -0
- package/src/flow/nodes/sync/flowConstant.ts +111 -0
- package/src/flow/nodes/sync/flowDerivation.ts +105 -0
- package/src/flow/nodes/sync/flowNode.ts +439 -0
- package/src/flow/nodes/sync/flowReadonly.ts +57 -0
- package/src/flow/nodes/sync/flowState.ts +101 -0
- package/src/flow/nodes/sync/index.ts +5 -0
- package/src/index.ts +2 -47
- package/src/solid/converters.ts +60 -199
- package/src/solid/index.ts +2 -8
- package/src/solid/primitives.ts +4 -0
- package/test/base/flowEffect.test.ts +108 -0
- package/test/base/flowGraph.test.ts +485 -0
- package/test/base/flowSignal.test.ts +372 -0
- package/test/collections/flowArray.asyncStates.test.ts +1553 -0
- package/test/collections/flowArray.scalars.test.ts +1129 -0
- package/test/collections/flowArray.states.test.ts +1365 -0
- package/test/collections/flowMap.asyncStates.test.ts +1105 -0
- package/test/collections/flowMap.scalars.test.ts +877 -0
- package/test/collections/flowMap.states.test.ts +1097 -0
- package/test/nodes/async/flowConstantAsync.test.ts +860 -0
- package/test/nodes/async/flowDerivationAsync.test.ts +1517 -0
- package/test/nodes/async/flowStateAsync.test.ts +1387 -0
- package/test/{resource.test.ts → nodes/await/advanced/resource.test.ts} +21 -19
- package/test/{resourceAsync.test.ts → nodes/await/advanced/resourceAsync.test.ts} +3 -1
- package/test/{stream.test.ts → nodes/await/advanced/stream.test.ts} +30 -28
- package/test/{streamAsync.test.ts → nodes/await/advanced/streamAsync.test.ts} +16 -14
- package/test/nodes/await/flowConstantAwait.test.ts +643 -0
- package/test/nodes/await/flowDerivationAwait.test.ts +1583 -0
- package/test/nodes/await/flowStateAwait.test.ts +999 -0
- package/test/nodes/mixed/derivation.test.ts +1527 -0
- package/test/nodes/sync/flowConstant.test.ts +620 -0
- package/test/nodes/sync/flowDerivation.test.ts +1373 -0
- package/test/nodes/sync/flowState.test.ts +945 -0
- package/test/solid/converters.test.ts +721 -0
- package/test/solid/primitives.test.ts +1031 -0
- package/tsconfig.json +2 -1
- package/vitest.config.ts +7 -1
- package/IMPLEMENTATION_GUIDE.md +0 -1578
- package/dist/types/advanced/array.d.ts.map +0 -1
- package/dist/types/advanced/index.d.ts +0 -9
- package/dist/types/advanced/index.d.ts.map +0 -1
- package/dist/types/advanced/map.d.ts +0 -166
- package/dist/types/advanced/map.d.ts.map +0 -1
- package/dist/types/advanced/resource.d.ts +0 -78
- package/dist/types/advanced/resource.d.ts.map +0 -1
- package/dist/types/advanced/resourceAsync.d.ts +0 -56
- package/dist/types/advanced/resourceAsync.d.ts.map +0 -1
- package/dist/types/advanced/stream.d.ts +0 -117
- package/dist/types/advanced/stream.d.ts.map +0 -1
- package/dist/types/advanced/streamAsync.d.ts +0 -97
- package/dist/types/advanced/streamAsync.d.ts.map +0 -1
- package/dist/types/basic/constant.d.ts +0 -60
- package/dist/types/basic/constant.d.ts.map +0 -1
- package/dist/types/basic/derivation.d.ts +0 -89
- package/dist/types/basic/derivation.d.ts.map +0 -1
- package/dist/types/basic/disposable.d.ts +0 -82
- package/dist/types/basic/disposable.d.ts.map +0 -1
- package/dist/types/basic/effect.d.ts +0 -67
- package/dist/types/basic/effect.d.ts.map +0 -1
- package/dist/types/basic/index.d.ts +0 -10
- package/dist/types/basic/index.d.ts.map +0 -1
- package/dist/types/basic/observable.d.ts +0 -83
- package/dist/types/basic/observable.d.ts.map +0 -1
- package/dist/types/basic/signal.d.ts +0 -69
- package/dist/types/basic/signal.d.ts.map +0 -1
- package/dist/types/basic/state.d.ts +0 -47
- package/dist/types/basic/state.d.ts.map +0 -1
- package/dist/types/basic/trackingContext.d.ts +0 -33
- package/dist/types/basic/trackingContext.d.ts.map +0 -1
- package/dist/types/creators.d.ts +0 -340
- package/dist/types/creators.d.ts.map +0 -1
- package/src/advanced/array.ts +0 -222
- package/src/advanced/index.ts +0 -12
- package/src/advanced/map.ts +0 -193
- package/src/basic/constant.ts +0 -97
- package/src/basic/derivation.ts +0 -147
- package/src/basic/disposable.ts +0 -86
- package/src/basic/effect.ts +0 -104
- package/src/basic/index.ts +0 -9
- package/src/basic/observable.ts +0 -109
- package/src/basic/signal.ts +0 -145
- package/src/basic/state.ts +0 -60
- package/src/basic/trackingContext.ts +0 -45
- package/src/creators.ts +0 -395
- package/test/array.test.ts +0 -600
- package/test/constant.test.ts +0 -44
- package/test/derivation.test.ts +0 -539
- package/test/effect.test.ts +0 -29
- package/test/map.test.ts +0 -240
- package/test/signal.test.ts +0 -72
- package/test/state.test.ts +0 -212
|
@@ -0,0 +1,1365 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from "vitest";
|
|
2
|
+
import { array, effect, type FlowState, state } from "#package";
|
|
3
|
+
|
|
4
|
+
describe("FlowArray", () => {
|
|
5
|
+
describe("unit", () => {
|
|
6
|
+
describe("initialization", () => {
|
|
7
|
+
it("should initialize with provided FlowState values", async () => {
|
|
8
|
+
const $array = array([state(1), state(2), state(3)]);
|
|
9
|
+
|
|
10
|
+
expect(
|
|
11
|
+
await Promise.all((await $array.pick()).map((state) => state.pick())),
|
|
12
|
+
).toEqual([1, 2, 3]);
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it("should initialize with empty array when no value provided", async () => {
|
|
16
|
+
const $array = array<FlowState<number>>();
|
|
17
|
+
|
|
18
|
+
expect(await $array.pick()).toEqual([]);
|
|
19
|
+
});
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
describe("disposal", () => {
|
|
23
|
+
it("should have disposed property set to false initially and true after disposal", () => {
|
|
24
|
+
const $array = array([state(1), state(2), state(3)]);
|
|
25
|
+
expect($array.disposed).toBe(false);
|
|
26
|
+
$array.dispose();
|
|
27
|
+
expect($array.disposed).toBe(true);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it("should throw error when disposed twice", () => {
|
|
31
|
+
const $array = array([state(1), state(2), state(3)]);
|
|
32
|
+
$array.dispose();
|
|
33
|
+
expect(() => $array.dispose()).toThrow(
|
|
34
|
+
"[PicoFlow] Primitive is disposed",
|
|
35
|
+
);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("should throw when pick is called after disposal", async () => {
|
|
39
|
+
const $array = array([state(1), state(2), state(3)]);
|
|
40
|
+
$array.dispose();
|
|
41
|
+
await expect($array.pick()).rejects.toThrow(
|
|
42
|
+
"[PicoFlow] Primitive is disposed",
|
|
43
|
+
);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it("should throw when set is called after disposal", async () => {
|
|
47
|
+
const $array = array([state(1), state(2), state(3)]);
|
|
48
|
+
$array.dispose();
|
|
49
|
+
await expect($array.set([state(1)])).rejects.toThrow(
|
|
50
|
+
"[PicoFlow] Primitive is disposed",
|
|
51
|
+
);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("should throw when setItem is called after disposal", async () => {
|
|
55
|
+
const $array = array([state(1), state(2), state(3)]);
|
|
56
|
+
$array.dispose();
|
|
57
|
+
await expect($array.update(0, state(3))).rejects.toThrow(
|
|
58
|
+
"[PicoFlow] Primitive is disposed",
|
|
59
|
+
);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it("should throw when push is called after disposal", async () => {
|
|
63
|
+
const $array = array([state(1), state(2), state(3)]);
|
|
64
|
+
$array.dispose();
|
|
65
|
+
await expect($array.push(state(3))).rejects.toThrow(
|
|
66
|
+
"[PicoFlow] Primitive is disposed",
|
|
67
|
+
);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it("should throw when pop is called after disposal", async () => {
|
|
71
|
+
const $array = array([state(1), state(2), state(3)]);
|
|
72
|
+
$array.dispose();
|
|
73
|
+
await expect($array.pop()).rejects.toThrow(
|
|
74
|
+
"[PicoFlow] Primitive is disposed",
|
|
75
|
+
);
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it("should throw when unshift is called after disposal", async () => {
|
|
79
|
+
const $array = array([state(1), state(2), state(3)]);
|
|
80
|
+
$array.dispose();
|
|
81
|
+
await expect($array.unshift(state(3))).rejects.toThrow(
|
|
82
|
+
"[PicoFlow] Primitive is disposed",
|
|
83
|
+
);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it("should throw when shift is called after disposal", async () => {
|
|
87
|
+
const $array = array([state(1), state(2), state(3)]);
|
|
88
|
+
$array.dispose();
|
|
89
|
+
await expect($array.shift()).rejects.toThrow(
|
|
90
|
+
"[PicoFlow] Primitive is disposed",
|
|
91
|
+
);
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it("should throw when splice is called after disposal", async () => {
|
|
95
|
+
const $array = array<FlowState<number>>([
|
|
96
|
+
state(0),
|
|
97
|
+
state(1),
|
|
98
|
+
state(2),
|
|
99
|
+
state(3),
|
|
100
|
+
state(4),
|
|
101
|
+
]);
|
|
102
|
+
$array.dispose();
|
|
103
|
+
await expect($array.splice(1, 2)).rejects.toThrow(
|
|
104
|
+
"[PicoFlow] Primitive is disposed",
|
|
105
|
+
);
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it("should throw when clear is called after disposal", async () => {
|
|
109
|
+
const $array = array<FlowState<number>>([
|
|
110
|
+
state(0),
|
|
111
|
+
state(1),
|
|
112
|
+
state(2),
|
|
113
|
+
state(3),
|
|
114
|
+
state(4),
|
|
115
|
+
]);
|
|
116
|
+
$array.dispose();
|
|
117
|
+
await expect($array.clear()).rejects.toThrow(
|
|
118
|
+
"[PicoFlow] Primitive is disposed",
|
|
119
|
+
);
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it("should dispose items when FlowArray is disposed", () => {
|
|
123
|
+
const $item1 = state(1);
|
|
124
|
+
const $item2 = state(2);
|
|
125
|
+
const $item3 = state(3);
|
|
126
|
+
const $array = array([$item1, $item2, $item3]);
|
|
127
|
+
|
|
128
|
+
expect($item1.disposed).toBe(false);
|
|
129
|
+
expect($item2.disposed).toBe(false);
|
|
130
|
+
expect($item3.disposed).toBe(false);
|
|
131
|
+
|
|
132
|
+
$array.dispose();
|
|
133
|
+
|
|
134
|
+
expect($item1.disposed).toBe(true);
|
|
135
|
+
expect($item2.disposed).toBe(true);
|
|
136
|
+
expect($item3.disposed).toBe(true);
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it("should dispose items with provided options when FlowArray is disposed", () => {
|
|
140
|
+
const $item1 = state(1);
|
|
141
|
+
const $item2 = state(2);
|
|
142
|
+
const $array = array([$item1, $item2]);
|
|
143
|
+
|
|
144
|
+
const $effect = effect((t) => {
|
|
145
|
+
$item1.get(t);
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
expect($effect.disposed).toBe(false);
|
|
149
|
+
|
|
150
|
+
$array.dispose({ self: true });
|
|
151
|
+
|
|
152
|
+
expect($item1.disposed).toBe(true);
|
|
153
|
+
expect($item2.disposed).toBe(true);
|
|
154
|
+
expect($effect.disposed).toBe(false);
|
|
155
|
+
});
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
describe("length", () => {
|
|
159
|
+
it("should return correct length", () => {
|
|
160
|
+
const $array = array([state(1), state(2), state(3)]);
|
|
161
|
+
expect($array.length).toBe(3);
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
it("should return zero for empty array", () => {
|
|
165
|
+
const $array = array<FlowState<number>>([]);
|
|
166
|
+
expect($array.length).toBe(0);
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
it("should throw when length is accessed after disposal", () => {
|
|
170
|
+
const $array = array([state(1), state(2), state(3)]);
|
|
171
|
+
$array.dispose();
|
|
172
|
+
expect(() => $array.length).toThrow("[PicoFlow] Primitive is disposed");
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
it("should update length when array is modified", async () => {
|
|
176
|
+
const $array = array<FlowState<number>>([]);
|
|
177
|
+
expect($array.length).toBe(0);
|
|
178
|
+
|
|
179
|
+
$array.push(state(1));
|
|
180
|
+
expect($array.length).toBe(1);
|
|
181
|
+
|
|
182
|
+
$array.push(state(2));
|
|
183
|
+
expect($array.length).toBe(2);
|
|
184
|
+
|
|
185
|
+
$array.pop();
|
|
186
|
+
expect($array.length).toBe(1);
|
|
187
|
+
|
|
188
|
+
$array.unshift(state(0));
|
|
189
|
+
expect($array.length).toBe(2);
|
|
190
|
+
|
|
191
|
+
$array.shift();
|
|
192
|
+
expect($array.length).toBe(1);
|
|
193
|
+
|
|
194
|
+
$array.clear();
|
|
195
|
+
expect($array.length).toBe(0);
|
|
196
|
+
});
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
describe("set", () => {
|
|
200
|
+
it("should update array when set is called with FlowStates", async () => {
|
|
201
|
+
const $array = array([state(0), state(1), state(2)]);
|
|
202
|
+
expect(
|
|
203
|
+
await Promise.all((await $array.pick()).map((state) => state.pick())),
|
|
204
|
+
).toEqual([0, 1, 2]);
|
|
205
|
+
|
|
206
|
+
await $array.set([state(1), state(2), state(3)]);
|
|
207
|
+
expect(
|
|
208
|
+
await Promise.all((await $array.pick()).map((state) => state.pick())),
|
|
209
|
+
).toEqual([1, 2, 3]);
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
it("should dispose items when set replaces array", () => {
|
|
213
|
+
const $oldItem1 = state(1);
|
|
214
|
+
const $oldItem2 = state(2);
|
|
215
|
+
const $array = array([$oldItem1, $oldItem2]);
|
|
216
|
+
|
|
217
|
+
expect($oldItem1.disposed).toBe(false);
|
|
218
|
+
expect($oldItem2.disposed).toBe(false);
|
|
219
|
+
|
|
220
|
+
$array.set([state(3), state(4)]);
|
|
221
|
+
|
|
222
|
+
expect($oldItem1.disposed).toBe(true);
|
|
223
|
+
expect($oldItem2.disposed).toBe(true);
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
it("should dispose items with self option when set replaces array", async () => {
|
|
227
|
+
const $oldItem = state(1);
|
|
228
|
+
const $array = array([$oldItem]);
|
|
229
|
+
|
|
230
|
+
const $effect = effect((t) => {
|
|
231
|
+
$array.get(t)[0].get(t);
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
expect($effect.disposed).toBe(false);
|
|
235
|
+
expect($oldItem.disposed).toBe(false);
|
|
236
|
+
|
|
237
|
+
await $array.set([state(2)]);
|
|
238
|
+
|
|
239
|
+
expect($oldItem.disposed).toBe(true);
|
|
240
|
+
expect($effect.disposed).toBe(false);
|
|
241
|
+
});
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
describe("update", () => {
|
|
245
|
+
it("should update item at specific index", async () => {
|
|
246
|
+
const $array = array([state(0), state(1), state(2)]);
|
|
247
|
+
expect(
|
|
248
|
+
await Promise.all((await $array.pick()).map((state) => state.pick())),
|
|
249
|
+
).toEqual([0, 1, 2]);
|
|
250
|
+
|
|
251
|
+
$array.update(0, state(1));
|
|
252
|
+
expect(
|
|
253
|
+
await Promise.all((await $array.pick()).map((state) => state.pick())),
|
|
254
|
+
).toEqual([1, 1, 2]);
|
|
255
|
+
|
|
256
|
+
$array.update(1, state(2));
|
|
257
|
+
expect(
|
|
258
|
+
await Promise.all((await $array.pick()).map((state) => state.pick())),
|
|
259
|
+
).toEqual([1, 2, 2]);
|
|
260
|
+
|
|
261
|
+
$array.update(2, state(3));
|
|
262
|
+
expect(
|
|
263
|
+
await Promise.all((await $array.pick()).map((state) => state.pick())),
|
|
264
|
+
).toEqual([1, 2, 3]);
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
it("should throw when update is called with negative index", async () => {
|
|
268
|
+
const $array = array([state(1), state(2), state(3)]);
|
|
269
|
+
await expect($array.update(-1, state(0))).rejects.toThrow(
|
|
270
|
+
"[PicoFlow] Index out of bounds",
|
|
271
|
+
);
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
it("should throw when update is called with index >= length", async () => {
|
|
275
|
+
const $array = array([state(1), state(2), state(3)]);
|
|
276
|
+
await expect($array.update(3, state(0))).rejects.toThrow(
|
|
277
|
+
"[PicoFlow] Index out of bounds",
|
|
278
|
+
);
|
|
279
|
+
await expect($array.update(10, state(0))).rejects.toThrow(
|
|
280
|
+
"[PicoFlow] Index out of bounds",
|
|
281
|
+
);
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
it("should handle boundary indices correctly", async () => {
|
|
285
|
+
const $array = array([state(0), state(1), state(2)]);
|
|
286
|
+
$array.update(0, state(10));
|
|
287
|
+
expect(
|
|
288
|
+
await Promise.all((await $array.pick()).map((state) => state.pick())),
|
|
289
|
+
).toEqual([10, 1, 2]);
|
|
290
|
+
|
|
291
|
+
$array.update(2, state(20));
|
|
292
|
+
expect(
|
|
293
|
+
await Promise.all((await $array.pick()).map((state) => state.pick())),
|
|
294
|
+
).toEqual([10, 1, 20]);
|
|
295
|
+
});
|
|
296
|
+
});
|
|
297
|
+
|
|
298
|
+
describe("push", () => {
|
|
299
|
+
it("should append FlowState to end of array", async () => {
|
|
300
|
+
const $array = array<FlowState<number>>([]);
|
|
301
|
+
expect(await $array.pick()).toEqual([]);
|
|
302
|
+
|
|
303
|
+
$array.push(state(0));
|
|
304
|
+
expect(
|
|
305
|
+
await Promise.all((await $array.pick()).map((state) => state.pick())),
|
|
306
|
+
).toEqual([0]);
|
|
307
|
+
|
|
308
|
+
$array.push(state(1));
|
|
309
|
+
expect(
|
|
310
|
+
await Promise.all((await $array.pick()).map((state) => state.pick())),
|
|
311
|
+
).toEqual([0, 1]);
|
|
312
|
+
|
|
313
|
+
$array.push(state(2));
|
|
314
|
+
expect(
|
|
315
|
+
await Promise.all((await $array.pick()).map((state) => state.pick())),
|
|
316
|
+
).toEqual([0, 1, 2]);
|
|
317
|
+
});
|
|
318
|
+
|
|
319
|
+
it("should handle operations on empty array", () => {
|
|
320
|
+
const $array = array<FlowState<number>>([]);
|
|
321
|
+
expect($array.length).toBe(0);
|
|
322
|
+
$array.push(state(1));
|
|
323
|
+
expect($array.length).toBe(1);
|
|
324
|
+
});
|
|
325
|
+
});
|
|
326
|
+
|
|
327
|
+
describe("pop", () => {
|
|
328
|
+
it("should remove last FlowState from array", async () => {
|
|
329
|
+
const $array = array([state(0), state(1), state(2)]);
|
|
330
|
+
expect(
|
|
331
|
+
await Promise.all((await $array.pick()).map((state) => state.pick())),
|
|
332
|
+
).toEqual([0, 1, 2]);
|
|
333
|
+
|
|
334
|
+
$array.pop();
|
|
335
|
+
expect(
|
|
336
|
+
await Promise.all((await $array.pick()).map((state) => state.pick())),
|
|
337
|
+
).toEqual([0, 1]);
|
|
338
|
+
|
|
339
|
+
$array.pop();
|
|
340
|
+
expect(
|
|
341
|
+
await Promise.all((await $array.pick()).map((state) => state.pick())),
|
|
342
|
+
).toEqual([0]);
|
|
343
|
+
|
|
344
|
+
$array.pop();
|
|
345
|
+
expect(
|
|
346
|
+
await Promise.all((await $array.pick()).map((state) => state.pick())),
|
|
347
|
+
).toEqual([]);
|
|
348
|
+
});
|
|
349
|
+
|
|
350
|
+
it("should dispose item when pop removes it", () => {
|
|
351
|
+
const $item = state(1);
|
|
352
|
+
const $array = array([$item]);
|
|
353
|
+
|
|
354
|
+
expect($item.disposed).toBe(false);
|
|
355
|
+
|
|
356
|
+
$array.pop();
|
|
357
|
+
|
|
358
|
+
expect($item.disposed).toBe(true);
|
|
359
|
+
});
|
|
360
|
+
|
|
361
|
+
it("should dispose item with self option when pop removes it", async () => {
|
|
362
|
+
const $item = state(1);
|
|
363
|
+
const $array = array([$item]);
|
|
364
|
+
|
|
365
|
+
const $effect = effect((t) => {
|
|
366
|
+
const item = $array.get(t)[0];
|
|
367
|
+
if (item) {
|
|
368
|
+
item.get(t);
|
|
369
|
+
}
|
|
370
|
+
});
|
|
371
|
+
|
|
372
|
+
expect($effect.disposed).toBe(false);
|
|
373
|
+
expect($item.disposed).toBe(false);
|
|
374
|
+
|
|
375
|
+
await $array.pop();
|
|
376
|
+
|
|
377
|
+
expect($item.disposed).toBe(true);
|
|
378
|
+
expect($effect.disposed).toBe(false);
|
|
379
|
+
});
|
|
380
|
+
|
|
381
|
+
it("should handle operations on empty array", () => {
|
|
382
|
+
const $array = array<FlowState<number>>([]);
|
|
383
|
+
expect($array.length).toBe(0);
|
|
384
|
+
$array.pop();
|
|
385
|
+
expect($array.length).toBe(0);
|
|
386
|
+
});
|
|
387
|
+
});
|
|
388
|
+
|
|
389
|
+
describe("unshift", () => {
|
|
390
|
+
it("should insert FlowState at beginning of array", async () => {
|
|
391
|
+
const $array = array<FlowState<number>>([]);
|
|
392
|
+
expect(await $array.pick()).toEqual([]);
|
|
393
|
+
|
|
394
|
+
$array.unshift(state(0));
|
|
395
|
+
expect(
|
|
396
|
+
await Promise.all((await $array.pick()).map((state) => state.pick())),
|
|
397
|
+
).toEqual([0]);
|
|
398
|
+
|
|
399
|
+
$array.unshift(state(1));
|
|
400
|
+
expect(
|
|
401
|
+
await Promise.all((await $array.pick()).map((state) => state.pick())),
|
|
402
|
+
).toEqual([1, 0]);
|
|
403
|
+
|
|
404
|
+
$array.unshift(state(2));
|
|
405
|
+
expect(
|
|
406
|
+
await Promise.all((await $array.pick()).map((state) => state.pick())),
|
|
407
|
+
).toEqual([2, 1, 0]);
|
|
408
|
+
});
|
|
409
|
+
});
|
|
410
|
+
|
|
411
|
+
describe("shift", () => {
|
|
412
|
+
it("should remove first FlowState from array", async () => {
|
|
413
|
+
const $array = array([state(0), state(1), state(2)]);
|
|
414
|
+
expect(
|
|
415
|
+
await Promise.all((await $array.pick()).map((state) => state.pick())),
|
|
416
|
+
).toEqual([0, 1, 2]);
|
|
417
|
+
|
|
418
|
+
$array.shift();
|
|
419
|
+
expect(
|
|
420
|
+
await Promise.all((await $array.pick()).map((state) => state.pick())),
|
|
421
|
+
).toEqual([1, 2]);
|
|
422
|
+
|
|
423
|
+
$array.shift();
|
|
424
|
+
expect(
|
|
425
|
+
await Promise.all((await $array.pick()).map((state) => state.pick())),
|
|
426
|
+
).toEqual([2]);
|
|
427
|
+
|
|
428
|
+
$array.shift();
|
|
429
|
+
expect(
|
|
430
|
+
await Promise.all((await $array.pick()).map((state) => state.pick())),
|
|
431
|
+
).toEqual([]);
|
|
432
|
+
});
|
|
433
|
+
|
|
434
|
+
it("should dispose item when shift removes it", () => {
|
|
435
|
+
const $item = state(1);
|
|
436
|
+
const $array = array([$item, state(2)]);
|
|
437
|
+
|
|
438
|
+
expect($item.disposed).toBe(false);
|
|
439
|
+
|
|
440
|
+
$array.shift();
|
|
441
|
+
|
|
442
|
+
expect($item.disposed).toBe(true);
|
|
443
|
+
});
|
|
444
|
+
|
|
445
|
+
it("should dispose item with self option when shift removes it", async () => {
|
|
446
|
+
const $item = state(1);
|
|
447
|
+
const $array = array([$item, state(2)]);
|
|
448
|
+
|
|
449
|
+
const $effect = effect((t) => {
|
|
450
|
+
$item.get(t);
|
|
451
|
+
});
|
|
452
|
+
|
|
453
|
+
expect($effect.disposed).toBe(false);
|
|
454
|
+
|
|
455
|
+
await $array.shift();
|
|
456
|
+
|
|
457
|
+
expect($item.disposed).toBe(true);
|
|
458
|
+
expect($effect.disposed).toBe(false);
|
|
459
|
+
});
|
|
460
|
+
|
|
461
|
+
it("should handle operations on empty array", () => {
|
|
462
|
+
const $array = array<FlowState<number>>([]);
|
|
463
|
+
expect($array.length).toBe(0);
|
|
464
|
+
$array.shift();
|
|
465
|
+
expect($array.length).toBe(0);
|
|
466
|
+
});
|
|
467
|
+
});
|
|
468
|
+
|
|
469
|
+
describe("splice", () => {
|
|
470
|
+
it("should remove FlowStates from array", async () => {
|
|
471
|
+
const $array = array<FlowState<number>>([
|
|
472
|
+
state(0),
|
|
473
|
+
state(1),
|
|
474
|
+
state(2),
|
|
475
|
+
state(3),
|
|
476
|
+
state(4),
|
|
477
|
+
]);
|
|
478
|
+
expect(
|
|
479
|
+
await Promise.all((await $array.pick()).map((state) => state.pick())),
|
|
480
|
+
).toEqual([0, 1, 2, 3, 4]);
|
|
481
|
+
|
|
482
|
+
$array.splice(1, 2);
|
|
483
|
+
expect(
|
|
484
|
+
await Promise.all((await $array.pick()).map((state) => state.pick())),
|
|
485
|
+
).toEqual([0, 3, 4]);
|
|
486
|
+
});
|
|
487
|
+
|
|
488
|
+
it("should insert FlowStates into array", async () => {
|
|
489
|
+
const $array = array<FlowState<number>>([
|
|
490
|
+
state(0),
|
|
491
|
+
state(1),
|
|
492
|
+
state(2),
|
|
493
|
+
state(3),
|
|
494
|
+
state(4),
|
|
495
|
+
]);
|
|
496
|
+
$array.splice(1, 0, state(10), state(11));
|
|
497
|
+
expect(
|
|
498
|
+
await Promise.all((await $array.pick()).map((state) => state.pick())),
|
|
499
|
+
).toEqual([0, 10, 11, 1, 2, 3, 4]);
|
|
500
|
+
});
|
|
501
|
+
|
|
502
|
+
it("should replace FlowStates in array", async () => {
|
|
503
|
+
const $array = array<FlowState<number>>([
|
|
504
|
+
state(0),
|
|
505
|
+
state(1),
|
|
506
|
+
state(2),
|
|
507
|
+
state(3),
|
|
508
|
+
state(4),
|
|
509
|
+
]);
|
|
510
|
+
$array.splice(1, 2, state(10), state(11));
|
|
511
|
+
expect(
|
|
512
|
+
await Promise.all((await $array.pick()).map((state) => state.pick())),
|
|
513
|
+
).toEqual([0, 10, 11, 3, 4]);
|
|
514
|
+
});
|
|
515
|
+
|
|
516
|
+
it("should dispose items when splice removes them", () => {
|
|
517
|
+
const $item1 = state(1);
|
|
518
|
+
const $item2 = state(2);
|
|
519
|
+
const $array = array([state(0), $item1, $item2, state(3)]);
|
|
520
|
+
|
|
521
|
+
expect($item1.disposed).toBe(false);
|
|
522
|
+
expect($item2.disposed).toBe(false);
|
|
523
|
+
|
|
524
|
+
$array.splice(1, 2);
|
|
525
|
+
|
|
526
|
+
expect($item1.disposed).toBe(true);
|
|
527
|
+
expect($item2.disposed).toBe(true);
|
|
528
|
+
});
|
|
529
|
+
|
|
530
|
+
it("should dispose items with self option when splice removes them", async () => {
|
|
531
|
+
const $item = state(1);
|
|
532
|
+
const $array = array([state(0), $item, state(2)]);
|
|
533
|
+
|
|
534
|
+
const $effect = effect((t) => {
|
|
535
|
+
$item.get(t);
|
|
536
|
+
});
|
|
537
|
+
|
|
538
|
+
expect($effect.disposed).toBe(false);
|
|
539
|
+
|
|
540
|
+
await $array.splice(1, 1);
|
|
541
|
+
|
|
542
|
+
expect($item.disposed).toBe(true);
|
|
543
|
+
expect($effect.disposed).toBe(false);
|
|
544
|
+
});
|
|
545
|
+
});
|
|
546
|
+
|
|
547
|
+
describe("clear", () => {
|
|
548
|
+
it("should remove all FlowStates from array", async () => {
|
|
549
|
+
const $array = array<FlowState<number>>([
|
|
550
|
+
state(0),
|
|
551
|
+
state(1),
|
|
552
|
+
state(2),
|
|
553
|
+
state(3),
|
|
554
|
+
state(4),
|
|
555
|
+
]);
|
|
556
|
+
expect(
|
|
557
|
+
await Promise.all((await $array.pick()).map((state) => state.pick())),
|
|
558
|
+
).toEqual([0, 1, 2, 3, 4]);
|
|
559
|
+
|
|
560
|
+
$array.clear();
|
|
561
|
+
expect(
|
|
562
|
+
await Promise.all((await $array.pick()).map((state) => state.pick())),
|
|
563
|
+
).toEqual([]);
|
|
564
|
+
});
|
|
565
|
+
|
|
566
|
+
it("should dispose all items when clear is called", () => {
|
|
567
|
+
const $item1 = state(1);
|
|
568
|
+
const $item2 = state(2);
|
|
569
|
+
const $item3 = state(3);
|
|
570
|
+
const $array = array([$item1, $item2, $item3]);
|
|
571
|
+
|
|
572
|
+
expect($item1.disposed).toBe(false);
|
|
573
|
+
expect($item2.disposed).toBe(false);
|
|
574
|
+
expect($item3.disposed).toBe(false);
|
|
575
|
+
|
|
576
|
+
$array.clear();
|
|
577
|
+
|
|
578
|
+
expect($item1.disposed).toBe(true);
|
|
579
|
+
expect($item2.disposed).toBe(true);
|
|
580
|
+
expect($item3.disposed).toBe(true);
|
|
581
|
+
});
|
|
582
|
+
|
|
583
|
+
it("should dispose items with self option when clear is called", async () => {
|
|
584
|
+
const $item = state(1);
|
|
585
|
+
const $array = array([$item, state(2)]);
|
|
586
|
+
|
|
587
|
+
const $effect = effect((t) => {
|
|
588
|
+
$item.get(t);
|
|
589
|
+
});
|
|
590
|
+
|
|
591
|
+
expect($effect.disposed).toBe(false);
|
|
592
|
+
|
|
593
|
+
await $array.clear();
|
|
594
|
+
|
|
595
|
+
expect($item.disposed).toBe(true);
|
|
596
|
+
expect($effect.disposed).toBe(false);
|
|
597
|
+
});
|
|
598
|
+
});
|
|
599
|
+
|
|
600
|
+
describe("$lastAction", () => {
|
|
601
|
+
describe("set action", () => {
|
|
602
|
+
it("should initialize $lastAction with set action on creation", async () => {
|
|
603
|
+
const $array = array([state(1), state(2), state(3)]);
|
|
604
|
+
const action = await $array.$lastAction.pick();
|
|
605
|
+
expect(action.type).toBe("set");
|
|
606
|
+
if (action.type === "set") {
|
|
607
|
+
expect(action.items).toHaveLength(3);
|
|
608
|
+
expect(action.items[0]).toBeInstanceOf(Object);
|
|
609
|
+
expect(action.items[1]).toBeInstanceOf(Object);
|
|
610
|
+
expect(action.items[2]).toBeInstanceOf(Object);
|
|
611
|
+
}
|
|
612
|
+
});
|
|
613
|
+
|
|
614
|
+
it("should update $lastAction with set action when set is called", async () => {
|
|
615
|
+
const $array = array([state(0), state(1), state(2)]);
|
|
616
|
+
await $array.set([state(1), state(2), state(3)]);
|
|
617
|
+
const action = await $array.$lastAction.pick();
|
|
618
|
+
expect(action.type).toBe("set");
|
|
619
|
+
if (action.type === "set") {
|
|
620
|
+
expect(action.items).toHaveLength(3);
|
|
621
|
+
}
|
|
622
|
+
});
|
|
623
|
+
|
|
624
|
+
it("should have correct structure for set action (items array)", async () => {
|
|
625
|
+
const $array = array([state(1), state(2), state(3)]);
|
|
626
|
+
const action = await $array.$lastAction.pick();
|
|
627
|
+
expect(action).toHaveProperty("type", "set");
|
|
628
|
+
if (action.type === "set") {
|
|
629
|
+
expect(action).toHaveProperty("items");
|
|
630
|
+
expect(Array.isArray(action.items)).toBe(true);
|
|
631
|
+
expect(action.items).toHaveLength(3);
|
|
632
|
+
}
|
|
633
|
+
});
|
|
634
|
+
});
|
|
635
|
+
|
|
636
|
+
describe("update action", () => {
|
|
637
|
+
it("should update $lastAction with setItem action when setItem is called", async () => {
|
|
638
|
+
const $array = array([state(0), state(1), state(2)]);
|
|
639
|
+
const $newItem = state(10);
|
|
640
|
+
$array.update(1, $newItem);
|
|
641
|
+
const action = await $array.$lastAction.pick();
|
|
642
|
+
expect(action.type).toBe("update");
|
|
643
|
+
if (action.type === "update") {
|
|
644
|
+
expect(action.index).toBe(1);
|
|
645
|
+
expect(action.item).toBe($newItem);
|
|
646
|
+
}
|
|
647
|
+
});
|
|
648
|
+
|
|
649
|
+
it("should have correct structure for update action (index and item)", async () => {
|
|
650
|
+
const $array = array([state(1), state(2), state(3)]);
|
|
651
|
+
const $newItem = state(10);
|
|
652
|
+
$array.update(0, $newItem);
|
|
653
|
+
const action = await $array.$lastAction.pick();
|
|
654
|
+
expect(action).toHaveProperty("type", "update");
|
|
655
|
+
if (action.type === "update") {
|
|
656
|
+
expect(action).toHaveProperty("index");
|
|
657
|
+
expect(action).toHaveProperty("item");
|
|
658
|
+
expect(action.index).toBe(0);
|
|
659
|
+
expect(action.item).toBe($newItem);
|
|
660
|
+
}
|
|
661
|
+
});
|
|
662
|
+
});
|
|
663
|
+
|
|
664
|
+
describe("push action", () => {
|
|
665
|
+
it("should update $lastAction with push action when push is called", async () => {
|
|
666
|
+
const $array = array<FlowState<number>>([]);
|
|
667
|
+
const $newItem = state(42);
|
|
668
|
+
$array.push($newItem);
|
|
669
|
+
const action = await $array.$lastAction.pick();
|
|
670
|
+
expect(action.type).toBe("push");
|
|
671
|
+
if (action.type === "push") {
|
|
672
|
+
expect(action.item).toBe($newItem);
|
|
673
|
+
}
|
|
674
|
+
});
|
|
675
|
+
|
|
676
|
+
it("should have correct structure for push action (item)", async () => {
|
|
677
|
+
const $array = array<FlowState<number>>([]);
|
|
678
|
+
const $newItem = state(100);
|
|
679
|
+
$array.push($newItem);
|
|
680
|
+
const action = await $array.$lastAction.pick();
|
|
681
|
+
expect(action).toHaveProperty("type", "push");
|
|
682
|
+
if (action.type === "push") {
|
|
683
|
+
expect(action).toHaveProperty("item");
|
|
684
|
+
expect(action.item).toBe($newItem);
|
|
685
|
+
}
|
|
686
|
+
});
|
|
687
|
+
});
|
|
688
|
+
|
|
689
|
+
describe("pop action", () => {
|
|
690
|
+
it("should update $lastAction with pop action when pop is called", async () => {
|
|
691
|
+
const $array = array([state(1), state(2), state(3)]);
|
|
692
|
+
$array.pop();
|
|
693
|
+
const action = await $array.$lastAction.pick();
|
|
694
|
+
expect(action).toEqual({
|
|
695
|
+
type: "pop",
|
|
696
|
+
});
|
|
697
|
+
});
|
|
698
|
+
|
|
699
|
+
it("should have correct structure for pop action (no additional fields)", async () => {
|
|
700
|
+
const $array = array([state(1), state(2), state(3)]);
|
|
701
|
+
$array.pop();
|
|
702
|
+
const action = await $array.$lastAction.pick();
|
|
703
|
+
expect(action).toHaveProperty("type", "pop");
|
|
704
|
+
if (action.type === "pop") {
|
|
705
|
+
expect(action).not.toHaveProperty("item");
|
|
706
|
+
expect(action).not.toHaveProperty("index");
|
|
707
|
+
expect(action).not.toHaveProperty("items");
|
|
708
|
+
}
|
|
709
|
+
});
|
|
710
|
+
});
|
|
711
|
+
|
|
712
|
+
describe("unshift action", () => {
|
|
713
|
+
it("should update $lastAction with unshift action when unshift is called", async () => {
|
|
714
|
+
const $array = array<FlowState<number>>([]);
|
|
715
|
+
const $newItem = state(42);
|
|
716
|
+
$array.unshift($newItem);
|
|
717
|
+
const action = await $array.$lastAction.pick();
|
|
718
|
+
expect(action.type).toBe("unshift");
|
|
719
|
+
if (action.type === "unshift") {
|
|
720
|
+
expect(action.item).toBe($newItem);
|
|
721
|
+
}
|
|
722
|
+
});
|
|
723
|
+
|
|
724
|
+
it("should have correct structure for unshift action (item)", async () => {
|
|
725
|
+
const $array = array<FlowState<number>>([]);
|
|
726
|
+
const $newItem = state(200);
|
|
727
|
+
$array.unshift($newItem);
|
|
728
|
+
const action = await $array.$lastAction.pick();
|
|
729
|
+
expect(action).toHaveProperty("type", "unshift");
|
|
730
|
+
if (action.type === "unshift") {
|
|
731
|
+
expect(action).toHaveProperty("item");
|
|
732
|
+
expect(action.item).toBe($newItem);
|
|
733
|
+
}
|
|
734
|
+
});
|
|
735
|
+
});
|
|
736
|
+
|
|
737
|
+
describe("shift action", () => {
|
|
738
|
+
it("should update $lastAction with shift action when shift is called", async () => {
|
|
739
|
+
const $array = array([state(1), state(2), state(3)]);
|
|
740
|
+
$array.shift();
|
|
741
|
+
const action = await $array.$lastAction.pick();
|
|
742
|
+
expect(action).toEqual({
|
|
743
|
+
type: "shift",
|
|
744
|
+
});
|
|
745
|
+
});
|
|
746
|
+
|
|
747
|
+
it("should have correct structure for shift action (no additional fields)", async () => {
|
|
748
|
+
const $array = array([state(1), state(2), state(3)]);
|
|
749
|
+
$array.shift();
|
|
750
|
+
const action = await $array.$lastAction.pick();
|
|
751
|
+
expect(action).toHaveProperty("type", "shift");
|
|
752
|
+
if (action.type === "shift") {
|
|
753
|
+
expect(action).not.toHaveProperty("item");
|
|
754
|
+
expect(action).not.toHaveProperty("index");
|
|
755
|
+
expect(action).not.toHaveProperty("items");
|
|
756
|
+
}
|
|
757
|
+
});
|
|
758
|
+
});
|
|
759
|
+
|
|
760
|
+
describe("splice action", () => {
|
|
761
|
+
it("should update $lastAction with splice action when splice is called", async () => {
|
|
762
|
+
const $array = array<FlowState<number>>([
|
|
763
|
+
state(0),
|
|
764
|
+
state(1),
|
|
765
|
+
state(2),
|
|
766
|
+
state(3),
|
|
767
|
+
state(4),
|
|
768
|
+
]);
|
|
769
|
+
const $newItem1 = state(10);
|
|
770
|
+
const $newItem2 = state(11);
|
|
771
|
+
$array.splice(1, 2, $newItem1, $newItem2);
|
|
772
|
+
const action = await $array.$lastAction.pick();
|
|
773
|
+
expect(action.type).toBe("splice");
|
|
774
|
+
if (action.type === "splice") {
|
|
775
|
+
expect(action.start).toBe(1);
|
|
776
|
+
expect(action.deleteCount).toBe(2);
|
|
777
|
+
expect(action.items).toHaveLength(2);
|
|
778
|
+
expect(action.items[0]).toBe($newItem1);
|
|
779
|
+
expect(action.items[1]).toBe($newItem2);
|
|
780
|
+
}
|
|
781
|
+
});
|
|
782
|
+
|
|
783
|
+
it("should have correct structure for splice action (start, deleteCount, items)", async () => {
|
|
784
|
+
const $array = array<FlowState<number>>([
|
|
785
|
+
state(0),
|
|
786
|
+
state(1),
|
|
787
|
+
state(2),
|
|
788
|
+
]);
|
|
789
|
+
const $newItem = state(100);
|
|
790
|
+
$array.splice(1, 1, $newItem);
|
|
791
|
+
const action = await $array.$lastAction.pick();
|
|
792
|
+
expect(action).toHaveProperty("type", "splice");
|
|
793
|
+
if (action.type === "splice") {
|
|
794
|
+
expect(action).toHaveProperty("start");
|
|
795
|
+
expect(action).toHaveProperty("deleteCount");
|
|
796
|
+
expect(action).toHaveProperty("items");
|
|
797
|
+
expect(action.start).toBe(1);
|
|
798
|
+
expect(action.deleteCount).toBe(1);
|
|
799
|
+
expect(Array.isArray(action.items)).toBe(true);
|
|
800
|
+
expect(action.items).toHaveLength(1);
|
|
801
|
+
expect(action.items[0]).toBe($newItem);
|
|
802
|
+
}
|
|
803
|
+
});
|
|
804
|
+
});
|
|
805
|
+
|
|
806
|
+
describe("clear action", () => {
|
|
807
|
+
it("should update $lastAction with clear action when clear is called", async () => {
|
|
808
|
+
const $array = array<FlowState<number>>([
|
|
809
|
+
state(0),
|
|
810
|
+
state(1),
|
|
811
|
+
state(2),
|
|
812
|
+
state(3),
|
|
813
|
+
state(4),
|
|
814
|
+
]);
|
|
815
|
+
$array.clear();
|
|
816
|
+
const action = await $array.$lastAction.pick();
|
|
817
|
+
expect(action).toEqual({
|
|
818
|
+
type: "clear",
|
|
819
|
+
});
|
|
820
|
+
});
|
|
821
|
+
|
|
822
|
+
it("should have correct structure for clear action (no additional fields)", async () => {
|
|
823
|
+
const $array = array<FlowState<number>>([
|
|
824
|
+
state(1),
|
|
825
|
+
state(2),
|
|
826
|
+
state(3),
|
|
827
|
+
]);
|
|
828
|
+
$array.clear();
|
|
829
|
+
const action = await $array.$lastAction.pick();
|
|
830
|
+
expect(action).toHaveProperty("type", "clear");
|
|
831
|
+
if (action.type === "clear") {
|
|
832
|
+
expect(action).not.toHaveProperty("item");
|
|
833
|
+
expect(action).not.toHaveProperty("index");
|
|
834
|
+
expect(action).not.toHaveProperty("items");
|
|
835
|
+
}
|
|
836
|
+
});
|
|
837
|
+
});
|
|
838
|
+
});
|
|
839
|
+
|
|
840
|
+
describe("edge cases", () => {
|
|
841
|
+
it("should handle multiple rapid operations", async () => {
|
|
842
|
+
const $array = array<FlowState<number>>([]);
|
|
843
|
+
$array.push(state(1));
|
|
844
|
+
$array.push(state(2));
|
|
845
|
+
$array.push(state(3));
|
|
846
|
+
$array.pop();
|
|
847
|
+
$array.shift();
|
|
848
|
+
$array.unshift(state(0));
|
|
849
|
+
expect(
|
|
850
|
+
await Promise.all((await $array.pick()).map((state) => state.pick())),
|
|
851
|
+
).toEqual([0, 2]);
|
|
852
|
+
});
|
|
853
|
+
|
|
854
|
+
it("should handle boundary indices correctly", async () => {
|
|
855
|
+
const $array = array([state(0), state(1), state(2)]);
|
|
856
|
+
$array.update(0, state(10));
|
|
857
|
+
expect(
|
|
858
|
+
await Promise.all((await $array.pick()).map((state) => state.pick())),
|
|
859
|
+
).toEqual([10, 1, 2]);
|
|
860
|
+
|
|
861
|
+
$array.update(2, state(20));
|
|
862
|
+
expect(
|
|
863
|
+
await Promise.all((await $array.pick()).map((state) => state.pick())),
|
|
864
|
+
).toEqual([10, 1, 20]);
|
|
865
|
+
});
|
|
866
|
+
});
|
|
867
|
+
});
|
|
868
|
+
|
|
869
|
+
describe("integration", () => {
|
|
870
|
+
describe("with effects", () => {
|
|
871
|
+
describe("reactivity", () => {
|
|
872
|
+
it("should call effect when initialized", async () => {
|
|
873
|
+
const $array = array([state(1), state(2), state(3)]);
|
|
874
|
+
const effectFn = vi.fn();
|
|
875
|
+
effect(async (t) =>
|
|
876
|
+
effectFn(
|
|
877
|
+
await Promise.all(
|
|
878
|
+
(await $array.get(t)).map((state) => state.get(t)),
|
|
879
|
+
),
|
|
880
|
+
),
|
|
881
|
+
);
|
|
882
|
+
|
|
883
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
|
|
884
|
+
expect(effectFn).toHaveBeenLastCalledWith([1, 2, 3]);
|
|
885
|
+
});
|
|
886
|
+
|
|
887
|
+
it("should call effect when updated with set", async () => {
|
|
888
|
+
const $array = array<FlowState<number>>([
|
|
889
|
+
state(0),
|
|
890
|
+
state(1),
|
|
891
|
+
state(2),
|
|
892
|
+
]);
|
|
893
|
+
const effectFn = vi.fn();
|
|
894
|
+
effect(async (t) =>
|
|
895
|
+
effectFn(
|
|
896
|
+
await Promise.all(
|
|
897
|
+
(await $array.get(t)).map((state) => state.get(t)),
|
|
898
|
+
),
|
|
899
|
+
),
|
|
900
|
+
);
|
|
901
|
+
|
|
902
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
|
|
903
|
+
expect(effectFn).toHaveBeenLastCalledWith([0, 1, 2]);
|
|
904
|
+
|
|
905
|
+
await $array.set([state(1), state(2), state(3)]);
|
|
906
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
|
|
907
|
+
expect(effectFn).toHaveBeenLastCalledWith([1, 2, 3]);
|
|
908
|
+
});
|
|
909
|
+
|
|
910
|
+
it("should call effect when updated with push/pop", async () => {
|
|
911
|
+
const $array = array<FlowState<number>>();
|
|
912
|
+
const effectFn = vi.fn();
|
|
913
|
+
effect(async (t) =>
|
|
914
|
+
effectFn(
|
|
915
|
+
await Promise.all(
|
|
916
|
+
(await $array.get(t)).map((state) => state.get(t)),
|
|
917
|
+
),
|
|
918
|
+
),
|
|
919
|
+
);
|
|
920
|
+
|
|
921
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
|
|
922
|
+
expect(effectFn).toHaveBeenLastCalledWith([]);
|
|
923
|
+
|
|
924
|
+
$array.push(state(0));
|
|
925
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
|
|
926
|
+
expect(effectFn).toHaveBeenLastCalledWith([0]);
|
|
927
|
+
|
|
928
|
+
$array.push(state(1));
|
|
929
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(3));
|
|
930
|
+
expect(effectFn).toHaveBeenLastCalledWith([0, 1]);
|
|
931
|
+
|
|
932
|
+
$array.push(state(2));
|
|
933
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(4));
|
|
934
|
+
expect(effectFn).toHaveBeenLastCalledWith([0, 1, 2]);
|
|
935
|
+
|
|
936
|
+
$array.pop();
|
|
937
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(5));
|
|
938
|
+
expect(effectFn).toHaveBeenLastCalledWith([0, 1]);
|
|
939
|
+
|
|
940
|
+
$array.pop();
|
|
941
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(6));
|
|
942
|
+
expect(effectFn).toHaveBeenLastCalledWith([0]);
|
|
943
|
+
|
|
944
|
+
$array.pop();
|
|
945
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(7));
|
|
946
|
+
expect(effectFn).toHaveBeenLastCalledWith([]);
|
|
947
|
+
});
|
|
948
|
+
|
|
949
|
+
it("should call effect when updated with unshift/shift", async () => {
|
|
950
|
+
const $array = array<FlowState<number>>();
|
|
951
|
+
const effectFn = vi.fn();
|
|
952
|
+
effect(async (t) =>
|
|
953
|
+
effectFn(
|
|
954
|
+
await Promise.all(
|
|
955
|
+
(await $array.get(t)).map((state) => state.get(t)),
|
|
956
|
+
),
|
|
957
|
+
),
|
|
958
|
+
);
|
|
959
|
+
|
|
960
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
|
|
961
|
+
expect(effectFn).toHaveBeenLastCalledWith([]);
|
|
962
|
+
|
|
963
|
+
$array.unshift(state(0));
|
|
964
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
|
|
965
|
+
expect(effectFn).toHaveBeenLastCalledWith([0]);
|
|
966
|
+
|
|
967
|
+
$array.unshift(state(1));
|
|
968
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(3));
|
|
969
|
+
expect(effectFn).toHaveBeenLastCalledWith([1, 0]);
|
|
970
|
+
|
|
971
|
+
$array.unshift(state(2));
|
|
972
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(4));
|
|
973
|
+
expect(effectFn).toHaveBeenLastCalledWith([2, 1, 0]);
|
|
974
|
+
|
|
975
|
+
$array.shift();
|
|
976
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(5));
|
|
977
|
+
expect(effectFn).toHaveBeenLastCalledWith([1, 0]);
|
|
978
|
+
|
|
979
|
+
$array.shift();
|
|
980
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(6));
|
|
981
|
+
expect(effectFn).toHaveBeenLastCalledWith([0]);
|
|
982
|
+
|
|
983
|
+
$array.shift();
|
|
984
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(7));
|
|
985
|
+
expect(effectFn).toHaveBeenLastCalledWith([]);
|
|
986
|
+
});
|
|
987
|
+
|
|
988
|
+
it("should call effect when updated with splice", async () => {
|
|
989
|
+
const $array = array<FlowState<number>>([
|
|
990
|
+
state(0),
|
|
991
|
+
state(1),
|
|
992
|
+
state(2),
|
|
993
|
+
state(3),
|
|
994
|
+
state(4),
|
|
995
|
+
]);
|
|
996
|
+
const effectFn = vi.fn();
|
|
997
|
+
effect(async (t) =>
|
|
998
|
+
effectFn($array.get(t).map((state) => state.get(t))),
|
|
999
|
+
);
|
|
1000
|
+
|
|
1001
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
|
|
1002
|
+
expect(effectFn).toHaveBeenLastCalledWith([0, 1, 2, 3, 4]);
|
|
1003
|
+
|
|
1004
|
+
$array.splice(1, 2);
|
|
1005
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
|
|
1006
|
+
expect(effectFn).toHaveBeenLastCalledWith([0, 3, 4]);
|
|
1007
|
+
|
|
1008
|
+
$array.splice(1, 0, state(1), state(2));
|
|
1009
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(3));
|
|
1010
|
+
expect(effectFn).toHaveBeenLastCalledWith([0, 1, 2, 3, 4]);
|
|
1011
|
+
});
|
|
1012
|
+
|
|
1013
|
+
it("should call effect when updated with clear", async () => {
|
|
1014
|
+
const $array = array<FlowState<number>>([
|
|
1015
|
+
state(0),
|
|
1016
|
+
state(1),
|
|
1017
|
+
state(2),
|
|
1018
|
+
state(3),
|
|
1019
|
+
state(4),
|
|
1020
|
+
]);
|
|
1021
|
+
const effectFn = vi.fn();
|
|
1022
|
+
effect(async (t) =>
|
|
1023
|
+
effectFn($array.get(t).map((state) => state.get(t))),
|
|
1024
|
+
);
|
|
1025
|
+
|
|
1026
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
|
|
1027
|
+
expect(effectFn).toHaveBeenLastCalledWith([0, 1, 2, 3, 4]);
|
|
1028
|
+
|
|
1029
|
+
$array.clear();
|
|
1030
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
|
|
1031
|
+
expect(effectFn).toHaveBeenLastCalledWith([]);
|
|
1032
|
+
});
|
|
1033
|
+
});
|
|
1034
|
+
|
|
1035
|
+
describe("item reactivity", () => {
|
|
1036
|
+
it("should call effect when item is updated", async () => {
|
|
1037
|
+
const $array = array<FlowState<number>>([
|
|
1038
|
+
state(0),
|
|
1039
|
+
state(1),
|
|
1040
|
+
state(2),
|
|
1041
|
+
]);
|
|
1042
|
+
const effectFn = vi.fn();
|
|
1043
|
+
const effect0Fn = vi.fn();
|
|
1044
|
+
const effect1Fn = vi.fn();
|
|
1045
|
+
const effect2Fn = vi.fn();
|
|
1046
|
+
|
|
1047
|
+
effect(async (t) =>
|
|
1048
|
+
effectFn($array.get(t).map((state) => state.get(t))),
|
|
1049
|
+
);
|
|
1050
|
+
effect(async (t) => effect0Fn(await (await $array.get(t))[0].get(t)));
|
|
1051
|
+
effect(async (t) => effect1Fn(await (await $array.get(t))[1].get(t)));
|
|
1052
|
+
effect(async (t) => effect2Fn(await (await $array.get(t))[2].get(t)));
|
|
1053
|
+
|
|
1054
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
|
|
1055
|
+
expect(effectFn).toHaveBeenLastCalledWith([0, 1, 2]);
|
|
1056
|
+
await vi.waitFor(() => expect(effect0Fn).toHaveBeenCalledTimes(1));
|
|
1057
|
+
expect(effect0Fn).toHaveBeenLastCalledWith(0);
|
|
1058
|
+
await vi.waitFor(() => expect(effect1Fn).toHaveBeenCalledTimes(1));
|
|
1059
|
+
expect(effect1Fn).toHaveBeenLastCalledWith(1);
|
|
1060
|
+
await vi.waitFor(() => expect(effect2Fn).toHaveBeenCalledTimes(1));
|
|
1061
|
+
expect(effect2Fn).toHaveBeenLastCalledWith(2);
|
|
1062
|
+
|
|
1063
|
+
(await $array.pick())[0].set(1);
|
|
1064
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
|
|
1065
|
+
expect(effectFn).toHaveBeenLastCalledWith([1, 1, 2]);
|
|
1066
|
+
await vi.waitFor(() => expect(effect0Fn).toHaveBeenCalledTimes(2));
|
|
1067
|
+
expect(effect0Fn).toHaveBeenLastCalledWith(1);
|
|
1068
|
+
await vi.waitFor(() => expect(effect1Fn).toHaveBeenCalledTimes(1));
|
|
1069
|
+
expect(effect1Fn).toHaveBeenLastCalledWith(1);
|
|
1070
|
+
await vi.waitFor(() => expect(effect2Fn).toHaveBeenCalledTimes(1));
|
|
1071
|
+
expect(effect2Fn).toHaveBeenLastCalledWith(2);
|
|
1072
|
+
|
|
1073
|
+
(await $array.pick())[1].set(2);
|
|
1074
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(3));
|
|
1075
|
+
expect(effectFn).toHaveBeenLastCalledWith([1, 2, 2]);
|
|
1076
|
+
await vi.waitFor(() => expect(effect0Fn).toHaveBeenCalledTimes(2));
|
|
1077
|
+
expect(effect0Fn).toHaveBeenLastCalledWith(1);
|
|
1078
|
+
await vi.waitFor(() => expect(effect1Fn).toHaveBeenCalledTimes(2));
|
|
1079
|
+
expect(effect1Fn).toHaveBeenLastCalledWith(2);
|
|
1080
|
+
await vi.waitFor(() => expect(effect2Fn).toHaveBeenCalledTimes(1));
|
|
1081
|
+
expect(effect2Fn).toHaveBeenLastCalledWith(2);
|
|
1082
|
+
|
|
1083
|
+
(await $array.pick())[2].set(3);
|
|
1084
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(4));
|
|
1085
|
+
expect(effectFn).toHaveBeenLastCalledWith([1, 2, 3]);
|
|
1086
|
+
await vi.waitFor(() => expect(effect0Fn).toHaveBeenCalledTimes(2));
|
|
1087
|
+
expect(effect0Fn).toHaveBeenLastCalledWith(1);
|
|
1088
|
+
await vi.waitFor(() => expect(effect1Fn).toHaveBeenCalledTimes(2));
|
|
1089
|
+
expect(effect1Fn).toHaveBeenLastCalledWith(2);
|
|
1090
|
+
await vi.waitFor(() => expect(effect2Fn).toHaveBeenCalledTimes(2));
|
|
1091
|
+
expect(effect2Fn).toHaveBeenLastCalledWith(3);
|
|
1092
|
+
});
|
|
1093
|
+
|
|
1094
|
+
it("should call effect when item is replaced", async () => {
|
|
1095
|
+
const $array = array<FlowState<number>>([
|
|
1096
|
+
state(0),
|
|
1097
|
+
state(1),
|
|
1098
|
+
state(2),
|
|
1099
|
+
]);
|
|
1100
|
+
const effectFn = vi.fn();
|
|
1101
|
+
const effect0Fn = vi.fn();
|
|
1102
|
+
|
|
1103
|
+
effect(async (t) =>
|
|
1104
|
+
effectFn($array.get(t).map((state) => state.get(t))),
|
|
1105
|
+
);
|
|
1106
|
+
effect(async (t) => effect0Fn($array.get(t)[0].get(t)));
|
|
1107
|
+
|
|
1108
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
|
|
1109
|
+
expect(effectFn).toHaveBeenLastCalledWith([0, 1, 2]);
|
|
1110
|
+
await vi.waitFor(() => expect(effect0Fn).toHaveBeenCalledTimes(1));
|
|
1111
|
+
expect(effect0Fn).toHaveBeenLastCalledWith(0);
|
|
1112
|
+
|
|
1113
|
+
$array.update(0, state(3));
|
|
1114
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
|
|
1115
|
+
expect(effectFn).toHaveBeenLastCalledWith([3, 1, 2]);
|
|
1116
|
+
await vi.waitFor(() => expect(effect0Fn).toHaveBeenCalledTimes(2));
|
|
1117
|
+
expect(effect0Fn).toHaveBeenLastCalledWith(3);
|
|
1118
|
+
|
|
1119
|
+
$array.shift();
|
|
1120
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(3));
|
|
1121
|
+
expect(effectFn).toHaveBeenLastCalledWith([1, 2]);
|
|
1122
|
+
await vi.waitFor(() => expect(effect0Fn).toHaveBeenCalledTimes(3));
|
|
1123
|
+
expect(effect0Fn).toHaveBeenLastCalledWith(1);
|
|
1124
|
+
|
|
1125
|
+
$array.unshift(state(4));
|
|
1126
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(4));
|
|
1127
|
+
expect(effectFn).toHaveBeenLastCalledWith([4, 1, 2]);
|
|
1128
|
+
await vi.waitFor(() => expect(effect0Fn).toHaveBeenCalledTimes(4));
|
|
1129
|
+
expect(effect0Fn).toHaveBeenLastCalledWith(4);
|
|
1130
|
+
});
|
|
1131
|
+
});
|
|
1132
|
+
|
|
1133
|
+
describe("lastAction reactivity", () => {
|
|
1134
|
+
describe("set action", () => {
|
|
1135
|
+
it("should call effect when $lastAction is updated with set action", async () => {
|
|
1136
|
+
const $array = array([state(0), state(1), state(2)]);
|
|
1137
|
+
const effectFn = vi.fn();
|
|
1138
|
+
effect(async (t) => effectFn(await $array.$lastAction.get(t)));
|
|
1139
|
+
|
|
1140
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
|
|
1141
|
+
expect(effectFn).toHaveBeenLastCalledWith(
|
|
1142
|
+
expect.objectContaining({
|
|
1143
|
+
type: "set",
|
|
1144
|
+
}),
|
|
1145
|
+
);
|
|
1146
|
+
|
|
1147
|
+
await $array.set([state(1), state(2), state(3)]);
|
|
1148
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
|
|
1149
|
+
expect(effectFn).toHaveBeenLastCalledWith(
|
|
1150
|
+
expect.objectContaining({
|
|
1151
|
+
type: "set",
|
|
1152
|
+
}),
|
|
1153
|
+
);
|
|
1154
|
+
});
|
|
1155
|
+
});
|
|
1156
|
+
|
|
1157
|
+
describe("update action", () => {
|
|
1158
|
+
it("should call effect when $lastAction is updated with setItem action", async () => {
|
|
1159
|
+
const $array = array([state(0), state(1), state(2)]);
|
|
1160
|
+
const effectFn = vi.fn();
|
|
1161
|
+
effect(async (t) => effectFn($array.$lastAction.get(t)));
|
|
1162
|
+
|
|
1163
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
|
|
1164
|
+
|
|
1165
|
+
const $newItem = state(10);
|
|
1166
|
+
$array.update(1, $newItem);
|
|
1167
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
|
|
1168
|
+
expect(effectFn).toHaveBeenLastCalledWith(
|
|
1169
|
+
expect.objectContaining({
|
|
1170
|
+
type: "update",
|
|
1171
|
+
index: 1,
|
|
1172
|
+
}),
|
|
1173
|
+
);
|
|
1174
|
+
});
|
|
1175
|
+
});
|
|
1176
|
+
|
|
1177
|
+
describe("push action", () => {
|
|
1178
|
+
it("should call effect when $lastAction is updated with push action", async () => {
|
|
1179
|
+
const $array = array<FlowState<number>>();
|
|
1180
|
+
const effectFn = vi.fn();
|
|
1181
|
+
effect(async (t) => effectFn(await $array.$lastAction.get(t)));
|
|
1182
|
+
|
|
1183
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
|
|
1184
|
+
expect(effectFn).toHaveBeenLastCalledWith(
|
|
1185
|
+
expect.objectContaining({
|
|
1186
|
+
type: "set",
|
|
1187
|
+
}),
|
|
1188
|
+
);
|
|
1189
|
+
|
|
1190
|
+
const $newItem = state(0);
|
|
1191
|
+
$array.push($newItem);
|
|
1192
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
|
|
1193
|
+
expect(effectFn).toHaveBeenLastCalledWith(
|
|
1194
|
+
expect.objectContaining({
|
|
1195
|
+
type: "push",
|
|
1196
|
+
}),
|
|
1197
|
+
);
|
|
1198
|
+
|
|
1199
|
+
const $newItem2 = state(1);
|
|
1200
|
+
$array.push($newItem2);
|
|
1201
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(3));
|
|
1202
|
+
expect(effectFn).toHaveBeenLastCalledWith(
|
|
1203
|
+
expect.objectContaining({
|
|
1204
|
+
type: "push",
|
|
1205
|
+
}),
|
|
1206
|
+
);
|
|
1207
|
+
});
|
|
1208
|
+
});
|
|
1209
|
+
|
|
1210
|
+
describe("pop action", () => {
|
|
1211
|
+
it("should call effect when $lastAction is updated with pop action", async () => {
|
|
1212
|
+
const $array = array<FlowState<number>>([
|
|
1213
|
+
state(0),
|
|
1214
|
+
state(1),
|
|
1215
|
+
state(2),
|
|
1216
|
+
]);
|
|
1217
|
+
const effectFn = vi.fn();
|
|
1218
|
+
effect(async (t) => effectFn(await $array.$lastAction.get(t)));
|
|
1219
|
+
|
|
1220
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
|
|
1221
|
+
|
|
1222
|
+
$array.pop();
|
|
1223
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
|
|
1224
|
+
expect(effectFn).toHaveBeenLastCalledWith({
|
|
1225
|
+
type: "pop",
|
|
1226
|
+
});
|
|
1227
|
+
|
|
1228
|
+
$array.pop();
|
|
1229
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(3));
|
|
1230
|
+
expect(effectFn).toHaveBeenLastCalledWith({
|
|
1231
|
+
type: "pop",
|
|
1232
|
+
});
|
|
1233
|
+
});
|
|
1234
|
+
});
|
|
1235
|
+
|
|
1236
|
+
describe("unshift action", () => {
|
|
1237
|
+
it("should call effect when $lastAction is updated with unshift action", async () => {
|
|
1238
|
+
const $array = array<FlowState<number>>();
|
|
1239
|
+
const effectFn = vi.fn();
|
|
1240
|
+
effect(async (t) => effectFn(await $array.$lastAction.get(t)));
|
|
1241
|
+
|
|
1242
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
|
|
1243
|
+
expect(effectFn).toHaveBeenLastCalledWith(
|
|
1244
|
+
expect.objectContaining({
|
|
1245
|
+
type: "set",
|
|
1246
|
+
}),
|
|
1247
|
+
);
|
|
1248
|
+
|
|
1249
|
+
const $newItem = state(0);
|
|
1250
|
+
$array.unshift($newItem);
|
|
1251
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
|
|
1252
|
+
expect(effectFn).toHaveBeenLastCalledWith(
|
|
1253
|
+
expect.objectContaining({
|
|
1254
|
+
type: "unshift",
|
|
1255
|
+
}),
|
|
1256
|
+
);
|
|
1257
|
+
|
|
1258
|
+
const $newItem2 = state(1);
|
|
1259
|
+
$array.unshift($newItem2);
|
|
1260
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(3));
|
|
1261
|
+
expect(effectFn).toHaveBeenLastCalledWith(
|
|
1262
|
+
expect.objectContaining({
|
|
1263
|
+
type: "unshift",
|
|
1264
|
+
}),
|
|
1265
|
+
);
|
|
1266
|
+
});
|
|
1267
|
+
});
|
|
1268
|
+
|
|
1269
|
+
describe("shift action", () => {
|
|
1270
|
+
it("should call effect when $lastAction is updated with shift action", async () => {
|
|
1271
|
+
const $array = array<FlowState<number>>([
|
|
1272
|
+
state(0),
|
|
1273
|
+
state(1),
|
|
1274
|
+
state(2),
|
|
1275
|
+
]);
|
|
1276
|
+
const effectFn = vi.fn();
|
|
1277
|
+
effect(async (t) => effectFn(await $array.$lastAction.get(t)));
|
|
1278
|
+
|
|
1279
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
|
|
1280
|
+
|
|
1281
|
+
$array.shift();
|
|
1282
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
|
|
1283
|
+
expect(effectFn).toHaveBeenLastCalledWith({
|
|
1284
|
+
type: "shift",
|
|
1285
|
+
});
|
|
1286
|
+
|
|
1287
|
+
$array.shift();
|
|
1288
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(3));
|
|
1289
|
+
expect(effectFn).toHaveBeenLastCalledWith({
|
|
1290
|
+
type: "shift",
|
|
1291
|
+
});
|
|
1292
|
+
});
|
|
1293
|
+
});
|
|
1294
|
+
|
|
1295
|
+
describe("splice action", () => {
|
|
1296
|
+
it("should call effect when $lastAction is updated with splice action", async () => {
|
|
1297
|
+
const $array = array<FlowState<number>>([
|
|
1298
|
+
state(0),
|
|
1299
|
+
state(1),
|
|
1300
|
+
state(2),
|
|
1301
|
+
state(3),
|
|
1302
|
+
state(4),
|
|
1303
|
+
]);
|
|
1304
|
+
const effectFn = vi.fn();
|
|
1305
|
+
effect(async (t) => effectFn(await $array.$lastAction.get(t)));
|
|
1306
|
+
|
|
1307
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
|
|
1308
|
+
expect(effectFn).toHaveBeenLastCalledWith(
|
|
1309
|
+
expect.objectContaining({
|
|
1310
|
+
type: "set",
|
|
1311
|
+
}),
|
|
1312
|
+
);
|
|
1313
|
+
|
|
1314
|
+
$array.splice(1, 2);
|
|
1315
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
|
|
1316
|
+
expect(effectFn).toHaveBeenLastCalledWith(
|
|
1317
|
+
expect.objectContaining({
|
|
1318
|
+
type: "splice",
|
|
1319
|
+
start: 1,
|
|
1320
|
+
deleteCount: 2,
|
|
1321
|
+
}),
|
|
1322
|
+
);
|
|
1323
|
+
|
|
1324
|
+
$array.splice(1, 0, state(1), state(2));
|
|
1325
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(3));
|
|
1326
|
+
expect(effectFn).toHaveBeenLastCalledWith(
|
|
1327
|
+
expect.objectContaining({
|
|
1328
|
+
type: "splice",
|
|
1329
|
+
start: 1,
|
|
1330
|
+
deleteCount: 0,
|
|
1331
|
+
}),
|
|
1332
|
+
);
|
|
1333
|
+
});
|
|
1334
|
+
});
|
|
1335
|
+
|
|
1336
|
+
describe("clear action", () => {
|
|
1337
|
+
it("should call effect when $lastAction is updated with clear action", async () => {
|
|
1338
|
+
const $array = array<FlowState<number>>([
|
|
1339
|
+
state(0),
|
|
1340
|
+
state(1),
|
|
1341
|
+
state(2),
|
|
1342
|
+
state(3),
|
|
1343
|
+
state(4),
|
|
1344
|
+
]);
|
|
1345
|
+
const effectFn = vi.fn();
|
|
1346
|
+
effect(async (t) => effectFn(await $array.$lastAction.get(t)));
|
|
1347
|
+
|
|
1348
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
|
|
1349
|
+
expect(effectFn).toHaveBeenLastCalledWith(
|
|
1350
|
+
expect.objectContaining({
|
|
1351
|
+
type: "set",
|
|
1352
|
+
}),
|
|
1353
|
+
);
|
|
1354
|
+
|
|
1355
|
+
$array.clear();
|
|
1356
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
|
|
1357
|
+
expect(effectFn).toHaveBeenLastCalledWith({
|
|
1358
|
+
type: "clear",
|
|
1359
|
+
});
|
|
1360
|
+
});
|
|
1361
|
+
});
|
|
1362
|
+
});
|
|
1363
|
+
});
|
|
1364
|
+
});
|
|
1365
|
+
});
|