hezarfen_frontend
Clone
https://git.tahinli.com/archive/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits
| 1 | import { expect, test } from "vitest"; |
| 2 | import { |
| 3 | addChoice, |
| 4 | blankChoiceSet, |
| 5 | moveChoice, |
| 6 | newChoiceId, |
| 7 | removeChoice, |
| 8 | setChoiceText, |
| 9 | setCorrect, |
| 10 | storedChoiceSet, |
| 11 | submitChoices, |
| 12 | } from "./choice-set"; |
| 13 | |
| 14 | const stored = () => |
| 15 | storedChoiceSet([{ id: "A", text: "one" }, { id: "B", text: "two" }, { id: "C", text: "three" }], "B"); |
| 16 | |
| 17 | test("a fresh set pre-marks nothing", () => { |
| 18 | const set = blankChoiceSet(); |
| 19 | expect(set.rows).toHaveLength(4); |
| 20 | expect(set.correct).toBeNull(); |
| 21 | expect(new Set(set.rows.map((r) => r.id)).size).toBe(4); |
| 22 | }); |
| 23 | |
| 24 | test("minted ids are unique and cannot collide with a stored ULID", () => { |
| 25 | expect(newChoiceId()).not.toBe(newChoiceId()); |
| 26 | expect(newChoiceId()).toContain(":"); // ':' is not in Crockford base32 |
| 27 | }); |
| 28 | |
| 29 | test("dropping a blank row leaves the marked row's id untouched", () => { |
| 30 | const set = setChoiceText(stored(), "A", " "); |
| 31 | expect(submitChoices(set)).toEqual({ |
| 32 | choices: [{ id: "B", text: "two" }, { id: "C", text: "three" }], |
| 33 | images: [null, null], |
| 34 | correct: "B", |
| 35 | }); |
| 36 | }); |
| 37 | |
| 38 | test("reordering carries ids, so the answer key follows its choice", () => { |
| 39 | const moved = moveChoice(stored(), "B", 2); |
| 40 | expect(moved.rows.map((r) => r.id)).toEqual(["A", "C", "B"]); |
| 41 | expect(submitChoices(moved).correct).toBe("B"); |
| 42 | }); |
| 43 | |
| 44 | test("removing or blanking the marked row unmarks it instead of shifting the key", () => { |
| 45 | expect(removeChoice(stored(), "B").correct).toBeNull(); |
| 46 | expect(submitChoices(setChoiceText(stored(), "B", "")).correct).toBeNull(); |
| 47 | }); |
| 48 | |
| 49 | test("a new row is markable, because it carries a client-minted key", () => { |
| 50 | const added = addChoice(stored(), 10); |
| 51 | const fresh = added.rows[3].id; |
| 52 | const marked = setChoiceText(setCorrect(added, fresh), fresh, "four"); |
| 53 | const out = submitChoices(marked); |
| 54 | expect(out.correct).toBe(fresh); |
| 55 | expect(out.choices.map((c) => c.id)).toContain(fresh); |
| 56 | }); |
| 57 | |
| 58 | test("add respects the max and setCorrect ignores an unknown id", () => { |
| 59 | expect(addChoice(blankChoiceSet(10), 10).rows).toHaveLength(10); |
| 60 | expect(setCorrect(stored(), "nope").correct).toBe("B"); |
| 61 | }); |