|
| 1 | +// noinspection JSUnusedGlobalSymbols |
| 2 | + |
| 3 | +/** |
| 4 | + * @file Unit tests for src/routes/__root.tsx |
| 5 | + * @description This file tests the root route, conditional devtools loading, and 404 handling. |
| 6 | + * It uses dynamic imports to test behavior under different NODE_ENV conditions and suppresses |
| 7 | + * known benign console errors for a clean test output. |
| 8 | + */ |
| 9 | + |
| 10 | +// region --- Imports |
| 11 | +import type React from "react" |
| 12 | +import type { ReactElement } from "react" |
| 13 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest" |
| 14 | +// endregion |
| 15 | + |
| 16 | +// region --- Mocks |
| 17 | +vi.mock("@tanstack/router-devtools", () => ({ |
| 18 | + TanStackRouterDevtools: (): ReactElement => <div data-testid="router-devtools-mock" />, |
| 19 | +})) |
| 20 | +vi.mock("@tanstack/react-query-devtools", () => ({ |
| 21 | + ReactQueryDevtools: (): ReactElement => <div data-testid="query-devtools-mock" />, |
| 22 | +})) |
| 23 | + |
| 24 | +vi.mock("@tanstack/react-router", async (importOriginal) => { |
| 25 | + const original = await importOriginal<typeof import("@tanstack/react-router")>() |
| 26 | + return { |
| 27 | + ...original, |
| 28 | + Outlet: (): ReactElement => <div data-testid="outlet-mock" />, |
| 29 | + Link: ({ to, children, ...props }: { to: string; children: React.ReactNode }): ReactElement => ( |
| 30 | + <a href={to} {...props}> |
| 31 | + {children} |
| 32 | + </a> |
| 33 | + ), |
| 34 | + } |
| 35 | +}) |
| 36 | + |
| 37 | +vi.mock("next-themes", () => ({ |
| 38 | + ThemeProvider: ({ children }: { children: React.ReactNode }) => <>{children}</>, |
| 39 | +})) |
| 40 | +// endregion |
| 41 | + |
| 42 | +describe("Root Route and Component", () => { |
| 43 | + let originalConsoleError: typeof console.error |
| 44 | + |
| 45 | + /** |
| 46 | + * @description Sets up mocks before each test. It resets all modules to allow for |
| 47 | + * dynamic imports with different environments and suppresses known benign errors. |
| 48 | + */ |
| 49 | + beforeEach(() => { |
| 50 | + vi.resetModules() |
| 51 | + originalConsoleError = console.error |
| 52 | + console.error = vi.fn((message: string) => { |
| 53 | + if (message.includes("Could not parse CSS stylesheet")) { |
| 54 | + return |
| 55 | + } |
| 56 | + originalConsoleError(message) |
| 57 | + }) |
| 58 | + }) |
| 59 | + |
| 60 | + /** |
| 61 | + * @description Cleans up and restores mocks after each test. |
| 62 | + */ |
| 63 | + afterEach(() => { |
| 64 | + console.error = originalConsoleError |
| 65 | + vi.unstubAllEnvs() |
| 66 | + }) |
| 67 | + |
| 68 | + // region --- Devtools Loading Logic |
| 69 | + describe("TanStackDevtools Loading Logic", () => { |
| 70 | + it("should render devtools in development mode", async () => { |
| 71 | + // Arrange |
| 72 | + vi.stubEnv("NODE_ENV", "development") |
| 73 | + const { render, screen } = await import("../test-utils") |
| 74 | + const { Route } = await import("@/routes/__root") |
| 75 | + const RootComponent = Route.options.component! |
| 76 | + |
| 77 | + // Act |
| 78 | + render(<RootComponent />) |
| 79 | + |
| 80 | + // Assert |
| 81 | + expect(screen.getByTestId("outlet-mock")).toBeInTheDocument() |
| 82 | + expect(await screen.findByTestId("router-devtools-mock")).toBeInTheDocument() |
| 83 | + expect(await screen.findByTestId("query-devtools-mock")).toBeInTheDocument() |
| 84 | + }) |
| 85 | + |
| 86 | + it("should not render devtools in production mode", async () => { |
| 87 | + // Arrange |
| 88 | + vi.stubEnv("NODE_ENV", "production") |
| 89 | + const { render, screen } = await import("../test-utils") |
| 90 | + const { Route } = await import("@/routes/__root") |
| 91 | + const RootComponent = Route.options.component! |
| 92 | + |
| 93 | + // Act |
| 94 | + render(<RootComponent />) |
| 95 | + |
| 96 | + // Assert |
| 97 | + expect(screen.getByTestId("outlet-mock")).toBeInTheDocument() |
| 98 | + expect(screen.queryByTestId("router-devtools-mock")).not.toBeInTheDocument() |
| 99 | + expect(screen.queryByTestId("query-devtools-mock")).not.toBeInTheDocument() |
| 100 | + }) |
| 101 | + }) |
| 102 | + // endregion |
| 103 | + |
| 104 | + // region --- Route Not Found Logic |
| 105 | + describe("Route Not Found Logic", () => { |
| 106 | + it("should render the NotFound component when using the notFoundComponent property", async () => { |
| 107 | + // Arrange |
| 108 | + const { render, screen } = await import("../test-utils") |
| 109 | + const { Route } = await import("@/routes/__root") |
| 110 | + const NotFoundComponent = Route.options.notFoundComponent! |
| 111 | + |
| 112 | + // Act |
| 113 | + render(<NotFoundComponent data={undefined} />) |
| 114 | + |
| 115 | + // Assert |
| 116 | + expect(screen.getByTestId("not-found")).toBeInTheDocument() |
| 117 | + }) |
| 118 | + }) |
| 119 | + // endregion |
| 120 | +}) |
0 commit comments