import {
List,
mapJoin,
Output,
reactive,
refkey,
StatementList,
} from "@alloy-js/core";
import { describe, expect, it } from "vitest";
import * as ts from "../src/index.js";
it("renders an object", () => {
expect().toRenderTo("{}");
});
it("renders an object with properties", () => {
expect(
,
,
).toRenderTo(`
{
foo: 1,
bar: 2
}
`);
});
it("renders an object with properties, mapped", () => {
const propValues = new Map([
["foo", "1"],
["bar", "2"],
]);
const props = mapJoin(
() => propValues,
(name, value) => {
return ;
},
{ joiner: ",\n" },
);
expect({props}).toRenderTo(`
{
foo: 1,
bar: 2
}
`);
});
it("is reactive", () => {
const jsValue = reactive(new Map());
const tree = ;
expect(tree).toRenderTo("{}");
jsValue.set("hi", 1);
expect(tree).toRenderTo(`
{
hi: 1,
}
`);
jsValue.set("bye", 2);
expect(tree).toRenderTo(`
{
hi: 1,
bye: 2,
}
`);
});
it("renders objects with arrays", () => {
const jsValue = {
a: [1, 2],
};
expect().toRenderTo(`
{
a: [1, 2],
}
`);
});
it("renders complex objects", () => {
const jsValue = {
a: 1,
b: "hello",
c: true,
d: {
prop: [1, 2, 3],
},
};
expect().toRenderTo(`
{
a: 1,
b: "hello",
c: true,
d: {
prop: [1, 2, 3],
},
}
`);
});
it("renders falsy values", () => {
const jsValue = {
a: 0,
b: "",
c: false,
d: null,
e: undefined,
};
expect().toRenderTo(`
{
a: 0,
b: "",
c: false,
d: null,
e: undefined,
}
`);
});
it("allows embedding things with functions", () => {
function Foo() {
return <>a b>;
}
const jsValue = {
a: () => "hello",
b: () => ,
};
expect().toRenderTo(`
{
a: hello,
b: a b,
}
`);
});
describe("symbols", () => {
it("can reference members", () => {
const innerRefkey = refkey();
const outerRefkey = refkey();
const decl = (
);
expect(decl).toRenderTo(`
const refme = {
foo: "hello",
};
refme.foo;
`);
});
it("can reference nested members", () => {
const varRefkey = refkey();
const fooRefkey = refkey();
const barRefkey = refkey();
const decl = (
);
expect(decl).toRenderTo(`
const refme = {
foo: {
bar: "hello",
}
};
refme.foo.bar;
`);
});
it("can reference nested members in other source files", () => {
const varRefkey = refkey();
const fooRefkey = refkey();
const barRefkey = refkey();
const decl = (
);
expect(decl).toRenderTo({
"foo.ts": `
export const refme = {
foo: {
bar: "hello",
}
}
`,
"bar.ts": `
import { refme } from "./foo.js";
console.log(refme.foo.bar);
`,
});
});
it("can reference nested members in other packages", () => {
const varRefkey = refkey();
const fooRefkey = refkey();
const barRefkey = refkey();
const decl = (
);
expect(decl).toRenderTo({
"sp/package.json": expect.anything(),
"dp/tsconfig.json": expect.anything(),
"dp/package.json": expect.anything(),
"sp/tsconfig.json": expect.anything(),
"sp/foo.ts": expect.anything(),
"dp/bar.ts": `
import { refme } from "SourcePackage";
console.log(refme.foo.bar);
`,
});
});
it("uses name policy", () => {
const key1 = refkey();
expect(
,
).toRenderTo({
"test.ts": `
const dispatcher = {
fooBar: null,
}
dispatcher.fooBar(data)
`,
});
});
});