Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| b05fd91411 | |||
| aaa9d0812b | |||
| 7568afb888 | |||
| 6371fca14a |
@@ -24,6 +24,3 @@ jobs:
|
|||||||
|
|
||||||
- name: Check linting
|
- name: Check linting
|
||||||
run: deno lint
|
run: deno lint
|
||||||
|
|
||||||
- name: Run tests
|
|
||||||
run: deno test -A --no-check tests/
|
|
||||||
|
|||||||
+2
-1
@@ -3,10 +3,11 @@ FROM denoland/deno:alpine
|
|||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
COPY . .
|
COPY . .
|
||||||
|
|
||||||
RUN deno cache main.ts --allow-import
|
RUN deno cache main.ts --allow-import
|
||||||
RUN deno task build
|
RUN deno task build
|
||||||
|
|
||||||
USER deno
|
|
||||||
EXPOSE 80
|
EXPOSE 80
|
||||||
EXPOSE 443
|
EXPOSE 443
|
||||||
|
|
||||||
|
|||||||
@@ -9,8 +9,7 @@
|
|||||||
"start": "deno run -A --unstable-ffi --watch=static/,routes/ dev.ts",
|
"start": "deno run -A --unstable-ffi --watch=static/,routes/ dev.ts",
|
||||||
"build": "deno run -A --unstable-ffi dev.ts build",
|
"build": "deno run -A --unstable-ffi dev.ts build",
|
||||||
"preview": "deno run -A --unstable-ffi main.ts",
|
"preview": "deno run -A --unstable-ffi main.ts",
|
||||||
"update": "deno run -A -r https://fresh.deno.dev/update .",
|
"update": "deno run -A -r https://fresh.deno.dev/update ."
|
||||||
"test": "deno test -A --no-check tests/"
|
|
||||||
},
|
},
|
||||||
"lint": {
|
"lint": {
|
||||||
"rules": {
|
"rules": {
|
||||||
@@ -36,9 +35,6 @@
|
|||||||
"@preact/signals": "https://esm.sh/*@preact/signals@1.2.2",
|
"@preact/signals": "https://esm.sh/*@preact/signals@1.2.2",
|
||||||
"@preact/signals-core": "https://esm.sh/*@preact/signals-core@1.5.1",
|
"@preact/signals-core": "https://esm.sh/*@preact/signals-core@1.5.1",
|
||||||
"$std/": "https://deno.land/std@0.216.0/",
|
"$std/": "https://deno.land/std@0.216.0/",
|
||||||
"@std/assert": "jsr:@std/assert@^1.0.0",
|
|
||||||
"@std/testing": "jsr:@std/testing@^1.0.0",
|
|
||||||
"happy-dom": "npm:happy-dom@^16.0.0",
|
|
||||||
"$root/": "./",
|
"$root/": "./",
|
||||||
"$apps/": "./routes/(apps)/"
|
"$apps/": "./routes/(apps)/"
|
||||||
},
|
},
|
||||||
|
|||||||
+10
-2
@@ -3,8 +3,9 @@
|
|||||||
// This file is automatically updated during development when running `dev.ts`.
|
// This file is automatically updated during development when running `dev.ts`.
|
||||||
|
|
||||||
import * as $_apps_layout from "./routes/(apps)/_layout.tsx";
|
import * as $_apps_layout from "./routes/(apps)/_layout.tsx";
|
||||||
import * as $_apps_middleware from "./routes/(apps)/_middleware.ts";
|
import * as $_apps_mobility_api_download from "./routes/(apps)/mobility/api/download.ts";
|
||||||
import * as $_apps_mobility_api_insert_mobility from "./routes/(apps)/mobility/api/insert_mobility.ts";
|
import * as $_apps_mobility_api_download_id_ from "./routes/(apps)/mobility/api/download/[id].ts";
|
||||||
|
import * as $_apps_mobility_api_insert_mobility from "./routes/(apps)/mobility/api/insert-mobility.ts";
|
||||||
import * as $_apps_mobility_index from "./routes/(apps)/mobility/index.tsx";
|
import * as $_apps_mobility_index from "./routes/(apps)/mobility/index.tsx";
|
||||||
import * as $_apps_mobility_partials_admin_edit_mobility from "./routes/(apps)/mobility/partials/(admin)/edit_mobility.tsx";
|
import * as $_apps_mobility_partials_admin_edit_mobility from "./routes/(apps)/mobility/partials/(admin)/edit_mobility.tsx";
|
||||||
import * as $_apps_mobility_partials_index from "./routes/(apps)/mobility/partials/index.tsx";
|
import * as $_apps_mobility_partials_index from "./routes/(apps)/mobility/partials/index.tsx";
|
||||||
@@ -40,8 +41,15 @@ import type { Manifest } from "$fresh/server.ts";
|
|||||||
const manifest = {
|
const manifest = {
|
||||||
routes: {
|
routes: {
|
||||||
"./routes/(apps)/_layout.tsx": $_apps_layout,
|
"./routes/(apps)/_layout.tsx": $_apps_layout,
|
||||||
|
<<<<<<< HEAD
|
||||||
"./routes/(apps)/_middleware.ts": $_apps_middleware,
|
"./routes/(apps)/_middleware.ts": $_apps_middleware,
|
||||||
"./routes/(apps)/mobility/api/insert_mobility.ts":
|
"./routes/(apps)/mobility/api/insert_mobility.ts":
|
||||||
|
=======
|
||||||
|
"./routes/(apps)/mobility/api/download.ts": $_apps_mobility_api_download,
|
||||||
|
"./routes/(apps)/mobility/api/download/[id].ts":
|
||||||
|
$_apps_mobility_api_download_id_,
|
||||||
|
"./routes/(apps)/mobility/api/insert-mobility.ts":
|
||||||
|
>>>>>>> 4f1011d (Ultimate fix and tested ! You can download contract now.)
|
||||||
$_apps_mobility_api_insert_mobility,
|
$_apps_mobility_api_insert_mobility,
|
||||||
"./routes/(apps)/mobility/index.tsx": $_apps_mobility_index,
|
"./routes/(apps)/mobility/index.tsx": $_apps_mobility_index,
|
||||||
"./routes/(apps)/mobility/partials/(admin)/edit_mobility.tsx":
|
"./routes/(apps)/mobility/partials/(admin)/edit_mobility.tsx":
|
||||||
|
|||||||
@@ -0,0 +1,36 @@
|
|||||||
|
import { Handlers } from "$fresh/server.ts";
|
||||||
|
import connect from "$root/databases/connect.ts";
|
||||||
|
|
||||||
|
export const handler: Handlers = {
|
||||||
|
async GET(_request, ctx) {
|
||||||
|
try {
|
||||||
|
const { id } = ctx.params;
|
||||||
|
|
||||||
|
if (!id) {
|
||||||
|
return new Response("Invalid request: Missing ID", { status: 400 });
|
||||||
|
}
|
||||||
|
|
||||||
|
using connection = connect("mobility");
|
||||||
|
|
||||||
|
const query = connection.database.prepare(
|
||||||
|
"SELECT attestationFile FROM mobility WHERE id = ?",
|
||||||
|
);
|
||||||
|
const result = query.get(id);
|
||||||
|
|
||||||
|
if (!result || !result.attestationFile) {
|
||||||
|
return new Response("No file found for the given ID", { status: 404 });
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Response(result.attestationFile, {
|
||||||
|
status: 200,
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/pdf",
|
||||||
|
"Content-Disposition": `attachment; filename="attestation_${id}.pdf"`,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error fetching file:", error);
|
||||||
|
return new Response("Failed to fetch file", { status: 500 });
|
||||||
|
}
|
||||||
|
},
|
||||||
|
};
|
||||||
@@ -0,0 +1,131 @@
|
|||||||
|
import { Handlers } from "$fresh/server.ts";
|
||||||
|
import connect from "$root/databases/connect.ts";
|
||||||
|
|
||||||
|
export const handler: Handlers = {
|
||||||
|
// deno-lint-ignore require-await
|
||||||
|
async GET() {
|
||||||
|
try {
|
||||||
|
using connection = connect("mobility");
|
||||||
|
|
||||||
|
const mobilities = connection.database.prepare(
|
||||||
|
`SELECT
|
||||||
|
mobility.id,
|
||||||
|
mobility.studentId,
|
||||||
|
mobility.startDate,
|
||||||
|
mobility.endDate,
|
||||||
|
mobility.weeksCount,
|
||||||
|
mobility.destinationCountry,
|
||||||
|
mobility.destinationName,
|
||||||
|
mobility.mobilityStatus,
|
||||||
|
mobility.attestationFile -- Inclure le fichier
|
||||||
|
FROM mobility`
|
||||||
|
).all();
|
||||||
|
|
||||||
|
const students = connection.database.prepare(
|
||||||
|
`SELECT
|
||||||
|
students.userId AS id,
|
||||||
|
students.firstName,
|
||||||
|
students.lastName,
|
||||||
|
students.promotionId AS promotionId,
|
||||||
|
promotions.name AS promotionName
|
||||||
|
FROM students.students
|
||||||
|
LEFT JOIN students.promotions ON students.promotionId = promotions.id`
|
||||||
|
).all();
|
||||||
|
|
||||||
|
const promotions = connection.database.prepare(
|
||||||
|
`SELECT id, name FROM students.promotions`
|
||||||
|
).all();
|
||||||
|
|
||||||
|
return new Response(
|
||||||
|
JSON.stringify({ mobilities, students, promotions }),
|
||||||
|
{
|
||||||
|
status: 200,
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
},
|
||||||
|
);
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error fetching mobility data:", error);
|
||||||
|
return new Response("Failed to fetch data", { status: 500 });
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
async POST(request) {
|
||||||
|
console.log("API /mobility/api/insert-mobility POST called");
|
||||||
|
|
||||||
|
try {
|
||||||
|
const formData = await request.formData();
|
||||||
|
const dataEntries = formData.getAll("data").map((item) => JSON.parse(item as string));
|
||||||
|
console.log("Parsed data entries:", dataEntries);
|
||||||
|
|
||||||
|
const fileMap: Record<string, Uint8Array> = {};
|
||||||
|
for (const [key, value] of formData.entries()) {
|
||||||
|
if (key.startsWith("file_") && value instanceof File) {
|
||||||
|
const studentId = key.split("_")[1];
|
||||||
|
const file = value as File;
|
||||||
|
fileMap[studentId] = new Uint8Array(await file.arrayBuffer());
|
||||||
|
console.log(`File processed for studentId ${studentId}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
using connection = connect("mobility");
|
||||||
|
const insertQuery = connection.database.prepare(
|
||||||
|
`INSERT INTO mobility (
|
||||||
|
id, studentId, startDate, endDate, weeksCount, destinationCountry, destinationName, mobilityStatus, attestationFile
|
||||||
|
)
|
||||||
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||||
|
ON CONFLICT(id) DO UPDATE SET
|
||||||
|
startDate = excluded.startDate,
|
||||||
|
endDate = excluded.endDate,
|
||||||
|
weeksCount = excluded.weeksCount,
|
||||||
|
destinationCountry = excluded.destinationCountry,
|
||||||
|
destinationName = excluded.destinationName,
|
||||||
|
mobilityStatus = excluded.mobilityStatus,
|
||||||
|
attestationFile = COALESCE(excluded.attestationFile, mobility.attestationFile)`
|
||||||
|
);
|
||||||
|
|
||||||
|
for (const mobility of dataEntries) {
|
||||||
|
const {
|
||||||
|
id = null,
|
||||||
|
studentId,
|
||||||
|
startDate,
|
||||||
|
endDate,
|
||||||
|
destinationCountry,
|
||||||
|
destinationName,
|
||||||
|
mobilityStatus = "N/A",
|
||||||
|
} = mobility;
|
||||||
|
|
||||||
|
let calculatedWeeksCount = null;
|
||||||
|
if (startDate && endDate) {
|
||||||
|
const start = new Date(startDate);
|
||||||
|
const end = new Date(endDate);
|
||||||
|
if (start <= end) {
|
||||||
|
const differenceInDays = Math.ceil(
|
||||||
|
(end.getTime() - start.getTime()) / (24 * 60 * 60 * 1000)
|
||||||
|
);
|
||||||
|
calculatedWeeksCount = Math.floor(differenceInDays / 7);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const attestationFile = fileMap[studentId] ?? null;
|
||||||
|
|
||||||
|
console.log(`Inserting/Updating mobility for studentId: ${studentId}`);
|
||||||
|
insertQuery.run(
|
||||||
|
id,
|
||||||
|
studentId,
|
||||||
|
startDate,
|
||||||
|
endDate,
|
||||||
|
calculatedWeeksCount,
|
||||||
|
destinationCountry,
|
||||||
|
destinationName,
|
||||||
|
mobilityStatus,
|
||||||
|
attestationFile
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log("Mobility data inserted/updated successfully.");
|
||||||
|
return new Response("Data inserted/updated successfully", { status: 200 });
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error inserting mobility data:", error);
|
||||||
|
return new Response("Failed to insert/update data", { status: 500 });
|
||||||
|
}
|
||||||
|
},
|
||||||
|
};
|
||||||
@@ -0,0 +1,84 @@
|
|||||||
|
import { Handlers } from "$fresh/server.ts";
|
||||||
|
import connect from "$root/databases/connect.ts";
|
||||||
|
|
||||||
|
// Rendre l'API plus simple car xlsx pour l'import c'est nul :/
|
||||||
|
export const handler: Handlers = {
|
||||||
|
// deno-lint-ignore require-await
|
||||||
|
async GET() {
|
||||||
|
try {
|
||||||
|
using connection = connect("students");
|
||||||
|
|
||||||
|
const promotions = connection.database.prepare(
|
||||||
|
"select id, name from promotions",
|
||||||
|
).all();
|
||||||
|
|
||||||
|
const students = connection.database
|
||||||
|
.prepare(
|
||||||
|
`select userId, firstName, lastName, mail, promotionId from students`,
|
||||||
|
)
|
||||||
|
.all();
|
||||||
|
|
||||||
|
return new Response(
|
||||||
|
JSON.stringify({ promotions, students }),
|
||||||
|
{
|
||||||
|
status: 200,
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
},
|
||||||
|
);
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error fetching data:", error);
|
||||||
|
return new Response("Failed to fetch data", { status: 500 });
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
async POST(request) {
|
||||||
|
console.log("API /students/api/insert_students called");
|
||||||
|
|
||||||
|
try {
|
||||||
|
const body = await request.json();
|
||||||
|
const { data, promoName } = body;
|
||||||
|
|
||||||
|
console.log("Received data:", { promoName, data });
|
||||||
|
|
||||||
|
if (!promoName || !Array.isArray(data)) {
|
||||||
|
throw new Error("Invalid request body");
|
||||||
|
}
|
||||||
|
|
||||||
|
using connection = connect("students");
|
||||||
|
|
||||||
|
connection.database.prepare(
|
||||||
|
"INSERT OR IGNORE INTO promotions (name) VALUES (?)",
|
||||||
|
).run(promoName);
|
||||||
|
|
||||||
|
const promoIdRow: { id: number } = connection.database
|
||||||
|
.prepare("SELECT id FROM promotions WHERE name = ?")
|
||||||
|
.get(promoName)!;
|
||||||
|
const promoId = promoIdRow.id;
|
||||||
|
|
||||||
|
console.log(`Promotion ID for "${promoName}":`, promoId);
|
||||||
|
|
||||||
|
const insertQuery = connection.database.prepare(
|
||||||
|
`INSERT INTO students
|
||||||
|
(userId, firstName, lastName, mail, promotionId)
|
||||||
|
VALUES (?, ?, ?, ?, ?)`,
|
||||||
|
);
|
||||||
|
|
||||||
|
for (const student of data) {
|
||||||
|
console.log("Inserting student:", student);
|
||||||
|
insertQuery.run(
|
||||||
|
student.Identifiant,
|
||||||
|
student.Nom,
|
||||||
|
student["Prénom"],
|
||||||
|
student.Mail,
|
||||||
|
promoId,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log("All data inserted successfully");
|
||||||
|
return new Response("Data inserted successfully", { status: 201 });
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error inserting data:", error);
|
||||||
|
return new Response("Failed to insert data", { status: 500 });
|
||||||
|
}
|
||||||
|
},
|
||||||
|
};
|
||||||
@@ -1,56 +0,0 @@
|
|||||||
// Mock de fetch() pour les tests
|
|
||||||
|
|
||||||
// deno-lint-ignore no-explicit-any
|
|
||||||
let _originalFetch: ((input: any, init?: any) => Promise<Response>) | null =
|
|
||||||
null;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Remplace globalThis.fetch par un mock qui retourne des réponses
|
|
||||||
* pré-configurées selon l'URL.
|
|
||||||
*
|
|
||||||
* @param routes - Map URL pattern → données de réponse (sera sérialisé en JSON)
|
|
||||||
*/
|
|
||||||
export function mockFetch(
|
|
||||||
routes: Record<string, unknown>,
|
|
||||||
): void {
|
|
||||||
_originalFetch = globalThis.fetch;
|
|
||||||
|
|
||||||
globalThis.fetch = (
|
|
||||||
input: string | URL | Request,
|
|
||||||
_init?: RequestInit,
|
|
||||||
): Promise<Response> => {
|
|
||||||
const url = typeof input === "string"
|
|
||||||
? input
|
|
||||||
: input instanceof URL
|
|
||||||
? input.toString()
|
|
||||||
: input.url;
|
|
||||||
|
|
||||||
for (const [pattern, data] of Object.entries(routes)) {
|
|
||||||
if (url.includes(pattern)) {
|
|
||||||
return Promise.resolve(
|
|
||||||
new Response(JSON.stringify(data), {
|
|
||||||
status: 200,
|
|
||||||
headers: { "Content-Type": "application/json" },
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return Promise.resolve(
|
|
||||||
new Response(JSON.stringify({ error: "Not Found" }), {
|
|
||||||
status: 404,
|
|
||||||
headers: { "Content-Type": "application/json" },
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Restaure le fetch original.
|
|
||||||
*/
|
|
||||||
export function restoreFetch(): void {
|
|
||||||
if (_originalFetch) {
|
|
||||||
globalThis.fetch = _originalFetch;
|
|
||||||
_originalFetch = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,107 +0,0 @@
|
|||||||
// Types et données de test pour l'API PolyMPR
|
|
||||||
|
|
||||||
export interface Student {
|
|
||||||
numEtud: number;
|
|
||||||
nom: string;
|
|
||||||
prenom: string;
|
|
||||||
idPromo: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface Promotion {
|
|
||||||
idPromo: number;
|
|
||||||
annee: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface Prof {
|
|
||||||
id: number;
|
|
||||||
nom: string;
|
|
||||||
prenom: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface Module {
|
|
||||||
id: number;
|
|
||||||
nom: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface Note {
|
|
||||||
note: number;
|
|
||||||
numEtud: number;
|
|
||||||
idModule: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface UE {
|
|
||||||
id: number;
|
|
||||||
nom: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface UeModule {
|
|
||||||
idModule: number;
|
|
||||||
idUE: number;
|
|
||||||
idPromo: number;
|
|
||||||
coeff: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface Enseignement {
|
|
||||||
idProf: number;
|
|
||||||
idModule: number;
|
|
||||||
idPromo: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface Ajustement {
|
|
||||||
numEtud: number;
|
|
||||||
idUE: number;
|
|
||||||
valeur: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
// --- Fixtures ---
|
|
||||||
|
|
||||||
export const students: Student[] = [
|
|
||||||
{ numEtud: 1, nom: "Dupont", prenom: "Alice", idPromo: 1 },
|
|
||||||
{ numEtud: 2, nom: "Martin", prenom: "Bob", idPromo: 1 },
|
|
||||||
{ numEtud: 3, nom: "Durand", prenom: "Claire", idPromo: 2 },
|
|
||||||
];
|
|
||||||
|
|
||||||
export const promotions: Promotion[] = [
|
|
||||||
{ idPromo: 1, annee: "2025-2026" },
|
|
||||||
{ idPromo: 2, annee: "2024-2025" },
|
|
||||||
];
|
|
||||||
|
|
||||||
export const profs: Prof[] = [
|
|
||||||
{ id: 1, nom: "Leclerc", prenom: "Jean" },
|
|
||||||
{ id: 2, nom: "Moreau", prenom: "Sophie" },
|
|
||||||
];
|
|
||||||
|
|
||||||
export const modules: Module[] = [
|
|
||||||
{ id: 1, nom: "Mathématiques" },
|
|
||||||
{ id: 2, nom: "Informatique" },
|
|
||||||
{ id: 3, nom: "Physique" },
|
|
||||||
];
|
|
||||||
|
|
||||||
export const notes: Note[] = [
|
|
||||||
{ note: 15, numEtud: 1, idModule: 1 },
|
|
||||||
{ note: 12, numEtud: 1, idModule: 2 },
|
|
||||||
{ note: 18, numEtud: 2, idModule: 1 },
|
|
||||||
{ note: 9, numEtud: 3, idModule: 3 },
|
|
||||||
];
|
|
||||||
|
|
||||||
export const ues: UE[] = [
|
|
||||||
{ id: 1, nom: "Sciences fondamentales" },
|
|
||||||
{ id: 2, nom: "Sciences appliquées" },
|
|
||||||
];
|
|
||||||
|
|
||||||
export const ueModules: UeModule[] = [
|
|
||||||
{ idModule: 1, idUE: 1, idPromo: 1, coeff: 3 },
|
|
||||||
{ idModule: 2, idUE: 2, idPromo: 1, coeff: 4 },
|
|
||||||
{ idModule: 3, idUE: 1, idPromo: 2, coeff: 2 },
|
|
||||||
];
|
|
||||||
|
|
||||||
export const enseignements: Enseignement[] = [
|
|
||||||
{ idProf: 1, idModule: 1, idPromo: 1 },
|
|
||||||
{ idProf: 2, idModule: 2, idPromo: 1 },
|
|
||||||
{ idProf: 1, idModule: 3, idPromo: 2 },
|
|
||||||
];
|
|
||||||
|
|
||||||
export const ajustements: Ajustement[] = [
|
|
||||||
{ numEtud: 1, idUE: 1, valeur: 0.5 },
|
|
||||||
{ numEtud: 3, idUE: 1, valeur: -1 },
|
|
||||||
];
|
|
||||||
@@ -1,55 +0,0 @@
|
|||||||
// Setup happy-dom + wrapper render pour les tests de composants Preact
|
|
||||||
|
|
||||||
import { Window } from "happy-dom";
|
|
||||||
|
|
||||||
let _window: Window | null = null;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Initialise un environnement DOM virtuel via happy-dom.
|
|
||||||
* À appeler avant de rendre des composants Preact dans les tests.
|
|
||||||
*/
|
|
||||||
export function setupDOM(): void {
|
|
||||||
_window = new Window({ url: "http://localhost" });
|
|
||||||
|
|
||||||
// Expose les globals DOM nécessaires à Preact
|
|
||||||
const globals = _window as unknown as Record<string, unknown>;
|
|
||||||
const target = globalThis as unknown as Record<string, unknown>;
|
|
||||||
|
|
||||||
for (
|
|
||||||
const key of [
|
|
||||||
"document",
|
|
||||||
"navigator",
|
|
||||||
"location",
|
|
||||||
"HTMLElement",
|
|
||||||
"HTMLInputElement",
|
|
||||||
"HTMLTextAreaElement",
|
|
||||||
"HTMLSelectElement",
|
|
||||||
"Event",
|
|
||||||
"CustomEvent",
|
|
||||||
"KeyboardEvent",
|
|
||||||
"MouseEvent",
|
|
||||||
"InputEvent",
|
|
||||||
"MutationObserver",
|
|
||||||
"requestAnimationFrame",
|
|
||||||
"cancelAnimationFrame",
|
|
||||||
]
|
|
||||||
) {
|
|
||||||
target[key] = globals[key];
|
|
||||||
}
|
|
||||||
|
|
||||||
target["window"] = _window;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Nettoie l'environnement DOM.
|
|
||||||
* À appeler dans un afterEach ou à la fin d'un test.
|
|
||||||
*/
|
|
||||||
export function cleanupDOM(): void {
|
|
||||||
if (_window) {
|
|
||||||
const doc = _window.document;
|
|
||||||
doc.body.innerHTML = "";
|
|
||||||
doc.head.innerHTML = "";
|
|
||||||
_window.close();
|
|
||||||
_window = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,61 +0,0 @@
|
|||||||
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