forked from webpack/webpack.js.org
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSidebarItem.test.jsx
More file actions
115 lines (101 loc) · 3.59 KB
/
SidebarItem.test.jsx
File metadata and controls
115 lines (101 loc) · 3.59 KB
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
/**
* @jest-environment jsdom
*/
// eslint-disable-next-line import/no-extraneous-dependencies
import { describe, expect, it } from "@jest/globals";
import { fireEvent, render, screen } from "@testing-library/react";
import { MemoryRouter } from "react-router-dom";
import SidebarItem from "./SidebarItem.jsx";
function renderWithRouter(ui, { route = "/" } = {}) {
return render(<MemoryRouter initialEntries={[route]}>{ui}</MemoryRouter>);
}
describe("SidebarItem", () => {
const defaultProps = {
title: "Getting Started",
url: "/guides/getting-started/",
currentPage: "/guides/",
anchors: [],
};
it("renders the title", () => {
renderWithRouter(<SidebarItem {...defaultProps} />);
expect(screen.getByText("Getting Started")).toBeTruthy();
});
it("renders collapsed by default when not matching currentPage", () => {
const { container } = renderWithRouter(<SidebarItem {...defaultProps} />);
const wrapper = container.firstChild;
expect(wrapper.getAttribute("data-open")).toBeNull();
});
it("renders expanded when url matches currentPage", () => {
const { container } = renderWithRouter(
<SidebarItem {...defaultProps} currentPage="/guides/getting-started" />,
);
const wrapper = container.firstChild;
expect(wrapper.getAttribute("data-open")).toBe("true");
});
it("matches URLs with regexp characters literally", () => {
const { container } = renderWithRouter(
<SidebarItem
{...defaultProps}
currentPage="/api/v3.0/[draft]"
url="/api/v3.0/[draft]/"
/>,
);
const wrapper = container.firstChild;
expect(wrapper.getAttribute("data-open")).toBe("true");
});
it("toggles open state when chevron button is clicked", () => {
const anchors = [
{
id: "introduction",
title: "Introduction",
title2: "Introduction",
level: 2,
},
{ id: "setup", title: "Setup", title2: "Setup", level: 2 },
];
const { container } = renderWithRouter(
<SidebarItem {...defaultProps} anchors={anchors} />,
);
const wrapper = container.firstChild;
expect(wrapper.getAttribute("data-open")).toBeNull();
const toggleButton = screen.getByRole("button", {
name: /toggle getting started section/i,
});
fireEvent.click(toggleButton);
expect(wrapper.getAttribute("data-open")).toBe("true");
fireEvent.click(toggleButton);
expect(wrapper.getAttribute("data-open")).toBeNull();
});
it("renders anchor links when anchors are provided", () => {
const anchors = [
{ id: "intro", title: "Introduction", title2: "Introduction", level: 2 },
{
id: "basic-setup",
title: "Basic Setup",
title2: "Basic Setup",
level: 2,
},
];
renderWithRouter(<SidebarItem {...defaultProps} anchors={anchors} />);
expect(screen.getByText("Introduction")).toBeTruthy();
expect(screen.getByText("Basic Setup")).toBeTruthy();
});
it("renders a bar icon when no anchors are provided", () => {
const { container } = renderWithRouter(
<SidebarItem {...defaultProps} anchors={[]} />,
);
// No toggle button should exist when there are no anchors
expect(screen.queryByRole("button")).toBeNull();
// The wrapper should still render
expect(container.firstChild).toBeTruthy();
});
it("matches snapshot", () => {
const anchors = [
{ id: "step-1", title: "Step 1", title2: "Step 1", level: 2 },
];
const { container } = renderWithRouter(
<SidebarItem {...defaultProps} anchors={anchors} />,
);
expect(container.firstChild).toMatchSnapshot();
});
});