hezarfen_frontend
Clone
https://git.tahinli.com/archive/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits
| 1 | import { afterEach, describe, expect, test, vi } from "vitest"; |
| 2 | import type { ClassGroup, ClassMember, PersonRef } from "@/api/client"; |
| 3 | import { classMemberCount, getStudentDirectory, invalidateStudentDirectory, sortByClass } from "@/lib/student-directory"; |
| 4 | import { resetInstanceLabelCache } from "@/lib/instance-labels"; |
| 5 | |
| 6 | const cls = (name: string, gradeLevel: number): ClassGroup => ({ |
| 7 | id: name, creator: null, name, grade_level: gradeLevel, year: null, teacher: null, |
| 8 | }); |
| 9 | const person = (name: string): PersonRef => ({ |
| 10 | id: name, username: name.toLowerCase(), display_name: name, student_number: null, |
| 11 | } as PersonRef); |
| 12 | |
| 13 | describe("sortByClass", () => { |
| 14 | test("orders by grade, then şube, then name; classless students last", () => { |
| 15 | const rows = [ |
| 16 | { person: person("Zeynep"), classes: [cls("10-B", 10)] }, |
| 17 | { person: person("Ali"), classes: [] }, |
| 18 | { person: person("Can"), classes: [cls("9-A", 9)] }, |
| 19 | { person: person("Ayşe"), classes: [cls("10-A", 10)] }, |
| 20 | { person: person("Burak"), classes: [cls("10-B", 10)] }, |
| 21 | { person: person("Deniz"), classes: [cls("9-B", 9)] }, |
| 22 | ]; |
| 23 | expect(sortByClass(rows).map((row) => row.person.display_name)) |
| 24 | .toEqual(["Can", "Deniz", "Ayşe", "Burak", "Zeynep", "Ali"]); |
| 25 | }); |
| 26 | |
| 27 | test("files a student in several classes under the earliest one", () => { |
| 28 | const rows = [ |
| 29 | { person: person("Ece"), classes: [cls("10-B", 10), cls("9-A", 9)] }, |
| 30 | { person: person("Mert"), classes: [cls("9-B", 9)] }, |
| 31 | ]; |
| 32 | const sorted = sortByClass(rows); |
| 33 | expect(sorted.map((row) => row.person.display_name)).toEqual(["Ece", "Mert"]); |
| 34 | expect(sorted[0].classes.map((c) => c.name)).toEqual(["9-A", "10-B"]); |
| 35 | }); |
| 36 | }); |
| 37 | |
| 38 | const member = (classId: string, user: PersonRef): ClassMember => ({ |
| 39 | id: `m-${user.id}`, class: classId, user, added_by: user, joined_at: 0, left_at: null, source_class_group: null, |
| 40 | }); |
| 41 | const jsonResponse = (data: unknown) => |
| 42 | new Response(JSON.stringify(data), { status: 200, headers: { "Content-Type": "application/json" } }); |
| 43 | |
| 44 | describe("getStudentDirectory", () => { |
| 45 | afterEach(() => { |
| 46 | invalidateStudentDirectory(); |
| 47 | resetInstanceLabelCache(); |
| 48 | vi.restoreAllMocks(); |
| 49 | }); |
| 50 | |
| 51 | test("with no class, reads every student and every class's members as before", async () => { |
| 52 | const calls: string[] = []; |
| 53 | const respond = (url: string): Response => { |
| 54 | calls.push(url); |
| 55 | if (url === "/api/users/search?q=&role=student") |
| 56 | return jsonResponse({ items: [person("Zeynep"), person("Ali")], total: 2, limit: null, offset: 0 }); |
| 57 | if (url === "/api/classes?limit=200") |
| 58 | return jsonResponse({ items: [cls("9-A", 9), cls("10-B", 10)], total: 2, limit: 200, offset: 0 }); |
| 59 | if (url === "/api/classes/9-A/members?limit=500&offset=0") |
| 60 | return jsonResponse({ items: [member("9-A", person("Ali"))], total: 1, limit: 500, offset: 0 }); |
| 61 | if (url === "/api/classes/10-B/members?limit=500&offset=0") return jsonResponse({ items: [], total: 0, limit: 500, offset: 0 }); |
| 62 | throw new Error(`unexpected fetch: ${url}`); |
| 63 | }; |
| 64 | vi.stubGlobal("fetch", vi.fn(async (input: RequestInfo | URL) => respond(String(input)))); |
| 65 | |
| 66 | const rows = await getStudentDirectory(); |
| 67 | |
| 68 | expect(calls).toEqual([ |
| 69 | "/api/users/search?q=&role=student", |
| 70 | "/api/classes?limit=200", |
| 71 | "/api/classes/9-A/members?limit=500&offset=0", |
| 72 | "/api/classes/10-B/members?limit=500&offset=0", |
| 73 | ]); |
| 74 | expect(rows.map((row) => row.person.display_name)).toEqual(["Ali", "Zeynep"]); |
| 75 | expect(rows[0].classes.map((c) => c.name)).toEqual(["9-A"]); |
| 76 | expect(rows[1].classes).toEqual([]); |
| 77 | }); |
| 78 | |
| 79 | test("with a class picked, reads only that roster, following offsets to total", async () => { |
| 80 | const calls: string[] = []; |
| 81 | const respond = (url: string): Response => { |
| 82 | calls.push(url); |
| 83 | if (url === "/api/classes/c1/members?limit=500&offset=0") |
| 84 | return jsonResponse({ items: [member("c1", person("Ali")), member("c1", person("Burak"))], total: 3, limit: 500, offset: 0 }); |
| 85 | if (url === "/api/classes/c1/members?limit=500&offset=2") |
| 86 | return jsonResponse({ items: [member("c1", person("Can"))], total: 3, limit: 500, offset: 2 }); |
| 87 | throw new Error(`unexpected fetch: ${url}`); |
| 88 | }; |
| 89 | vi.stubGlobal("fetch", vi.fn(async (input: RequestInfo | URL) => respond(String(input)))); |
| 90 | const nineA = { ...cls("9-A", 9), id: "c1" }; |
| 91 | |
| 92 | const rows = await getStudentDirectory(nineA); |
| 93 | |
| 94 | expect(calls).toEqual(["/api/classes/c1/members?limit=500&offset=0", "/api/classes/c1/members?limit=500&offset=2"]); |
| 95 | expect(rows.map((row) => row.person.display_name)).toEqual(["Ali", "Burak", "Can"]); |
| 96 | expect(rows.every((row) => row.classes.length === 1 && row.classes[0] === nineA)).toBe(true); |
| 97 | }); |
| 98 | |
| 99 | test("with a class picked and a roster within one page, fetches it once", async () => { |
| 100 | const calls: string[] = []; |
| 101 | const respond = (url: string): Response => { |
| 102 | calls.push(url); |
| 103 | if (url === "/api/classes/c2/members?limit=500&offset=0") |
| 104 | return jsonResponse({ items: [member("c2", person("Ali"))], total: 1, limit: 500, offset: 0 }); |
| 105 | throw new Error(`unexpected fetch: ${url}`); |
| 106 | }; |
| 107 | vi.stubGlobal("fetch", vi.fn(async (input: RequestInfo | URL) => respond(String(input)))); |
| 108 | |
| 109 | const rows = await getStudentDirectory({ ...cls("9-A", 9), id: "c2" }); |
| 110 | |
| 111 | expect(calls).toEqual(["/api/classes/c2/members?limit=500&offset=0"]); |
| 112 | expect(rows.map((row) => row.person.display_name)).toEqual(["Ali"]); |
| 113 | }); |
| 114 | |
| 115 | test("reuses a roster for later visits and the class count, then invalidates it", async () => { |
| 116 | const fetch = vi.fn(async () => jsonResponse({ items: [member("c3", person("Ali"))], total: 1, limit: 500, offset: 0 })); |
| 117 | vi.stubGlobal("fetch", fetch); |
| 118 | const picked = { ...cls("9-A", 9), id: "c3" }; |
| 119 | await getStudentDirectory(picked); |
| 120 | await getStudentDirectory(picked); |
| 121 | expect(await classMemberCount("c3")).toBe(1); |
| 122 | expect(fetch).toHaveBeenCalledTimes(1); |
| 123 | invalidateStudentDirectory(); |
| 124 | await getStudentDirectory(picked); |
| 125 | expect(fetch).toHaveBeenCalledTimes(2); |
| 126 | }); |
| 127 | |
| 128 | test("retries a class whose first roster request failed", async () => { |
| 129 | const fetch = vi.fn() |
| 130 | .mockRejectedValueOnce(new Error("offline")) |
| 131 | .mockResolvedValue(jsonResponse({ items: [], total: 0, limit: 500, offset: 0 })); |
| 132 | vi.stubGlobal("fetch", fetch); |
| 133 | const picked = { ...cls("9-A", 9), id: "c4" }; |
| 134 | await expect(getStudentDirectory(picked)).rejects.toThrow(); |
| 135 | expect(await getStudentDirectory(picked)).toEqual([]); |
| 136 | expect(fetch).toHaveBeenCalledTimes(2); |
| 137 | }); |
| 138 | }); |