hezarfen_frontend
Clone
https://git.tahinli.com/archive/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiBdcf123aBurak Boduroğlu2 months ago674 commits
| 1 | import { render, screen } from "@solidjs/testing-library"; |
| 2 | import type { BadgeCatalogEntry, ProfileStats } from "@/api/client"; |
| 3 | import { BadgeGrid } from "@/components/users/badge-grid"; |
| 4 | import { PreferencesProvider } from "@/stores/preferences-context"; |
| 5 | |
| 6 | const catalog: BadgeCatalogEntry[] = [ |
| 7 | { id: "homework_submitted_1", stat: "homework_submitted", threshold: 1 }, |
| 8 | { id: "homework_submitted_10", stat: "homework_submitted", threshold: 10 }, |
| 9 | { id: "study_streak_7", stat: "study_streak", threshold: 7 }, |
| 10 | // An id this build has no copy for — it must render, not crash. |
| 11 | { id: "streak_1000", stat: "streak_days", threshold: 1000 }, |
| 12 | ]; |
| 13 | |
| 14 | const stats: ProfileStats = { |
| 15 | pomodoro_sessions: 0, |
| 16 | pomodoro_focus_ms: 0, |
| 17 | courses: 0, |
| 18 | classes: 0, |
| 19 | homework_submitted_total: 7, |
| 20 | homework_on_time_total: 0, |
| 21 | exam_sat_total: 0, |
| 22 | pomodoro_finished_total: 0, |
| 23 | pomodoro_focus_ms_total: 0, |
| 24 | marks_given_total: 0, |
| 25 | lessons_held_total: 0, |
| 26 | pool_approved_total: 0, |
| 27 | pool_published_total: 0, |
| 28 | lessons_attended_total: 0, |
| 29 | high_mark_total: 0, |
| 30 | study_streak_total: 7, |
| 31 | }; |
| 32 | |
| 33 | function renderGrid(earned: { id: string; earned_at: number }[]) { |
| 34 | return render(() => ( |
| 35 | <PreferencesProvider> |
| 36 | <BadgeGrid catalog={catalog} earned={earned} stats={stats} /> |
| 37 | </PreferencesProvider> |
| 38 | )); |
| 39 | } |
| 40 | |
| 41 | function openBadges() { |
| 42 | screen.getByRole("button", { name: /Badges/ }).click(); |
| 43 | } |
| 44 | |
| 45 | afterEach(() => vi.restoreAllMocks()); |
| 46 | |
| 47 | test("shows the whole catalogue, earned and locked alike", () => { |
| 48 | renderGrid([{ id: "homework_submitted_1", earned_at: 1_700_000_000_000 }]); |
| 49 | openBadges(); |
| 50 | expect(screen.getAllByRole("generic", { hidden: true }).length).toBeGreaterThan(0); |
| 51 | // Both the earned and the unearned tier are present — the incomplete grid is |
| 52 | // the point, so an unearned badge is never hidden. |
| 53 | expect(screen.getByText("First Steps")).toBeTruthy(); |
| 54 | expect(screen.getByText("Steady Hand")).toBeTruthy(); |
| 55 | }); |
| 56 | |
| 57 | test("an unearned badge shows its progress inline, an earned one shows its date", () => { |
| 58 | renderGrid([{ id: "homework_submitted_1", earned_at: 1_700_000_000_000 }]); |
| 59 | openBadges(); |
| 60 | // The tooltip is hover-only, so the tile itself must carry the numbers: |
| 61 | // 7 of 10 done on the second tier. |
| 62 | expect(screen.getAllByText("7 / 10").length).toBeGreaterThan(0); |
| 63 | expect(screen.getAllByText(/Earned/).length).toBeGreaterThan(0); |
| 64 | // The remaining count rides the accessible name, which is what the tooltip |
| 65 | // repeats — a keyboard or screen-reader user gets it without hovering. |
| 66 | expect(screen.getByLabelText(/Steady Hand — 3 to go/)).toBeTruthy(); |
| 67 | expect(screen.getByLabelText(/Study Week — 0 to go/)).toBeTruthy(); |
| 68 | }); |
| 69 | |
| 70 | test("a badge whose stat this build does not know shows no progress bar", () => { |
| 71 | renderGrid([]); |
| 72 | openBadges(); |
| 73 | // Falls back to the raw id for the label and to the generic locked line. |
| 74 | expect(screen.getByText("streak_1000")).toBeTruthy(); |
| 75 | expect(screen.getAllByText("Not earned yet").length).toBe(1); |
| 76 | }); |
| 77 | |
| 78 | test("every tile is focusable and labelled, so the tooltip is reachable by keyboard", () => { |
| 79 | renderGrid([]); |
| 80 | openBadges(); |
| 81 | const tiles = screen.getAllByLabelText(/—/); |
| 82 | expect(tiles.length).toBe(catalog.length); |
| 83 | for (const tile of tiles) expect(tile.getAttribute("tabindex")).toBe("0"); |
| 84 | }); |