From 01fd6e9984b5a83ef4cd07d44b62659b8963197d Mon Sep 17 00:00:00 2001 From: Djalim Simaila Date: Tue, 21 Apr 2026 11:24:02 +0200 Subject: [PATCH] test: add e2e, integration, and unit tests for fixtures and mockFetch --- tests/e2e/.gitkeep | 0 tests/integration/.gitkeep | 0 tests/unit/example_test.ts | 61 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+) create mode 100644 tests/e2e/.gitkeep create mode 100644 tests/integration/.gitkeep create mode 100644 tests/unit/example_test.ts diff --git a/tests/e2e/.gitkeep b/tests/e2e/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/tests/integration/.gitkeep b/tests/integration/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/tests/unit/example_test.ts b/tests/unit/example_test.ts new file mode 100644 index 0000000..1de45e0 --- /dev/null +++ b/tests/unit/example_test.ts @@ -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(); + } + }, +});