|
1 | 1 | import { BrowserRouter } from "react-router-dom"; |
| 2 | +import { QueryClient, QueryClientProvider } from "react-query"; |
2 | 3 | import { render, screen } from "@testing-library/react"; |
| 4 | +import { vi } from "vitest"; |
3 | 5 | import "@testing-library/jest-dom"; |
4 | 6 |
|
5 | 7 | import ModelNav from "../ModelNav"; |
| 8 | +import { useRemodels } from "../../../hooks"; |
6 | 9 |
|
7 | | -const renderComponent = () => { |
| 10 | +import type { UseQueryResult } from "react-query"; |
| 11 | +import type { ApiResponse, RemodelResponse } from "../../../types/shared"; |
| 12 | + |
| 13 | +vi.mock("../../../hooks", () => ({ |
| 14 | + useRemodels: vi.fn(), |
| 15 | +})); |
| 16 | + |
| 17 | +vi.mock("../../../state/brandStoreState", () => ({ |
| 18 | + brandIdState: "mock-brand-id", |
| 19 | +})); |
| 20 | + |
| 21 | +vi.mock("jotai", () => ({ |
| 22 | + useAtomValue: vi.fn(() => "mock-brand-id"), |
| 23 | +})); |
| 24 | + |
| 25 | +const queryClient = new QueryClient(); |
| 26 | + |
| 27 | +const renderComponent = (sectionName: string) => { |
8 | 28 | return render( |
9 | 29 | <BrowserRouter> |
10 | | - <ModelNav sectionName="policies" /> |
| 30 | + <QueryClientProvider client={queryClient}> |
| 31 | + <ModelNav sectionName={sectionName} /> |
| 32 | + </QueryClientProvider> |
11 | 33 | </BrowserRouter>, |
12 | 34 | ); |
13 | 35 | }; |
14 | 36 |
|
15 | 37 | describe("ModelNav", () => { |
16 | 38 | it("highlights the correct navigation item", () => { |
17 | | - renderComponent(); |
| 39 | + const mockUseRemodels = vi.mocked(useRemodels); |
| 40 | + mockUseRemodels.mockReturnValue({ |
| 41 | + data: { |
| 42 | + success: true, |
| 43 | + data: { |
| 44 | + allowlist: [], |
| 45 | + "next-cursor": null, |
| 46 | + }, |
| 47 | + }, |
| 48 | + } as unknown as UseQueryResult<ApiResponse<RemodelResponse>, Error>); |
| 49 | + |
| 50 | + renderComponent("policies"); |
18 | 51 | const currentLink = screen.getByRole("tab", { name: "Policies" }); |
19 | 52 | expect(currentLink.getAttribute("aria-selected")).toBe("true"); |
20 | 53 | }); |
21 | 54 |
|
22 | 55 | it("doesn't highlight other navigation links", () => { |
23 | | - renderComponent(); |
| 56 | + const mockUseRemodels = vi.mocked(useRemodels); |
| 57 | + mockUseRemodels.mockReturnValue({ |
| 58 | + data: { |
| 59 | + success: true, |
| 60 | + data: { |
| 61 | + allowlist: [], |
| 62 | + "next-cursor": null, |
| 63 | + }, |
| 64 | + }, |
| 65 | + } as unknown as UseQueryResult<ApiResponse<RemodelResponse>, Error>); |
| 66 | + |
| 67 | + renderComponent("policies"); |
24 | 68 | const currentLink = screen.getByRole("tab", { name: "Overview" }); |
25 | 69 | expect(currentLink.getAttribute("aria-selected")).toBe("false"); |
26 | 70 | }); |
| 71 | + |
| 72 | + it("shows Remodel tab when useRemodels returns success: true", () => { |
| 73 | + const mockUseRemodels = vi.mocked(useRemodels); |
| 74 | + mockUseRemodels.mockReturnValue({ |
| 75 | + data: { |
| 76 | + success: true, |
| 77 | + data: { |
| 78 | + allowlist: [], |
| 79 | + "next-cursor": null, |
| 80 | + }, |
| 81 | + }, |
| 82 | + } as unknown as UseQueryResult<ApiResponse<RemodelResponse>, Error>); |
| 83 | + |
| 84 | + renderComponent("overview"); |
| 85 | + expect(screen.getByRole("tab", { name: "Remodel" })).toBeInTheDocument(); |
| 86 | + }); |
| 87 | + |
| 88 | + it("hides Remodel tab when useRemodels returns success: false", () => { |
| 89 | + const mockUseRemodels = vi.mocked(useRemodels); |
| 90 | + mockUseRemodels.mockReturnValue({ |
| 91 | + data: { |
| 92 | + success: false, |
| 93 | + message: "Remodeling not available", |
| 94 | + data: { |
| 95 | + allowlist: [], |
| 96 | + "next-cursor": null, |
| 97 | + }, |
| 98 | + }, |
| 99 | + } as unknown as UseQueryResult<ApiResponse<RemodelResponse>, Error>); |
| 100 | + |
| 101 | + renderComponent("overview"); |
| 102 | + expect( |
| 103 | + screen.queryByRole("tab", { name: "Remodel" }), |
| 104 | + ).not.toBeInTheDocument(); |
| 105 | + }); |
| 106 | + |
| 107 | + it("hides Remodel tab when useRemodels returns no data", () => { |
| 108 | + const mockUseRemodels = vi.mocked(useRemodels); |
| 109 | + mockUseRemodels.mockReturnValue({ |
| 110 | + data: undefined, |
| 111 | + } as unknown as UseQueryResult<ApiResponse<RemodelResponse>, Error>); |
| 112 | + |
| 113 | + renderComponent("overview"); |
| 114 | + expect( |
| 115 | + screen.queryByRole("tab", { name: "Remodel" }), |
| 116 | + ).not.toBeInTheDocument(); |
| 117 | + }); |
27 | 118 | }); |
0 commit comments