|
| 1 | +import React from "react"; |
| 2 | +import { fireEvent, render } from "@testing-library/react"; |
| 3 | + |
| 4 | +import { SkipToMainContent } from "../SkipToMainContent"; |
| 5 | + |
| 6 | +describe("SkipToMainContent", () => { |
| 7 | + it("should render correctly", () => { |
| 8 | + const { container, rerender } = render(<SkipToMainContent mainId="" />); |
| 9 | + expect(container).toMatchSnapshot(); |
| 10 | + |
| 11 | + rerender( |
| 12 | + <SkipToMainContent mainId="main-content" id="skip" unstyled> |
| 13 | + Skip |
| 14 | + </SkipToMainContent> |
| 15 | + ); |
| 16 | + expect(container).toMatchSnapshot(); |
| 17 | + }); |
| 18 | + |
| 19 | + it("should focus the main element when clicked", () => { |
| 20 | + const onClick = jest.fn(); |
| 21 | + const { getByRole } = render( |
| 22 | + <> |
| 23 | + <SkipToMainContent mainId="main-content" onClick={onClick} /> |
| 24 | + <main tabIndex={-1} id="main-content" /> |
| 25 | + </> |
| 26 | + ); |
| 27 | + |
| 28 | + fireEvent.click(getByRole("link")); |
| 29 | + expect(onClick).toBeCalledTimes(1); |
| 30 | + |
| 31 | + expect(document.activeElement).toBe(getByRole("main")); |
| 32 | + }); |
| 33 | + |
| 34 | + describe("non-prod", () => { |
| 35 | + it("should log an error if there is no main element with the provided mainId when clicked", () => { |
| 36 | + const error = jest.spyOn(console, "error").mockImplementation(() => {}); |
| 37 | + const { getByRole } = render(<SkipToMainContent mainId="main-content" />); |
| 38 | + |
| 39 | + fireEvent.click(getByRole("link")); |
| 40 | + expect(error).toBeCalledWith( |
| 41 | + 'Unable to find a main element to focus with an id of: "main-content".' |
| 42 | + ); |
| 43 | + |
| 44 | + error.mockRestore(); |
| 45 | + }); |
| 46 | + |
| 47 | + it("should log an error if there is no main element with the provided mainI when clicked and try to find a <main> id to suggest", () => { |
| 48 | + const error = jest.spyOn(console, "error").mockImplementation(() => {}); |
| 49 | + const { getByRole } = render( |
| 50 | + <> |
| 51 | + <SkipToMainContent mainId="main-content" /> |
| 52 | + <main tabIndex={-1} id="mian-content" /> |
| 53 | + </> |
| 54 | + ); |
| 55 | + |
| 56 | + fireEvent.click(getByRole("link")); |
| 57 | + expect(error).toBeCalledWith( |
| 58 | + 'Unable to find a main element to focus with an id of: "main-content".' |
| 59 | + ); |
| 60 | + expect(error).toBeCalledWith( |
| 61 | + `However, a "<main>" element was found with an id: "mian-content". Should this be the "mainId" prop for the "SkipToMainContent" component?` |
| 62 | + ); |
| 63 | + |
| 64 | + error.mockRestore(); |
| 65 | + }); |
| 66 | + }); |
| 67 | + |
| 68 | + describe("prod", () => { |
| 69 | + const env = process.env.NODE_ENV; |
| 70 | + beforeAll(() => { |
| 71 | + process.env.NODE_ENV = "production"; |
| 72 | + }); |
| 73 | + |
| 74 | + afterAll(() => { |
| 75 | + process.env.NODE_ENV = env; |
| 76 | + }); |
| 77 | + |
| 78 | + it("should not log an error if there is no main element with the provided mainId when clicked", () => { |
| 79 | + expect(process.env.NODE_ENV).toBe("production"); |
| 80 | + const error = jest.spyOn(console, "error").mockImplementation(() => {}); |
| 81 | + const { getByRole } = render(<SkipToMainContent mainId="main-content" />); |
| 82 | + |
| 83 | + fireEvent.click(getByRole("link")); |
| 84 | + expect(error).not.toBeCalled(); |
| 85 | + |
| 86 | + error.mockRestore(); |
| 87 | + }); |
| 88 | + |
| 89 | + it("should not log an error if there is no main element with the provided mainI when clicked and try to find a <main> id to suggest", () => { |
| 90 | + expect(process.env.NODE_ENV).toBe("production"); |
| 91 | + const error = jest.spyOn(console, "error").mockImplementation(() => {}); |
| 92 | + const { getByRole } = render( |
| 93 | + <> |
| 94 | + <SkipToMainContent mainId="main-content" /> |
| 95 | + <main tabIndex={-1} id="mian-content" /> |
| 96 | + </> |
| 97 | + ); |
| 98 | + |
| 99 | + fireEvent.click(getByRole("link")); |
| 100 | + expect(error).not.toBeCalled(); |
| 101 | + |
| 102 | + error.mockRestore(); |
| 103 | + }); |
| 104 | + }); |
| 105 | +}); |
0 commit comments