-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBackToTop.test.jsx
More file actions
102 lines (84 loc) · 2.85 KB
/
BackToTop.test.jsx
File metadata and controls
102 lines (84 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import { render, screen, fireEvent } from "@testing-library/react";
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
import { BackToTop } from "./BackToTop";
describe("BackToTop", () => {
let scrollToMock;
beforeEach(() => {
scrollToMock = vi.fn();
window.scrollTo = scrollToMock;
Object.defineProperty(document.documentElement, "scrollTop", {
value: 0,
writable: true,
configurable: true,
});
});
afterEach(() => {
vi.restoreAllMocks();
});
it("renders the back to top button", () => {
render(<BackToTop />);
const button = screen.getByRole("button", { name: /voltar ao topo/i });
expect(button).toBeInTheDocument();
});
it("is hidden when scroll position is below minHeight", () => {
render(<BackToTop minHeight={300} />);
const button = screen.getByRole("button", { name: /voltar ao topo/i });
expect(button).toHaveClass("opacity-0");
expect(button).toHaveClass("pointer-events-none");
});
it("becomes visible when scroll position exceeds minHeight", () => {
Object.defineProperty(document.documentElement, "scrollTop", {
value: 400,
writable: true,
configurable: true,
});
render(<BackToTop minHeight={300} />);
fireEvent.scroll(document);
const button = screen.getByRole("button", { name: /voltar ao topo/i });
expect(button).toHaveClass("opacity-100");
});
it("scrolls to top when clicked", () => {
Object.defineProperty(document.documentElement, "scrollTop", {
value: 500,
writable: true,
configurable: true,
});
render(<BackToTop />);
fireEvent.scroll(document);
const button = screen.getByRole("button", { name: /voltar ao topo/i });
fireEvent.click(button);
expect(scrollToMock).toHaveBeenCalledWith({
top: 0,
behavior: "smooth",
});
});
it("scrolls to custom position when scrollTo prop is set", () => {
Object.defineProperty(document.documentElement, "scrollTop", {
value: 500,
writable: true,
configurable: true,
});
render(<BackToTop scrollTo={100} />);
fireEvent.scroll(document);
const button = screen.getByRole("button", { name: /voltar ao topo/i });
fireEvent.click(button);
expect(scrollToMock).toHaveBeenCalledWith({
top: 100,
behavior: "smooth",
});
});
it("applies custom className", () => {
render(<BackToTop className="custom-class" />);
const button = screen.getByRole("button", { name: /voltar ao topo/i });
expect(button).toHaveClass("custom-class");
});
it("cleans up scroll event listener on unmount", () => {
const removeEventListenerSpy = vi.spyOn(document, "removeEventListener");
const { unmount } = render(<BackToTop />);
unmount();
expect(removeEventListenerSpy).toHaveBeenCalledWith(
"scroll",
expect.any(Function)
);
});
});