test: add e2e, integration, and unit tests for fixtures and mockFetch

This commit is contained in:
2026-04-21 11:24:02 +02:00
committed by djalim
parent 332286c085
commit 01fd6e9984
3 changed files with 61 additions and 0 deletions
View File
View File
+61
View File
@@ -0,0 +1,61 @@
import { assertEquals, assertExists } from "@std/assert";
import { mockFetch, restoreFetch } from "../helpers/api_mock.ts";
import { notes, students } from "../helpers/fixtures.ts";
import { cleanupDOM, setupDOM } from "../helpers/render.ts";
Deno.test("fixtures - students have expected shape", () => {
assertEquals(students.length, 3);
assertEquals(students[0].nom, "Dupont");
assertExists(students[0].numEtud);
});
Deno.test("mockFetch - returns mocked data for matching route", async () => {
mockFetch({
"/students": students,
"/notes": notes,
});
try {
const res = await fetch("http://localhost/api/students");
assertEquals(res.status, 200);
const data = await res.json();
assertEquals(data.length, 3);
assertEquals(data[0].nom, "Dupont");
} finally {
restoreFetch();
}
});
Deno.test("mockFetch - returns 404 for unknown routes", async () => {
mockFetch({});
try {
const res = await fetch("http://localhost/api/unknown");
assertEquals(res.status, 404);
} finally {
restoreFetch();
}
});
Deno.test({
name: "happy-dom - document is available after setup",
sanitizeResources: false,
sanitizeOps: false,
fn() {
setupDOM();
try {
const doc = globalThis.document;
assertExists(doc);
const div = doc.createElement("div");
div.textContent = "hello";
doc.body.appendChild(div);
assertEquals(doc.body.textContent, "hello");
} finally {
cleanupDOM();
}
},
});