|
| 1 | +import { describe, expect, test } from "vitest"; |
| 2 | +import { createStore, reconcile, unwrap } from "../../store/src/server.js"; |
| 3 | + |
| 4 | +describe("reconcile array length on the server", () => { |
| 5 | + const cases: [string, (number | undefined)[], (number | undefined)[]][] = [ |
| 6 | + ["shortens an array", [1, 2, 3], [4]], |
| 7 | + ["clears an array", [1, 2], []], |
| 8 | + ["grows an array", [1], [1, 2, 3]], |
| 9 | + ["preserves trailing undefined entries", [1], [1, undefined]], |
| 10 | + ["preserves sparse array length", [1], new Array(3)] |
| 11 | + ]; |
| 12 | + |
| 13 | + test.each(cases)("%s at the root", (_, initial, next) => { |
| 14 | + const original = initial.slice(); |
| 15 | + const [state, setState] = createStore(original); |
| 16 | + setState(reconcile(next)); |
| 17 | + expect(state).toHaveLength(next.length); |
| 18 | + expect([...state]).toEqual([...next]); |
| 19 | + expect(unwrap(state)).toBe(original); |
| 20 | + }); |
| 21 | + |
| 22 | + test.each(cases)("%s on a nested path", (_, initial, next) => { |
| 23 | + const original = initial.slice(); |
| 24 | + const [state, setState] = createStore({ items: original }); |
| 25 | + setState("items", reconcile(next)); |
| 26 | + expect(state.items).toHaveLength(next.length); |
| 27 | + expect([...state.items]).toEqual([...next]); |
| 28 | + expect(unwrap(state.items)).toBe(original); |
| 29 | + }); |
| 30 | +}); |
0 commit comments