Implement Map ujsx component for dynamic array mapping
This commit is contained in:
87
test/component/map.test.ts
Normal file
87
test/component/map.test.ts
Normal file
@@ -0,0 +1,87 @@
|
||||
import { describe, it, expect } from "vitest";
|
||||
import { Map } from "../../src/component/map.js";
|
||||
import type { MapOver, MapProps } from "../../src/component/map.js";
|
||||
import type { UElement, UNode } from "@alkdev/ujsx";
|
||||
import type { CallResult } from "../../src/schema/edge.js";
|
||||
import { signal } from "@preact/signals-core";
|
||||
|
||||
describe("Map", () => {
|
||||
it("produces UElement with type map", () => {
|
||||
const el = Map({
|
||||
over: ["a", "b"],
|
||||
as: "item",
|
||||
} as MapProps & { children?: UNode[] });
|
||||
expect(el.type).toBe("map");
|
||||
});
|
||||
|
||||
it("preserves static array as over prop", () => {
|
||||
const items = [1, 2, 3];
|
||||
const el = Map({
|
||||
over: items,
|
||||
as: "item",
|
||||
} as MapProps & { children?: UNode[] });
|
||||
expect((el as UElement).props.over).toBe(items);
|
||||
});
|
||||
|
||||
it("preserves function as over prop", () => {
|
||||
const fn: MapOver = (results: Record<string, CallResult>) =>
|
||||
results["fetch-items"]!.output as unknown[];
|
||||
const el = Map({
|
||||
over: fn,
|
||||
as: "item",
|
||||
} as MapProps & { children?: UNode[] });
|
||||
expect((el as UElement).props.over).toBe(fn);
|
||||
});
|
||||
|
||||
it("preserves signal as over prop", () => {
|
||||
const sig = signal(["x", "y"]);
|
||||
const el = Map({
|
||||
over: sig as unknown as MapOver,
|
||||
as: "item",
|
||||
} as MapProps & { children?: UNode[] });
|
||||
expect((el as UElement).props.over).toBe(sig);
|
||||
});
|
||||
|
||||
it("preserves as prop", () => {
|
||||
const el = Map({
|
||||
over: [1, 2],
|
||||
as: "element",
|
||||
} as MapProps & { children?: UNode[] });
|
||||
expect((el as UElement).props.as).toBe("element");
|
||||
});
|
||||
|
||||
it("includes children", () => {
|
||||
const child: UElement = { type: "operation", props: { name: "process" }, children: [] };
|
||||
const el = Map({
|
||||
over: [1, 2],
|
||||
as: "item",
|
||||
children: [child],
|
||||
} as MapProps & { children?: UNode[] });
|
||||
expect(el.children).toEqual([child]);
|
||||
});
|
||||
|
||||
it("defaults children to empty array", () => {
|
||||
const el = Map({
|
||||
over: [],
|
||||
as: "item",
|
||||
} as MapProps & { children?: UNode[] });
|
||||
expect(el.children).toEqual([]);
|
||||
});
|
||||
|
||||
it("is a valid UElement (has type, props, children)", () => {
|
||||
const el = Map({
|
||||
over: [],
|
||||
as: "item",
|
||||
} as MapProps & { children?: UNode[] });
|
||||
expect(el).toHaveProperty("type");
|
||||
expect(el).toHaveProperty("props");
|
||||
expect(el).toHaveProperty("children");
|
||||
expect(typeof el.type).toBe("string");
|
||||
expect(typeof el.props).toBe("object");
|
||||
expect(Array.isArray(el.children)).toBe(true);
|
||||
});
|
||||
|
||||
it("displayName is Map", () => {
|
||||
expect(Map.displayName).toBe("Map");
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user