87 lines
2.5 KiB
TypeScript
87 lines
2.5 KiB
TypeScript
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");
|
|
});
|
|
}); |