hezarfen_frontend
Clone
https://git.tahinli.com/archive/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiBc35a83aBurak Boduroğlu4 months ago440 commits
| 1 | import { expect, test } from "vitest"; |
| 2 | import type { ExamQuestion, StudentAnswer } from "@/api/client"; |
| 3 | import { joinAnswerSheet } from "./answer-sheet"; |
| 4 | |
| 5 | function question(id: string, overrides: Partial<ExamQuestion> = {}): ExamQuestion { |
| 6 | return { |
| 7 | id, |
| 8 | exam: "e1", |
| 9 | subject: "s1", |
| 10 | text: `Question ${id}`, |
| 11 | kind: "choice", |
| 12 | points: 10, |
| 13 | choices: [{ id: "cA", text: "a" }, { id: "cB", text: "b" }], |
| 14 | correct: "cA", |
| 15 | ...overrides, |
| 16 | }; |
| 17 | } |
| 18 | |
| 19 | function answer(questionId: string, overrides: Partial<StudentAnswer> = {}): StudentAnswer { |
| 20 | return { |
| 21 | question: questionId, |
| 22 | selected: "cA", |
| 23 | text: null, |
| 24 | updated_at: 1, |
| 25 | is_correct: true, |
| 26 | ...overrides, |
| 27 | }; |
| 28 | } |
| 29 | |
| 30 | test("pairs each question with its answer by id, keeping question order", () => { |
| 31 | const rows = joinAnswerSheet( |
| 32 | [question("q1"), question("q2")], |
| 33 | [answer("q2", { selected: "cB", is_correct: false }), answer("q1")], |
| 34 | ); |
| 35 | expect(rows.map((r) => r.question.id)).toEqual(["q1", "q2"]); |
| 36 | expect(rows[0].answer?.is_correct).toBe(true); |
| 37 | expect(rows[1].answer?.selected).toBe("cB"); |
| 38 | }); |
| 39 | |
| 40 | test("unanswered question yields a null answer", () => { |
| 41 | const rows = joinAnswerSheet([question("q1")], []); |
| 42 | expect(rows).toHaveLength(1); |
| 43 | expect(rows[0].answer).toBeNull(); |
| 44 | }); |
| 45 | |
| 46 | test("answers for deleted questions are dropped", () => { |
| 47 | const rows = joinAnswerSheet([question("q1")], [answer("gone")]); |
| 48 | expect(rows).toHaveLength(1); |
| 49 | expect(rows[0].answer).toBeNull(); |
| 50 | }); |
| 51 | |
| 52 | test("text answers carry the student's text through", () => { |
| 53 | const rows = joinAnswerSheet( |
| 54 | [question("q1", { kind: "text", choices: null, correct: null })], |
| 55 | [answer("q1", { selected: null, text: "my essay", is_correct: null })], |
| 56 | ); |
| 57 | expect(rows[0].answer?.text).toBe("my essay"); |
| 58 | }); |