@ersbeth/picoflow 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. package/api/doc/picoflow.array.md +55 -0
  2. package/api/doc/picoflow.flowarray._constructor_.md +49 -0
  3. package/api/doc/picoflow.flowarray._lastaction.md +13 -0
  4. package/api/doc/picoflow.flowarray.clear.md +17 -0
  5. package/api/doc/picoflow.flowarray.dispose.md +55 -0
  6. package/api/doc/picoflow.flowarray.get.md +19 -0
  7. package/api/doc/picoflow.flowarray.length.md +13 -0
  8. package/api/doc/picoflow.flowarray.md +273 -0
  9. package/api/doc/picoflow.flowarray.pop.md +17 -0
  10. package/api/doc/picoflow.flowarray.push.md +53 -0
  11. package/api/doc/picoflow.flowarray.set.md +53 -0
  12. package/api/doc/picoflow.flowarray.setitem.md +69 -0
  13. package/api/doc/picoflow.flowarray.shift.md +17 -0
  14. package/api/doc/picoflow.flowarray.splice.md +85 -0
  15. package/api/doc/picoflow.flowarray.unshift.md +53 -0
  16. package/api/doc/picoflow.flowarrayaction.md +37 -0
  17. package/api/doc/picoflow.flowdisposable.dispose.md +55 -0
  18. package/api/doc/picoflow.flowdisposable.md +43 -0
  19. package/api/doc/picoflow.flowsignal.dispose.md +39 -1
  20. package/api/doc/picoflow.flowsignal.md +3 -2
  21. package/api/doc/picoflow.isdisposable.md +55 -0
  22. package/api/doc/picoflow.md +70 -0
  23. package/api/picoflow.public.api.md +63 -2
  24. package/dist/picoflow.js +188 -4
  25. package/dist/types/advanced/array.d.ts +116 -0
  26. package/dist/types/advanced/array.d.ts.map +1 -0
  27. package/dist/types/advanced/index.d.ts +2 -0
  28. package/dist/types/advanced/index.d.ts.map +1 -1
  29. package/dist/types/basic/disposable.d.ts +23 -0
  30. package/dist/types/basic/disposable.d.ts.map +1 -0
  31. package/dist/types/basic/index.d.ts +2 -0
  32. package/dist/types/basic/index.d.ts.map +1 -1
  33. package/dist/types/basic/signal.d.ts +5 -2
  34. package/dist/types/basic/signal.d.ts.map +1 -1
  35. package/dist/types/creators.d.ts +9 -0
  36. package/dist/types/creators.d.ts.map +1 -1
  37. package/dist/types/index.d.ts +4 -3
  38. package/dist/types/index.d.ts.map +1 -1
  39. package/package.json +1 -1
  40. package/src/advanced/array.ts +224 -0
  41. package/src/advanced/index.ts +2 -0
  42. package/src/basic/disposable.ts +27 -0
  43. package/src/basic/index.ts +2 -0
  44. package/src/basic/observable.ts +2 -2
  45. package/src/basic/signal.ts +17 -5
  46. package/src/creators.ts +12 -0
  47. package/src/index.ts +5 -0
  48. package/test/array.test.ts +620 -0
