|
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 mockScrollToElement = vi.fn() |
|
7
|
const mockUseScrollController = vi.fn(() => ({ |
|
8
|
scrollableElement: null as HTMLElement | null, |
|
9
|
scrollToElement: mockScrollToElement, |
|
10
|
})) |
|
11
|
const mockUseSidebar = vi.fn(() => ({ |
|
12
|
activePath: null as string | null, |
|
13
|
})) |
|
14
|
|
|
15
|
// mock components |
|
16
|
vi.mock("docs-ui", () => ({ |
|
17
|
useScrollController: () => mockUseScrollController(), |
|
18
|
useSidebar: () => mockUseSidebar(), |
|
19
|
H2: ({ |
|
20
|
passRef, |
|
21
|
...props |
|
22
|
}: { |
|
23
|
passRef: React.RefObject<HTMLHeadingElement> |
|
24
|
} & React.HTMLAttributes<HTMLHeadingElement>) => ( |
|
25
|
<h2 {...props} ref={passRef} /> |
|
26
|
), |
|
27
|
})) |
|
28
|
vi.mock("docs-utils", () => ({ |
|
29
|
getSectionId: vi.fn(() => "section-id"), |
|
30
|
})) |
|
31
|
|
|
32
|
import H2 from ".." |
|
33
|
|
|
34
|
beforeEach(() => { |
|
35
|
vi.clearAllMocks() |
|
36
|
cleanup() |
|
37
|
}) |
|
38
|
|
|
39
|
describe("render", () => { |
|
40
|
test("renders children", () => { |
|
41
|
const { container } = render(<H2>Test</H2>) |
|
42
|
const h2 = container.querySelector("h2") |
|
43
|
expect(h2).toBeInTheDocument() |
|
44
|
expect(h2).toHaveTextContent("Test") |
|
45
|
}) |
|
46
|
|
|
47
|
test("renders with custom props", () => { |
|
48
|
const { container } = render(<H2 className="test-class">Test</H2>) |
|
49
|
const h2 = container.querySelector("h2") |
|
50
|
expect(h2).toBeInTheDocument() |
|
51
|
expect(h2).toHaveClass("test-class") |
|
52
|
}) |
|
53
|
}) |
|
54
|
|
|
55
|
describe("section id", () => { |
|
56
|
test("uses generated id", () => { |
|
57
|
const { container } = render(<H2>Test</H2>) |
|
58
|
const h2 = container.querySelector("h2") |
|
59
|
expect(h2).toBeInTheDocument() |
|
60
|
expect(h2).toHaveAttribute("id", "section-id") |
|
61
|
}) |
|
62
|
}) |
|
63
|
|
|
64
|
describe("scroll", () => { |
|
65
|
test("scrolls to element when active path matches id", () => { |
|
66
|
mockUseScrollController.mockReturnValueOnce({ |
|
67
|
scrollableElement: document.body, |
|
68
|
scrollToElement: mockScrollToElement, |
|
69
|
}) |
|
70
|
mockUseSidebar.mockReturnValueOnce({ |
|
71
|
activePath: "section-id", |
|
72
|
}) |
|
73
|
render(<H2>Test</H2>) |
|
74
|
const heading = document.querySelector("h2") |
|
75
|
expect(heading).toBeInTheDocument() |
|
76
|
expect(mockScrollToElement).toHaveBeenCalledWith(heading) |
|
77
|
}) |
|
78
|
|
|
79
|
test("does not scroll to element when active path does not match id", () => { |
|
80
|
mockUseScrollController.mockReturnValueOnce({ |
|
81
|
scrollableElement: document.body, |
|
82
|
scrollToElement: mockScrollToElement, |
|
83
|
}) |
|
84
|
mockUseSidebar.mockReturnValueOnce({ |
|
85
|
activePath: "other-section-id", |
|
86
|
}) |
|
87
|
render(<H2>Test</H2>) |
|
88
|
const heading = document.querySelector("h2") |
|
89
|
expect(heading).toBeInTheDocument() |
|
90
|
expect(mockScrollToElement).not.toHaveBeenCalled() |
|
91
|
}) |
|
92
|
}) |