Skip to content

Commit 3f6e866

Browse files
committed
test(link): added new tests for SkipToMainContent
1 parent ba72630 commit 3f6e866

File tree

3 files changed

+131
-0
lines changed

3 files changed

+131
-0
lines changed

packages/link/src/SkipToMainContent.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ export const SkipToMainContent = forwardRef<
109109
);
110110
});
111111

112+
/* istanbul ignore next */
112113
if (process.env.NODE_ENV !== "production") {
113114
try {
114115
const PropTypes = require("prop-types");
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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+
});
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`SkipToMainContent should render correctly 1`] = `
4+
<div>
5+
<a
6+
class="rmd-link rmd-link-skip rmd-link-skip--styled"
7+
href="#"
8+
id="skip-to-main-content"
9+
>
10+
Skip to main content
11+
</a>
12+
</div>
13+
`;
14+
15+
exports[`SkipToMainContent should render correctly 2`] = `
16+
<div>
17+
<a
18+
class="rmd-link rmd-link-skip"
19+
href="#main-content"
20+
id="skip"
21+
>
22+
Skip
23+
</a>
24+
</div>
25+
`;

0 commit comments

Comments
 (0)