-
-
Notifications
You must be signed in to change notification settings - Fork 533
/
Copy pathtooltip-props.spec.js
215 lines (164 loc) · 6.04 KB
/
tooltip-props.spec.js
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import { render, screen, waitFor } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import '@testing-library/jest-dom'
import { TooltipController as Tooltip } from '../components/TooltipController'
// Tell Jest to mock all timeout functions
jest.useRealTimers()
// eslint-disable-next-line react/prop-types
const TooltipProps = ({ id, ...tooltipParams }) => (
<>
<span data-tooltip-id={id}>Lorem Ipsum</span>
<Tooltip id={id} {...tooltipParams} />
</>
)
describe('tooltip props', () => {
test('tooltip without element reference', async () => {
const { container } = render(<TooltipProps content="Hello World!" />)
const anchorElement = screen.getByText('Lorem Ipsum')
await userEvent.hover(anchorElement)
await waitFor(() => {
expect(screen.queryByText('Hello World!')).not.toBeInTheDocument()
expect(screen.queryByRole('tooltip')).not.toBeInTheDocument()
})
expect(container).toMatchSnapshot()
})
test('basic tooltip', async () => {
const { container } = render(<TooltipProps id="basic-example" content="Hello World!" />)
const anchorElement = screen.getByText('Lorem Ipsum')
await userEvent.hover(anchorElement)
const tooltip = await screen.findByRole('tooltip')
expect(tooltip).toHaveAttribute('style')
expect(tooltip).toBeInTheDocument()
expect(container).toMatchSnapshot()
})
test('tooltip with place', async () => {
const { container } = render(
<TooltipProps id="example-place" content="Hello World!" place="right" />,
)
const anchorElement = screen.getByText('Lorem Ipsum')
await userEvent.hover(anchorElement)
const tooltip = await screen.findByRole('tooltip')
expect(tooltip).toHaveAttribute('style')
expect(tooltip).toBeInTheDocument()
expect(container).toMatchSnapshot()
})
test('tooltip with html', async () => {
const { container } = render(<TooltipProps id="example-html" html="<div>Hello World!<div>" />)
const anchorElement = screen.getByText('Lorem Ipsum')
await userEvent.hover(anchorElement)
const tooltip = await screen.findByRole('tooltip')
expect(tooltip).toHaveAttribute('style')
expect(tooltip).toBeInTheDocument()
expect(container).toMatchSnapshot()
})
test('clickable tooltip', async () => {
const id = 'example-clickable'
const mockCallBack = jest.fn()
const { container } = render(
<>
<span data-tooltip-id={id}>Lorem Ipsum</span>
<Tooltip id={id} clickable>
<button onClick={mockCallBack}>button</button>
</Tooltip>
</>,
)
const anchorElement = screen.getByText('Lorem Ipsum')
await userEvent.hover(anchorElement)
const tooltip = await screen.findByRole('tooltip')
expect(tooltip).toHaveAttribute('style')
const button = await screen.findByRole('button')
await userEvent.click(button)
expect(tooltip).toBeInTheDocument()
expect(button).toBeInTheDocument()
expect(mockCallBack).toHaveBeenCalled()
expect(container).toMatchSnapshot()
})
test('tooltip with delay show', async () => {
const { container } = render(
<TooltipProps id="example-delay-show" content="Hello World!" delayShow={300} />,
)
const anchorElement = screen.getByText('Lorem Ipsum')
await userEvent.hover(anchorElement)
await waitFor(() => {
expect(screen.queryByRole('tooltip')).not.toBeInTheDocument()
})
await waitFor(() => {
expect(screen.queryByRole('tooltip')).toHaveClass('react-tooltip__show')
})
expect(container).toMatchSnapshot()
})
test('tooltip with delay hide', async () => {
const { container } = render(
<TooltipProps id="example-delay-hide" content="Hello World!" delayHide={300} />,
)
const anchorElement = screen.getByText('Lorem Ipsum')
await userEvent.hover(anchorElement)
const tooltip = await screen.findByRole('tooltip')
expect(tooltip).toHaveAttribute('style')
expect(tooltip).toBeInTheDocument()
await userEvent.unhover(anchorElement)
await waitFor(
() => {
expect(tooltip).toBeInTheDocument()
},
{
timeout: 200,
},
)
await waitFor(
() => {
expect(tooltip).not.toBeInTheDocument()
},
{
timeout: 500,
},
)
expect(container).toMatchSnapshot()
})
test('tooltip with custom position', async () => {
const { container } = render(
<TooltipProps id="example-place" content="Hello World!" position={{ x: 0, y: 0 }} />,
)
const anchorElement = screen.getByText('Lorem Ipsum')
await userEvent.hover(anchorElement)
const tooltip = await screen.findByRole('tooltip')
expect(tooltip).toHaveAttribute('style')
expect(tooltip).toBeInTheDocument()
expect(container).toMatchSnapshot()
})
test('tooltip with float', async () => {
const { container } = render(<TooltipProps id="example-float" content="Hello World!" float />)
const anchorElement = screen.getByText('Lorem Ipsum')
await userEvent.hover(anchorElement)
const tooltip = await screen.findByRole('tooltip')
expect(tooltip).toHaveAttribute('style')
expect(tooltip).toBeInTheDocument()
expect(container).toMatchSnapshot()
})
test('tooltip with disableTooltip return true', async () => {
render(
<TooltipProps
id="example-disableTooltip-true"
content="Hello World!"
disableTooltip={() => true}
/>,
)
const anchorElement = screen.getByText('Lorem Ipsum')
await userEvent.hover(anchorElement)
expect(screen.queryByRole('tooltip')).not.toBeInTheDocument()
})
test('tooltip with disableTooltip return false', async () => {
const { container } = render(
<TooltipProps
id="example-disableTooltip-false"
content="Hello World!"
disableTooltip={() => false}
/>,
)
const anchorElement = screen.getByText('Lorem Ipsum')
await userEvent.hover(anchorElement)
const tooltip = await screen.findByRole('tooltip')
expect(tooltip).toBeInTheDocument()
expect(container).toMatchSnapshot()
})
})