Skip to content

Commit

Permalink
test: add tests for lib
Browse files Browse the repository at this point in the history
  • Loading branch information
Flowrey committed Sep 14, 2024
1 parent 453b316 commit ec268a1
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 33 deletions.
36 changes: 36 additions & 0 deletions src/js/lib.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { TeachingUnitRepository } from "./teaching_unit/repository";
import { TeachingUnitView } from "./teaching_unit/view";
import { TeachingUnit, ValidationError } from "./teaching_unit/model";

export function getCurriculum(doc: Document) {
const curriculum = doc.getElementById("parcours");
if (curriculum == null) {
throw new Error("no curriculum founded");
}
return curriculum;
}

export function getTeachingUnitElements(curriculum: Element) {
return Array.from(curriculum.getElementsByClassName("ue"));
}

export function main(doc: Document) {
const curriculum = getCurriculum(doc);
const teachingUnitElements = getTeachingUnitElements(curriculum);
teachingUnitElements.forEach(async (el: Element) => {
try {
const teachingUnit = TeachingUnit.fromElement(el);
teachingUnit.state =
await TeachingUnitRepository.getStateFromLocalStorage(teachingUnit);

new TeachingUnitRepository(teachingUnit);
new TeachingUnitView(el, teachingUnit);
} catch (error) {
if (error instanceof ValidationError) {
console.warn(`Failed to get teaching unit from element (${error})`);
} else {
console.error(`An unexpected error occurred (${error})`);
}
}
});
}
35 changes: 2 additions & 33 deletions src/js/main.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,3 @@
import { TeachingUnitRepository } from "./teaching_unit/repository";
import { TeachingUnitView } from "./teaching_unit/view";
import { TeachingUnit, ValidationError } from "./teaching_unit/model";
import { main } from "./lib";

function getCurriculum() {
const curriculum = document.getElementById("parcours");
if (curriculum == null) {
throw new Error("no curriculum founded");
}
return curriculum;
}

function getTeachingUnitElements(curriculum: Element) {
return Array.from(curriculum.getElementsByClassName("ue"));
}

const curriculum = getCurriculum();
const teachingUnitElements = getTeachingUnitElements(curriculum);
teachingUnitElements.forEach(async (el: Element) => {
try {
const teachingUnit = TeachingUnit.fromElement(el);
teachingUnit.state =
await TeachingUnitRepository.getStateFromLocalStorage(teachingUnit);

new TeachingUnitRepository(teachingUnit);
new TeachingUnitView(el, teachingUnit);
} catch (error) {
if (error instanceof ValidationError) {
console.warn(`Failed to get teaching unit from element (${error})`);
} else {
console.error(`An unexpected error occurred (${error})`);
}
}
});
main(document);
47 changes: 47 additions & 0 deletions tests/lib.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { newHTMLTeachingUnit } from "./teaching_unit.test";
import { main } from "../src/js/lib";
import userEvent from "@testing-library/user-event";
import browser from "./__mocks__/webextension-polyfill";
import "@testing-library/jest-dom";

beforeEach(() => {
browser.storage.local.get.mockImplementation(() => Promise.resolve({}));
document.body.innerHTML =
'<div id="parcours">' +
newHTMLTeachingUnit("Mathematical tools for IT", "UTC501", 3) +
newHTMLTeachingUnit("Fundamentals of Operating Systems", "UTC502", 3) +
"</div>";
});

test("main throws an error if no curriculum is founded", () => {
document.getElementById("parcours").setAttribute("id", "foo");

expect(() => {
main(document);
}).toThrow("no curriculum founded");
});

test("main don't throws and error if a teaching unit is invalid", async () => {
document.getElementById("parcours").innerHTML =
newHTMLTeachingUnit(null, "UTC501", 3) +
newHTMLTeachingUnit("Fundamentals of Operating Systems", "UTC502", 3);
const user = userEvent.setup();
const el = document.getElementsByClassName("ue")[1];
main(document);
expect(el).not.toHaveClass("selected");

await user.click(el);

expect(el).toHaveClass("selected");
});

test("clicking on a teachingUnit change his state", async () => {
const user = userEvent.setup();
const el = document.getElementsByClassName("ue")[0];
main(document);
expect(el).not.toHaveClass("selected");

await user.click(el);

expect(el).toHaveClass("selected");
});

0 comments on commit ec268a1

Please sign in to comment.