@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,1129 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from "vitest";
|
|
2
|
+
import { array, effect, state } from "#package";
|
|
3
|
+
|
|
4
|
+
describe("FlowArray", () => {
|
|
5
|
+
describe("unit", () => {
|
|
6
|
+
describe("initialization", () => {
|
|
7
|
+
it("should initialize with provided values", async () => {
|
|
8
|
+
const $array = array([1, 2, 3]);
|
|
9
|
+
|
|
10
|
+
expect(await $array.pick()).toEqual([1, 2, 3]);
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
it("should initialize with empty array when no value provided", async () => {
|
|
14
|
+
const $array = array<number>();
|
|
15
|
+
|
|
16
|
+
expect(await $array.pick()).toEqual([]);
|
|
17
|
+
});
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
describe("disposal", () => {
|
|
21
|
+
it("should have disposed property set to false initially and true after disposal", () => {
|
|
22
|
+
const $array = array([1, 2, 3]);
|
|
23
|
+
expect($array.disposed).toBe(false);
|
|
24
|
+
$array.dispose();
|
|
25
|
+
expect($array.disposed).toBe(true);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it("should throw error when disposed twice", () => {
|
|
29
|
+
const $array = array([1, 2, 3]);
|
|
30
|
+
$array.dispose();
|
|
31
|
+
expect(() => $array.dispose()).toThrow(
|
|
32
|
+
"[PicoFlow] Primitive is disposed",
|
|
33
|
+
);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it("should throw when pick is called after disposal", async () => {
|
|
37
|
+
const $array = array([1, 2, 3]);
|
|
38
|
+
$array.dispose();
|
|
39
|
+
await expect($array.pick()).rejects.toThrow(
|
|
40
|
+
"[PicoFlow] Primitive is disposed",
|
|
41
|
+
);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it("should throw when set is called after disposal", async () => {
|
|
45
|
+
const $array = array([1, 2, 3]);
|
|
46
|
+
$array.dispose();
|
|
47
|
+
await expect($array.set([1])).rejects.toThrow(
|
|
48
|
+
"[PicoFlow] Primitive is disposed",
|
|
49
|
+
);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it("should throw when setItem is called after disposal", async () => {
|
|
53
|
+
const $array = array([1, 2, 3]);
|
|
54
|
+
$array.dispose();
|
|
55
|
+
await expect($array.update(0, 3)).rejects.toThrow(
|
|
56
|
+
"[PicoFlow] Primitive is disposed",
|
|
57
|
+
);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it("should throw when push is called after disposal", async () => {
|
|
61
|
+
const $array = array([1, 2, 3]);
|
|
62
|
+
$array.dispose();
|
|
63
|
+
await expect($array.push(3)).rejects.toThrow(
|
|
64
|
+
"[PicoFlow] Primitive is disposed",
|
|
65
|
+
);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it("should throw when pop is called after disposal", async () => {
|
|
69
|
+
const $array = array([1, 2, 3]);
|
|
70
|
+
$array.dispose();
|
|
71
|
+
await expect($array.pop()).rejects.toThrow(
|
|
72
|
+
"[PicoFlow] Primitive is disposed",
|
|
73
|
+
);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it("should throw when unshift is called after disposal", async () => {
|
|
77
|
+
const $array = array([1, 2, 3]);
|
|
78
|
+
$array.dispose();
|
|
79
|
+
await expect($array.unshift(3)).rejects.toThrow(
|
|
80
|
+
"[PicoFlow] Primitive is disposed",
|
|
81
|
+
);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it("should throw when shift is called after disposal", async () => {
|
|
85
|
+
const $array = array([1, 2, 3]);
|
|
86
|
+
$array.dispose();
|
|
87
|
+
await expect($array.shift()).rejects.toThrow(
|
|
88
|
+
"[PicoFlow] Primitive is disposed",
|
|
89
|
+
);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it("should throw when splice is called after disposal", async () => {
|
|
93
|
+
const $array = array<number>([0, 1, 2, 3, 4]);
|
|
94
|
+
$array.dispose();
|
|
95
|
+
await expect($array.splice(1, 2)).rejects.toThrow(
|
|
96
|
+
"[PicoFlow] Primitive is disposed",
|
|
97
|
+
);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it("should throw when clear is called after disposal", async () => {
|
|
101
|
+
const $array = array<number>([0, 1, 2, 3, 4]);
|
|
102
|
+
$array.dispose();
|
|
103
|
+
await expect($array.clear()).rejects.toThrow(
|
|
104
|
+
"[PicoFlow] Primitive is disposed",
|
|
105
|
+
);
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it("should dispose items when FlowArray is disposed", () => {
|
|
109
|
+
const $item1 = state(1);
|
|
110
|
+
const $item2 = state(2);
|
|
111
|
+
const $item3 = state(3);
|
|
112
|
+
const $array = array([$item1, $item2, $item3]);
|
|
113
|
+
|
|
114
|
+
expect($item1.disposed).toBe(false);
|
|
115
|
+
expect($item2.disposed).toBe(false);
|
|
116
|
+
expect($item3.disposed).toBe(false);
|
|
117
|
+
|
|
118
|
+
$array.dispose();
|
|
119
|
+
|
|
120
|
+
expect($item1.disposed).toBe(true);
|
|
121
|
+
expect($item2.disposed).toBe(true);
|
|
122
|
+
expect($item3.disposed).toBe(true);
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it("should dispose items with provided options when FlowArray is disposed", () => {
|
|
126
|
+
const $item1 = state(1);
|
|
127
|
+
const $item2 = state(2);
|
|
128
|
+
const $array = array([$item1, $item2]);
|
|
129
|
+
|
|
130
|
+
const $effect = effect((t) => {
|
|
131
|
+
$item1.get(t);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
expect($effect.disposed).toBe(false);
|
|
135
|
+
|
|
136
|
+
$array.dispose({ self: true });
|
|
137
|
+
|
|
138
|
+
expect($item1.disposed).toBe(true);
|
|
139
|
+
expect($item2.disposed).toBe(true);
|
|
140
|
+
expect($effect.disposed).toBe(false);
|
|
141
|
+
});
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
describe("length", () => {
|
|
145
|
+
it("should return correct length", () => {
|
|
146
|
+
const $array = array([1, 2, 3]);
|
|
147
|
+
expect($array.length).toBe(3);
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
it("should return zero for empty array", () => {
|
|
151
|
+
const $array = array<number>([]);
|
|
152
|
+
expect($array.length).toBe(0);
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
it("should throw when length is accessed after disposal", () => {
|
|
156
|
+
const $array = array([1, 2, 3]);
|
|
157
|
+
$array.dispose();
|
|
158
|
+
expect(() => $array.length).toThrow("[PicoFlow] Primitive is disposed");
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
it("should update length when array is modified", async () => {
|
|
162
|
+
const $array = array<number>([]);
|
|
163
|
+
expect($array.length).toBe(0);
|
|
164
|
+
|
|
165
|
+
$array.push(1);
|
|
166
|
+
expect($array.length).toBe(1);
|
|
167
|
+
|
|
168
|
+
$array.push(2);
|
|
169
|
+
expect($array.length).toBe(2);
|
|
170
|
+
|
|
171
|
+
$array.pop();
|
|
172
|
+
expect($array.length).toBe(1);
|
|
173
|
+
|
|
174
|
+
$array.unshift(0);
|
|
175
|
+
expect($array.length).toBe(2);
|
|
176
|
+
|
|
177
|
+
$array.shift();
|
|
178
|
+
expect($array.length).toBe(1);
|
|
179
|
+
|
|
180
|
+
$array.clear();
|
|
181
|
+
expect($array.length).toBe(0);
|
|
182
|
+
});
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
describe("set", () => {
|
|
186
|
+
it("should update array when set is called", async () => {
|
|
187
|
+
const $array = array([0, 1, 2]);
|
|
188
|
+
expect(await $array.pick()).toEqual([0, 1, 2]);
|
|
189
|
+
|
|
190
|
+
await $array.set([1, 2, 3]);
|
|
191
|
+
expect(await $array.pick()).toEqual([1, 2, 3]);
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
it("should dispose items when set replaces array", () => {
|
|
195
|
+
const $oldItem1 = state(1);
|
|
196
|
+
const $oldItem2 = state(2);
|
|
197
|
+
const $array = array([$oldItem1, $oldItem2]);
|
|
198
|
+
|
|
199
|
+
expect($oldItem1.disposed).toBe(false);
|
|
200
|
+
expect($oldItem2.disposed).toBe(false);
|
|
201
|
+
|
|
202
|
+
$array.set([state(3), state(4)]);
|
|
203
|
+
|
|
204
|
+
expect($oldItem1.disposed).toBe(true);
|
|
205
|
+
expect($oldItem2.disposed).toBe(true);
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
it("should dispose items with self option when set replaces array", async () => {
|
|
209
|
+
const $oldItem = state(1);
|
|
210
|
+
const $array = array([$oldItem]);
|
|
211
|
+
|
|
212
|
+
const $effect = effect((t) => {
|
|
213
|
+
$oldItem.get(t);
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
expect($effect.disposed).toBe(false);
|
|
217
|
+
|
|
218
|
+
await $array.set([state(2)]);
|
|
219
|
+
|
|
220
|
+
expect($oldItem.disposed).toBe(true);
|
|
221
|
+
expect($effect.disposed).toBe(false);
|
|
222
|
+
});
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
describe("setItem", () => {
|
|
226
|
+
it("should update item at specific index", async () => {
|
|
227
|
+
const $array = array([0, 1, 2]);
|
|
228
|
+
expect(await $array.pick()).toEqual([0, 1, 2]);
|
|
229
|
+
|
|
230
|
+
$array.update(0, 1);
|
|
231
|
+
expect(await $array.pick()).toEqual([1, 1, 2]);
|
|
232
|
+
|
|
233
|
+
$array.update(1, 2);
|
|
234
|
+
expect(await $array.pick()).toEqual([1, 2, 2]);
|
|
235
|
+
|
|
236
|
+
$array.update(2, 3);
|
|
237
|
+
expect(await $array.pick()).toEqual([1, 2, 3]);
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
it("should throw when setItem is called with negative index", async () => {
|
|
241
|
+
const $array = array([1, 2, 3]);
|
|
242
|
+
await expect($array.update(-1, 0)).rejects.toThrow(
|
|
243
|
+
"[PicoFlow] Index out of bounds",
|
|
244
|
+
);
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
it("should throw when setItem is called with index >= length", async () => {
|
|
248
|
+
const $array = array([1, 2, 3]);
|
|
249
|
+
await expect($array.update(3, 0)).rejects.toThrow(
|
|
250
|
+
"[PicoFlow] Index out of bounds",
|
|
251
|
+
);
|
|
252
|
+
await expect($array.update(10, 0)).rejects.toThrow(
|
|
253
|
+
"[PicoFlow] Index out of bounds",
|
|
254
|
+
);
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
it("should handle boundary indices correctly", async () => {
|
|
258
|
+
const $array = array([0, 1, 2]);
|
|
259
|
+
$array.update(0, 10);
|
|
260
|
+
expect(await $array.pick()).toEqual([10, 1, 2]);
|
|
261
|
+
|
|
262
|
+
$array.update(2, 20);
|
|
263
|
+
expect(await $array.pick()).toEqual([10, 1, 20]);
|
|
264
|
+
});
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
describe("push", () => {
|
|
268
|
+
it("should append item to end of array", async () => {
|
|
269
|
+
const $array = array<number>([]);
|
|
270
|
+
expect(await $array.pick()).toEqual([]);
|
|
271
|
+
|
|
272
|
+
$array.push(0);
|
|
273
|
+
expect(await $array.pick()).toEqual([0]);
|
|
274
|
+
|
|
275
|
+
$array.push(1);
|
|
276
|
+
expect(await $array.pick()).toEqual([0, 1]);
|
|
277
|
+
|
|
278
|
+
$array.push(2);
|
|
279
|
+
expect(await $array.pick()).toEqual([0, 1, 2]);
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
it("should handle operations on empty array", () => {
|
|
283
|
+
const $array = array<number>([]);
|
|
284
|
+
expect($array.length).toBe(0);
|
|
285
|
+
$array.push(1);
|
|
286
|
+
expect($array.length).toBe(1);
|
|
287
|
+
});
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
describe("pop", () => {
|
|
291
|
+
it("should remove last item from array", async () => {
|
|
292
|
+
const $array = array([0, 1, 2]);
|
|
293
|
+
expect(await $array.pick()).toEqual([0, 1, 2]);
|
|
294
|
+
|
|
295
|
+
$array.pop();
|
|
296
|
+
expect(await $array.pick()).toEqual([0, 1]);
|
|
297
|
+
|
|
298
|
+
$array.pop();
|
|
299
|
+
expect(await $array.pick()).toEqual([0]);
|
|
300
|
+
|
|
301
|
+
$array.pop();
|
|
302
|
+
expect(await $array.pick()).toEqual([]);
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
it("should dispose item when pop removes it", () => {
|
|
306
|
+
const $item = state(1);
|
|
307
|
+
const $array = array([$item]);
|
|
308
|
+
|
|
309
|
+
expect($item.disposed).toBe(false);
|
|
310
|
+
|
|
311
|
+
$array.pop();
|
|
312
|
+
|
|
313
|
+
expect($item.disposed).toBe(true);
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
it("should dispose item with self option when pop removes it", async () => {
|
|
317
|
+
const $item = state(1);
|
|
318
|
+
const $array = array([$item]);
|
|
319
|
+
|
|
320
|
+
const $effect = effect((t) => {
|
|
321
|
+
$item.get(t);
|
|
322
|
+
});
|
|
323
|
+
|
|
324
|
+
expect($effect.disposed).toBe(false);
|
|
325
|
+
|
|
326
|
+
await $array.pop();
|
|
327
|
+
|
|
328
|
+
expect($item.disposed).toBe(true);
|
|
329
|
+
expect($effect.disposed).toBe(false);
|
|
330
|
+
});
|
|
331
|
+
|
|
332
|
+
it("should handle operations on empty array", () => {
|
|
333
|
+
const $array = array<number>([]);
|
|
334
|
+
expect($array.length).toBe(0);
|
|
335
|
+
$array.pop();
|
|
336
|
+
expect($array.length).toBe(0);
|
|
337
|
+
});
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
describe("unshift", () => {
|
|
341
|
+
it("should insert item at beginning of array", async () => {
|
|
342
|
+
const $array = array<number>([]);
|
|
343
|
+
expect(await $array.pick()).toEqual([]);
|
|
344
|
+
|
|
345
|
+
$array.unshift(0);
|
|
346
|
+
expect(await $array.pick()).toEqual([0]);
|
|
347
|
+
|
|
348
|
+
$array.unshift(1);
|
|
349
|
+
expect(await $array.pick()).toEqual([1, 0]);
|
|
350
|
+
|
|
351
|
+
$array.unshift(2);
|
|
352
|
+
expect(await $array.pick()).toEqual([2, 1, 0]);
|
|
353
|
+
});
|
|
354
|
+
});
|
|
355
|
+
|
|
356
|
+
describe("shift", () => {
|
|
357
|
+
it("should remove first item from array", async () => {
|
|
358
|
+
const $array = array([0, 1, 2]);
|
|
359
|
+
expect(await $array.pick()).toEqual([0, 1, 2]);
|
|
360
|
+
|
|
361
|
+
$array.shift();
|
|
362
|
+
expect(await $array.pick()).toEqual([1, 2]);
|
|
363
|
+
|
|
364
|
+
$array.shift();
|
|
365
|
+
expect(await $array.pick()).toEqual([2]);
|
|
366
|
+
|
|
367
|
+
$array.shift();
|
|
368
|
+
expect(await $array.pick()).toEqual([]);
|
|
369
|
+
});
|
|
370
|
+
|
|
371
|
+
it("should dispose item when shift removes it", () => {
|
|
372
|
+
const $item = state(1);
|
|
373
|
+
const $array = array([$item, state(2)]);
|
|
374
|
+
|
|
375
|
+
expect($item.disposed).toBe(false);
|
|
376
|
+
|
|
377
|
+
$array.shift();
|
|
378
|
+
|
|
379
|
+
expect($item.disposed).toBe(true);
|
|
380
|
+
});
|
|
381
|
+
|
|
382
|
+
it("should dispose item with self option when shift removes it", async () => {
|
|
383
|
+
const $item = state(1);
|
|
384
|
+
const $array = array([$item, state(2)]);
|
|
385
|
+
|
|
386
|
+
const $effect = effect((t) => {
|
|
387
|
+
$item.get(t);
|
|
388
|
+
});
|
|
389
|
+
|
|
390
|
+
expect($effect.disposed).toBe(false);
|
|
391
|
+
|
|
392
|
+
await $array.shift();
|
|
393
|
+
|
|
394
|
+
expect($item.disposed).toBe(true);
|
|
395
|
+
expect($effect.disposed).toBe(false);
|
|
396
|
+
});
|
|
397
|
+
|
|
398
|
+
it("should handle operations on empty array", () => {
|
|
399
|
+
const $array = array<number>([]);
|
|
400
|
+
expect($array.length).toBe(0);
|
|
401
|
+
$array.shift();
|
|
402
|
+
expect($array.length).toBe(0);
|
|
403
|
+
});
|
|
404
|
+
});
|
|
405
|
+
|
|
406
|
+
describe("splice", () => {
|
|
407
|
+
it("should remove items from array", async () => {
|
|
408
|
+
const $array = array<number>([0, 1, 2, 3, 4]);
|
|
409
|
+
expect(await $array.pick()).toEqual([0, 1, 2, 3, 4]);
|
|
410
|
+
|
|
411
|
+
$array.splice(1, 2);
|
|
412
|
+
expect(await $array.pick()).toEqual([0, 3, 4]);
|
|
413
|
+
});
|
|
414
|
+
|
|
415
|
+
it("should insert items into array", async () => {
|
|
416
|
+
const $array = array<number>([0, 1, 2, 3, 4]);
|
|
417
|
+
$array.splice(1, 0, 10, 11);
|
|
418
|
+
expect(await $array.pick()).toEqual([0, 10, 11, 1, 2, 3, 4]);
|
|
419
|
+
});
|
|
420
|
+
|
|
421
|
+
it("should replace items in array", async () => {
|
|
422
|
+
const $array = array<number>([0, 1, 2, 3, 4]);
|
|
423
|
+
$array.splice(1, 2, 10, 11);
|
|
424
|
+
expect(await $array.pick()).toEqual([0, 10, 11, 3, 4]);
|
|
425
|
+
});
|
|
426
|
+
|
|
427
|
+
it("should dispose items when splice removes them", () => {
|
|
428
|
+
const $item1 = state(1);
|
|
429
|
+
const $item2 = state(2);
|
|
430
|
+
const $array = array([state(0), $item1, $item2, state(3)]);
|
|
431
|
+
|
|
432
|
+
expect($item1.disposed).toBe(false);
|
|
433
|
+
expect($item2.disposed).toBe(false);
|
|
434
|
+
|
|
435
|
+
$array.splice(1, 2);
|
|
436
|
+
|
|
437
|
+
expect($item1.disposed).toBe(true);
|
|
438
|
+
expect($item2.disposed).toBe(true);
|
|
439
|
+
});
|
|
440
|
+
|
|
441
|
+
it("should dispose items with self option when splice removes them", async () => {
|
|
442
|
+
const $item = state(1);
|
|
443
|
+
const $array = array([state(0), $item, state(2)]);
|
|
444
|
+
|
|
445
|
+
const $effect = effect((t) => {
|
|
446
|
+
$item.get(t);
|
|
447
|
+
});
|
|
448
|
+
|
|
449
|
+
expect($effect.disposed).toBe(false);
|
|
450
|
+
|
|
451
|
+
await $array.splice(1, 1);
|
|
452
|
+
|
|
453
|
+
expect($item.disposed).toBe(true);
|
|
454
|
+
expect($effect.disposed).toBe(false);
|
|
455
|
+
});
|
|
456
|
+
});
|
|
457
|
+
|
|
458
|
+
describe("clear", () => {
|
|
459
|
+
it("should remove all items from array", async () => {
|
|
460
|
+
const $array = array<number>([0, 1, 2, 3, 4]);
|
|
461
|
+
expect(await $array.pick()).toEqual([0, 1, 2, 3, 4]);
|
|
462
|
+
|
|
463
|
+
$array.clear();
|
|
464
|
+
expect(await $array.pick()).toEqual([]);
|
|
465
|
+
});
|
|
466
|
+
|
|
467
|
+
it("should dispose all items when clear is called", () => {
|
|
468
|
+
const $item1 = state(1);
|
|
469
|
+
const $item2 = state(2);
|
|
470
|
+
const $item3 = state(3);
|
|
471
|
+
const $array = array([$item1, $item2, $item3]);
|
|
472
|
+
|
|
473
|
+
expect($item1.disposed).toBe(false);
|
|
474
|
+
expect($item2.disposed).toBe(false);
|
|
475
|
+
expect($item3.disposed).toBe(false);
|
|
476
|
+
|
|
477
|
+
$array.clear();
|
|
478
|
+
|
|
479
|
+
expect($item1.disposed).toBe(true);
|
|
480
|
+
expect($item2.disposed).toBe(true);
|
|
481
|
+
expect($item3.disposed).toBe(true);
|
|
482
|
+
});
|
|
483
|
+
|
|
484
|
+
it("should dispose items with self option when clear is called", async () => {
|
|
485
|
+
const $item = state(1);
|
|
486
|
+
const $array = array([$item, state(2)]);
|
|
487
|
+
|
|
488
|
+
const $effect = effect((t) => {
|
|
489
|
+
$item.get(t);
|
|
490
|
+
});
|
|
491
|
+
|
|
492
|
+
expect($effect.disposed).toBe(false);
|
|
493
|
+
|
|
494
|
+
await $array.clear();
|
|
495
|
+
|
|
496
|
+
expect($item.disposed).toBe(true);
|
|
497
|
+
expect($effect.disposed).toBe(false);
|
|
498
|
+
});
|
|
499
|
+
});
|
|
500
|
+
|
|
501
|
+
describe("$lastAction", () => {
|
|
502
|
+
describe("set action", () => {
|
|
503
|
+
it("should initialize $lastAction with set action on creation", async () => {
|
|
504
|
+
const $array = array([1, 2, 3]);
|
|
505
|
+
const action = await $array.$lastAction.pick();
|
|
506
|
+
expect(action).toEqual({
|
|
507
|
+
type: "set",
|
|
508
|
+
items: [1, 2, 3],
|
|
509
|
+
});
|
|
510
|
+
});
|
|
511
|
+
|
|
512
|
+
it("should update $lastAction with set action when set is called", async () => {
|
|
513
|
+
const $array = array([0, 1, 2]);
|
|
514
|
+
await $array.set([1, 2, 3]);
|
|
515
|
+
const action = await $array.$lastAction.pick();
|
|
516
|
+
expect(action).toEqual({
|
|
517
|
+
type: "set",
|
|
518
|
+
items: [1, 2, 3],
|
|
519
|
+
});
|
|
520
|
+
});
|
|
521
|
+
|
|
522
|
+
it("should have correct structure for set action (items array)", async () => {
|
|
523
|
+
const $array = array([1, 2, 3]);
|
|
524
|
+
const action = await $array.$lastAction.pick();
|
|
525
|
+
expect(action).toHaveProperty("type", "set");
|
|
526
|
+
if (action.type === "set") {
|
|
527
|
+
expect(action).toHaveProperty("items");
|
|
528
|
+
expect(Array.isArray(action.items)).toBe(true);
|
|
529
|
+
expect(action.items).toEqual([1, 2, 3]);
|
|
530
|
+
}
|
|
531
|
+
});
|
|
532
|
+
});
|
|
533
|
+
|
|
534
|
+
describe("update action", () => {
|
|
535
|
+
it("should update $lastAction with setItem action when setItem is called", async () => {
|
|
536
|
+
const $array = array([0, 1, 2]);
|
|
537
|
+
$array.update(1, 10);
|
|
538
|
+
const action = await $array.$lastAction.pick();
|
|
539
|
+
expect(action).toEqual({
|
|
540
|
+
type: "update",
|
|
541
|
+
index: 1,
|
|
542
|
+
item: 10,
|
|
543
|
+
});
|
|
544
|
+
});
|
|
545
|
+
|
|
546
|
+
it("should have correct structure for setItem action (index and item)", async () => {
|
|
547
|
+
const $array = array([1, 2, 3]);
|
|
548
|
+
$array.update(0, 10);
|
|
549
|
+
const action = await $array.$lastAction.pick();
|
|
550
|
+
expect(action).toHaveProperty("type", "update");
|
|
551
|
+
if (action.type === "update") {
|
|
552
|
+
expect(action).toHaveProperty("index");
|
|
553
|
+
expect(action).toHaveProperty("item");
|
|
554
|
+
expect(action.index).toBe(0);
|
|
555
|
+
expect(action.item).toBe(10);
|
|
556
|
+
}
|
|
557
|
+
});
|
|
558
|
+
});
|
|
559
|
+
|
|
560
|
+
describe("push action", () => {
|
|
561
|
+
it("should update $lastAction with push action when push is called", async () => {
|
|
562
|
+
const $array = array<number>([]);
|
|
563
|
+
$array.push(42);
|
|
564
|
+
const action = await $array.$lastAction.pick();
|
|
565
|
+
expect(action).toEqual({
|
|
566
|
+
type: "push",
|
|
567
|
+
item: 42,
|
|
568
|
+
});
|
|
569
|
+
});
|
|
570
|
+
|
|
571
|
+
it("should have correct structure for push action (item)", async () => {
|
|
572
|
+
const $array = array<number>([]);
|
|
573
|
+
$array.push(100);
|
|
574
|
+
const action = await $array.$lastAction.pick();
|
|
575
|
+
expect(action).toHaveProperty("type", "push");
|
|
576
|
+
if (action.type === "push") {
|
|
577
|
+
expect(action).toHaveProperty("item");
|
|
578
|
+
expect(action.item).toBe(100);
|
|
579
|
+
}
|
|
580
|
+
});
|
|
581
|
+
});
|
|
582
|
+
|
|
583
|
+
describe("pop action", () => {
|
|
584
|
+
it("should update $lastAction with pop action when pop is called", async () => {
|
|
585
|
+
const $array = array([1, 2, 3]);
|
|
586
|
+
$array.pop();
|
|
587
|
+
const action = await $array.$lastAction.pick();
|
|
588
|
+
expect(action).toEqual({
|
|
589
|
+
type: "pop",
|
|
590
|
+
});
|
|
591
|
+
});
|
|
592
|
+
|
|
593
|
+
it("should have correct structure for pop action (no additional fields)", async () => {
|
|
594
|
+
const $array = array([1, 2, 3]);
|
|
595
|
+
$array.pop();
|
|
596
|
+
const action = await $array.$lastAction.pick();
|
|
597
|
+
expect(action).toHaveProperty("type", "pop");
|
|
598
|
+
if (action.type === "pop") {
|
|
599
|
+
expect(action).not.toHaveProperty("item");
|
|
600
|
+
expect(action).not.toHaveProperty("index");
|
|
601
|
+
expect(action).not.toHaveProperty("items");
|
|
602
|
+
}
|
|
603
|
+
});
|
|
604
|
+
});
|
|
605
|
+
|
|
606
|
+
describe("unshift action", () => {
|
|
607
|
+
it("should update $lastAction with unshift action when unshift is called", async () => {
|
|
608
|
+
const $array = array<number>([]);
|
|
609
|
+
$array.unshift(42);
|
|
610
|
+
const action = await $array.$lastAction.pick();
|
|
611
|
+
expect(action).toEqual({
|
|
612
|
+
type: "unshift",
|
|
613
|
+
item: 42,
|
|
614
|
+
});
|
|
615
|
+
});
|
|
616
|
+
|
|
617
|
+
it("should have correct structure for unshift action (item)", async () => {
|
|
618
|
+
const $array = array<number>([]);
|
|
619
|
+
$array.unshift(200);
|
|
620
|
+
const action = await $array.$lastAction.pick();
|
|
621
|
+
expect(action).toHaveProperty("type", "unshift");
|
|
622
|
+
if (action.type === "unshift") {
|
|
623
|
+
expect(action).toHaveProperty("item");
|
|
624
|
+
expect(action.item).toBe(200);
|
|
625
|
+
}
|
|
626
|
+
});
|
|
627
|
+
});
|
|
628
|
+
|
|
629
|
+
describe("shift action", () => {
|
|
630
|
+
it("should update $lastAction with shift action when shift is called", async () => {
|
|
631
|
+
const $array = array([1, 2, 3]);
|
|
632
|
+
$array.shift();
|
|
633
|
+
const action = await $array.$lastAction.pick();
|
|
634
|
+
expect(action).toEqual({
|
|
635
|
+
type: "shift",
|
|
636
|
+
});
|
|
637
|
+
});
|
|
638
|
+
|
|
639
|
+
it("should have correct structure for shift action (no additional fields)", async () => {
|
|
640
|
+
const $array = array([1, 2, 3]);
|
|
641
|
+
$array.shift();
|
|
642
|
+
const action = await $array.$lastAction.pick();
|
|
643
|
+
expect(action).toHaveProperty("type", "shift");
|
|
644
|
+
if (action.type === "shift") {
|
|
645
|
+
expect(action).not.toHaveProperty("item");
|
|
646
|
+
expect(action).not.toHaveProperty("index");
|
|
647
|
+
expect(action).not.toHaveProperty("items");
|
|
648
|
+
}
|
|
649
|
+
});
|
|
650
|
+
});
|
|
651
|
+
|
|
652
|
+
describe("splice action", () => {
|
|
653
|
+
it("should update $lastAction with splice action when splice is called", async () => {
|
|
654
|
+
const $array = array<number>([0, 1, 2, 3, 4]);
|
|
655
|
+
$array.splice(1, 2, 10, 11);
|
|
656
|
+
const action = await $array.$lastAction.pick();
|
|
657
|
+
expect(action).toEqual({
|
|
658
|
+
type: "splice",
|
|
659
|
+
start: 1,
|
|
660
|
+
deleteCount: 2,
|
|
661
|
+
items: [10, 11],
|
|
662
|
+
});
|
|
663
|
+
});
|
|
664
|
+
|
|
665
|
+
it("should have correct structure for splice action (start, deleteCount, items)", async () => {
|
|
666
|
+
const $array = array<number>([0, 1, 2]);
|
|
667
|
+
$array.splice(1, 1, 100);
|
|
668
|
+
const action = await $array.$lastAction.pick();
|
|
669
|
+
expect(action).toHaveProperty("type", "splice");
|
|
670
|
+
if (action.type === "splice") {
|
|
671
|
+
expect(action).toHaveProperty("start");
|
|
672
|
+
expect(action).toHaveProperty("deleteCount");
|
|
673
|
+
expect(action).toHaveProperty("items");
|
|
674
|
+
expect(action.start).toBe(1);
|
|
675
|
+
expect(action.deleteCount).toBe(1);
|
|
676
|
+
expect(Array.isArray(action.items)).toBe(true);
|
|
677
|
+
expect(action.items).toEqual([100]);
|
|
678
|
+
}
|
|
679
|
+
});
|
|
680
|
+
});
|
|
681
|
+
|
|
682
|
+
describe("clear action", () => {
|
|
683
|
+
it("should update $lastAction with clear action when clear is called", async () => {
|
|
684
|
+
const $array = array<number>([0, 1, 2, 3, 4]);
|
|
685
|
+
$array.clear();
|
|
686
|
+
const action = await $array.$lastAction.pick();
|
|
687
|
+
expect(action).toEqual({
|
|
688
|
+
type: "clear",
|
|
689
|
+
});
|
|
690
|
+
});
|
|
691
|
+
|
|
692
|
+
it("should have correct structure for clear action (no additional fields)", async () => {
|
|
693
|
+
const $array = array<number>([1, 2, 3]);
|
|
694
|
+
$array.clear();
|
|
695
|
+
const action = await $array.$lastAction.pick();
|
|
696
|
+
expect(action).toHaveProperty("type", "clear");
|
|
697
|
+
if (action.type === "clear") {
|
|
698
|
+
expect(action).not.toHaveProperty("item");
|
|
699
|
+
expect(action).not.toHaveProperty("index");
|
|
700
|
+
expect(action).not.toHaveProperty("items");
|
|
701
|
+
}
|
|
702
|
+
});
|
|
703
|
+
});
|
|
704
|
+
});
|
|
705
|
+
|
|
706
|
+
describe("edge cases", () => {
|
|
707
|
+
it("should handle multiple rapid operations", async () => {
|
|
708
|
+
const $array = array<number>([]);
|
|
709
|
+
$array.push(1);
|
|
710
|
+
$array.push(2);
|
|
711
|
+
$array.push(3);
|
|
712
|
+
$array.pop();
|
|
713
|
+
$array.shift();
|
|
714
|
+
$array.unshift(0);
|
|
715
|
+
expect(await $array.pick()).toEqual([0, 2]);
|
|
716
|
+
});
|
|
717
|
+
|
|
718
|
+
it("should handle boundary indices correctly", async () => {
|
|
719
|
+
const $array = array([0, 1, 2]);
|
|
720
|
+
$array.update(0, 10);
|
|
721
|
+
$array.update(2, 20);
|
|
722
|
+
expect(await $array.pick()).toEqual([10, 1, 20]);
|
|
723
|
+
});
|
|
724
|
+
});
|
|
725
|
+
});
|
|
726
|
+
|
|
727
|
+
describe("integration", () => {
|
|
728
|
+
describe("with effects", () => {
|
|
729
|
+
describe("reactivity", () => {
|
|
730
|
+
it("should call effect when initialized", async () => {
|
|
731
|
+
const $array = array([1, 2, 3]);
|
|
732
|
+
const effectFn = vi.fn();
|
|
733
|
+
effect((t) => effectFn($array.get(t)));
|
|
734
|
+
|
|
735
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
|
|
736
|
+
expect(effectFn).toHaveBeenLastCalledWith([1, 2, 3]);
|
|
737
|
+
});
|
|
738
|
+
|
|
739
|
+
it("should call effect when updated with set", async () => {
|
|
740
|
+
const $array = array<number>([0, 1, 2]);
|
|
741
|
+
const effectFn = vi.fn();
|
|
742
|
+
effect((t) => effectFn($array.get(t)));
|
|
743
|
+
|
|
744
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
|
|
745
|
+
expect(effectFn).toHaveBeenLastCalledWith([0, 1, 2]);
|
|
746
|
+
|
|
747
|
+
await $array.set([1, 2, 3]);
|
|
748
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
|
|
749
|
+
expect(effectFn).toHaveBeenLastCalledWith([1, 2, 3]);
|
|
750
|
+
});
|
|
751
|
+
|
|
752
|
+
it("should call effect when updated with push/pop", async () => {
|
|
753
|
+
const $array = array<number>();
|
|
754
|
+
const effectFn = vi.fn();
|
|
755
|
+
effect((t) => effectFn($array.get(t)));
|
|
756
|
+
|
|
757
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
|
|
758
|
+
expect(effectFn).toHaveBeenLastCalledWith([]);
|
|
759
|
+
|
|
760
|
+
$array.push(0);
|
|
761
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
|
|
762
|
+
expect(effectFn).toHaveBeenLastCalledWith([0]);
|
|
763
|
+
|
|
764
|
+
$array.push(1);
|
|
765
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(3));
|
|
766
|
+
expect(effectFn).toHaveBeenLastCalledWith([0, 1]);
|
|
767
|
+
|
|
768
|
+
$array.push(2);
|
|
769
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(4));
|
|
770
|
+
expect(effectFn).toHaveBeenLastCalledWith([0, 1, 2]);
|
|
771
|
+
|
|
772
|
+
$array.pop();
|
|
773
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(5));
|
|
774
|
+
expect(effectFn).toHaveBeenLastCalledWith([0, 1]);
|
|
775
|
+
|
|
776
|
+
$array.pop();
|
|
777
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(6));
|
|
778
|
+
expect(effectFn).toHaveBeenLastCalledWith([0]);
|
|
779
|
+
|
|
780
|
+
$array.pop();
|
|
781
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(7));
|
|
782
|
+
expect(effectFn).toHaveBeenLastCalledWith([]);
|
|
783
|
+
});
|
|
784
|
+
|
|
785
|
+
it("should call effect when updated with unshift/shift", async () => {
|
|
786
|
+
const $array = array<number>();
|
|
787
|
+
const effectFn = vi.fn();
|
|
788
|
+
effect((t) => effectFn($array.get(t)));
|
|
789
|
+
|
|
790
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
|
|
791
|
+
expect(effectFn).toHaveBeenLastCalledWith([]);
|
|
792
|
+
|
|
793
|
+
$array.unshift(0);
|
|
794
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
|
|
795
|
+
expect(effectFn).toHaveBeenLastCalledWith([0]);
|
|
796
|
+
|
|
797
|
+
$array.unshift(1);
|
|
798
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(3));
|
|
799
|
+
expect(effectFn).toHaveBeenLastCalledWith([1, 0]);
|
|
800
|
+
|
|
801
|
+
$array.unshift(2);
|
|
802
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(4));
|
|
803
|
+
expect(effectFn).toHaveBeenLastCalledWith([2, 1, 0]);
|
|
804
|
+
|
|
805
|
+
$array.shift();
|
|
806
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(5));
|
|
807
|
+
expect(effectFn).toHaveBeenLastCalledWith([1, 0]);
|
|
808
|
+
|
|
809
|
+
$array.shift();
|
|
810
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(6));
|
|
811
|
+
expect(effectFn).toHaveBeenLastCalledWith([0]);
|
|
812
|
+
|
|
813
|
+
$array.shift();
|
|
814
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(7));
|
|
815
|
+
expect(effectFn).toHaveBeenLastCalledWith([]);
|
|
816
|
+
});
|
|
817
|
+
|
|
818
|
+
it("should call effect when updated with splice", async () => {
|
|
819
|
+
const $array = array<number>([0, 1, 2, 3, 4]);
|
|
820
|
+
const effectFn = vi.fn();
|
|
821
|
+
effect((t) => effectFn($array.get(t)));
|
|
822
|
+
|
|
823
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
|
|
824
|
+
expect(effectFn).toHaveBeenLastCalledWith([0, 1, 2, 3, 4]);
|
|
825
|
+
|
|
826
|
+
$array.splice(1, 2);
|
|
827
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
|
|
828
|
+
expect(effectFn).toHaveBeenLastCalledWith([0, 3, 4]);
|
|
829
|
+
|
|
830
|
+
$array.splice(1, 0, 1, 2);
|
|
831
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(3));
|
|
832
|
+
expect(effectFn).toHaveBeenLastCalledWith([0, 1, 2, 3, 4]);
|
|
833
|
+
});
|
|
834
|
+
|
|
835
|
+
it("should call effect when updated with clear", async () => {
|
|
836
|
+
const $array = array<number>([0, 1, 2, 3, 4]);
|
|
837
|
+
const effectFn = vi.fn();
|
|
838
|
+
effect((t) => effectFn($array.get(t)));
|
|
839
|
+
|
|
840
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
|
|
841
|
+
expect(effectFn).toHaveBeenLastCalledWith([0, 1, 2, 3, 4]);
|
|
842
|
+
|
|
843
|
+
$array.clear();
|
|
844
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
|
|
845
|
+
expect(effectFn).toHaveBeenLastCalledWith([]);
|
|
846
|
+
});
|
|
847
|
+
});
|
|
848
|
+
|
|
849
|
+
describe("item reactivity", () => {
|
|
850
|
+
it("should call effect when item is updated", async () => {
|
|
851
|
+
const $array = array<number>([0, 1, 2]);
|
|
852
|
+
const effectFn = vi.fn();
|
|
853
|
+
const effect0Fn = vi.fn();
|
|
854
|
+
const effect1Fn = vi.fn();
|
|
855
|
+
const effect2Fn = vi.fn();
|
|
856
|
+
|
|
857
|
+
effect((t) => effectFn($array.get(t)));
|
|
858
|
+
effect((t) => effect0Fn($array.get(t)[0]));
|
|
859
|
+
effect((t) => effect1Fn($array.get(t)[1]));
|
|
860
|
+
effect((t) => effect2Fn($array.get(t)[2]));
|
|
861
|
+
|
|
862
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
|
|
863
|
+
expect(effectFn).toHaveBeenLastCalledWith([0, 1, 2]);
|
|
864
|
+
await vi.waitFor(() => expect(effect0Fn).toHaveBeenCalledTimes(1));
|
|
865
|
+
expect(effect0Fn).toHaveBeenLastCalledWith(0);
|
|
866
|
+
await vi.waitFor(() => expect(effect1Fn).toHaveBeenCalledTimes(1));
|
|
867
|
+
expect(effect1Fn).toHaveBeenLastCalledWith(1);
|
|
868
|
+
await vi.waitFor(() => expect(effect2Fn).toHaveBeenCalledTimes(1));
|
|
869
|
+
expect(effect2Fn).toHaveBeenLastCalledWith(2);
|
|
870
|
+
|
|
871
|
+
$array.update(0, 1);
|
|
872
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
|
|
873
|
+
expect(effectFn).toHaveBeenLastCalledWith([1, 1, 2]);
|
|
874
|
+
await vi.waitFor(() => expect(effect0Fn).toHaveBeenCalledTimes(2));
|
|
875
|
+
expect(effect0Fn).toHaveBeenLastCalledWith(1);
|
|
876
|
+
await vi.waitFor(() => expect(effect1Fn).toHaveBeenCalledTimes(2));
|
|
877
|
+
expect(effect1Fn).toHaveBeenLastCalledWith(1);
|
|
878
|
+
await vi.waitFor(() => expect(effect2Fn).toHaveBeenCalledTimes(2));
|
|
879
|
+
expect(effect2Fn).toHaveBeenLastCalledWith(2);
|
|
880
|
+
|
|
881
|
+
$array.update(1, 2);
|
|
882
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(3));
|
|
883
|
+
expect(effectFn).toHaveBeenLastCalledWith([1, 2, 2]);
|
|
884
|
+
await vi.waitFor(() => expect(effect0Fn).toHaveBeenCalledTimes(3));
|
|
885
|
+
expect(effect0Fn).toHaveBeenLastCalledWith(1);
|
|
886
|
+
await vi.waitFor(() => expect(effect1Fn).toHaveBeenCalledTimes(3));
|
|
887
|
+
expect(effect1Fn).toHaveBeenLastCalledWith(2);
|
|
888
|
+
await vi.waitFor(() => expect(effect2Fn).toHaveBeenCalledTimes(3));
|
|
889
|
+
expect(effect2Fn).toHaveBeenLastCalledWith(2);
|
|
890
|
+
|
|
891
|
+
$array.update(2, 3);
|
|
892
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(4));
|
|
893
|
+
expect(effectFn).toHaveBeenLastCalledWith([1, 2, 3]);
|
|
894
|
+
await vi.waitFor(() => expect(effect0Fn).toHaveBeenCalledTimes(4));
|
|
895
|
+
expect(effect0Fn).toHaveBeenLastCalledWith(1);
|
|
896
|
+
await vi.waitFor(() => expect(effect1Fn).toHaveBeenCalledTimes(4));
|
|
897
|
+
expect(effect1Fn).toHaveBeenLastCalledWith(2);
|
|
898
|
+
await vi.waitFor(() => expect(effect2Fn).toHaveBeenCalledTimes(4));
|
|
899
|
+
expect(effect2Fn).toHaveBeenLastCalledWith(3);
|
|
900
|
+
});
|
|
901
|
+
|
|
902
|
+
it("should call effect when item is replaced", async () => {
|
|
903
|
+
const $array = array<number>([0, 1, 2]);
|
|
904
|
+
const effectFn = vi.fn();
|
|
905
|
+
const effect0Fn = vi.fn();
|
|
906
|
+
|
|
907
|
+
effect((t) => effectFn($array.get(t)));
|
|
908
|
+
effect((t) => effect0Fn($array.get(t)[0]));
|
|
909
|
+
|
|
910
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
|
|
911
|
+
expect(effectFn).toHaveBeenLastCalledWith([0, 1, 2]);
|
|
912
|
+
await vi.waitFor(() => expect(effect0Fn).toHaveBeenCalledTimes(1));
|
|
913
|
+
expect(effect0Fn).toHaveBeenLastCalledWith(0);
|
|
914
|
+
|
|
915
|
+
$array.update(0, 3);
|
|
916
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
|
|
917
|
+
expect(effectFn).toHaveBeenLastCalledWith([3, 1, 2]);
|
|
918
|
+
await vi.waitFor(() => expect(effect0Fn).toHaveBeenCalledTimes(2));
|
|
919
|
+
expect(effect0Fn).toHaveBeenLastCalledWith(3);
|
|
920
|
+
|
|
921
|
+
$array.shift();
|
|
922
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(3));
|
|
923
|
+
expect(effectFn).toHaveBeenLastCalledWith([1, 2]);
|
|
924
|
+
await vi.waitFor(() => expect(effect0Fn).toHaveBeenCalledTimes(3));
|
|
925
|
+
expect(effect0Fn).toHaveBeenLastCalledWith(1);
|
|
926
|
+
|
|
927
|
+
$array.unshift(4);
|
|
928
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(4));
|
|
929
|
+
expect(effectFn).toHaveBeenLastCalledWith([4, 1, 2]);
|
|
930
|
+
await vi.waitFor(() => expect(effect0Fn).toHaveBeenCalledTimes(4));
|
|
931
|
+
expect(effect0Fn).toHaveBeenLastCalledWith(4);
|
|
932
|
+
});
|
|
933
|
+
});
|
|
934
|
+
|
|
935
|
+
describe("lastAction reactivity", () => {
|
|
936
|
+
describe("set action", () => {
|
|
937
|
+
it("should call effect when $lastAction is updated with set action", async () => {
|
|
938
|
+
const $array = array([0, 1, 2]);
|
|
939
|
+
const effectFn = vi.fn();
|
|
940
|
+
effect((t) => effectFn($array.$lastAction.get(t)));
|
|
941
|
+
|
|
942
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
|
|
943
|
+
expect(effectFn).toHaveBeenLastCalledWith({
|
|
944
|
+
type: "set",
|
|
945
|
+
items: [0, 1, 2],
|
|
946
|
+
});
|
|
947
|
+
|
|
948
|
+
await $array.set([1, 2, 3]);
|
|
949
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
|
|
950
|
+
expect(effectFn).toHaveBeenLastCalledWith({
|
|
951
|
+
type: "set",
|
|
952
|
+
items: [1, 2, 3],
|
|
953
|
+
});
|
|
954
|
+
});
|
|
955
|
+
});
|
|
956
|
+
|
|
957
|
+
describe("setItem action", () => {
|
|
958
|
+
it("should call effect when $lastAction is updated with setItem action", async () => {
|
|
959
|
+
const $array = array([0, 1, 2]);
|
|
960
|
+
const effectFn = vi.fn();
|
|
961
|
+
effect((t) => effectFn($array.$lastAction.get(t)));
|
|
962
|
+
|
|
963
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
|
|
964
|
+
|
|
965
|
+
$array.update(1, 10);
|
|
966
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
|
|
967
|
+
expect(effectFn).toHaveBeenLastCalledWith({
|
|
968
|
+
type: "update",
|
|
969
|
+
index: 1,
|
|
970
|
+
item: 10,
|
|
971
|
+
});
|
|
972
|
+
});
|
|
973
|
+
});
|
|
974
|
+
|
|
975
|
+
describe("push action", () => {
|
|
976
|
+
it("should call effect when $lastAction is updated with push action", async () => {
|
|
977
|
+
const $array = array<number>();
|
|
978
|
+
const effectFn = vi.fn();
|
|
979
|
+
effect((t) => effectFn($array.$lastAction.get(t)));
|
|
980
|
+
|
|
981
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
|
|
982
|
+
expect(effectFn).toHaveBeenLastCalledWith({
|
|
983
|
+
type: "set",
|
|
984
|
+
items: [],
|
|
985
|
+
});
|
|
986
|
+
|
|
987
|
+
$array.push(0);
|
|
988
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
|
|
989
|
+
expect(effectFn).toHaveBeenLastCalledWith({
|
|
990
|
+
type: "push",
|
|
991
|
+
item: 0,
|
|
992
|
+
});
|
|
993
|
+
|
|
994
|
+
$array.push(1);
|
|
995
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(3));
|
|
996
|
+
expect(effectFn).toHaveBeenLastCalledWith({
|
|
997
|
+
type: "push",
|
|
998
|
+
item: 1,
|
|
999
|
+
});
|
|
1000
|
+
});
|
|
1001
|
+
});
|
|
1002
|
+
|
|
1003
|
+
describe("pop action", () => {
|
|
1004
|
+
it("should call effect when $lastAction is updated with pop action", async () => {
|
|
1005
|
+
const $array = array<number>([0, 1, 2]);
|
|
1006
|
+
const effectFn = vi.fn();
|
|
1007
|
+
effect((t) => effectFn($array.$lastAction.get(t)));
|
|
1008
|
+
|
|
1009
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
|
|
1010
|
+
|
|
1011
|
+
$array.pop();
|
|
1012
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
|
|
1013
|
+
expect(effectFn).toHaveBeenLastCalledWith({
|
|
1014
|
+
type: "pop",
|
|
1015
|
+
});
|
|
1016
|
+
|
|
1017
|
+
$array.pop();
|
|
1018
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(3));
|
|
1019
|
+
expect(effectFn).toHaveBeenLastCalledWith({
|
|
1020
|
+
type: "pop",
|
|
1021
|
+
});
|
|
1022
|
+
});
|
|
1023
|
+
});
|
|
1024
|
+
|
|
1025
|
+
describe("unshift action", () => {
|
|
1026
|
+
it("should call effect when $lastAction is updated with unshift action", async () => {
|
|
1027
|
+
const $array = array<number>();
|
|
1028
|
+
const effectFn = vi.fn();
|
|
1029
|
+
effect((t) => effectFn($array.$lastAction.get(t)));
|
|
1030
|
+
|
|
1031
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
|
|
1032
|
+
expect(effectFn).toHaveBeenLastCalledWith({
|
|
1033
|
+
type: "set",
|
|
1034
|
+
items: [],
|
|
1035
|
+
});
|
|
1036
|
+
|
|
1037
|
+
$array.unshift(0);
|
|
1038
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
|
|
1039
|
+
expect(effectFn).toHaveBeenLastCalledWith({
|
|
1040
|
+
type: "unshift",
|
|
1041
|
+
item: 0,
|
|
1042
|
+
});
|
|
1043
|
+
|
|
1044
|
+
$array.unshift(1);
|
|
1045
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(3));
|
|
1046
|
+
expect(effectFn).toHaveBeenLastCalledWith({
|
|
1047
|
+
type: "unshift",
|
|
1048
|
+
item: 1,
|
|
1049
|
+
});
|
|
1050
|
+
});
|
|
1051
|
+
});
|
|
1052
|
+
|
|
1053
|
+
describe("shift action", () => {
|
|
1054
|
+
it("should call effect when $lastAction is updated with shift action", async () => {
|
|
1055
|
+
const $array = array<number>([0, 1, 2]);
|
|
1056
|
+
const effectFn = vi.fn();
|
|
1057
|
+
effect((t) => effectFn($array.$lastAction.get(t)));
|
|
1058
|
+
|
|
1059
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
|
|
1060
|
+
|
|
1061
|
+
$array.shift();
|
|
1062
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
|
|
1063
|
+
expect(effectFn).toHaveBeenLastCalledWith({
|
|
1064
|
+
type: "shift",
|
|
1065
|
+
});
|
|
1066
|
+
|
|
1067
|
+
$array.shift();
|
|
1068
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(3));
|
|
1069
|
+
expect(effectFn).toHaveBeenLastCalledWith({
|
|
1070
|
+
type: "shift",
|
|
1071
|
+
});
|
|
1072
|
+
});
|
|
1073
|
+
});
|
|
1074
|
+
|
|
1075
|
+
describe("splice action", () => {
|
|
1076
|
+
it("should call effect when $lastAction is updated with splice action", async () => {
|
|
1077
|
+
const $array = array<number>([0, 1, 2, 3, 4]);
|
|
1078
|
+
const effectFn = vi.fn();
|
|
1079
|
+
effect((t) => effectFn($array.$lastAction.get(t)));
|
|
1080
|
+
|
|
1081
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
|
|
1082
|
+
expect(effectFn).toHaveBeenLastCalledWith({
|
|
1083
|
+
type: "set",
|
|
1084
|
+
items: [0, 1, 2, 3, 4],
|
|
1085
|
+
});
|
|
1086
|
+
|
|
1087
|
+
$array.splice(1, 2);
|
|
1088
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
|
|
1089
|
+
expect(effectFn).toHaveBeenLastCalledWith({
|
|
1090
|
+
type: "splice",
|
|
1091
|
+
start: 1,
|
|
1092
|
+
deleteCount: 2,
|
|
1093
|
+
items: [],
|
|
1094
|
+
});
|
|
1095
|
+
|
|
1096
|
+
$array.splice(1, 0, 1, 2);
|
|
1097
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(3));
|
|
1098
|
+
expect(effectFn).toHaveBeenLastCalledWith({
|
|
1099
|
+
type: "splice",
|
|
1100
|
+
start: 1,
|
|
1101
|
+
deleteCount: 0,
|
|
1102
|
+
items: [1, 2],
|
|
1103
|
+
});
|
|
1104
|
+
});
|
|
1105
|
+
});
|
|
1106
|
+
|
|
1107
|
+
describe("clear action", () => {
|
|
1108
|
+
it("should call effect when $lastAction is updated with clear action", async () => {
|
|
1109
|
+
const $array = array<number>([0, 1, 2, 3, 4]);
|
|
1110
|
+
const effectFn = vi.fn();
|
|
1111
|
+
effect((t) => effectFn($array.$lastAction.get(t)));
|
|
1112
|
+
|
|
1113
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
|
|
1114
|
+
expect(effectFn).toHaveBeenLastCalledWith({
|
|
1115
|
+
type: "set",
|
|
1116
|
+
items: [0, 1, 2, 3, 4],
|
|
1117
|
+
});
|
|
1118
|
+
|
|
1119
|
+
$array.clear();
|
|
1120
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
|
|
1121
|
+
expect(effectFn).toHaveBeenLastCalledWith({
|
|
1122
|
+
type: "clear",
|
|
1123
|
+
});
|
|
1124
|
+
});
|
|
1125
|
+
});
|
|
1126
|
+
});
|
|
1127
|
+
});
|
|
1128
|
+
});
|
|
1129
|
+
});
|