-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Expand file tree
/
Copy pathindex.js
More file actions
256 lines (216 loc) · 6.69 KB
/
index.js
File metadata and controls
256 lines (216 loc) · 6.69 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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/**
* External dependencies
*/
import { render, screen, act } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
/**
* Internal dependencies
*/
import Tooltip from '../';
/**
* WordPress dependencies
*/
import { TOOLTIP_DELAY } from '../index.js';
describe( 'Tooltip', () => {
describe( '#render()', () => {
it( 'should not render the tooltip if multiple children are passed', () => {
render(
<Tooltip text="Help text">
<button>Button 1</button>
<button>Button 2</button>
</Tooltip>
);
const button = screen.getByText( 'Button 1' );
button.focus();
expect( screen.queryByText( 'Help text' ) ).not.toBeInTheDocument();
} );
it( 'should render children', () => {
render(
<Tooltip text="Help text">
<button>Hover Me!</button>
</Tooltip>
);
expect(
screen.getByRole( 'button', { name: 'Hover Me!' } )
).toBeInTheDocument();
expect( screen.queryByText( 'Help text' ) ).not.toBeInTheDocument();
} );
it( 'should render children with additional tooltip when focused', () => {
const mockOnFocus = jest.fn();
render(
<Tooltip text="Help text">
<button onFocus={ mockOnFocus }>Hover Me!</button>
</Tooltip>
);
const button = screen.getByRole( 'button', { name: 'Hover Me!' } );
expect( button ).toBeInTheDocument();
// Before focus, the tooltip is not shown.
expect( screen.queryByText( 'Help text' ) ).not.toBeInTheDocument();
button.focus();
// Tooltip is shown after focusing the anchor.
expect( screen.getByText( 'Help text' ) ).toBeInTheDocument();
expect( mockOnFocus ).toHaveBeenCalledWith(
expect.objectContaining( {
type: 'focus',
} )
);
} );
it( 'should render children with additional tooltip when hovered', async () => {
const user = userEvent.setup( {
advanceTimers: jest.advanceTimersByTime,
} );
render(
<Tooltip text="Help text">
<button>Hover Me!</button>
</Tooltip>
);
const button = screen.getByRole( 'button', { name: 'Hover Me!' } );
expect( button ).toBeInTheDocument();
await user.hover( button );
// Tooltip hasn't appeared yet
expect( screen.queryByText( 'Help text' ) ).not.toBeInTheDocument();
act( () => jest.advanceTimersByTime( TOOLTIP_DELAY ) );
// Tooltip shows after the delay
expect( screen.getByText( 'Help text' ) ).toBeInTheDocument();
} );
it( 'should not show tooltip on focus as result of mouse click', async () => {
const user = userEvent.setup( {
advanceTimers: jest.advanceTimersByTime,
} );
jest.useFakeTimers();
render(
<Tooltip text="Help text">
<button>Hover Me!</button>
</Tooltip>
);
const button = screen.getByRole( 'button' );
await user.click( button );
setTimeout( () => {
expect(
screen.queryByText( 'Help text' )
).not.toBeInTheDocument();
jest.runOnlyPendingTimers();
jest.useRealTimers();
}, TOOLTIP_DELAY );
} );
it( 'should show tooltip on delayed mouseenter', async () => {
const user = userEvent.setup( {
advanceTimers: jest.advanceTimersByTime,
} );
const originalMouseEnter = jest.fn();
jest.useFakeTimers();
render(
<Tooltip text="Help text">
<button
onMouseEnter={ originalMouseEnter }
onFocus={ originalMouseEnter }
>
<span>Hover Me!</span>
</button>
</Tooltip>
);
const button = screen.getByRole( 'button' );
await user.hover( button );
expect( screen.queryByText( 'Help text' ) ).not.toBeInTheDocument();
expect( originalMouseEnter ).toHaveBeenCalledTimes( 1 );
setTimeout( () => {
expect( screen.getByText( 'Help text' ) ).toBeInTheDocument();
jest.runOnlyPendingTimers();
jest.useRealTimers();
}, TOOLTIP_DELAY );
} );
it( 'should respect custom delay prop when showing tooltip', async () => {
const user = userEvent.setup( {
advanceTimers: jest.advanceTimersByTime,
} );
const originalMouseEnter = jest.fn();
jest.useFakeTimers();
render(
<Tooltip text="Help text" delay={ 2000 }>
<button
onMouseEnter={ originalMouseEnter }
onFocus={ originalMouseEnter }
>
<span>Hover Me!</span>
</button>
</Tooltip>
);
const button = screen.getByRole( 'button' );
await user.hover( button );
expect( screen.queryByText( 'Help text' ) ).not.toBeInTheDocument();
expect( originalMouseEnter ).toHaveBeenCalledTimes( 1 );
// Tooltip does not yet exist after default delay, because custom delay is passed.
setTimeout( () => {
expect(
screen.queryByText( 'Help text' )
).not.toBeInTheDocument();
}, TOOLTIP_DELAY );
// Tooltip appears after custom delay.
setTimeout( () => {
expect( screen.getByText( 'Help text' ) ).toBeInTheDocument();
jest.runOnlyPendingTimers();
jest.useRealTimers();
}, 2000 );
} );
it( 'should show tooltip when an element is disabled', async () => {
const user = userEvent.setup( {
advanceTimers: jest.advanceTimersByTime,
} );
const { container } = render(
<Tooltip text="Show helpful text here">
<button disabled>Click me</button>
</Tooltip>
);
const button = screen.getByRole( 'button' );
const buttonRect = button.getBoundingClientRect();
const eventCatcher =
container.getElementsByClassName( 'event-catcher' )[ 0 ];
const eventCatcherRect = eventCatcher.getBoundingClientRect();
expect( buttonRect ).toEqual( eventCatcherRect );
await user.hover( eventCatcher );
setTimeout( () => {
expect( screen.getByText( 'Help text' ) ).toBeInTheDocument();
}, TOOLTIP_DELAY );
} );
it( 'should not emit events back to children when they are disabled', async () => {
const user = userEvent.setup( {
advanceTimers: jest.advanceTimersByTime,
} );
const handleClick = jest.fn();
const { container } = render(
<Tooltip text="Show helpful text here">
<button disabled onClick={ handleClick }>
Click me
</button>
</Tooltip>
);
const eventCatcher =
container.getElementsByClassName( 'event-catcher' )[ 0 ];
await user.click( eventCatcher );
expect( handleClick ).not.toHaveBeenCalled();
} );
it( 'should cancel pending setIsOver on mouseleave', async () => {
const user = userEvent.setup( {
advanceTimers: jest.advanceTimersByTime,
} );
const originalMouseEnter = jest.fn();
render(
<Tooltip text="Help text">
<button
onMouseEnter={ originalMouseEnter }
onFocus={ originalMouseEnter }
>
Hover Me!
</button>
</Tooltip>
);
const button = screen.getByRole( 'button' );
await user.hover( button );
setTimeout( () => {
expect(
screen.queryByText( 'Help text' )
).not.toBeInTheDocument();
}, TOOLTIP_DELAY );
} );
} );
} );