@@ -0,0 +1,620 @@
1
+ import { describe, expect, test, vi } from "vitest";
2
+ import { type FlowState, array, effect, state } from "#package";
3
+
4
+ describe("array", () => {
5
+ test("is initialized", () => {
6
+ const $array = array([state(1), state(2), state(3)]);
7
+
8
+ expect($array.get().map((state) => state.get())).toEqual([1, 2, 3]);
9
+ });
10
+
11
+ test("is updated (set)", () => {
12
+ const $array = array<FlowState<number>>([state(0), state(1), state(2)]);
13
+ expect($array.get().map((state) => state.get())).toEqual([0, 1, 2]);
14
+
15
+ $array.set([state(1), state(2), state(3)]);
16
+ expect($array.get().map((state) => state.get())).toEqual([1, 2, 3]);
17
+ });
18
+
19
+ test("is updated (setItem)", () => {
20
+ const $array = array<FlowState<number>>([state(0), state(1), state(2)]);
21
+ expect($array.get().map((state) => state.get())).toEqual([0, 1, 2]);
22
+
23
+ $array.setItem(0, state(1));
24
+ expect($array.get().map((state) => state.get())).toEqual([1, 1, 2]);
25
+
26
+ $array.setItem(1, state(2));
27
+ expect($array.get().map((state) => state.get())).toEqual([1, 2, 2]);
28
+
29
+ $array.setItem(2, state(3));
30
+ expect($array.get().map((state) => state.get())).toEqual([1, 2, 3]);
31
+ });
32
+
33
+ test("is updated (push/pop)", () => {
34
+ const $array = array<FlowState<number>>();
35
+ expect($array.get()).toEqual([]);
36
+
37
+ $array.push(state(0));
38
+ expect($array.get().map((state) => state.get())).toEqual([0]);
39
+
40
+ $array.push(state(1));
41
+ expect($array.get().map((state) => state.get())).toEqual([0, 1]);
42
+
43
+ $array.push(state(2));
44
+ expect($array.get().map((state) => state.get())).toEqual([0, 1, 2]);
45
+
46
+ $array.pop();
47
+ expect($array.get().map((state) => state.get())).toEqual([0, 1]);
48
+
49
+ $array.pop();
50
+ expect($array.get().map((state) => state.get())).toEqual([0]);
51
+
52
+ $array.pop();
53
+ expect($array.get().map((state) => state.get())).toEqual([]);
54
+ });
55
+
56
+ test("is updated (unshift/shift)", () => {
57
+ const $array = array<FlowState<number>>();
58
+ expect($array.get()).toEqual([]);
59
+
60
+ $array.unshift(state(0));
61
+ expect($array.get().map((state) => state.get())).toEqual([0]);
62
+
63
+ $array.unshift(state(1));
64
+ expect($array.get().map((state) => state.get())).toEqual([1, 0]);
65
+
66
+ $array.unshift(state(2));
67
+ expect($array.get().map((state) => state.get())).toEqual([2, 1, 0]);
68
+
69
+ $array.shift();
70
+ expect($array.get().map((state) => state.get())).toEqual([1, 0]);
71
+
72
+ $array.shift();
73
+ expect($array.get().map((state) => state.get())).toEqual([0]);
74
+
75
+ $array.shift();
76
+ expect($array.get().map((state) => state.get())).toEqual([]);
77
+ });
78
+
79
+ test("is updated (splice)", () => {
80
+ const $array = array<FlowState<number>>([
81
+ state(0),
82
+ state(1),
83
+ state(2),
84
+ state(3),
85
+ state(4),
86
+ ]);
87
+ expect($array.get().map((state) => state.get())).toEqual([
88
+ 0, 1, 2, 3, 4,
89
+ ]);
90
+
91
+ $array.splice(1, 2);
92
+ expect($array.get().map((state) => state.get())).toEqual([0, 3, 4]);
93
+
94
+ $array.splice(1, 0, state(1), state(2));
95
+ expect($array.get().map((state) => state.get())).toEqual([
96
+ 0, 1, 2, 3, 4,
97
+ ]);
98
+ });
99
+
100
+ test("is updated (clear)", () => {
101
+ const $array = array<FlowState<number>>([
102
+ state(0),
103
+ state(1),
104
+ state(2),
105
+ state(3),
106
+ state(4),
107
+ ]);
108
+ expect($array.get().map((state) => state.get())).toEqual([
109
+ 0, 1, 2, 3, 4,
110
+ ]);
111
+
112
+ $array.clear();
113
+ expect($array.get().map((state) => state.get())).toEqual([]);
114
+ });
115
+
116
+ test("is item updated", () => {
117
+ const $array = array<FlowState<number>>([
118
+ state(0),
119
+ state(1),
120
+ state(2),
121
+ state(3),
122
+ state(4),
123
+ ]);
124
+ expect($array.get().map((state) => state.get())).toEqual([
125
+ 0, 1, 2, 3, 4,
126
+ ]);
127
+
128
+ $array.get()[0].set(1);
129
+ expect($array.get().map((state) => state.get())).toEqual([
130
+ 1, 1, 2, 3, 4,
131
+ ]);
132
+
133
+ $array.get()[1].set(2);
134
+ expect($array.get().map((state) => state.get())).toEqual([
135
+ 1, 2, 2, 3, 4,
136
+ ]);
137
+
138
+ $array.get()[2].set(3);
139
+ expect($array.get().map((state) => state.get())).toEqual([
140
+ 1, 2, 3, 3, 4,
141
+ ]);
142
+
143
+ $array.get()[3].set(4);
144
+ expect($array.get().map((state) => state.get())).toEqual([
145
+ 1, 2, 3, 4, 4,
146
+ ]);
147
+
148
+ $array.get()[4].set(5);
149
+ expect($array.get().map((state) => state.get())).toEqual([
150
+ 1, 2, 3, 4, 5,
151
+ ]);
152
+ });
153
+
154
+ test("throws when disposed (get)", () => {
155
+ const $array = array([state(1), state(2), state(3)]);
156
+ $array.dispose();
157
+ expect(() => $array.get()).toThrowError(
158
+ "[PicoFlow] Primitive is disposed",
159
+ );
160
+ });
161
+
162
+ test("throws when disposed (set)", () => {
163
+ const $array = array([state(1), state(2), state(3)]);
164
+ $array.dispose();
165
+ expect(() => $array.set([state(1)])).toThrowError(
166
+ "[PicoFlow] Primitive is disposed",
167
+ );
168
+ });
169
+
170
+ test("throws when disposed (setItem)", () => {
171
+ const $array = array([state(1), state(2), state(3)]);
172
+ $array.dispose();
173
+ expect(() => $array.setItem(0, state(3))).toThrowError(
174
+ "[PicoFlow] Primitive is disposed",
175
+ );
176
+ });
177
+
178
+ test("throws when disposed (push)", () => {
179
+ const $array = array([state(1), state(2), state(3)]);
180
+ $array.dispose();
181
+ expect(() => $array.push(state(3))).toThrowError(
182
+ "[PicoFlow] Primitive is disposed",
183
+ );
184
+ });
185
+
186
+ test("throws when disposed (pop)", () => {
187
+ const $array = array([state(1), state(2), state(3)]);
188
+ $array.dispose();
189
+ expect(() => $array.pop()).toThrowError(
190
+ "[PicoFlow] Primitive is disposed",
191
+ );
192
+ });
193
+
194
+ test("throws when disposed (unshift)", () => {
195
+ const $array = array([state(1), state(2), state(3)]);
196
+ $array.dispose();
197
+ expect(() => $array.unshift(state(3))).toThrowError(
198
+ "[PicoFlow] Primitive is disposed",
199
+ );
200
+ });
201
+
202
+ test("throws when disposed (shift)", () => {
203
+ const $array = array([state(1), state(2), state(3)]);
204
+ $array.dispose();
205
+ expect(() => $array.shift()).toThrowError(
206
+ "[PicoFlow] Primitive is disposed",
207
+ );
208
+ });
209
+
210
+ test("throws when disposed (splice)", () => {
211
+ const $array = array<FlowState<number>>([
212
+ state(0),
213
+ state(1),
214
+ state(2),
215
+ state(3),
216
+ state(4),
217
+ ]);
218
+ $array.dispose();
219
+ expect(() => $array.splice(1, 2)).toThrowError(
220
+ "[PicoFlow] Primitive is disposed",
221
+ );
222
+ });
223
+ });
224
+
225
+ describe("effect", () => {
226
+ test("called when initialized", () => {
227
+ const $array = array([state(1), state(2), state(3)]);
228
+ const effectFn = vi.fn();
229
+ effect((get) => effectFn(get($array).map((state) => get(state))));
230
+
231
+ expect(effectFn).toHaveBeenCalledTimes(1);
232
+ expect(effectFn).toHaveBeenLastCalledWith([1, 2, 3]);
233
+ });
234
+
235
+ test("called when updated (set)", () => {
236
+ const $array = array<FlowState<number>>([state(0), state(1), state(2)]);
237
+ const effectFn = vi.fn();
238
+ effect((get) => effectFn(get($array).map((state) => get(state))));
239
+
240
+ expect(effectFn).toHaveBeenCalledTimes(1);
241
+ expect(effectFn).toHaveBeenLastCalledWith([0, 1, 2]);
242
+
243
+ $array.set([state(1), state(2), state(3)]);
244
+ expect(effectFn).toHaveBeenLastCalledWith([1, 2, 3]);
245
+ });
246
+
247
+ test("called when updated (push/pop)", () => {
248
+ const $array = array<FlowState<number>>();
249
+ const effectFn = vi.fn();
250
+ effect((get) => effectFn(get($array).map((state) => get(state))));
251
+
252
+ expect(effectFn).toHaveBeenCalledTimes(1);
253
+ expect(effectFn).toHaveBeenLastCalledWith([]);
254
+
255
+ $array.push(state(0));
256
+ expect(effectFn).toHaveBeenCalledTimes(2);
257
+ expect(effectFn).toHaveBeenLastCalledWith([0]);
258
+
259
+ $array.push(state(1));
260
+ expect(effectFn).toHaveBeenCalledTimes(3);
261
+ expect(effectFn).toHaveBeenLastCalledWith([0, 1]);
262
+
263
+ $array.push(state(2));
264
+ expect(effectFn).toHaveBeenCalledTimes(4);
265
+ expect(effectFn).toHaveBeenLastCalledWith([0, 1, 2]);
266
+
267
+ $array.pop();
268
+ expect(effectFn).toHaveBeenCalledTimes(5);
269
+ expect(effectFn).toHaveBeenLastCalledWith([0, 1]);
270
+
271
+ $array.pop();
272
+ expect(effectFn).toHaveBeenCalledTimes(6);
273
+ expect(effectFn).toHaveBeenLastCalledWith([0]);
274
+
275
+ $array.pop();
276
+ expect(effectFn).toHaveBeenCalledTimes(7);
277
+ expect(effectFn).toHaveBeenLastCalledWith([]);
278
+ });
279
+
280
+ test("called when updated (unshift/shift)", () => {
281
+ const $array = array<FlowState<number>>();
282
+ const effectFn = vi.fn();
283
+ effect((get) => effectFn(get($array).map((state) => get(state))));
284
+
285
+ expect(effectFn).toHaveBeenCalledTimes(1);
286
+ expect(effectFn).toHaveBeenLastCalledWith([]);
287
+
288
+ $array.unshift(state(0));
289
+ expect(effectFn).toHaveBeenCalledTimes(2);
290
+ expect(effectFn).toHaveBeenLastCalledWith([0]);
291
+
292
+ $array.unshift(state(1));
293
+ expect(effectFn).toHaveBeenCalledTimes(3);
294
+ expect(effectFn).toHaveBeenLastCalledWith([1, 0]);
295
+
296
+ $array.unshift(state(2));
297
+ expect(effectFn).toHaveBeenCalledTimes(4);
298
+ expect(effectFn).toHaveBeenLastCalledWith([2, 1, 0]);
299
+
300
+ $array.shift();
301
+ expect(effectFn).toHaveBeenCalledTimes(5);
302
+ expect(effectFn).toHaveBeenLastCalledWith([1, 0]);
303
+
304
+ $array.shift();
305
+ expect(effectFn).toHaveBeenCalledTimes(6);
306
+ expect(effectFn).toHaveBeenLastCalledWith([0]);
307
+
308
+ $array.shift();
309
+ expect(effectFn).toHaveBeenCalledTimes(7);
310
+ expect(effectFn).toHaveBeenLastCalledWith([]);
311
+ });
312
+
313
+ test("called when updated (splice)", () => {
314
+ const $array = array<FlowState<number>>([
315
+ state(0),
316
+ state(1),
317
+ state(2),
318
+ state(3),
319
+ state(4),
320
+ ]);
321
+ const effectFn = vi.fn();
322
+ effect((get) => effectFn(get($array).map((state) => get(state))));
323
+
324
+ expect(effectFn).toHaveBeenCalledTimes(1);
325
+ expect(effectFn).toHaveBeenLastCalledWith([0, 1, 2, 3, 4]);
326
+
327
+ $array.splice(1, 2);
328
+ expect(effectFn).toHaveBeenCalledTimes(2);
329
+ expect(effectFn).toHaveBeenLastCalledWith([0, 3, 4]);
330
+
331
+ $array.splice(1, 0, state(1), state(2));
332
+ expect(effectFn).toHaveBeenCalledTimes(3);
333
+ expect(effectFn).toHaveBeenLastCalledWith([0, 1, 2, 3, 4]);
334
+ });
335
+
336
+ test("called when updated (clear)", () => {
337
+ const $array = array<FlowState<number>>([
338
+ state(0),
339
+ state(1),
340
+ state(2),
341
+ state(3),
342
+ state(4),
343
+ ]);
344
+ const effectFn = vi.fn();
345
+ effect((get) => effectFn(get($array).map((state) => get(state))));
346
+
347
+ expect(effectFn).toHaveBeenCalledTimes(1);
348
+ expect(effectFn).toHaveBeenLastCalledWith([0, 1, 2, 3, 4]);
349
+
350
+ $array.clear();
351
+ expect(effectFn).toHaveBeenCalledTimes(2);
352
+ expect(effectFn).toHaveBeenLastCalledWith([]);
353
+ });
354
+
355
+ test("called when item updated", () => {
356
+ const $array = array<FlowState<number>>([state(0), state(1), state(2)]);
357
+ const effectFn = vi.fn();
358
+ const effect0Fn = vi.fn();
359
+ const effect1Fn = vi.fn();
360
+ const effect2Fn = vi.fn();
361
+
362
+ effect((get) => effectFn(get($array).map((state) => get(state))));
363
+ effect((get) => effect0Fn(get(get($array)[0])));
364
+ effect((get) => effect1Fn(get(get($array)[1])));
365
+ effect((get) => effect2Fn(get(get($array)[2])));
366
+
367
+ expect(effectFn).toHaveBeenCalledTimes(1);
368
+ expect(effectFn).toHaveBeenLastCalledWith([0, 1, 2]);
369
+ expect(effect0Fn).toHaveBeenCalledTimes(1);
370
+ expect(effect0Fn).toHaveBeenLastCalledWith(0);
371
+ expect(effect1Fn).toHaveBeenCalledTimes(1);
372
+ expect(effect1Fn).toHaveBeenLastCalledWith(1);
373
+ expect(effect2Fn).toHaveBeenCalledTimes(1);
374
+ expect(effect2Fn).toHaveBeenLastCalledWith(2);
375
+
376
+ $array.get()[0].set(1);
377
+ expect(effectFn).toHaveBeenCalledTimes(2);
378
+ expect(effectFn).toHaveBeenLastCalledWith([1, 1, 2]);
379
+ expect(effect0Fn).toHaveBeenCalledTimes(2);
380
+ expect(effect0Fn).toHaveBeenLastCalledWith(1);
381
+ expect(effect1Fn).toHaveBeenCalledTimes(1);
382
+ expect(effect1Fn).toHaveBeenLastCalledWith(1);
383
+ expect(effect2Fn).toHaveBeenCalledTimes(1);
384
+ expect(effect2Fn).toHaveBeenLastCalledWith(2);
385
+
386
+ $array.get()[1].set(2);
387
+ expect(effectFn).toHaveBeenCalledTimes(3);
388
+ expect(effectFn).toHaveBeenLastCalledWith([1, 2, 2]);
389
+ expect(effect0Fn).toHaveBeenCalledTimes(2);
390
+ expect(effect0Fn).toHaveBeenLastCalledWith(1);
391
+ expect(effect1Fn).toHaveBeenCalledTimes(2);
392
+ expect(effect1Fn).toHaveBeenLastCalledWith(2);
393
+ expect(effect2Fn).toHaveBeenCalledTimes(1);
394
+ expect(effect2Fn).toHaveBeenLastCalledWith(2);
395
+
396
+ $array.get()[2].set(3);
397
+ expect(effectFn).toHaveBeenCalledTimes(4);
398
+ expect(effectFn).toHaveBeenLastCalledWith([1, 2, 3]);
399
+ expect(effect0Fn).toHaveBeenCalledTimes(2);
400
+ expect(effect0Fn).toHaveBeenLastCalledWith(1);
401
+ expect(effect1Fn).toHaveBeenCalledTimes(2);
402
+ expect(effect1Fn).toHaveBeenLastCalledWith(2);
403
+ expect(effect2Fn).toHaveBeenCalledTimes(2);
404
+ expect(effect2Fn).toHaveBeenLastCalledWith(3);
405
+ });
406
+
407
+ test("called when item replaced", () => {
408
+ const $array = array<FlowState<number>>([state(0), state(1), state(2)]);
409
+ const effectFn = vi.fn();
410
+ const effect0Fn = vi.fn();
411
+
412
+ effect((get) => effectFn(get($array).map((state) => get(state))));
413
+ effect((get) => effect0Fn(get(get($array)[0])));
414
+
415
+ expect(effectFn).toHaveBeenCalledTimes(1);
416
+ expect(effectFn).toHaveBeenLastCalledWith([0, 1, 2]);
417
+ expect(effect0Fn).toHaveBeenCalledTimes(1);
418
+ expect(effect0Fn).toHaveBeenLastCalledWith(0);
419
+
420
+ $array.setItem(0, state(3));
421
+ expect(effectFn).toHaveBeenCalledTimes(2);
422
+ expect(effectFn).toHaveBeenLastCalledWith([3, 1, 2]);
423
+ expect(effect0Fn).toHaveBeenCalledTimes(2);
424
+ expect(effect0Fn).toHaveBeenLastCalledWith(3);
425
+
426
+ $array.shift();
427
+ expect(effectFn).toHaveBeenCalledTimes(3);
428
+ expect(effectFn).toHaveBeenLastCalledWith([1, 2]);
429
+ expect(effect0Fn).toHaveBeenCalledTimes(3);
430
+ expect(effect0Fn).toHaveBeenLastCalledWith(1);
431
+
432
+ $array.unshift(state(4));
433
+ expect(effectFn).toHaveBeenCalledTimes(4);
434
+ expect(effectFn).toHaveBeenLastCalledWith([4, 1, 2]);
435
+ expect(effect0Fn).toHaveBeenCalledTimes(4);
436
+ expect(effect0Fn).toHaveBeenLastCalledWith(4);
437
+ });
438
+
439
+ describe("lastAction", () => {
440
+ test("called when initialized", () => {
441
+ const $array = array([1, 2, 3]);
442
+ const effectFn = vi.fn();
443
+ effect((get) => effectFn(get($array.$lastAction)));
444
+
445
+ expect(effectFn).toHaveBeenCalledTimes(1);
446
+ expect(effectFn).toHaveBeenLastCalledWith({
447
+ type: "set",
448
+ items: [1, 2, 3],
449
+ });
450
+ });
451
+
452
+ test("called when updated (set)", () => {
453
+ const $array = array([0, 1, 2]);
454
+ const effectFn = vi.fn();
455
+ effect((get) => effectFn(get($array.$lastAction)));
456
+
457
+ expect(effectFn).toHaveBeenCalledTimes(1);
458
+ expect(effectFn).toHaveBeenLastCalledWith({
459
+ type: "set",
460
+ items: [0, 1, 2],
461
+ });
462
+ $array.set([1, 2, 3]);
463
+ expect(effectFn).toHaveBeenCalledTimes(2);
464
+ expect(effectFn).toHaveBeenLastCalledWith({
465
+ type: "set",
466
+ items: [1, 2, 3],
467
+ });
468
+ });
469
+
470
+ test("called when updated (push/pop)", () => {
471
+ const $array = array<number>();
472
+ const effectFn = vi.fn();
473
+ effect((get) => effectFn(get($array.$lastAction)));
474
+
475
+ expect(effectFn).toHaveBeenCalledTimes(1);
476
+ expect(effectFn).toHaveBeenLastCalledWith({
477
+ type: "set",
478
+ items: [],
479
+ });
480
+
481
+ $array.push(0);
482
+ expect(effectFn).toHaveBeenCalledTimes(2);
483
+ expect(effectFn).toHaveBeenLastCalledWith({
484
+ type: "push",
485
+ item: 0,
486
+ });
487
+
488
+ $array.push(1);
489
+ expect(effectFn).toHaveBeenCalledTimes(3);
490
+ expect(effectFn).toHaveBeenLastCalledWith({
491
+ type: "push",
492
+ item: 1,
493
+ });
494
+
495
+ $array.push(2);
496
+ expect(effectFn).toHaveBeenCalledTimes(4);
497
+ expect(effectFn).toHaveBeenLastCalledWith({
498
+ type: "push",
499
+ item: 2,
500
+ });
501
+
502
+ $array.pop();
503
+ expect(effectFn).toHaveBeenCalledTimes(5);
504
+ expect(effectFn).toHaveBeenLastCalledWith({
505
+ type: "pop",
506
+ });
507
+
508
+ $array.pop();
509
+ expect(effectFn).toHaveBeenCalledTimes(6);
510
+ expect(effectFn).toHaveBeenLastCalledWith({
511
+ type: "pop",
512
+ });
513
+
514
+ $array.pop();
515
+ expect(effectFn).toHaveBeenCalledTimes(7);
516
+ expect(effectFn).toHaveBeenLastCalledWith({
517
+ type: "pop",
518
+ });
519
+ });
520
+
521
+ test("called when updated (unshift/shift)", () => {
522
+ const $array = array<number>();
523
+ const effectFn = vi.fn();
524
+ effect((get) => effectFn(get($array.$lastAction)));
525
+
526
+ expect(effectFn).toHaveBeenCalledTimes(1);
527
+ expect(effectFn).toHaveBeenLastCalledWith({
528
+ type: "set",
529
+ items: [],
530
+ });
531
+
532
+ $array.unshift(0);
533
+ expect(effectFn).toHaveBeenCalledTimes(2);
534
+ expect(effectFn).toHaveBeenLastCalledWith({
535
+ type: "unshift",
536
+ item: 0,
537
+ });
538
+
539
+ $array.unshift(1);
540
+ expect(effectFn).toHaveBeenCalledTimes(3);
541
+ expect(effectFn).toHaveBeenLastCalledWith({
542
+ type: "unshift",
543
+ item: 1,
544
+ });
545
+
546
+ $array.unshift(2);
547
+ expect(effectFn).toHaveBeenCalledTimes(4);
548
+ expect(effectFn).toHaveBeenLastCalledWith({
549
+ type: "unshift",
550
+ item: 2,
551
+ });
552
+
553
+ $array.shift();
554
+ expect(effectFn).toHaveBeenCalledTimes(5);
555
+ expect(effectFn).toHaveBeenLastCalledWith({
556
+ type: "shift",
557
+ });
558
+
559
+ $array.shift();
560
+ expect(effectFn).toHaveBeenCalledTimes(6);
561
+ expect(effectFn).toHaveBeenLastCalledWith({
562
+ type: "shift",
563
+ });
564
+
565
+ $array.shift();
566
+ expect(effectFn).toHaveBeenCalledTimes(7);
567
+ expect(effectFn).toHaveBeenLastCalledWith({
568
+ type: "shift",
569
+ });
570
+ });
571
+
572
+ test("called when updated (splice)", () => {
573
+ const $array = array<number>([0, 1, 2, 3, 4]);
574
+ const effectFn = vi.fn();
575
+ effect((get) => effectFn(get($array.$lastAction)));
576
+
577
+ expect(effectFn).toHaveBeenCalledTimes(1);
578
+ expect(effectFn).toHaveBeenLastCalledWith({
579
+ type: "set",
580
+ items: [0, 1, 2, 3, 4],
581
+ });
582
+
583
+ $array.splice(1, 2);
584
+ expect(effectFn).toHaveBeenCalledTimes(2);
585
+ expect(effectFn).toHaveBeenLastCalledWith({
586
+ type: "splice",
587
+ start: 1,
588
+ deleteCount: 2,
589
+ items: [],
590
+ });
591
+
592
+ $array.splice(1, 0, 1, 2);
593
+ expect(effectFn).toHaveBeenCalledTimes(3);
594
+ expect(effectFn).toHaveBeenLastCalledWith({
595
+ type: "splice",
596
+ start: 1,
597
+ deleteCount: 0,
598
+ items: [1, 2],
599
+ });
600
+ });
601
+
602
+ test("called when updated (clear)", () => {
603
+ const $array = array<number>([0, 1, 2, 3, 4]);
604
+ const effectFn = vi.fn();
605
+ effect((get) => effectFn(get($array.$lastAction)));
606
+
607
+ expect(effectFn).toHaveBeenCalledTimes(1);
608
+ expect(effectFn).toHaveBeenLastCalledWith({
609
+ type: "set",
610
+ items: [0, 1, 2, 3, 4],
611
+ });
612
+
613
+ $array.clear();
614
+ expect(effectFn).toHaveBeenCalledTimes(2);
615
+ expect(effectFn).toHaveBeenLastCalledWith({
616
+ type: "clear",
617
+ });
618
+ });
619
+ });
620
+ });