1 import React from "react"
2 import { afterEach, beforeEach, describe, expect, test, vi } from "vitest"
3 import { cleanup, fireEvent, render } from "@testing-library/react"
4 import { SiteConfigProvider, useSiteConfig } from "../index"
5 import type { DocsConfig } from "types"
6
7 // mock global config
8 vi.mock("@/global-config", () => ({
9 globalConfig: {
10 baseUrl: "https://global.example.com",
11 basePath: "/global",
12 },
13 }))
14
15 vi.mock("../..", () => ({
16 GITHUB_ISSUES_LINK: "https://github.com/test/issues",
17 }))
18
19 // mock BrowserProvider
20 vi.mock("@/providers/BrowserProvider", () => ({
21 useIsBrowser: () => ({
22 isBrowser: true,
23 }),
24 }))
25
26 const TestComponent = () => {
27 const {
28 config,
29 setConfig,
30 frontmatter,
31 setFrontmatter,
32 toc,
33 setToc,
34 isInProduct,
35 productView,
36 } = useSiteConfig()
37 return (
38 <div>
39 <div data-testid="base-url">{config.baseUrl}</div>
40 <div data-testid="project-title">{config.project.title}</div>
41 <div data-testid="frontmatter-title">{frontmatter.title || "none"}</div>
42 <div data-testid="version-number">{config.version?.number}</div>
43 <div data-testid="release-url">{config.version?.releaseUrl}</div>
44 <div data-testid="toc-length">{toc?.length || 0}</div>
45 <div data-testid="ai-assistant-feature">
46 {String(config.features?.aiAssistant ?? false)}
47 </div>
48 <div data-testid="is-in-product">{String(isInProduct)}</div>
49 <div data-testid="product-view">{productView || "none"}</div>
50 <button
51 data-testid="update-config"
52 onClick={() =>
53 setConfig((prev) => ({
54 ...prev,
55 baseUrl: "https://updated.example.com",
56 }))
57 }
58 >
59 Update Config
60 </button>
61 <button
62 data-testid="update-frontmatter"
63 onClick={() => setFrontmatter({ title: "Updated Title" })}
64 >
65 Update Frontmatter
66 </button>
67 <button
68 data-testid="update-toc"
69 onClick={() =>
70 setToc([
71 {
72 title: "Item 1",
73 id: "item1",
74 level: 1,
75 },
76 ])
77 }
78 >
79 Update ToC
80 </button>
81 </div>
82 )
83 }
84
85 describe("SiteConfigProvider", () => {
86 beforeEach(() => {
87 vi.clearAllMocks()
88 })
89
90 afterEach(() => {
91 cleanup()
92 })
93
94 describe("rendering", () => {
95 test("renders children", () => {
96 const { container } = render(
97 <SiteConfigProvider>
98 <div>Test</div>
99 </SiteConfigProvider>
100 )
101 expect(container).toHaveTextContent("Test")
102 })
103 })
104
105 describe("useSiteConfig hook", () => {
106 test("initializes with default config", () => {
107 const { getByTestId } = render(
108 <SiteConfigProvider>
109 <TestComponent />
110 </SiteConfigProvider>
111 )
112
113 expect(getByTestId("base-url")).toHaveTextContent(
114 "https://global.example.com"
115 )
116 expect(getByTestId("project-title")).toHaveTextContent("")
117 })
118
119 test("merges provided config with defaults", () => {
120 const config: DocsConfig = {
121 baseUrl: "https://custom.example.com",
122 basePath: "/custom",
123 project: {
124 title: "Custom Project",
125 key: "custom",
126 },
127 sidebars: [],
128 logo: "",
129 version: {
130 number: "1.0.0",
131 releaseUrl: "https://github.com/test/releases/tag/1.0.0",
132 releaseDate: "2024-01-01",
133 },
134 }
135
136 const { getByTestId } = render(
137 <SiteConfigProvider config={config}>
138 <TestComponent />
139 </SiteConfigProvider>
140 )
141
142 expect(getByTestId("base-url")).toHaveTextContent(
143 "https://custom.example.com"
144 )
145 expect(getByTestId("project-title")).toHaveTextContent("Custom Project")
146 expect(getByTestId("version-number")).toHaveTextContent("1.0.0")
147 expect(getByTestId("release-url")).toHaveTextContent(
148 "https://github.com/test/releases/tag/1.0.0"
149 )
150 })
151
152 test("initializes with empty frontmatter", () => {
153 const { getByTestId } = render(
154 <SiteConfigProvider>
155 <TestComponent />
156 </SiteConfigProvider>
157 )
158
159 expect(getByTestId("frontmatter-title")).toHaveTextContent("none")
160 })
161
162 test("initializes with null toc", () => {
163 const { getByTestId } = render(
164 <SiteConfigProvider>
165 <TestComponent />
166 </SiteConfigProvider>
167 )
168
169 expect(getByTestId("toc-length")).toHaveTextContent("0")
170 })
171
172 test("setConfig updates config", () => {
173 const { getByTestId } = render(
174 <SiteConfigProvider>
175 <TestComponent />
176 </SiteConfigProvider>
177 )
178
179 fireEvent.click(getByTestId("update-config"))
180
181 expect(getByTestId("base-url")).toHaveTextContent(
182 "https://updated.example.com"
183 )
184 })
185
186 test("setFrontmatter updates frontmatter", () => {
187 const { getByTestId } = render(
188 <SiteConfigProvider>
189 <TestComponent />
190 </SiteConfigProvider>
191 )
192
193 fireEvent.click(getByTestId("update-frontmatter"))
194
195 expect(getByTestId("frontmatter-title")).toHaveTextContent(
196 "Updated Title"
197 )
198 })
199
200 test("setToc updates toc", () => {
201 const { getByTestId } = render(
202 <SiteConfigProvider>
203 <TestComponent />
204 </SiteConfigProvider>
205 )
206
207 fireEvent.click(getByTestId("update-toc"))
208
209 expect(getByTestId("toc-length")).toHaveTextContent("1")
210 })
211
212 test("enables ai assistant feature by default", () => {
213 const { getByTestId } = render(
214 <SiteConfigProvider>
215 <TestComponent />
216 </SiteConfigProvider>
217 )
218
219 expect(getByTestId("ai-assistant-feature")).toHaveTextContent("true")
220 })
221
222 test("allows overriding ai assistant feature via config", () => {
223 const config: DocsConfig = {
224 baseUrl: "",
225 project: {
226 title: "",
227 key: "",
228 },
229 sidebars: [],
230 logo: "",
231 features: {
232 aiAssistant: false,
233 },
234 version: {
235 number: "1.0.0",
236 releaseUrl: "",
237 releaseDate: "",
238 }
239 }
240
241 const { getByTestId } = render(
242 <SiteConfigProvider config={config}>
243 <TestComponent />
244 </SiteConfigProvider>
245 )
246
247 expect(getByTestId("ai-assistant-feature")).toHaveTextContent("false")
248 })
249
250 test("throws error when used outside provider", () => {
251 const consoleSpy = vi.spyOn(console, "error").mockImplementation(() => {})
252
253 expect(() => {
254 render(<TestComponent />)
255 }).toThrow("useSiteConfig must be used inside a SiteConfigProvider")
256
257 consoleSpy.mockRestore()
258 })
259 })
260
261 describe("product view properties", () => {
262 test("initializes with isInProduct false when no product view", () => {
263 const { getByTestId } = render(
264 <SiteConfigProvider>
265 <TestComponent />
266 </SiteConfigProvider>
267 )
268
269 expect(getByTestId("is-in-product")).toHaveTextContent("false")
270 expect(getByTestId("product-view")).toHaveTextContent("none")
271 })
272
273 test("detects bloom product view from URL search params", () => {
274 // Mock location.search
275 delete (global as any).location
276 ;(global as any).location = {
277 search: "?view=bloom",
278 }
279
280 const { getByTestId } = render(
281 <SiteConfigProvider>
282 <TestComponent />
283 </SiteConfigProvider>
284 )
285
286 expect(getByTestId("is-in-product")).toHaveTextContent("true")
287 expect(getByTestId("product-view")).toHaveTextContent("bloom")
288 })
289
290 test("ignores invalid product view from URL", () => {
291 // Mock location.search with invalid view
292 delete (global as any).location
293 ;(global as any).location = {
294 search: "?view=invalid",
295 }
296
297 const { getByTestId } = render(
298 <SiteConfigProvider>
299 <TestComponent />
300 </SiteConfigProvider>
301 )
302
303 expect(getByTestId("is-in-product")).toHaveTextContent("false")
304 expect(getByTestId("product-view")).toHaveTextContent("none")
305 })
306
307 test("handles missing view parameter", () => {
308 // Mock location.search without view parameter
309 delete (global as any).location
310 ;(global as any).location = {
311 search: "?other=param",
312 }
313
314 const { getByTestId } = render(
315 <SiteConfigProvider>
316 <TestComponent />
317 </SiteConfigProvider>
318 )
319
320 expect(getByTestId("is-in-product")).toHaveTextContent("false")
321 expect(getByTestId("product-view")).toHaveTextContent("none")
322 })
323
324 test("handles empty search params", () => {
325 // Mock location.search as empty
326 delete (global as any).location
327 ;(global as any).location = {
328 search: "",
329 }
330
331 const { getByTestId } = render(
332 <SiteConfigProvider>
333 <TestComponent />
334 </SiteConfigProvider>
335 )
336
337 expect(getByTestId("is-in-product")).toHaveTextContent("false")
338 expect(getByTestId("product-view")).toHaveTextContent("none")
339 })
340 })
341 })