forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiBdcf123aBurak Boduroğlu2 months ago674 commits

answer-sheet.test.ts58 lines, 1.7 KiBHistoryRawDownload
1import { expect, test } from "vitest";
2import type { ExamQuestion, StudentAnswer } from "@/api/client";
3import { joinAnswerSheet } from "./answer-sheet";
4
5function 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
19function 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
30test("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
40test("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
46test("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
52test("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});