forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

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

session-roll-call.test.tsx127 lines, 4.8 KiBHistoryRawDownload
1import { fireEvent, render, screen, waitFor, within } from "@solidjs/testing-library";
2import { SessionRollCall } from "@/components/sessions/session-roll-call";
3import { PreferencesProvider } from "@/stores/preferences-context";
4
5const { getSessionAttendance, postSessionAttendance } = vi.hoisted(() => ({
6 getSessionAttendance: vi.fn(async () => ({ items: [], total: 0, limit: null, offset: 0 })),
7 postSessionAttendance: vi.fn(async (_sessionId: string, body: { user_id: string; status: string }) => ({
8 id: `attendance-${body.user_id}`,
9 session: "session-1",
10 class_course: "instance-1",
11 user: { id: body.user_id, username: body.user_id, display_name: null },
12 status: body.status,
13 marked_by: { id: "manager-1", username: "manager", display_name: null },
14 })),
15}));
16
17vi.mock("@/api/sessions", () => ({
18 getSessionAttendance,
19 postSessionAttendance,
20 deleteSessionAttendanceByUserId: vi.fn(async () => undefined),
21}));
22
23vi.mock("@/api/settings", () => ({
24 getSettings: async () => ({ attendance_statuses: ["present", "absent", "late", "excused"] }),
25}));
26
27const enrollment = (id: string, name: string) => ({
28 id: `enrollment-${id}`,
29 class_course: "instance-1",
30 user: { id, username: id, display_name: name },
31 enrolled_by: { id: "manager-1", username: "manager", display_name: null },
32 source: null,
33});
34const roster = [enrollment("student-1", "Ada Öğrenci")];
35
36const teacher = { id: "teacher-1", username: "teacher", display_name: "Deniz Öğretmen" };
37
38afterEach(() => {
39 vi.clearAllMocks();
40});
41
42const mount = (props: { roster?: ReturnType<typeof enrollment>[]; canMarkTeacher?: boolean } = {}) =>
43 render(() => (
44 <PreferencesProvider>
45 <SessionRollCall
46 sessionId="session-1"
47 roster={props.roster ?? roster}
48 teacher={teacher}
49 canMarkTeacher={props.canMarkTeacher ?? false}
50 />
51 </PreferencesProvider>
52 ));
53const statusButtons = (name: string) => within(screen.getByRole("radiogroup", { name }));
54/** The status buttons unlock once the saved marks have loaded. */
55const ready = () => screen.findByText(/\d+ of \d+ marked|kişiden \d+ işaretlendi/);
56
57test("lets manager-level users mark the session teacher alongside enrolled students", async () => {
58 mount({ canMarkTeacher: true });
59
60 expect(await screen.findByText("Ada Öğrenci")).toBeTruthy();
61 await ready();
62 fireEvent.click(statusButtons("Deniz Öğretmen").getByRole("radio", { name: /Present|Var|Mevcut/ }));
63
64 await waitFor(() => {
65 expect(postSessionAttendance).toHaveBeenCalledWith("session-1", { user_id: "teacher-1", status: "present" });
66 });
67});
68
69test("keeps the teacher out of roll call for users without staff-attendance rights", async () => {
70 mount();
71
72 expect(await screen.findByText("Ada Öğrenci")).toBeTruthy();
73 expect(screen.queryByText("Deniz Öğretmen")).toBeNull();
74 expect(screen.getAllByRole("radiogroup")).toHaveLength(1);
75});
76
77test("one tap saves that row, with no roster refetch", async () => {
78 mount();
79 await ready();
80 const absent = statusButtons("Ada Öğrenci").getByRole("radio", { name: /Absent|Yok|Devamsız/ });
81
82 fireEvent.click(absent);
83
84 await waitFor(() => expect(postSessionAttendance).toHaveBeenCalledWith("session-1", { user_id: "student-1", status: "absent" }));
85 expect(absent.getAttribute("aria-checked")).toBe("true");
86 expect(getSessionAttendance).toHaveBeenCalledTimes(1);
87});
88
89test("mark the rest present fills only the unmarked students", async () => {
90 getSessionAttendance.mockResolvedValueOnce({
91 items: [{
92 id: "a-2",
93 session: "session-1",
94 class_course: "instance-1",
95 user: { id: "student-2", username: "student-2", display_name: "Bora" },
96 status: "absent",
97 marked_by: { id: "teacher-1", username: "teacher", display_name: null },
98 }],
99 total: 1,
100 limit: null,
101 offset: 0,
102 } as never);
103 mount({
104 roster: [enrollment("student-1", "Ada"), enrollment("student-2", "Bora"), enrollment("student-3", "Cem")],
105 canMarkTeacher: true,
106 });
107 expect(await screen.findByText(/1 of 4 marked|4 kişiden 1/)).toBeTruthy();
108
109 fireEvent.click(screen.getByRole("button", { name: /Mark the rest present|Kalanları Var/ }));
110
111 await waitFor(() => expect(postSessionAttendance).toHaveBeenCalledTimes(2));
112 const marked = postSessionAttendance.mock.calls.map(([, body]) => body.user_id).sort();
113 expect(marked).toEqual(["student-1", "student-3"]);
114 expect(await screen.findByText(/3 of 4 marked|4 kişiden 3/)).toBeTruthy();
115});
116
117test("a failed save puts the row back as it was", async () => {
118 postSessionAttendance.mockRejectedValueOnce(new Error("offline"));
119 mount();
120 await ready();
121 const late = statusButtons("Ada Öğrenci").getByRole("radio", { name: /Late|Geç/ });
122
123 fireEvent.click(late);
124
125 await waitFor(() => expect(screen.getByRole("alert")).toBeTruthy());
126 expect(late.getAttribute("aria-checked")).toBe("false");
127});