-
Notifications
You must be signed in to change notification settings - Fork 36
/
Section.test.jsx
194 lines (163 loc) · 6.77 KB
/
Section.test.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
* Copyright (c) [2022-2023] SUSE LLC
*
* All Rights Reserved.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of version 2 of the GNU General Public License as published
* by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, contact SUSE LLC.
*
* To contact SUSE LLC about this file by physical or electronic mail, you may
* find current contact information at www.suse.com.
*/
import React from "react";
import { screen, within } from "@testing-library/react";
import { plainRender, installerRender } from "~/test-utils";
import { Section } from "~/components/core";
describe("Section", () => {
beforeAll(() => {
jest.spyOn(console, "error").mockImplementation();
});
afterAll(() => {
console.error.mockRestore();
});
describe("when title is given", () => {
it("renders the section header", () => {
plainRender(<Section title="Settings" />);
screen.getByRole("banner");
});
it("renders given title as section heading", () => {
plainRender(<Section title="Settings" />);
const header = screen.getByRole("banner");
within(header).getByRole("heading", { name: "Settings" });
});
it("renders an icon if valid icon name is given", () => {
const { container } = plainRender(<Section title="Settings" icon="settings" />);
const icon = container.querySelector("svg");
expect(icon).toHaveAttribute("data-icon-name", "settings");
});
it("does not render an icon if icon name not given", () => {
const { container } = plainRender(<Section title="Settings" />);
const icon = container.querySelector("svg");
expect(icon).toBeNull();
// Check that <Icon /> component was not mounted with 'undefined'
expect(console.error).not.toHaveBeenCalled();
});
it("does not render an icon if not valid icon name is given", () => {
const { container } = plainRender(<Section title="Settings" icon="not-valid-icon-name" />);
const icon = container.querySelector("svg");
expect(icon).toBeNull();
});
it("renders given description as part of the header", () => {
plainRender(
<Section title="Settings" description="Short explanation to help the user, if needed" />
);
const header = screen.getByRole("banner");
within(header).getByText(/Short explanation/);
});
});
describe("when title is not given", () => {
it("does not render the section header", async () => {
plainRender(<Section description="Does not matter" />);
const header = await screen.queryByRole("banner");
expect(header).not.toBeInTheDocument();
});
it("does not render a section heading", async () => {
plainRender(<Section description="Does not matter" />);
const heading = await screen.queryByRole("heading");
expect(heading).not.toBeInTheDocument();
});
it("does not render the section icon", () => {
const { container } = plainRender(<Section icon="settings" />);
const icon = container.querySelector("svg");
expect(icon).toBeNull();
});
});
describe("when aria-label is given", () => {
it("sets aria-label attribute", () => {
plainRender(<Section title="Settings" aria-label="User settings" />);
const section = screen.getByRole("region", { name: "User settings" });
expect(section).toHaveAttribute("aria-label", "User settings");
});
it("does not set aria-labelledby", () => {
plainRender(<Section title="Settings" aria-label="User settings" />);
const section = screen.getByRole("region", { name: "User settings" });
expect(section).not.toHaveAttribute("aria-labelledby");
});
});
describe("when aria-label is not given", () => {
it("sets aria-labelledby if title is provided", () => {
plainRender(<Section title="Settings" />);
const section = screen.getByRole("region", { name: "Settings" });
expect(section).toHaveAttribute("aria-labelledby");
});
it("does not set aria-label", () => {
plainRender(<Section title="Settings" />);
const section = screen.getByRole("region", { name: "Settings" });
expect(section).not.toHaveAttribute("aria-label");
});
});
it("sets predictable header id if name is given", () => {
plainRender(<Section title="Settings" name="settings" />);
screen.getByRole("heading", { name: "Settings", id: "settings-header-section" });
});
it("sets partially random header id if name is not given", () => {
plainRender(<Section title="Settings" name="settings" />);
screen.getByRole("heading", { name: "Settings", id: /.*(-header-section)$/ });
});
it("renders a polite live region", () => {
plainRender(<Section title="Settings" />);
const section = screen.getByRole("region", { name: "Settings" });
expect(section).toHaveAttribute("aria-live", "polite");
});
it("renders given errors", () => {
plainRender(
<Section id="settings" title="Awesome settings" errors={[{ message: "Something went wrong" }]} />
);
screen.getByText("Something went wrong");
});
it("renders given content", () => {
plainRender(
<Section title="Settings">
A settings summary
</Section>
);
screen.getByText("A settings summary");
});
it("does not set aria-busy", () => {
plainRender(<Section title="Settings" />);
screen.getByRole("region", { name: "Settings", busy: false });
});
describe("when set as loading", () => {
it("sets aria-busy", () => {
plainRender(<Section title="Settings" loading />);
screen.getByRole("region", { busy: true });
});
it("renders the loading icon if title was given", () => {
const { container } = plainRender(<Section title="Settings" loading />);
const icon = container.querySelector("svg");
expect(icon).toHaveAttribute("data-icon-name", "loading");
});
it("does not render the loading icon if title was not given", () => {
const { container } = plainRender(<Section loading />);
const icon = container.querySelector("svg");
expect(icon).toBeNull();
});
});
describe("when path is given", () => {
it("renders a link for navigating to it", async () => {
installerRender(<Section title="Settings" path="/settings" />);
const heading = screen.getByRole("heading", { name: "Settings" });
const link = within(heading).getByRole("link", { name: "Settings" });
expect(link).toHaveAttribute("href", "/settings");
});
});
});