|
1
|
import React from "react" |
|
2
|
import { beforeEach, describe, expect, test, vi } from "vitest" |
|
3
|
import { fireEvent, render, waitFor } from "@testing-library/react" |
|
4
|
import { TooltipProps } from "../../Tooltip" |
|
5
|
|
|
6
|
// mock functions |
|
7
|
const mockHandleCopy = vi.fn() |
|
8
|
const defaultUseCopyReturn = { |
|
9
|
isCopied: false, |
|
10
|
handleCopy: mockHandleCopy, |
|
11
|
} |
|
12
|
const mockUseCopy = vi.fn(() => defaultUseCopyReturn) |
|
13
|
const mockOnCopy = vi.fn() |
|
14
|
// mock components |
|
15
|
vi.mock("@/components/Tooltip", () => ({ |
|
16
|
Tooltip: ({ |
|
17
|
children, |
|
18
|
text, |
|
19
|
tooltipClassName, |
|
20
|
innerClassName, |
|
21
|
}: TooltipProps) => ( |
|
22
|
<div |
|
23
|
data-testid="tooltip" |
|
24
|
data-text={text} |
|
25
|
data-tooltip-class-name={tooltipClassName} |
|
26
|
data-inner-class-name={innerClassName} |
|
27
|
> |
|
28
|
{children} |
|
29
|
</div> |
|
30
|
), |
|
31
|
})) |
|
32
|
vi.mock("@/hooks/use-copy", () => ({ |
|
33
|
useCopy: () => mockUseCopy(), |
|
34
|
})) |
|
35
|
|
|
36
|
import { CopyButton } from "../index" |
|
37
|
|
|
38
|
beforeEach(() => { |
|
39
|
vi.clearAllMocks() |
|
40
|
}) |
|
41
|
|
|
42
|
describe("render", () => { |
|
43
|
test("renders copy button", () => { |
|
44
|
const { container } = render(<CopyButton text="Copy to Clipboard" />) |
|
45
|
const copyButton = container.querySelector("[data-testid='copy-button']") |
|
46
|
expect(copyButton).toBeInTheDocument() |
|
47
|
const tooltip = container.querySelector("[data-testid='tooltip']") |
|
48
|
expect(tooltip).toBeInTheDocument() |
|
49
|
expect(tooltip).toHaveAttribute("data-text", "Copy to Clipboard") |
|
50
|
}) |
|
51
|
|
|
52
|
test("renders copy button with tooltip text", () => { |
|
53
|
const { container } = render( |
|
54
|
<CopyButton text="Copy to Clipboard" tooltipText="Custom Copy" /> |
|
55
|
) |
|
56
|
const copyButton = container.querySelector("[data-testid='copy-button']") |
|
57
|
expect(copyButton).toBeInTheDocument() |
|
58
|
const tooltip = container.querySelector("[data-testid='tooltip']") |
|
59
|
expect(tooltip).toBeInTheDocument() |
|
60
|
expect(tooltip).toHaveAttribute("data-text", "Custom Copy") |
|
61
|
}) |
|
62
|
|
|
63
|
test("renders copy button with tooltip class name", () => { |
|
64
|
const { container } = render( |
|
65
|
<CopyButton text="Copy to Clipboard" tooltipClassName="custom-tooltip" /> |
|
66
|
) |
|
67
|
const copyButton = container.querySelector("[data-testid='copy-button']") |
|
68
|
expect(copyButton).toBeInTheDocument() |
|
69
|
const tooltip = container.querySelector("[data-testid='tooltip']") |
|
70
|
expect(tooltip).toBeInTheDocument() |
|
71
|
expect(tooltip).toHaveAttribute("data-tooltip-class-name", "custom-tooltip") |
|
72
|
}) |
|
73
|
|
|
74
|
test("renders copy button with tooltip inner class name", () => { |
|
75
|
const { container } = render( |
|
76
|
<CopyButton |
|
77
|
text="Copy to Clipboard" |
|
78
|
tooltipInnerClassName="custom-tooltip-inner" |
|
79
|
/> |
|
80
|
) |
|
81
|
const copyButton = container.querySelector("[data-testid='copy-button']") |
|
82
|
expect(copyButton).toBeInTheDocument() |
|
83
|
const tooltip = container.querySelector("[data-testid='tooltip']") |
|
84
|
expect(tooltip).toBeInTheDocument() |
|
85
|
expect(tooltip).toHaveAttribute( |
|
86
|
"data-inner-class-name", |
|
87
|
"custom-tooltip-inner" |
|
88
|
) |
|
89
|
}) |
|
90
|
|
|
91
|
test("renders copy button with button class name", () => { |
|
92
|
const { container } = render( |
|
93
|
<CopyButton text="Copy to Clipboard" buttonClassName="custom-button" /> |
|
94
|
) |
|
95
|
const copyButton = container.querySelector("[data-testid='copy-button']") |
|
96
|
expect(copyButton).toBeInTheDocument() |
|
97
|
expect(copyButton).toHaveClass("custom-button") |
|
98
|
}) |
|
99
|
|
|
100
|
test("renders copy button with handleTouch", () => { |
|
101
|
const { container } = render( |
|
102
|
<CopyButton text="Copy to Clipboard" handleTouch /> |
|
103
|
) |
|
104
|
const copyButton = container.querySelector("[data-testid='copy-button']") |
|
105
|
expect(copyButton).toBeInTheDocument() |
|
106
|
const tooltip = container.querySelector("[data-testid='tooltip']") |
|
107
|
expect(tooltip).toBeInTheDocument() |
|
108
|
expect(tooltip).toHaveAttribute("data-tooltip-class-name", "!block") |
|
109
|
}) |
|
110
|
|
|
111
|
test("tooltip text changes to 'Copied!' when isCopied is true", () => { |
|
112
|
mockUseCopy.mockReturnValue({ |
|
113
|
...defaultUseCopyReturn, |
|
114
|
isCopied: true, |
|
115
|
}) |
|
116
|
const { container } = render(<CopyButton text="Copy to Clipboard" />) |
|
117
|
const tooltip = container.querySelector("[data-testid='tooltip']") |
|
118
|
expect(tooltip).toBeInTheDocument() |
|
119
|
expect(tooltip).toHaveAttribute("data-text", "Copied!") |
|
120
|
}) |
|
121
|
}) |
|
122
|
|
|
123
|
describe("interactions", () => { |
|
124
|
test("calls handleCopy when copy button is clicked", () => { |
|
125
|
const { container } = render(<CopyButton text="Copy to Clipboard" />) |
|
126
|
const copyButton = container.querySelector("[data-testid='copy-button']") |
|
127
|
expect(copyButton).toBeInTheDocument() |
|
128
|
fireEvent.click(copyButton!) |
|
129
|
expect(mockHandleCopy).toHaveBeenCalled() |
|
130
|
}) |
|
131
|
|
|
132
|
test("calls handleCopy when copy button is clicked with handleTouch", () => { |
|
133
|
const { container } = render( |
|
134
|
<CopyButton text="Copy to Clipboard" handleTouch /> |
|
135
|
) |
|
136
|
const copyButton = container.querySelector("[data-testid='copy-button']") |
|
137
|
expect(copyButton).toBeInTheDocument() |
|
138
|
fireEvent.click(copyButton!) |
|
139
|
expect(mockHandleCopy).toHaveBeenCalled() |
|
140
|
}) |
|
141
|
|
|
142
|
test("doesn't call handleCopy when copy button is touched then clicked, with handleTouch", async () => { |
|
143
|
const { container } = render( |
|
144
|
<CopyButton text="Copy to Clipboard" handleTouch /> |
|
145
|
) |
|
146
|
const copyButton = container.querySelector("[data-testid='copy-button']") |
|
147
|
expect(copyButton).toBeInTheDocument() |
|
148
|
fireEvent.touchEnd(copyButton!) |
|
149
|
fireEvent.click(copyButton!) |
|
150
|
expect(mockHandleCopy).toHaveBeenCalledTimes(0) |
|
151
|
}) |
|
152
|
|
|
153
|
test("doesn't call handleCopy when button is touched and handleTouch is true and touchCount is 0", () => { |
|
154
|
const { container } = render( |
|
155
|
<CopyButton text="Copy to Clipboard" handleTouch /> |
|
156
|
) |
|
157
|
const copyButton = container.querySelector("[data-testid='copy-button']") |
|
158
|
expect(copyButton).toBeInTheDocument() |
|
159
|
fireEvent.touchEnd(copyButton!) |
|
160
|
expect(mockHandleCopy).not.toHaveBeenCalled() |
|
161
|
}) |
|
162
|
|
|
163
|
test("calls handleCopy when button is touched and handleTouch is true and touchCount is 1", () => { |
|
164
|
const { container } = render( |
|
165
|
<CopyButton text="Copy to Clipboard" handleTouch /> |
|
166
|
) |
|
167
|
const copyButton = container.querySelector("[data-testid='copy-button']") |
|
168
|
expect(copyButton).toBeInTheDocument() |
|
169
|
fireEvent.touchEnd(copyButton!) |
|
170
|
expect(mockHandleCopy).not.toHaveBeenCalled() |
|
171
|
fireEvent.touchEnd(copyButton!) |
|
172
|
expect(mockHandleCopy).toHaveBeenCalled() |
|
173
|
}) |
|
174
|
|
|
175
|
test("calls onCopy when copy button is clicked", () => { |
|
176
|
const { container } = render( |
|
177
|
<CopyButton text="Copy to Clipboard" onCopy={mockOnCopy} /> |
|
178
|
) |
|
179
|
const copyButton = container.querySelector("[data-testid='copy-button']") |
|
180
|
expect(copyButton).toBeInTheDocument() |
|
181
|
fireEvent.click(copyButton!) |
|
182
|
expect(mockOnCopy).toHaveBeenCalled() |
|
183
|
}) |
|
184
|
|
|
185
|
test("calls onCopy when button is touched and handleTouch is true and touchCount is 1", () => { |
|
186
|
const { container } = render( |
|
187
|
<CopyButton text="Copy to Clipboard" handleTouch onCopy={mockOnCopy} /> |
|
188
|
) |
|
189
|
const copyButton = container.querySelector("[data-testid='copy-button']") |
|
190
|
expect(copyButton).toBeInTheDocument() |
|
191
|
fireEvent.touchEnd(copyButton!) |
|
192
|
expect(mockOnCopy).not.toHaveBeenCalled() |
|
193
|
fireEvent.touchEnd(copyButton!) |
|
194
|
expect(mockOnCopy).toHaveBeenCalled() |
|
195
|
}) |
|
196
|
}) |