hezarfen_frontend
Clone
https://git.tahinli.com/archive/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits
| 1 | import { |
| 2 | eventStatusLabelKey, |
| 3 | isRollCallOpen, |
| 4 | orderRollCallStatuses, |
| 5 | rollCallChanges, |
| 6 | saveRollCallChanges, |
| 7 | } from "@/lib/event-roll-call"; |
| 8 | |
| 9 | test("core statuses get event wording, school-defined ones do not", () => { |
| 10 | expect(eventStatusLabelKey("present")).toBe("events.status.present"); |
| 11 | expect(eventStatusLabelKey("absent")).toBe("events.status.absent"); |
| 12 | expect(eventStatusLabelKey("late")).toBe("events.status.late"); |
| 13 | expect(eventStatusLabelKey("excused")).toBe("events.status.excused"); |
| 14 | expect(eventStatusLabelKey("medical")).toBeNull(); |
| 15 | }); |
| 16 | |
| 17 | test("present leads the status list, the rest keep the school's order", () => { |
| 18 | expect(orderRollCallStatuses(["absent", "late", "present", "excused"])).toEqual(["present", "absent", "late", "excused"]); |
| 19 | expect(orderRollCallStatuses(["absent", "late"])).toEqual(["absent", "late"]); |
| 20 | }); |
| 21 | |
| 22 | test("only drafted statuses that differ from the saved mark are sent", () => { |
| 23 | const saved = { a: "present", b: null, c: "late" }; |
| 24 | const draft = { a: "present", b: "absent", c: "excused", d: "present" }; |
| 25 | expect(rollCallChanges(saved, draft)).toEqual([ |
| 26 | { userId: "b", status: "absent" }, |
| 27 | { userId: "c", status: "excused" }, |
| 28 | { userId: "d", status: "present" }, |
| 29 | ]); |
| 30 | }); |
| 31 | |
| 32 | test("an empty draft has nothing to save", () => { |
| 33 | expect(rollCallChanges({ a: "present" }, {})).toEqual([]); |
| 34 | expect(rollCallChanges({ a: "present" }, { a: undefined })).toEqual([]); |
| 35 | }); |
| 36 | |
| 37 | test("roll call opens at the start time, or at once without one", () => { |
| 38 | expect(isRollCallOpen(2000, 1000)).toBe(false); |
| 39 | expect(isRollCallOpen(1000, 1000)).toBe(true); |
| 40 | expect(isRollCallOpen(1000, 2000)).toBe(true); |
| 41 | expect(isRollCallOpen(null, 0)).toBe(true); |
| 42 | expect(isRollCallOpen(undefined, 0)).toBe(true); |
| 43 | }); |
| 44 | |
| 45 | test("saving sends rows in order, keeps going past a failure and reports progress", async () => { |
| 46 | const sent: string[] = []; |
| 47 | const progress: [number, number][] = []; |
| 48 | const boom = new Error("outside audience"); |
| 49 | const result = await saveRollCallChanges( |
| 50 | [ |
| 51 | { userId: "a", status: "present" }, |
| 52 | { userId: "b", status: "absent" }, |
| 53 | { userId: "c", status: "late" }, |
| 54 | ], |
| 55 | async (change) => { |
| 56 | sent.push(change.userId); |
| 57 | if (change.userId === "b") throw boom; |
| 58 | }, |
| 59 | (done, total) => progress.push([done, total]), |
| 60 | ); |
| 61 | expect(sent).toEqual(["a", "b", "c"]); |
| 62 | expect(result.saved.map((change) => change.userId)).toEqual(["a", "c"]); |
| 63 | expect(result.failed).toEqual([{ change: { userId: "b", status: "absent" }, error: boom }]); |
| 64 | expect(progress).toEqual([[1, 3], [2, 3], [3, 3]]); |
| 65 | }); |