hezarfen_frontend
Clone
https://git.tahinli.com/Hezarfen/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits
| 1 | import { createRoot } from "solid-js"; |
| 2 | import { createStudentClassLabels } from "./student-classes"; |
| 3 | |
| 4 | const calls: string[] = []; |
| 5 | const classesByUser: Record<string, string[]> = { |
| 6 | s1: ["10-A"], |
| 7 | s2: ["11-B", "Seçmeli Fizik"], |
| 8 | s3: [], |
| 9 | s9: ["9-C"], |
| 10 | }; |
| 11 | |
| 12 | vi.mock("@/api/classes", () => ({ |
| 13 | getClassesByUserId: async (userId: string) => { |
| 14 | calls.push(userId); |
| 15 | const names = classesByUser[userId]; |
| 16 | if (!names) throw new Error("403"); |
| 17 | return { items: names.map((name, index) => ({ id: `${userId}-${index}`, name })), total: names.length, limit: null, offset: 0 }; |
| 18 | }, |
| 19 | })); |
| 20 | |
| 21 | const flush = () => new Promise((resolve) => setTimeout(resolve, 0)); |
| 22 | |
| 23 | // Solid holds effects until the root's synchronous body returns, so the root |
| 24 | // is entered and left before anything is awaited. |
| 25 | function withRoot<T>(build: () => T): [T, () => void] { |
| 26 | let value!: T; |
| 27 | let dispose!: () => void; |
| 28 | createRoot((disposeRoot) => { |
| 29 | dispose = disposeRoot; |
| 30 | value = build(); |
| 31 | }); |
| 32 | return [value, dispose]; |
| 33 | } |
| 34 | |
| 35 | describe("createStudentClassLabels", () => { |
| 36 | beforeEach(() => { |
| 37 | calls.length = 0; |
| 38 | }); |
| 39 | |
| 40 | it("resolves a class name per student and joins multiple memberships", async () => { |
| 41 | const [labels, dispose] = withRoot(() => createStudentClassLabels(() => ["s1", "s2"])); |
| 42 | await flush(); |
| 43 | expect(labels().s1).toBe("10-A"); |
| 44 | expect(labels().s2).toBe("11-B, Seçmeli Fizik"); |
| 45 | dispose(); |
| 46 | }); |
| 47 | |
| 48 | it("leaves a student with no class — or a refused lookup — unlabelled", async () => { |
| 49 | const [labels, dispose] = withRoot(() => createStudentClassLabels(() => ["s3", "forbidden"])); |
| 50 | await flush(); |
| 51 | expect(labels().s3).toBeUndefined(); |
| 52 | expect(labels().forbidden).toBeUndefined(); |
| 53 | dispose(); |
| 54 | }); |
| 55 | |
| 56 | it("asks the backend once per student, however often the list is read", async () => { |
| 57 | const [, disposeFirst] = withRoot(() => createStudentClassLabels(() => ["s9"])); |
| 58 | await flush(); |
| 59 | const [, disposeSecond] = withRoot(() => createStudentClassLabels(() => ["s9"])); |
| 60 | await flush(); |
| 61 | expect(calls.filter((id) => id === "s9")).toHaveLength(1); |
| 62 | disposeFirst(); |
| 63 | disposeSecond(); |
| 64 | }); |
| 65 | }); |