hezarfen_frontend
Clone
https://git.tahinli.com/archive/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiBdcf123aBurak Boduroğlu2 months ago674 commits
| 1 | import { fireEvent, render, screen } from "@solidjs/testing-library"; |
| 2 | import type { QuestionFormInitial, QuestionValues } from "@/components/exams/question-form"; |
| 3 | import { QuestionForm } from "@/components/exams/question-form"; |
| 4 | import { PreferencesProvider } from "@/stores/preferences-context"; |
| 5 | |
| 6 | // The form loads school settings only for the upload size cap; keep it off the network. |
| 7 | vi.mock("@/api/settings", () => ({ getSettings: async () => ({ max_file_bytes: 5_000_000 }) })); |
| 8 | |
| 9 | const subjects = [ |
| 10 | { id: "s-1", name: "Algebra" }, |
| 11 | { id: "s-2", name: "Geometry" }, |
| 12 | ] as never as { id: string; name: string }[]; |
| 13 | |
| 14 | function mount(props: Partial<Parameters<typeof QuestionForm>[0]> = {}) { |
| 15 | const onSubmit = vi.fn(async (_values: QuestionValues) => {}); |
| 16 | const result = render(() => ( |
| 17 | <PreferencesProvider> |
| 18 | <QuestionForm subjects={subjects as never} onSubmit={onSubmit} onCancel={() => {}} {...props} /> |
| 19 | </PreferencesProvider> |
| 20 | )); |
| 21 | const submit = () => fireEvent.submit(result.container.querySelector("form")!); |
| 22 | return { ...result, onSubmit, submit }; |
| 23 | } |
| 24 | |
| 25 | const type = (el: Element, value: string) => fireEvent.input(el, { target: { value } }); |
| 26 | |
| 27 | afterEach(() => vi.restoreAllMocks()); |
| 28 | |
| 29 | describe("stored subject that is not pickable", () => { |
| 30 | const initial: QuestionFormInitial = { |
| 31 | subject: "s-gone", |
| 32 | text: "What is 2 + 2?", |
| 33 | kind: "text", |
| 34 | points: 1, |
| 35 | choices: null, |
| 36 | correct: null, |
| 37 | }; |
| 38 | |
| 39 | test("shows it as an explicit disabled option instead of silently swapping", () => { |
| 40 | mount({ initial }); |
| 41 | const select = screen.getByLabelText("Subject") as HTMLSelectElement; |
| 42 | expect(select.value).toBe("s-gone"); |
| 43 | const option = Array.from(select.options).find((o) => o.value === "s-gone")!; |
| 44 | expect(option.disabled).toBe(true); |
| 45 | expect(option.textContent).toContain("no longer available"); |
| 46 | }); |
| 47 | |
| 48 | test("blocks submit until a pickable subject is chosen", async () => { |
| 49 | const { onSubmit, submit, container } = mount({ initial }); |
| 50 | await submit(); |
| 51 | expect(onSubmit).not.toHaveBeenCalled(); |
| 52 | // Same wording sits under the select as help text, so read the error box itself. |
| 53 | expect(container.querySelector('[class*="bg-destructive/10"]')?.textContent).toMatch( |
| 54 | /not in the list you can pick from/i, |
| 55 | ); |
| 56 | |
| 57 | fireEvent.change(screen.getByLabelText("Subject"), { target: { value: "s-2" } }); |
| 58 | await submit(); |
| 59 | expect(onSubmit).toHaveBeenCalledTimes(1); |
| 60 | expect(onSubmit.mock.calls[0][0].subject_id).toBe("s-2"); |
| 61 | }); |
| 62 | }); |
| 63 | |
| 64 | describe("correct answer", () => { |
| 65 | test("nothing is pre-marked on a fresh form", () => { |
| 66 | mount(); |
| 67 | // Every marker button still shows its letter; a marked row shows a check icon instead. |
| 68 | const markers = screen.getAllByTitle("Mark correct"); |
| 69 | expect(markers.map((m) => m.textContent)).toEqual(["A", "B", "C", "D"]); |
| 70 | }); |
| 71 | |
| 72 | test("submit is blocked with a message when no choice is marked", async () => { |
| 73 | const { onSubmit, submit, container } = mount(); |
| 74 | fireEvent.change(screen.getByLabelText("Subject"), { target: { value: "s-1" } }); |
| 75 | type(container.querySelector("#question-text")!, "Pick one"); |
| 76 | const choiceInputs = container.querySelectorAll("[data-choice-id] input:not([type=file])"); |
| 77 | type(choiceInputs[0], "four"); |
| 78 | type(choiceInputs[1], "five"); |
| 79 | await submit(); |
| 80 | |
| 81 | expect(onSubmit).not.toHaveBeenCalled(); |
| 82 | expect(screen.getByText(/Mark one choice as the correct answer/i)).toBeTruthy(); |
| 83 | |
| 84 | fireEvent.click(screen.getAllByTitle("Mark correct")[1]); |
| 85 | await submit(); |
| 86 | expect(onSubmit).toHaveBeenCalledTimes(1); |
| 87 | const values = onSubmit.mock.calls[0][0]; |
| 88 | expect(values.correct).toBe(values.choices![1].id); |
| 89 | }); |
| 90 | }); |
| 91 | |
| 92 | test("a stored option keeps its own image after the rows above it are removed", () => { |
| 93 | const initial: QuestionFormInitial = { |
| 94 | subject: "s-1", |
| 95 | text: "Which one?", |
| 96 | kind: "choice", |
| 97 | points: 1, |
| 98 | choices: [ |
| 99 | { id: "c-1", text: "Alpha" }, |
| 100 | { id: "c-2", text: "Bravo" }, |
| 101 | { id: "c-3", text: "Charlie" }, |
| 102 | { id: "c-4", text: "Delta" }, |
| 103 | ], |
| 104 | correct: "c-2", |
| 105 | // Only Bravo has a picture; it must stay on Bravo whatever row it sits in. |
| 106 | choice_images: [null, { name: "bravo.png", size: 10, mime: "image/png" } as never, null, null], |
| 107 | }; |
| 108 | const { container } = mount({ initial, choiceImageSrc: (id: string) => `/img/${id}` }); |
| 109 | |
| 110 | const imageOf = (choiceId: string) => |
| 111 | container.querySelector(`[data-choice-id="${choiceId}"] img`)?.getAttribute("src") ?? null; |
| 112 | expect(imageOf("c-2")).toBe("/img/c-2"); |
| 113 | |
| 114 | // Drop Alpha: Bravo slides up to the first row. |
| 115 | fireEvent.click(container.querySelector('[data-choice-id="c-1"] button:last-of-type')!); |
| 116 | expect(container.querySelector('[data-choice-id="c-1"]')).toBeNull(); |
| 117 | expect(imageOf("c-2")).toBe("/img/c-2"); |
| 118 | for (const id of ["c-3", "c-4"]) expect(imageOf(id)).toBeNull(); |
| 119 | }); |