Files
WRNexusJS/examples/basic-app/app/example.test.ts
2026-07-12 15:55:18 +05:30

77 lines
2.2 KiB
TypeScript

/**
* Example WrNexus tests. Run with `wrnexus test` (or `bun test`).
*
* - renderComponent: compile + render a .wrn component to HTML
* - callRoute: call an API handler with a fake Request
* - createHarness: boot the whole app on an ephemeral port and fetch real routes
*
* Everything comes from one import: `@wrnexus/test`.
*/
import {
test,
expect,
describe,
beforeAll,
afterAll,
renderComponent,
callRoute,
createHarness,
type Harness,
} from "@wrnexus/test";
// --- Component-level (fast, no server) -------------------------------------
const COUNTER = `component Counter {
props {
start = 0
label = "Count"
}
state count = start
view { <button @click="count++">{label}: {count}</button> }
}`;
test("Counter renders its label and initial value", async () => {
const html = await renderComponent(COUNTER, { start: 5, label: "Clicks" });
expect(html).toContain("Clicks");
expect(html).toContain("5");
});
// --- Route-level (fast, no server) -----------------------------------------
test("POST /api/echo echoes the JSON body", async () => {
const { POST } = await import("./api/echo.ts");
const res = await callRoute(
POST,
new Request("http://test/api/echo", {
method: "POST",
body: JSON.stringify({ hi: "there" }),
headers: { "content-type": "application/json" },
}),
);
expect(await res.json()).toEqual({ received: { hi: "there" } });
});
// --- App-level integration (real in-process server) ------------------------
describe("full app", () => {
let app: Harness;
beforeAll(async () => {
app = await createHarness(import.meta.dir + "/..");
});
afterAll(() => app?.close());
test("home page responds with HTML", async () => {
const res = await app.fetch("/");
expect(res.status).toBe(200);
expect(res.headers.get("content-type")).toContain("text/html");
});
test("GET /api/hello returns a translated greeting", async () => {
const res = await app.fetch("/api/hello");
expect(res.status).toBe(200);
const body = (await res.json()) as { message: string; lang: string };
expect(typeof body.message).toBe("string");
expect(body.lang).toBeTruthy();
});
});