test: add e2e, integration, and unit tests for fixtures and mockFetch
This commit is contained in:
@@ -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();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user