35 lines
1.4 KiB
TypeScript
35 lines
1.4 KiB
TypeScript
import { expect, test } from "bun:test";
|
|
import { parse } from "@wrnexus/syntax";
|
|
import { generateBrowserModule } from "../src/client-codegen.ts";
|
|
|
|
const source = `component FunctionDefaults {
|
|
props { timezone = "UTC" }
|
|
state selected = ""
|
|
functions {
|
|
client function choose(section = "connections", hour = 9) { selected = section; return hour }
|
|
shared function label(prefix = "event", suffix = prefix + " details", zone = timezone) { return [prefix, suffix, zone] }
|
|
}
|
|
view { <button @click="choose()">Choose</button> }
|
|
}`;
|
|
test("browser functions retain defaults, prior-argument references and prop references", () => {
|
|
const module = generateBrowserModule(parse(source));
|
|
const functions = new Function(
|
|
module.replace(/\bexport\s+(?=const|function)/g, "") + ";return __wrnexusClientFunctions;",
|
|
)();
|
|
const context = {
|
|
state: { selected: "" },
|
|
props: { timezone: "Asia/Kolkata" },
|
|
batch: (apply: () => void) => apply(),
|
|
};
|
|
expect(functions.choose(context)).toBe(9);
|
|
expect(context.state.selected).toBe("connections");
|
|
expect(functions.label(context)).toEqual(["event", "event details", "Asia/Kolkata"]);
|
|
expect(functions.label(context, "meeting")).toEqual([
|
|
"meeting",
|
|
"meeting details",
|
|
"Asia/Kolkata",
|
|
]);
|
|
expect(functions.choose(context, undefined, 0)).toBe(0);
|
|
expect(functions.label(context, null, "explicit", "UTC")).toEqual([null, "explicit", "UTC"]);
|
|
});
|