Merge branch 'feat/component-map'
This commit is contained in:
@@ -4,5 +4,7 @@ export { Operation } from "./operation.js";
|
|||||||
export type { OperationProps } from "./operation.js";
|
export type { OperationProps } from "./operation.js";
|
||||||
export { Parallel } from "./parallel.js";
|
export { Parallel } from "./parallel.js";
|
||||||
export type { ParallelProps } from "./parallel.js";
|
export type { ParallelProps } from "./parallel.js";
|
||||||
|
export { Map } from "./map.js";
|
||||||
|
export type { MapOver, MapProps } from "./map.js";
|
||||||
export { Sequential } from "./sequential.js";
|
export { Sequential } from "./sequential.js";
|
||||||
export type { SequentialProps } from "./sequential.js";
|
export type { SequentialProps } from "./sequential.js";
|
||||||
@@ -1 +1,24 @@
|
|||||||
export {};
|
import type { UComponent, UElement, UNode, PropValue, UniversalProps } from "@alkdev/ujsx";
|
||||||
|
import type { Signal } from "@preact/signals-core";
|
||||||
|
import type { CallResult } from "../schema/edge.js";
|
||||||
|
|
||||||
|
export type MapOver = Signal<unknown[]> | unknown[] | ((results: Record<string, CallResult>) => unknown[]);
|
||||||
|
|
||||||
|
export interface MapProps {
|
||||||
|
over: MapOver;
|
||||||
|
as: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const Map: UComponent<MapProps & UniversalProps> = (
|
||||||
|
props: (MapProps & UniversalProps) & { children?: UNode[] },
|
||||||
|
): UElement => {
|
||||||
|
const { over, as, children, ...rest } = props;
|
||||||
|
const elementProps: UniversalProps = { ...rest, over: over as PropValue, as };
|
||||||
|
return {
|
||||||
|
type: "map",
|
||||||
|
props: elementProps,
|
||||||
|
children: children ?? [],
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
Map.displayName = "Map";
|
||||||
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