|
1
|
import React from "react" |
|
2
|
import { beforeEach, describe, expect, test, vi } from "vitest" |
|
3
|
import { cleanup, render } from "@testing-library/react" |
|
4
|
import PageTitleProvider from "../page-title" |
|
5
|
import { Sidebar } from "types" |
|
6
|
|
|
7
|
// Mock functions |
|
8
|
const mockUseSidebar = vi.fn(() => ({ |
|
9
|
activePath: "test-path" as string | null, |
|
10
|
activeItem: { |
|
11
|
type: "link", |
|
12
|
path: "test-path", |
|
13
|
title: "Test Item", |
|
14
|
}, |
|
15
|
})) |
|
16
|
const mockUseArea = vi.fn(() => ({ |
|
17
|
displayedArea: "Store", |
|
18
|
})) |
|
19
|
|
|
20
|
vi.mock("docs-ui", () => ({ |
|
21
|
useSidebar: () => mockUseSidebar(), |
|
22
|
})) |
|
23
|
|
|
24
|
vi.mock("@/providers/area", () => ({ |
|
25
|
useArea: () => mockUseArea(), |
|
26
|
})) |
|
27
|
|
|
28
|
describe("PageTitleProvider", () => { |
|
29
|
beforeEach(() => { |
|
30
|
vi.clearAllMocks() |
|
31
|
cleanup() |
|
32
|
document.title = "" |
|
33
|
mockUseSidebar.mockReturnValue({ |
|
34
|
activePath: "test-path", |
|
35
|
activeItem: { |
|
36
|
type: "link", |
|
37
|
path: "test-path", |
|
38
|
title: "Test Item", |
|
39
|
}, |
|
40
|
}) |
|
41
|
mockUseArea.mockReturnValue({ |
|
42
|
displayedArea: "Store", |
|
43
|
}) |
|
44
|
}) |
|
45
|
|
|
46
|
describe("rendering", () => { |
|
47
|
test("renders children", () => { |
|
48
|
const { getByText } = render( |
|
49
|
<PageTitleProvider> |
|
50
|
<div>Test Content</div> |
|
51
|
</PageTitleProvider> |
|
52
|
) |
|
53
|
expect(getByText("Test Content")).toBeInTheDocument() |
|
54
|
}) |
|
55
|
}) |
|
56
|
|
|
57
|
describe("document title", () => { |
|
58
|
test("sets title to suffix when activePath is null", () => { |
|
59
|
mockUseSidebar.mockReturnValue({ |
|
60
|
activePath: null, |
|
61
|
activeItem: { |
|
62
|
type: "link", |
|
63
|
path: "test-path", |
|
64
|
title: "Test Item", |
|
65
|
}, |
|
66
|
}) |
|
67
|
render( |
|
68
|
<PageTitleProvider> |
|
69
|
<div>Test</div> |
|
70
|
</PageTitleProvider> |
|
71
|
) |
|
72
|
expect(document.title).toBe("Medusa Store API Reference") |
|
73
|
}) |
|
74
|
|
|
75
|
test("sets title to suffix when activePath is empty string", () => { |
|
76
|
mockUseSidebar.mockReturnValue({ |
|
77
|
activePath: "", |
|
78
|
activeItem: { |
|
79
|
type: "link", |
|
80
|
path: "test-path", |
|
81
|
title: "Test Item", |
|
82
|
}, |
|
83
|
}) |
|
84
|
render( |
|
85
|
<PageTitleProvider> |
|
86
|
<div>Test</div> |
|
87
|
</PageTitleProvider> |
|
88
|
) |
|
89
|
expect(document.title).toBe("Medusa Store API Reference") |
|
90
|
}) |
|
91
|
|
|
92
|
test("sets title with activeItem title when activeItem.path matches activePath", () => { |
|
93
|
const mockItem: Sidebar.SidebarItemLink = { |
|
94
|
type: "link", |
|
95
|
path: "/test-path", |
|
96
|
title: "Test Item", |
|
97
|
} |
|
98
|
mockUseSidebar.mockReturnValue({ |
|
99
|
activePath: "/test-path", |
|
100
|
activeItem: mockItem, |
|
101
|
}) |
|
102
|
|
|
103
|
render( |
|
104
|
<PageTitleProvider> |
|
105
|
<div>Test</div> |
|
106
|
</PageTitleProvider> |
|
107
|
) |
|
108
|
|
|
109
|
expect(document.title).toBe("Test Item - Medusa Store API Reference") |
|
110
|
}) |
|
111
|
|
|
112
|
test("sets title with child item title when activeItem has matching child", () => { |
|
113
|
const mockChildItem: Sidebar.SidebarItemLink = { |
|
114
|
type: "link", |
|
115
|
path: "/child-path", |
|
116
|
title: "Child Item", |
|
117
|
} |
|
118
|
const mockItem: Sidebar.SidebarItemLink = { |
|
119
|
type: "link", |
|
120
|
path: "/parent-path", |
|
121
|
title: "Parent Item", |
|
122
|
children: [mockChildItem], |
|
123
|
} |
|
124
|
mockUseSidebar.mockReturnValue({ |
|
125
|
activePath: "/child-path", |
|
126
|
activeItem: mockItem, |
|
127
|
}) |
|
128
|
|
|
129
|
render( |
|
130
|
<PageTitleProvider> |
|
131
|
<div>Test</div> |
|
132
|
</PageTitleProvider> |
|
133
|
) |
|
134
|
|
|
135
|
expect(document.title).toBe("Child Item - Medusa Store API Reference") |
|
136
|
}) |
|
137
|
|
|
138
|
test("sets title to suffix when activeItem has no matching child", () => { |
|
139
|
const mockItem: Sidebar.SidebarItemLink = { |
|
140
|
type: "link", |
|
141
|
path: "/parent-path", |
|
142
|
title: "Parent Item", |
|
143
|
children: [ |
|
144
|
{ |
|
145
|
type: "link", |
|
146
|
path: "/other-path", |
|
147
|
title: "Other Item", |
|
148
|
}, |
|
149
|
], |
|
150
|
} |
|
151
|
mockUseSidebar.mockReturnValue({ |
|
152
|
activePath: "/child-path", |
|
153
|
activeItem: mockItem, |
|
154
|
}) |
|
155
|
|
|
156
|
render( |
|
157
|
<PageTitleProvider> |
|
158
|
<div>Test</div> |
|
159
|
</PageTitleProvider> |
|
160
|
) |
|
161
|
|
|
162
|
expect(document.title).toBe("Medusa Store API Reference") |
|
163
|
}) |
|
164
|
|
|
165
|
test("updates title when displayedArea changes", () => { |
|
166
|
mockUseArea.mockReturnValue({ |
|
167
|
displayedArea: "Admin", |
|
168
|
}) |
|
169
|
render( |
|
170
|
<PageTitleProvider> |
|
171
|
<div>Test</div> |
|
172
|
</PageTitleProvider> |
|
173
|
) |
|
174
|
expect(document.title).toBe("Test Item - Medusa Admin API Reference") |
|
175
|
}) |
|
176
|
|
|
177
|
test("updates title when activePath changes", () => { |
|
178
|
const { rerender } = render( |
|
179
|
<PageTitleProvider> |
|
180
|
<div>Test</div> |
|
181
|
</PageTitleProvider> |
|
182
|
) |
|
183
|
|
|
184
|
expect(document.title).toBe("Test Item - Medusa Store API Reference") |
|
185
|
|
|
186
|
const mockItem: Sidebar.SidebarItemLink = { |
|
187
|
type: "link", |
|
188
|
path: "/new-path", |
|
189
|
title: "New Item", |
|
190
|
} |
|
191
|
mockUseSidebar.mockReturnValue({ |
|
192
|
activePath: "/new-path", |
|
193
|
activeItem: mockItem, |
|
194
|
}) |
|
195
|
|
|
196
|
rerender( |
|
197
|
<PageTitleProvider> |
|
198
|
<div>Test</div> |
|
199
|
</PageTitleProvider> |
|
200
|
) |
|
201
|
|
|
202
|
expect(document.title).toBe("New Item - Medusa Store API Reference") |
|
203
|
}) |
|
204
|
}) |
|
205
|
}) |
|
206
|
|