xonecas.io
70160
Ce

cel-tui / files

cel-tui / packages / core / src / state-identity.test.ts

1.5 KB
1import { describe, expect, test } from "bun:test";
2import { layout } from "./layout.js";
3import { HStack, VStack } from "./primitives/stacks.js";
4import { Text } from "./primitives/text.js";
5import { inspectMountedStateKeys } from "./state-identity.js";
6
7describe("state identity", () => {
8  test("rejects the same keyed node object mounted twice", () => {
9    const repeated = HStack({ stateKey: "duplicate", onClick: () => {} }, [
10      Text("button"),
11    ]);
12    const root = layout(VStack({}, [repeated, repeated]), 20, 4);
13
14    expect(() => inspectMountedStateKeys([root])).toThrow(
15      'Duplicate stateKey "duplicate" in viewport',
16    );
17  });
18
19  test("rejects duplicate keys across layers", () => {
20    const first = layout(
21      VStack({ stateKey: "shared", height: "100%" }, []),
22      20,
23      4,
24    );
25    const second = layout(
26      VStack({ stateKey: "shared", height: "100%" }, []),
27      20,
28      4,
29    );
30
31    expect(() => inspectMountedStateKeys([first, second])).toThrow(
32      'Duplicate stateKey "shared" in viewport',
33    );
34  });
35
36  test("keeps numeric and string keys distinct", () => {
37    const root = layout(
38      VStack({}, [
39        HStack({ stateKey: 1, onClick: () => {} }, [Text("number")]),
40        HStack({ stateKey: "1", onClick: () => {} }, [Text("string")]),
41      ]),
42      20,
43      4,
44    );
45
46    const snapshot = inspectMountedStateKeys([root]);
47    expect(snapshot.byKey.size).toBe(2);
48    expect(snapshot.byKey.get(1)?.kind).toBe("hstack");
49    expect(snapshot.byKey.get("1")?.kind).toBe("hstack");
50  });
51});