hezarfen_frontend
Clone
https://git.tahinli.com/Hezarfen/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits
| 1 | import { cleanup, fireEvent, render, screen } from "@solidjs/testing-library"; |
| 2 | import { |
| 3 | Outlet, |
| 4 | RouterProvider, |
| 5 | createMemoryHistory, |
| 6 | createRootRoute, |
| 7 | createRoute, |
| 8 | createRouter, |
| 9 | } from "@tanstack/solid-router"; |
| 10 | import { afterEach, expect, test } from "vitest"; |
| 11 | import { RouteErrorFallback } from "@/components/layout/route-error"; |
| 12 | |
| 13 | afterEach(cleanup); |
| 14 | |
| 15 | test("a page that throws is replaced in place and the shell stays", async () => { |
| 16 | let failures = 1; |
| 17 | const root = createRootRoute({ |
| 18 | component: () => ( |
| 19 | <div> |
| 20 | <nav>SHELL NAV</nav> |
| 21 | <Outlet /> |
| 22 | </div> |
| 23 | ), |
| 24 | }); |
| 25 | const page = createRoute({ |
| 26 | getParentRoute: () => root, |
| 27 | path: "/", |
| 28 | component: () => { |
| 29 | if (failures-- > 0) throw new Error("list failed"); |
| 30 | return <p>PAGE OK</p>; |
| 31 | }, |
| 32 | }); |
| 33 | const router = createRouter({ |
| 34 | routeTree: root.addChildren([page]), |
| 35 | history: createMemoryHistory({ initialEntries: ["/"] }), |
| 36 | defaultErrorComponent: RouteErrorFallback, |
| 37 | }); |
| 38 | |
| 39 | render(() => <RouterProvider router={router} />); |
| 40 | |
| 41 | expect(await screen.findByRole("alert")).toBeTruthy(); |
| 42 | expect(screen.getByText("SHELL NAV")).toBeTruthy(); |
| 43 | |
| 44 | fireEvent.click(screen.getByRole("button")); |
| 45 | expect(await screen.findByText("PAGE OK")).toBeTruthy(); |
| 46 | }); |
| 47 | |
| 48 | test("the app router uses it for every route", async () => { |
| 49 | const { router } = await import("@/routes/router"); |
| 50 | expect(router.options.defaultErrorComponent).toBe(RouteErrorFallback); |
| 51 | }); |
| 52 | |
| 53 | test("a 404 shows not-found with a way back to the list, not a retry", async () => { |
| 54 | const { ApiError } = await import("@/api/client"); |
| 55 | const root = createRootRoute({ component: () => <Outlet /> }); |
| 56 | const list = createRoute({ getParentRoute: () => root, path: "/courses", component: () => <p>LIST</p> }); |
| 57 | const detail = createRoute({ |
| 58 | getParentRoute: () => root, |
| 59 | path: "/courses/$id", |
| 60 | component: () => { |
| 61 | throw new ApiError(404, "not found"); |
| 62 | }, |
| 63 | }); |
| 64 | const router = createRouter({ |
| 65 | routeTree: root.addChildren([list, detail]), |
| 66 | history: createMemoryHistory({ initialEntries: ["/courses/bad"] }), |
| 67 | defaultErrorComponent: RouteErrorFallback, |
| 68 | }); |
| 69 | |
| 70 | render(() => <RouterProvider router={router} />); |
| 71 | |
| 72 | const back = await screen.findByRole("link", { name: /^(Back to the list|Listeye dön)$/ }); |
| 73 | expect(back.getAttribute("href")).toBe("/courses"); |
| 74 | expect(screen.queryByRole("button")).toBeNull(); |
| 75 | }); |
| 76 | |
| 77 | test("a rate-limited page is centered with an illustration, waits out retry-after and names the cause", async () => { |
| 78 | const { ApiError } = await import("@/api/client"); |
| 79 | const root = createRootRoute({ component: () => <Outlet /> }); |
| 80 | const page = createRoute({ |
| 81 | getParentRoute: () => root, |
| 82 | path: "/exams", |
| 83 | component: () => { |
| 84 | throw new ApiError(429, "too many requests", 15, null, "rate_limited"); |
| 85 | }, |
| 86 | }); |
| 87 | const router = createRouter({ |
| 88 | routeTree: root.addChildren([page]), |
| 89 | history: createMemoryHistory({ initialEntries: ["/exams"] }), |
| 90 | defaultErrorComponent: RouteErrorFallback, |
| 91 | }); |
| 92 | |
| 93 | render(() => <RouterProvider router={router} />); |
| 94 | |
| 95 | const alert = await screen.findByRole("alert"); |
| 96 | expect(alert.querySelector(".illustration")).toBeTruthy(); |
| 97 | expect(screen.getByText(/^(This page could not load|Bu sayfa yüklenemedi)$/)).toBeTruthy(); |
| 98 | expect(screen.getByText(/^(Try again in 15s\.|15 sn sonra tekrar dene\.)$/)).toBeTruthy(); |
| 99 | |
| 100 | // Retrying before retry-after only earns another 429. |
| 101 | const retry = screen.getByRole("button") as HTMLButtonElement; |
| 102 | expect(retry.disabled).toBe(true); |
| 103 | expect(retry.textContent).toMatch(/15/); |
| 104 | |
| 105 | // The technical details carry the real fields of the error. |
| 106 | const details = alert.querySelector("dl")!; |
| 107 | expect(details.textContent).toContain("429"); |
| 108 | expect(details.textContent).toContain("rate_limited"); |
| 109 | expect(details.textContent).toContain("/exams"); |
| 110 | expect(details.textContent).toContain("too many requests"); |
| 111 | expect(details.textContent).toMatch(/rate limited|hız sınırı/); |
| 112 | }); |