|
1 | | -import { QueryClient } from "@tanstack/react-query"; |
2 | | -import { render } from "@testing-library/react"; |
3 | | -import { test } from "vitest"; |
4 | | - |
5 | | -import { CrudContainer } from "@/components/crud-container"; |
6 | | -import { TestProviders } from "@/providers"; |
7 | | - |
8 | | -test("Renders CrudContainer", () => { |
9 | | - const queryClient = new QueryClient(); |
10 | | - render( |
11 | | - <TestProviders client={queryClient}> |
12 | | - <CrudContainer title="test"> |
13 | | - <div /> |
14 | | - </CrudContainer> |
15 | | - </TestProviders>, |
| 1 | +import { cleanup, fireEvent, render, screen } from "@testing-library/react"; |
| 2 | +import type React from "react"; |
| 3 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 4 | + |
| 5 | +import { ROUTES } from "@/constants/routes"; |
| 6 | +import { ConfigurationContext } from "@/providers/ConfigurationProvider"; |
| 7 | +import { SignInUserContext } from "@/providers/SignInUserProvider"; |
| 8 | +import { CrudContainer } from "./index"; |
| 9 | + |
| 10 | +const { |
| 11 | + mockNavigate, |
| 12 | + mockUseMutation, |
| 13 | + mockMutateSignOut, |
| 14 | + mockSignedInUserRefetch, |
| 15 | + mockUseIsMobile, |
| 16 | + menuPropsRef, |
| 17 | +} = vi.hoisted(() => ({ |
| 18 | + mockNavigate: vi.fn(), |
| 19 | + mockUseMutation: vi.fn(), |
| 20 | + mockMutateSignOut: vi.fn(), |
| 21 | + mockSignedInUserRefetch: vi.fn(), |
| 22 | + mockUseIsMobile: vi.fn(), |
| 23 | + menuPropsRef: { current: [] as any[] }, |
| 24 | +})); |
| 25 | + |
| 26 | +vi.mock("@tanstack/react-query", () => ({ |
| 27 | + useMutation: (options: unknown) => mockUseMutation(options), |
| 28 | +})); |
| 29 | + |
| 30 | +vi.mock("react-router-dom", async () => { |
| 31 | + const actual = |
| 32 | + await vi.importActual<typeof import("react-router-dom")>( |
| 33 | + "react-router-dom", |
| 34 | + ); |
| 35 | + return { |
| 36 | + ...actual, |
| 37 | + Link: ({ to, children }: { to: string; children: React.ReactNode }) => ( |
| 38 | + <a href={to}>{children}</a> |
| 39 | + ), |
| 40 | + useNavigate: () => mockNavigate, |
| 41 | + useParams: () => ({ model: "users" }), |
| 42 | + }; |
| 43 | +}); |
| 44 | + |
| 45 | +vi.mock("react-helmet-async", () => ({ |
| 46 | + Helmet: ({ children }: { children: React.ReactNode }) => <>{children}</>, |
| 47 | +})); |
| 48 | + |
| 49 | +vi.mock("@/hooks/useIsMobile", () => ({ |
| 50 | + useIsMobile: () => mockUseIsMobile(), |
| 51 | +})); |
| 52 | + |
| 53 | +vi.mock("@/fetchers/fetchers", () => ({ |
| 54 | + postFetcher: vi.fn(), |
| 55 | +})); |
| 56 | + |
| 57 | +vi.mock("@/helpers/title", () => ({ |
| 58 | + getTitleFromModel: (m: { name: string }) => m.name.toUpperCase(), |
| 59 | +})); |
| 60 | + |
| 61 | +vi.mock("react-i18next", async () => { |
| 62 | + const actual = |
| 63 | + await vi.importActual<typeof import("react-i18next")>("react-i18next"); |
| 64 | + return { |
| 65 | + ...actual, |
| 66 | + useTranslation: () => ({ |
| 67 | + t: (key: string) => key, |
| 68 | + i18n: { changeLanguage: vi.fn() }, |
| 69 | + }), |
| 70 | + }; |
| 71 | +}); |
| 72 | + |
| 73 | +vi.mock("antd", () => ({ |
| 74 | + theme: { |
| 75 | + useToken: () => ({ |
| 76 | + token: { colorBgContainer: "#fff", colorPrimary: "#000" }, |
| 77 | + }), |
| 78 | + }, |
| 79 | + Layout: Object.assign( |
| 80 | + ({ children }: { children: React.ReactNode }) => <div>{children}</div>, |
| 81 | + { |
| 82 | + Header: ({ children }: { children: React.ReactNode }) => ( |
| 83 | + <header>{children}</header> |
| 84 | + ), |
| 85 | + Sider: ({ children }: { children: React.ReactNode }) => ( |
| 86 | + <aside>{children}</aside> |
| 87 | + ), |
| 88 | + }, |
| 89 | + ), |
| 90 | + Typography: { |
| 91 | + Title: ({ children }: { children: React.ReactNode }) => <h5>{children}</h5>, |
| 92 | + }, |
| 93 | + Row: ({ children }: { children: React.ReactNode }) => <div>{children}</div>, |
| 94 | + Col: ({ children }: { children: React.ReactNode }) => <div>{children}</div>, |
| 95 | + Space: ({ children }: { children: React.ReactNode }) => <div>{children}</div>, |
| 96 | + Image: ({ alt }: { alt: string }) => <img alt={alt} />, |
| 97 | + Input: ({ |
| 98 | + onChange, |
| 99 | + value, |
| 100 | + }: { |
| 101 | + onChange?: (e: { target: { value: string } }) => void; |
| 102 | + value?: string; |
| 103 | + }) => ( |
| 104 | + <div> |
| 105 | + <span>{value || ""}</span> |
| 106 | + <button |
| 107 | + type="button" |
| 108 | + onClick={() => onChange?.({ target: { value: "use" } })} |
| 109 | + > |
| 110 | + trigger-search-menu |
| 111 | + </button> |
| 112 | + </div> |
| 113 | + ), |
| 114 | + Menu: (props: any) => { |
| 115 | + menuPropsRef.current.push(props); |
| 116 | + return ( |
| 117 | + <div> |
| 118 | + {(props.items || []).map((item: any) => ( |
| 119 | + <div key={item.key}> |
| 120 | + <button |
| 121 | + type="button" |
| 122 | + onClick={() => props.onClick?.({ key: item.key })} |
| 123 | + > |
| 124 | + menu-{item.key} |
| 125 | + </button> |
| 126 | + {(item.children || []).map((child: any) => ( |
| 127 | + <button |
| 128 | + key={child.key} |
| 129 | + type="button" |
| 130 | + onClick={() => props.onClick?.({ key: child.key })} |
| 131 | + > |
| 132 | + menu-{child.key} |
| 133 | + </button> |
| 134 | + ))} |
| 135 | + </div> |
| 136 | + ))} |
| 137 | + </div> |
| 138 | + ); |
| 139 | + }, |
| 140 | + Card: ({ |
| 141 | + title, |
| 142 | + children, |
| 143 | + }: { |
| 144 | + title: React.ReactNode; |
| 145 | + children: React.ReactNode; |
| 146 | + }) => ( |
| 147 | + <div> |
| 148 | + <div>{title}</div> |
| 149 | + {children} |
| 150 | + </div> |
| 151 | + ), |
| 152 | + Skeleton: ({ children }: { children: React.ReactNode }) => ( |
| 153 | + <div>{children}</div> |
| 154 | + ), |
| 155 | +})); |
| 156 | + |
| 157 | +const configurationValue: any = { |
| 158 | + configuration: { |
| 159 | + site_name: "FastAdmin", |
| 160 | + site_header_logo: "/logo.png", |
| 161 | + username_field: "username", |
| 162 | + models: [ |
| 163 | + { name: "users", permissions: [], actions: [], fields: [] }, |
| 164 | + { name: "posts", permissions: [], actions: [], fields: [] }, |
| 165 | + ], |
| 166 | + dashboard_widgets: [], |
| 167 | + }, |
| 168 | +}; |
| 169 | + |
| 170 | +const renderCrud = (signedIn: boolean, isMobile: boolean) => { |
| 171 | + mockUseIsMobile.mockReturnValue(isMobile); |
| 172 | + return render( |
| 173 | + <ConfigurationContext.Provider value={configurationValue}> |
| 174 | + <SignInUserContext.Provider |
| 175 | + value={ |
| 176 | + { |
| 177 | + signedIn, |
| 178 | + signedInUser: { id: "1", username: "bob" }, |
| 179 | + signedInUserRefetch: mockSignedInUserRefetch, |
| 180 | + } as any |
| 181 | + } |
| 182 | + > |
| 183 | + <CrudContainer |
| 184 | + title="Users" |
| 185 | + viewOnSite="https://example.com" |
| 186 | + headerActions={<div>header-actions</div>} |
| 187 | + bottomActions={<div>bottom-actions</div>} |
| 188 | + > |
| 189 | + <div>content</div> |
| 190 | + </CrudContainer> |
| 191 | + </SignInUserContext.Provider> |
| 192 | + </ConfigurationContext.Provider>, |
16 | 193 | ); |
| 194 | +}; |
| 195 | + |
| 196 | +describe("CrudContainer", () => { |
| 197 | + beforeEach(() => { |
| 198 | + vi.clearAllMocks(); |
| 199 | + menuPropsRef.current = []; |
| 200 | + mockUseMutation.mockReturnValue({ mutate: mockMutateSignOut }); |
| 201 | + }); |
| 202 | + |
| 203 | + afterEach(() => { |
| 204 | + cleanup(); |
| 205 | + }); |
| 206 | + |
| 207 | + it("handles sidebar, right menu actions and search filtering on desktop", () => { |
| 208 | + renderCrud(true, false); |
| 209 | + |
| 210 | + fireEvent.click(screen.getByRole("button", { name: "menu-dashboard" })); |
| 211 | + expect(mockNavigate).toHaveBeenCalledWith(ROUTES.HOME); |
| 212 | + |
| 213 | + fireEvent.click(screen.getByRole("button", { name: "menu-users" })); |
| 214 | + expect(mockNavigate).toHaveBeenCalledWith("/list/users"); |
| 215 | + |
| 216 | + fireEvent.click(screen.getByRole("button", { name: "menu-sign-out" })); |
| 217 | + expect(mockMutateSignOut).toHaveBeenCalled(); |
| 218 | + |
| 219 | + const mutationOptions = mockUseMutation.mock.calls[0][0] as any; |
| 220 | + mutationOptions.onSuccess(); |
| 221 | + expect(mockSignedInUserRefetch).toHaveBeenCalled(); |
| 222 | + |
| 223 | + fireEvent.click( |
| 224 | + screen.getByRole("button", { name: "trigger-search-menu" }), |
| 225 | + ); |
| 226 | + expect(screen.queryByRole("button", { name: "menu-posts" })).toBeNull(); |
| 227 | + expect(screen.getByText("header-actions")).toBeTruthy(); |
| 228 | + expect(screen.getByText("bottom-actions")).toBeTruthy(); |
| 229 | + expect(screen.getByText("View on site")).toBeTruthy(); |
| 230 | + }); |
| 231 | + |
| 232 | + it("redirects to sign-in when user is not signed in", () => { |
| 233 | + renderCrud(false, false); |
| 234 | + expect(mockNavigate).toHaveBeenCalledWith(ROUTES.SIGN_IN); |
| 235 | + }); |
| 236 | + |
| 237 | + it("covers mobile menu and default menu branches", () => { |
| 238 | + renderCrud(true, true); |
| 239 | + const menus = menuPropsRef.current; |
| 240 | + expect(menus.length).toBeGreaterThan(0); |
| 241 | + menus[0].onClick({ key: "users" }); |
| 242 | + expect(mockNavigate).toHaveBeenCalledWith("/list/users"); |
| 243 | + menus[menus.length - 1].onClick({ key: "unknown" }); |
| 244 | + expect(mockMutateSignOut).toHaveBeenCalledTimes(0); |
| 245 | + }); |
17 | 246 | }); |
0 commit comments