import type { Props } from "@alloy-js/core"; import { refkey, render, StatementList } from "@alloy-js/core"; import { describe, expect, it } from "vitest"; import { ArrowFunction } from "../src/components/ArrowFunction.jsx"; import { VarDeclaration } from "../src/index.js"; import type { ParameterDescriptor } from "../src/parameter-descriptor.js"; import { TestFile } from "./utils.js"; it("create basic function", () => { expect( , ).toRenderTo(` () => {} `); }); it("can be an async function", () => { expect( , ).toRenderTo(` async () => {} `); }); it("can be an async with returnType", () => { expect( , ).toRenderTo(` async (): Promise => {} `); }); it("can be an async with returnType element", () => { function Foo(_props?: Props) { return <>Foo; } expect( } /> , ).toRenderTo(` async (): Promise => {} `); }); it("supports parameters by element", () => { const decl = ( return a + b; a, b ); expect({decl}).toRenderTo(` (a, b) => { return a + b; } `); }); it("supports type parameters by descriptor object", () => { const decl = ( ); expect({decl}).toRenderTo(` () => {} `); }); it("supports type parameters by descriptor array", () => { const decl = ; expect({decl}).toRenderTo(` () => {} `); }); it("supports type parameters by element", () => { const decl = ( a, b ); expect({decl}).toRenderTo(` () => {} `); }); describe("symbols", () => { it("creates a nested scope", () => { const innerRefkey = refkey(); const outerRefkey = refkey(); const decl = ( {innerRefkey} 1 2 {outerRefkey} ); expect({decl}).toRenderTo(` () => { refme; const refme = 1; }; const refme = 2; refme; `); }); it("throws an error when trying to access a symbol in a nested scope", () => { const innerRefkey = refkey(); const decl = ( <> 1 ;{innerRefkey} ); expect(() => render({decl}, { insertFinalNewLine: false }), ).toThrow(/Cannot reference a symbol/); }); it("creates symbols for parameters", () => { const rk = refkey(); const decl = ( {rk} ); expect({decl}).toRenderTo(` (sym: any) => { () => { sym } } `); }); it("creates symbols for parameters and addresses conflicts", () => { const decl = ( 1; ); expect({decl}).toRenderTo(` (conflict: any) => { const conflict_2 = 1; } `); }); it("create optional parameters", () => { const paramDesc: ParameterDescriptor = { name: "foo", refkey: refkey(), type: "any", optional: true, }; const decl = ( console.log(foo); ); expect({decl}).toRenderTo(` (foo?: any) => { console.log(foo); } `); }); it("create rest parameters", () => { const paramDesc: ParameterDescriptor = { name: "foo", refkey: refkey(), type: "any[]", rest: true, }; const decl = ( console.log(foo); ); expect({decl}).toRenderTo(` (...foo: any[]) => { console.log(foo); } `); }); });