|
1
|
import React from "react" |
|
2
|
import { beforeEach, describe, expect, test, vi } from "vitest" |
|
3
|
import { cleanup, render } from "@testing-library/react" |
|
4
|
|
|
5
|
// mock functions |
|
6
|
const mockSetActivePath = vi.fn() |
|
7
|
const mockUseActiveOnScroll = vi.fn((options: unknown) => ({ |
|
8
|
activeItemId: "", |
|
9
|
})) |
|
10
|
const mockUseSidebar = vi.fn(() => ({ |
|
11
|
setActivePath: mockSetActivePath, |
|
12
|
})) |
|
13
|
|
|
14
|
// mock components |
|
15
|
vi.mock("docs-ui", () => ({ |
|
16
|
useActiveOnScroll: (options: unknown) => mockUseActiveOnScroll(options), |
|
17
|
useSidebar: () => mockUseSidebar(), |
|
18
|
getLinkWithBasePath: (path: string) => path, |
|
19
|
})) |
|
20
|
vi.mock("@/providers/area", () => ({ |
|
21
|
useArea: () => ({ area: "store" }), |
|
22
|
})) |
|
23
|
|
|
24
|
import Section from ".." |
|
25
|
|
|
26
|
beforeEach(() => { |
|
27
|
vi.clearAllMocks() |
|
28
|
cleanup() |
|
29
|
|
|
30
|
window.history.scrollRestoration = "auto" |
|
31
|
}) |
|
32
|
|
|
33
|
describe("rendering", () => { |
|
34
|
test("passes checkActiveOnScroll prop to useActiveOnScroll", () => { |
|
35
|
render(<Section checkActiveOnScroll>Test</Section>) |
|
36
|
expect(mockUseActiveOnScroll).toHaveBeenCalledWith({ |
|
37
|
rootElm: undefined, |
|
38
|
enable: true, |
|
39
|
useDefaultIfNoActive: false, |
|
40
|
maxLevel: 2, |
|
41
|
}) |
|
42
|
}) |
|
43
|
}) |
|
44
|
|
|
45
|
describe("effect hooks", () => { |
|
46
|
test("sets active path to the section page path when active item id is not empty", () => { |
|
47
|
mockUseActiveOnScroll.mockReturnValue({ |
|
48
|
activeItemId: "test", |
|
49
|
}) |
|
50
|
const replaceStateSpy = vi.spyOn(window.history, "replaceState") |
|
51
|
render(<Section>Test</Section>) |
|
52
|
expect(mockSetActivePath).toHaveBeenCalledWith("/store/test") |
|
53
|
expect(replaceStateSpy).toHaveBeenCalledWith(null, "", "/store/test") |
|
54
|
replaceStateSpy.mockRestore() |
|
55
|
}) |
|
56
|
|
|
57
|
test("maps the introduction heading to the area index path", () => { |
|
58
|
mockUseActiveOnScroll.mockReturnValue({ |
|
59
|
activeItemId: "introduction", |
|
60
|
}) |
|
61
|
render(<Section>Test</Section>) |
|
62
|
expect(mockSetActivePath).toHaveBeenCalledWith("/store") |
|
63
|
}) |
|
64
|
|
|
65
|
test("does not set active path when active item id is empty", () => { |
|
66
|
mockUseActiveOnScroll.mockReturnValue({ |
|
67
|
activeItemId: "", |
|
68
|
}) |
|
69
|
const replaceStateSpy = vi.spyOn(window.history, "replaceState") |
|
70
|
render(<Section>Test</Section>) |
|
71
|
expect(mockSetActivePath).not.toHaveBeenCalled() |
|
72
|
expect(replaceStateSpy).not.toHaveBeenCalled() |
|
73
|
replaceStateSpy.mockRestore() |
|
74
|
}) |
|
75
|
|
|
76
|
test("disables scroll restoration when history is available", () => { |
|
77
|
render(<Section>Test</Section>) |
|
78
|
expect(history.scrollRestoration).toBe("manual") |
|
79
|
}) |
|
80
|
|
|
81
|
test("does not disable scroll restoration when history is not available", () => { |
|
82
|
delete (window.history as any).scrollRestoration |
|
83
|
render(<Section>Test</Section>) |
|
84
|
expect(history.scrollRestoration).not.toBe("manual") |
|
85
|
}) |
|
86
|
}) |