|
| 1 | +import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; |
| 2 | +import { render, screen } from "@testing-library/react"; |
| 3 | +import userEvent from "@testing-library/user-event"; |
| 4 | +import { MemoryRouter } from "react-router-dom"; |
| 5 | +import { AuthContext } from "@/contexts/authContext"; |
| 6 | +import useAlertStore from "@/stores/alertStore"; |
| 7 | +import useAuthStore from "@/stores/authStore"; |
| 8 | +import type { AuthContextType } from "@/types/contexts/auth"; |
| 9 | +import { axe } from "@/utils/a11y-test"; |
| 10 | + |
| 11 | +const mockResetPasswordMutate = jest.fn(); |
| 12 | +const mockUpdateUserMutate = jest.fn(); |
| 13 | +const mockAddApiKeyMutate = jest.fn(); |
| 14 | + |
| 15 | +jest.mock("@radix-ui/react-form", () => { |
| 16 | + const React = require("react"); |
| 17 | + return { |
| 18 | + __esModule: true, |
| 19 | + Root: ({ children, ...props }) => |
| 20 | + React.createElement("form", props, children), |
| 21 | + Field: ({ children, name }) => |
| 22 | + React.createElement("div", { "data-field": name }, children), |
| 23 | + Label: ({ children, ...props }) => |
| 24 | + React.createElement("label", props, children), |
| 25 | + Control: ({ children }) => children, |
| 26 | + Message: ({ children, ...props }) => |
| 27 | + React.createElement("p", props, children), |
| 28 | + Submit: ({ children }) => children, |
| 29 | + }; |
| 30 | +}); |
| 31 | + |
| 32 | +jest.mock("@/controllers/API/queries/api-keys", () => ({ |
| 33 | + usePostAddApiKey: () => ({ mutate: mockAddApiKeyMutate }), |
| 34 | +})); |
| 35 | + |
| 36 | +jest.mock("@/controllers/API/queries/auth", () => ({ |
| 37 | + useResetPassword: () => ({ mutate: mockResetPasswordMutate }), |
| 38 | + useUpdateUser: () => ({ mutate: mockUpdateUserMutate }), |
| 39 | +})); |
| 40 | + |
| 41 | +const PROFILE_PICTURES = { |
| 42 | + files: [], |
| 43 | + People: ["avatar-01.svg"], |
| 44 | +}; |
| 45 | + |
| 46 | +jest.mock("@/controllers/API/queries/files", () => ({ |
| 47 | + useGetProfilePicturesQuery: () => ({ |
| 48 | + isLoading: false, |
| 49 | + isFetching: false, |
| 50 | + data: PROFILE_PICTURES, |
| 51 | + }), |
| 52 | +})); |
| 53 | + |
| 54 | +jest.mock( |
| 55 | + "@/pages/SettingsPage/pages/GeneralPage/components/ProfilePictureForm/components/profilePictureChooserComponent/hooks/use-preload-images", |
| 56 | + () => { |
| 57 | + const { useEffect } = require("react"); |
| 58 | + return { |
| 59 | + __esModule: true, |
| 60 | + default: (setImagesLoaded: (v: boolean) => void) => { |
| 61 | + useEffect(() => { |
| 62 | + setImagesLoaded(true); |
| 63 | + }, []); |
| 64 | + }, |
| 65 | + }; |
| 66 | + }, |
| 67 | +); |
| 68 | + |
| 69 | +jest.mock("@/customization/utils/custom-pre-load-image-url", () => ({ |
| 70 | + customPreLoadImageUrl: (path: string) => |
| 71 | + `/api/v1/files/profile_pictures/${path}`, |
| 72 | +})); |
| 73 | + |
| 74 | +jest.mock("@/customization/components/custom-terms-links", () => ({ |
| 75 | + CustomTermsLinks: () => <div>Terms links</div>, |
| 76 | +})); |
| 77 | + |
| 78 | +import GeneralPage from "../index"; |
| 79 | + |
| 80 | +const AUTH_CONTEXT_VALUE: AuthContextType = { |
| 81 | + accessToken: "token", |
| 82 | + login: jest.fn(), |
| 83 | + userData: { id: "1" } as AuthContextType["userData"], |
| 84 | + setUserData: jest.fn(), |
| 85 | + authenticationErrorCount: 0, |
| 86 | + apiKey: null, |
| 87 | + setApiKey: jest.fn(), |
| 88 | + storeApiKey: jest.fn(), |
| 89 | + getUser: jest.fn(), |
| 90 | + clearAuthSession: jest.fn(), |
| 91 | +}; |
| 92 | + |
| 93 | +function renderGeneralPage() { |
| 94 | + const queryClient = new QueryClient(); |
| 95 | + return render( |
| 96 | + <MemoryRouter> |
| 97 | + <QueryClientProvider client={queryClient}> |
| 98 | + <AuthContext.Provider value={AUTH_CONTEXT_VALUE}> |
| 99 | + <GeneralPage /> |
| 100 | + </AuthContext.Provider> |
| 101 | + </QueryClientProvider> |
| 102 | + </MemoryRouter>, |
| 103 | + ); |
| 104 | +} |
| 105 | + |
| 106 | +describe("GeneralPage accessibility", () => { |
| 107 | + beforeAll(() => { |
| 108 | + if (!Element.prototype.hasPointerCapture) { |
| 109 | + Element.prototype.hasPointerCapture = jest.fn(() => false); |
| 110 | + } |
| 111 | + if (!Element.prototype.releasePointerCapture) { |
| 112 | + Element.prototype.releasePointerCapture = jest.fn(); |
| 113 | + } |
| 114 | + if (!Element.prototype.scrollIntoView) { |
| 115 | + Element.prototype.scrollIntoView = jest.fn(); |
| 116 | + } |
| 117 | + }); |
| 118 | + |
| 119 | + beforeEach(() => { |
| 120 | + jest.clearAllMocks(); |
| 121 | + useAlertStore.setState({ |
| 122 | + notificationList: [], |
| 123 | + tempNotificationList: [], |
| 124 | + }); |
| 125 | + useAuthStore.setState({ autoLogin: false }); |
| 126 | + }); |
| 127 | + |
| 128 | + it("should_have_no_axe_violations_on_initial_render", async () => { |
| 129 | + const { container } = renderGeneralPage(); |
| 130 | + |
| 131 | + expect(await axe(container)).toHaveNoViolations(); |
| 132 | + }); |
| 133 | + |
| 134 | + it("should_expose_a_single_page_heading_for_the_page_title", () => { |
| 135 | + renderGeneralPage(); |
| 136 | + |
| 137 | + expect( |
| 138 | + screen.getByRole("heading", { name: /general/i }), |
| 139 | + ).toBeInTheDocument(); |
| 140 | + }); |
| 141 | + |
| 142 | + it("renders_the_language_password_and_profile_picture_sections", () => { |
| 143 | + renderGeneralPage(); |
| 144 | + |
| 145 | + expect( |
| 146 | + screen.getByRole("combobox", { name: "Select language" }), |
| 147 | + ).toBeInTheDocument(); |
| 148 | + // One "Save" submit button per section (password + profile picture). |
| 149 | + expect(screen.getAllByRole("button", { name: /save/i })).toHaveLength(2); |
| 150 | + }); |
| 151 | + |
| 152 | + it("gives_the_password_and_profile_picture_forms_distinct_accessible_names", () => { |
| 153 | + // Regression lock: axe-core only treats a <form> as a "form" landmark |
| 154 | + // once it has an accessible name, so an unlabeled duplicate never shows |
| 155 | + // up as an axe violation (verified: landmark-unique is "inapplicable" |
| 156 | + // for unlabeled forms, not passing). The IBM ACE page scanner is |
| 157 | + // stricter and flags any duplicate unlabeled forms, which is what |
| 158 | + // caught this on the real /settings/general page. |
| 159 | + renderGeneralPage(); |
| 160 | + |
| 161 | + expect(screen.getByRole("form", { name: "Password" })).toBeInTheDocument(); |
| 162 | + expect( |
| 163 | + screen.getByRole("form", { name: "Profile Picture" }), |
| 164 | + ).toBeInTheDocument(); |
| 165 | + }); |
| 166 | + |
| 167 | + it("should_have_no_axe_violations_with_the_language_popover_open", async () => { |
| 168 | + const user = userEvent.setup(); |
| 169 | + renderGeneralPage(); |
| 170 | + |
| 171 | + await user.click(screen.getByRole("combobox", { name: "Select language" })); |
| 172 | + await screen.findByRole("listbox"); |
| 173 | + |
| 174 | + // Radix portals the popover to document.body, outside the render |
| 175 | + // container, and the region rule is a page-level landmark concern that |
| 176 | + // a bare unit render cannot satisfy. |
| 177 | + expect( |
| 178 | + await axe(document.body, { rules: { region: { enabled: false } } }), |
| 179 | + ).toHaveNoViolations(); |
| 180 | + }); |
| 181 | + |
| 182 | + it("does_not_render_the_password_form_when_autologin_is_enabled", () => { |
| 183 | + useAuthStore.setState({ autoLogin: true }); |
| 184 | + renderGeneralPage(); |
| 185 | + |
| 186 | + expect( |
| 187 | + screen.queryByPlaceholderText("settings.passwordPlaceholder"), |
| 188 | + ).not.toBeInTheDocument(); |
| 189 | + }); |
| 190 | +}); |
0 commit comments