/** * @jsxImportSource solid-js * @jest-environment jsdom */ import { createRoot, createSignal, createUniqueId, JSX, children } from "../../src"; declare module "solid-js/jsx-runtime" { namespace JSX { interface Directives { getRef: boolean; } } } describe("Basic element attributes", () => { test("spread", () => { let div: HTMLDivElement; const props: JSX.HTMLAttributes = { id: "main", title: "main", children:

Hi

, ref: (ref: HTMLDivElement) => { div = ref; }, onClick: () => console.log("clicked") }, d = createRoot(() =>
) as HTMLDivElement & { $$click: any }; expect(div!).toBe(d); expect(d.id).toBe("main"); expect(d.title).toBe("main"); expect(d.$$click).toBeDefined(); expect(d.innerHTML).toBe("

Hi

"); }); test("classList", () => { const classes = { first: true, second: false, "third fourth": true }, d = (
) as HTMLDivElement; expect(d.className).toBe("first third fourth"); }); test("ternary expression triggered", done => { let div: HTMLDivElement; createRoot(() => { const [s, setS] = createSignal(0); div = (
{s() > 5 ? "Large" : "Small"}
) as HTMLDivElement; expect(div.innerHTML).toBe("Small"); setTimeout(() => { setS(7); expect(div.innerHTML).toBe("Large"); done(); }); }); }); test("boolean expression triggered once", () => { let div1: HTMLDivElement, div2: HTMLDivElement; createRoot(() => { const [s, setS] = createSignal(6);
{s() > 5 && (div1 = (
) as HTMLDivElement)}
; div2 = div1; setS(7); expect(div1).toBe(div2); }); }); test("directives work properly", () => { let ref: HTMLDivElement, el!: HTMLDivElement, getRef = (el: HTMLDivElement) => (ref = el), d = (
) as HTMLDivElement; expect(ref!).toBe(el); }); test("uniqueId", () => { let div: HTMLDivElement; createRoot(() => { const id = createUniqueId(); div = (
) as HTMLDivElement; }); expect((div!.firstChild as HTMLLabelElement).htmlFor).toBe( (div!.firstChild!.nextSibling as HTMLInputElement).id ); }); test("children", () => { const Comp = (props: { children?: JSX.Element }) => { const c = children(() => props.children); return ( <> {c.toArray().map(i => (
{i}
))} ); }; const res: HTMLDivElement = createRoot(() => { return (
Hello Hello Jake
as HTMLDivElement ); }); expect(res.innerHTML).toBe( "
Hello
Hello
Jake
" ); }); });