-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
index.tsx
301 lines (229 loc) · 7.23 KB
/
index.tsx
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/**
* External dependencies
*/
import { render, screen, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
/**
* WordPress dependencies
*/
import { shortcutAriaLabel } from '@wordpress/keycodes';
/**
* Internal dependencies
*/
import Button from '../../../button';
import { ToolTip } from '..';
import { TOOLTIP_DELAY } from '../../../tooltip/';
const props = {
children: <Button>Button</Button>,
text: 'tooltip text',
delay: TOOLTIP_DELAY,
};
describe( 'ToolTip', () => {
it( 'should not render the tooltip if multiple children are passed', async () => {
const user = userEvent.setup();
// mock expected console error
const err = jest
.spyOn( console, 'error' )
.mockImplementation( () => {} );
expect( () => {
render(
// expected TS error since Tooltip cannot have more than one child element
// @ts-expect-error
<ToolTip { ...props }>
<Button>This is a button</Button>
<Button>This is another button</Button>
</ToolTip>
);
} ).toThrow();
expect( console ).toHaveErrored();
expect( err ).toHaveBeenCalled();
await user.tab();
expect(
screen.queryByRole( 'tooltip', { name: /tooltip text/i } )
).not.toBeInTheDocument();
err.mockReset();
} );
it( 'should not render the tooltip if there is no focus', () => {
render( <ToolTip { ...props } /> );
expect(
screen.getByRole( 'button', { name: /Button/i } )
).toBeVisible();
expect(
screen.queryByRole( 'tooltip', { name: /tooltip text/i } )
).not.toBeInTheDocument();
} );
it( 'should render the tooltip when focusing on the tooltip anchor via tab', async () => {
const user = userEvent.setup();
render( <ToolTip { ...props } /> );
const button = screen.getByRole( 'button', { name: /Button/i } );
await user.tab();
expect( button ).toHaveFocus();
await waitFor( () =>
expect(
screen.getByRole( 'tooltip', { name: /tooltip text/i } )
).toBeVisible()
);
await user.tab();
await user.unhover( button );
expect(
screen.queryByRole( 'tooltip', { name: /tooltip text/i } )
).not.toBeInTheDocument();
} );
it( 'should not show tooltip on focus as result of mouse click', async () => {
const user = userEvent.setup();
render( <ToolTip { ...props } /> );
await user.click( screen.getByRole( 'button', { name: /Button/i } ) );
expect(
screen.queryByRole( 'tooltip', { name: /tooltip text/i } )
).not.toBeInTheDocument();
} );
it( 'should respect custom delay prop when showing tooltip', async () => {
const user = userEvent.setup( { delay: TOOLTIP_DELAY } );
const CUSTOM_DELAY = TOOLTIP_DELAY + 25;
render( <ToolTip { ...props } delay={ CUSTOM_DELAY } /> );
const button = screen.getByRole( 'button', { name: /Button/i } );
await user.hover( button );
expect(
screen.queryByRole( 'tooltip', { name: /tooltip text/i } )
).not.toBeInTheDocument();
await waitFor( () =>
expect(
screen.getByRole( 'tooltip', { name: /tooltip text/i } )
).toBeVisible()
);
await user.unhover( button );
expect(
screen.queryByRole( 'tooltip', { name: /tooltip text/i } )
).not.toBeInTheDocument();
} );
it( 'should not show tooltip if the mouse leaves the tooltip anchor before set delay', async () => {
const user = userEvent.setup();
const onMouseEnterMock = jest.fn();
const onMouseLeaveMock = jest.fn();
const MOUSE_LEAVE_DELAY = TOOLTIP_DELAY - 200;
render(
<>
<ToolTip { ...props }>
<Button
onMouseEnter={ onMouseEnterMock }
onMouseLeave={ onMouseLeaveMock }
>
Button 1
</Button>
</ToolTip>
<Button>Button 2</Button>
</>
);
await user.hover(
screen.getByRole( 'button', {
name: 'Button 1',
} )
);
// ToolTip hasn't appeared yet
expect(
screen.queryByRole( 'tooltip', { name: /tooltip text/i } )
).not.toBeInTheDocument();
expect( onMouseEnterMock ).toHaveBeenCalledTimes( 1 );
// Advance time by MOUSE_LEAVE_DELAY time
await new Promise( ( resolve ) =>
setTimeout( resolve, MOUSE_LEAVE_DELAY )
);
expect(
screen.queryByRole( 'tooltip', { name: /tooltip text/i } )
).not.toBeInTheDocument();
// Hover the other button, meaning that the mouse will leave the tooltip anchor
await user.hover(
screen.getByRole( 'button', {
name: 'Button 2',
} )
);
// ToolTip still hasn't appeared yet
expect(
screen.queryByRole( 'tooltip', { name: /tooltip text/i } )
).not.toBeInTheDocument();
expect( onMouseEnterMock ).toHaveBeenCalledTimes( 1 );
expect( onMouseLeaveMock ).toHaveBeenCalledTimes( 1 );
// Advance time again, so that we reach the full TOOLTIP_DELAY time
await new Promise( ( resolve ) =>
setTimeout( resolve, TOOLTIP_DELAY )
);
// ToolTip won't show, since the mouse has left the tooltip anchor
expect(
screen.queryByRole( 'tooltip', { name: /tooltip text/i } )
).not.toBeInTheDocument();
} );
it( 'should render the tooltip when the tooltip anchor is hovered', async () => {
const user = userEvent.setup();
render( <ToolTip { ...props } /> );
const button = screen.getByRole( 'button', { name: /Button/i } );
await user.hover( button );
await waitFor( () =>
expect(
screen.getByRole( 'tooltip', { name: /tooltip text/i } )
).toBeVisible()
);
await user.unhover( button );
expect(
screen.queryByRole( 'tooltip', { name: /tooltip text/i } )
).not.toBeInTheDocument();
} );
it( 'should show tooltip when an element is disabled', async () => {
const user = userEvent.setup();
render(
<ToolTip { ...props }>
<Button aria-disabled>Button</Button>
</ToolTip>
);
const button = screen.getByRole( 'button', { name: /Button/i } );
expect( button ).toBeVisible();
expect( button ).toHaveAttribute( 'aria-disabled' );
await user.hover( button );
await waitFor( () =>
expect(
screen.getByRole( 'tooltip', { name: /tooltip text/i } )
).toBeVisible()
);
await user.unhover( button );
expect(
screen.queryByRole( 'tooltip', { name: /tooltip text/i } )
).not.toBeInTheDocument();
} );
it( 'should render the shortcut display text when a string is passed as the shortcut', async () => {
const user = userEvent.setup();
render( <ToolTip { ...props } shortcut="shortcut text" /> );
const button = screen.getByRole( 'button', { name: /Button/i } );
await user.hover( button );
await waitFor( () =>
expect( screen.getByText( 'shortcut text' ) ).toBeVisible()
);
await user.unhover( button );
expect(
screen.queryByRole( 'tooltip', { name: /tooltip text/i } )
).not.toBeInTheDocument();
} );
it( 'should render the keyboard shortcut display text and aria-label when an object is passed as the shortcut', async () => {
const user = userEvent.setup();
render(
<ToolTip
{ ...props }
shortcut={ {
display: '⇧⌘,',
ariaLabel: shortcutAriaLabel.primaryShift( ',' ),
} }
/>
);
const button = screen.getByRole( 'button', { name: /Button/i } );
await user.hover( button );
await waitFor( () =>
expect( screen.getByText( '⇧⌘,' ) ).toBeVisible()
);
expect( screen.getByText( '⇧⌘,' ) ).toHaveAttribute(
'aria-label',
'Control + Shift + Comma'
);
await user.unhover( button );
expect(
screen.queryByRole( 'tooltip', { name: /tooltip text/i } )
).not.toBeInTheDocument();
} );
} );