|
1
|
import React from "react" |
|
2
|
import { beforeEach, describe, expect, test, vi } from "vitest" |
|
3
|
import { render } from "@testing-library/react" |
|
4
|
|
|
5
|
// mock data |
|
6
|
const sidebarHistory = ["sidebar_1", "sidebar_2", "sidebar_3"] |
|
7
|
const sidebars = [ |
|
8
|
{ |
|
9
|
sidebar_id: "sidebar_1", |
|
10
|
title: "sidebar_1", |
|
11
|
items: [ |
|
12
|
{ |
|
13
|
type: "link", |
|
14
|
title: "sidebar_1_item_1", |
|
15
|
link: "/sidebar_1_item_1", |
|
16
|
path: "/sidebar_1_item_1", |
|
17
|
}, |
|
18
|
], |
|
19
|
}, |
|
20
|
{ |
|
21
|
sidebar_id: "sidebar_2", |
|
22
|
title: "sidebar_2", |
|
23
|
items: [ |
|
24
|
{ |
|
25
|
type: "link", |
|
26
|
title: "sidebar_2_item_1", |
|
27
|
link: "/sidebar_2_item_1", |
|
28
|
path: "/sidebar_2_item_1", |
|
29
|
}, |
|
30
|
], |
|
31
|
}, |
|
32
|
{ |
|
33
|
sidebar_id: "sidebar_duplicate", |
|
34
|
title: "sidebar_duplicate", |
|
35
|
items: [ |
|
36
|
{ |
|
37
|
type: "link", |
|
38
|
title: "sidebar_1_item_1", |
|
39
|
link: "/sidebar_1_item_1", |
|
40
|
path: "/sidebar_1_item_1", |
|
41
|
}, |
|
42
|
], |
|
43
|
}, |
|
44
|
] |
|
45
|
const baseUrl = "https://example.com" |
|
46
|
const basePath = "/docs" |
|
47
|
|
|
48
|
// mock functions |
|
49
|
const getSidebar = (sidebar_id: string) => { |
|
50
|
return sidebars.find((sidebar) => sidebar.sidebar_id === sidebar_id) |
|
51
|
} |
|
52
|
const getSidebarFirstLinkChild = (sidebar: Sidebar.Sidebar) => { |
|
53
|
return sidebar.items[0] as Sidebar.SidebarItemLink | undefined |
|
54
|
} |
|
55
|
const mockUseSidebar = vi.fn(() => ({ |
|
56
|
sidebarHistory, |
|
57
|
getSidebarFirstLinkChild, |
|
58
|
getSidebar, |
|
59
|
})) |
|
60
|
|
|
61
|
// mock components |
|
62
|
vi.mock("next/link", () => ({ |
|
63
|
default: ({ children, ...props }: React.ComponentProps<"a">) => ( |
|
64
|
<a {...props}>{children}</a> |
|
65
|
), |
|
66
|
})) |
|
67
|
|
|
68
|
vi.mock("@/providers/Sidebar", () => ({ |
|
69
|
useSidebar: () => mockUseSidebar(), |
|
70
|
})) |
|
71
|
|
|
72
|
vi.mock("@/providers/SiteConfig", () => ({ |
|
73
|
useSiteConfig: () => ({ |
|
74
|
config: { |
|
75
|
breadcrumbOptions: { |
|
76
|
startItems: [{ title: "start", link: "/start" }], |
|
77
|
}, |
|
78
|
baseUrl, |
|
79
|
basePath, |
|
80
|
}, |
|
81
|
}), |
|
82
|
})) |
|
83
|
|
|
84
|
import { Breadcrumbs } from ".." |
|
85
|
import { Sidebar } from "types" |
|
86
|
|
|
87
|
beforeEach(() => { |
|
88
|
mockUseSidebar.mockReturnValue({ |
|
89
|
sidebarHistory, |
|
90
|
getSidebarFirstLinkChild, |
|
91
|
getSidebar, |
|
92
|
}) |
|
93
|
}) |
|
94
|
|
|
95
|
describe("rendering", () => { |
|
96
|
test("renders config start item only when no sidebar history is provided", () => { |
|
97
|
mockUseSidebar.mockReturnValue({ |
|
98
|
sidebarHistory: [], |
|
99
|
getSidebarFirstLinkChild: () => undefined, |
|
100
|
getSidebar, |
|
101
|
}) |
|
102
|
const { container } = render(<Breadcrumbs />) |
|
103
|
expect(container).toBeInTheDocument() |
|
104
|
const startItem = container.querySelector("a[href='/start']") |
|
105
|
expect(startItem).toBeInTheDocument() |
|
106
|
}) |
|
107
|
|
|
108
|
test("renders breadcrumbs for sidebar with one history item", () => { |
|
109
|
mockUseSidebar.mockReturnValue({ |
|
110
|
sidebarHistory: ["sidebar_1"], |
|
111
|
getSidebarFirstLinkChild, |
|
112
|
getSidebar, |
|
113
|
}) |
|
114
|
const { container } = render(<Breadcrumbs />) |
|
115
|
expect(container).toBeInTheDocument() |
|
116
|
const breadcrumbs = container.querySelectorAll("a") |
|
117
|
expect(breadcrumbs).toHaveLength(2) |
|
118
|
expect(breadcrumbs[0]).toHaveTextContent("start") |
|
119
|
expect(breadcrumbs[1]).toHaveTextContent("sidebar_1") |
|
120
|
expect(breadcrumbs[1]).toHaveAttribute("href", "/sidebar_1_item_1") |
|
121
|
}) |
|
122
|
|
|
123
|
test("renders breadcrumbs for sidebar with two history items", () => { |
|
124
|
mockUseSidebar.mockReturnValue({ |
|
125
|
sidebarHistory: ["sidebar_1", "sidebar_2"], |
|
126
|
getSidebarFirstLinkChild, |
|
127
|
getSidebar, |
|
128
|
}) |
|
129
|
const { container } = render(<Breadcrumbs />) |
|
130
|
expect(container).toBeInTheDocument() |
|
131
|
const breadcrumbs = container.querySelectorAll("a") |
|
132
|
expect(breadcrumbs).toHaveLength(3) |
|
133
|
expect(breadcrumbs[0]).toHaveTextContent("start") |
|
134
|
expect(breadcrumbs[1]).toHaveTextContent("sidebar_1") |
|
135
|
expect(breadcrumbs[1]).toHaveAttribute("href", "/sidebar_1_item_1") |
|
136
|
expect(breadcrumbs[2]).toHaveTextContent("sidebar_2") |
|
137
|
expect(breadcrumbs[2]).toHaveAttribute("href", "/sidebar_2_item_1") |
|
138
|
}) |
|
139
|
|
|
140
|
test("deduplicates breadcrumb items with the same link", () => { |
|
141
|
mockUseSidebar.mockReturnValue({ |
|
142
|
sidebarHistory: ["sidebar_1", "sidebar_duplicate"], |
|
143
|
getSidebarFirstLinkChild, |
|
144
|
getSidebar, |
|
145
|
}) |
|
146
|
const { container } = render(<Breadcrumbs />) |
|
147
|
expect(container).toBeInTheDocument() |
|
148
|
const breadcrumbs = container.querySelectorAll("a") |
|
149
|
// start + sidebar_1 only (sidebar_duplicate has same link, deduplicated) |
|
150
|
expect(breadcrumbs).toHaveLength(2) |
|
151
|
expect(breadcrumbs[0]).toHaveTextContent("start") |
|
152
|
expect(breadcrumbs[1]).toHaveTextContent("sidebar_1") |
|
153
|
expect(breadcrumbs[1]).toHaveAttribute("href", "/sidebar_1_item_1") |
|
154
|
}) |
|
155
|
|
|
156
|
test("renders json-ld breadcrumb list", () => { |
|
157
|
mockUseSidebar.mockReturnValue({ |
|
158
|
sidebarHistory: ["sidebar_1", "sidebar_2"], |
|
159
|
getSidebarFirstLinkChild, |
|
160
|
getSidebar, |
|
161
|
}) |
|
162
|
const { container } = render(<Breadcrumbs />) |
|
163
|
expect(container).toBeInTheDocument() |
|
164
|
const jsonLd = container.querySelector("script[type='application/ld+json']") |
|
165
|
expect(jsonLd).toBeInTheDocument() |
|
166
|
expect(jsonLd).toHaveAttribute("type", "application/ld+json") |
|
167
|
const content = jsonLd?.textContent |
|
168
|
expect(content).toBeDefined() |
|
169
|
const parsedContent = JSON.parse(content!) |
|
170
|
const baseLink = `${baseUrl}${basePath}`.replace(/\/+$/, "") |
|
171
|
expect(parsedContent).toEqual({ |
|
172
|
"@context": "https://schema.org", |
|
173
|
"@type": "BreadcrumbList", |
|
174
|
itemListElement: [ |
|
175
|
{ |
|
176
|
"@type": "ListItem", |
|
177
|
position: 1, |
|
178
|
name: "start", |
|
179
|
item: `${baseLink}/start`, |
|
180
|
}, |
|
181
|
{ |
|
182
|
"@type": "ListItem", |
|
183
|
position: 2, |
|
184
|
name: "sidebar_1", |
|
185
|
item: `${baseLink}/sidebar_1_item_1`, |
|
186
|
}, |
|
187
|
{ |
|
188
|
"@type": "ListItem", |
|
189
|
position: 3, |
|
190
|
name: "sidebar_2", |
|
191
|
item: `${baseLink}/sidebar_2_item_1`, |
|
192
|
}, |
|
193
|
], |
|
194
|
}) |
|
195
|
}) |
|
196
|
}) |