feat(tests): add fresh-testing-library tests (#1990)

This commit is contained in:
Craig Doremus
2023-11-15 18:25:13 +01:00
committed by GitHub
parent 5d690bede1
commit b5816cd001
20 changed files with 447 additions and 8 deletions
+37
View File
@@ -0,0 +1,37 @@
import { createHandlerContext } from "$fresh-testing-library/server.ts";
import { expect } from "$fresh-testing-library/expect.ts";
import { handler } from "../../../www/routes/index.tsx";
import manifest from "../../../demo/fresh.gen.ts";
Deno.test("index.tsx route handler test...", async (t) => {
await t.step("should redirect to init.ts", () => {
expect(handler.GET).toBeDefined();
const req = new Request("http://localhost:8000/");
req.headers.set("user-agent", "Chrome");
// @ts-ignore manifest typing doesn't matter
const ctx = createHandlerContext(req, { manifest });
// @ts-ignore handler is not null
const resp = handler.GET(req, ctx) as Response;
console.log("resp", resp);
expect(resp.status).toBe(307);
expect(resp.headers.get("Location")?.endsWith("init.ts"));
});
await t.step("should not redirect and return status=200", () => {
expect(handler.GET).toBeDefined();
const req = new Request("http://localhost:8000/");
req.headers.set("user-agent", "Chrome");
req.headers.set("accept", "text/html");
// @ts-ignore manifest typing doesn't matter
const ctx = createHandlerContext(req, { manifest });
// mock render to return Response-like object
// @ts-ignore mock object used
ctx.render = () => {
return ({ body: "Hello World!", status: 200 });
};
// @ts-ignore handler is not null
const resp = handler.GET(req, ctx) as Response;
// console.log("resp", resp);
expect(resp.status).toBe(200);
});
});
@@ -0,0 +1,14 @@
import { createMiddlewareHandlerContext } from "$fresh-testing-library/server.ts";
import { expect } from "$fresh-testing-library/expect.ts";
import { handler } from "../../../www/routes/_middleware.ts";
import manifest from "../../../demo/fresh.gen.ts";
Deno.test("should return middleware status=200 & content-type", async () => {
const req = new Request(`http://localhost:3000/`);
// @ts-ignore ignores "type ... is not assignable to type Manifest" error
const ctx = createMiddlewareHandlerContext(req, { manifest });
const resp = await handler(req, ctx);
// console.log("Response", resp);
expect(resp.status).toBe(200);
expect(resp.headers.get("content-type")).toBe("text/plain;charset=UTF-8");
});