-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
85 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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})`); | ||
} | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
}); |