The old assertions only searched REACTIVE_RUNTIME for substrings; they never touched buildApiRequest and were not anchored to the content-type line they claimed to guard, so they could not detect drift on either side. Replace with a fixture-driven test that runs both implementations on the same (path, method, input) cases and compares the actual request they produce. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
53 lines
1.7 KiB
TypeScript
53 lines
1.7 KiB
TypeScript
import { expect, test } from "bun:test";
|
|
import { buildApiRequest } from "../src/api-request.ts";
|
|
|
|
test("GET builds a query string", () => {
|
|
expect(buildApiRequest("/api/users", "GET", { name: "Ajay" }).url).toBe("/api/users?name=Ajay");
|
|
});
|
|
|
|
test("GET omits undefined, null and empty string", () => {
|
|
const built = buildApiRequest("/api/users", "GET", {
|
|
name: "Ajay",
|
|
age: undefined,
|
|
team: null,
|
|
note: "",
|
|
});
|
|
|
|
expect(built.url).toBe("/api/users?name=Ajay");
|
|
});
|
|
|
|
test("GET keeps 0 and false", () => {
|
|
// A filter of 0 or false is a real value; dropping it silently would be a bug.
|
|
const built = buildApiRequest("/api/users", "GET", { count: 0, active: false });
|
|
|
|
expect(built.url).toContain("count=0");
|
|
expect(built.url).toContain("active=false");
|
|
});
|
|
|
|
test("GET has no body", () => {
|
|
expect(buildApiRequest("/api/users", "GET", { name: "Ajay" }).body).toBeUndefined();
|
|
});
|
|
|
|
test("POST sends a JSON body and no query", () => {
|
|
const built = buildApiRequest("/api/users", "POST", { name: "Ajay" });
|
|
|
|
expect(built.url).toBe("/api/users");
|
|
expect(built.body).toBe(JSON.stringify({ name: "Ajay" }));
|
|
expect(built.contentType).toBe("application/json");
|
|
});
|
|
|
|
test("POST with no input sends an empty object", () => {
|
|
expect(buildApiRequest("/api/users", "POST", undefined).body).toBe("{}");
|
|
});
|
|
|
|
test("values are encoded", () => {
|
|
expect(buildApiRequest("/api/users", "GET", { name: "a b&c" }).url).toBe(
|
|
"/api/users?name=a%20b%26c",
|
|
);
|
|
});
|
|
|
|
// The behavioural agreement test between this builder and the browser
|
|
// runtime's wrnexusCallApi lives in packages/csr/test/api-request-agreement.test.ts,
|
|
// which can host the happy-dom harness needed to run the runtime and compare
|
|
// outputs. This file has no DOM available.
|