-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathButton.test.js
More file actions
400 lines (356 loc) · 13.7 KB
/
Button.test.js
File metadata and controls
400 lines (356 loc) · 13.7 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
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/*
* Copyright 2022 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
import {act, pointerMap, render} from '@react-spectrum/test-utils-internal';
import {Button, ButtonContext, Dialog, DialogTrigger, Heading, Modal, ProgressBar, Text} from '../';
import React, {useState} from 'react';
import userEvent from '@testing-library/user-event';
describe('Button', () => {
let user;
beforeAll(() => {
user = userEvent.setup({delay: null, pointerMap});
jest.useFakeTimers();
});
afterEach(() => {
// clear any live announcers from pending buttons
act(() => jest.runAllTimers());
});
it('should render a button with default class', () => {
let {getByRole} = render(<Button>Test</Button>);
let button = getByRole('button');
expect(button).toHaveAttribute('class', 'react-aria-Button');
});
it('should render a button with custom class', () => {
let {getByRole} = render(<Button className="test">Test</Button>);
let button = getByRole('button');
expect(button).toHaveAttribute('class', 'test');
});
it('should support DOM props', () => {
let {getByRole} = render(<Button data-foo="bar">Test</Button>);
let button = getByRole('button');
expect(button).toHaveAttribute('data-foo', 'bar');
});
it('should support form props', () => {
let {getByRole} = render(<form id="foo"><Button form="foo" formMethod="post">Test</Button></form>);
let button = getByRole('button');
expect(button).toHaveAttribute('form', 'foo');
expect(button).toHaveAttribute('formMethod', 'post');
});
it('should support accessibility props', () => {
let {getByRole} = render(<Button aria-current="page">Test</Button>);
let button = getByRole('button');
expect(button).toHaveAttribute('aria-current', 'page');
});
it('should not have aria-disabled defined by default', () => {
let {getByRole} = render(<Button>Test</Button>);
let button = getByRole('button');
expect(button).not.toHaveAttribute('aria-disabled');
});
it('should support aria-disabled passthrough', () => {
let {getByRole} = render(<Button aria-disabled="true">Test</Button>);
let button = getByRole('button');
expect(button).toHaveAttribute('aria-disabled', 'true');
});
it('should support slot', () => {
let {getByRole} = render(
<ButtonContext.Provider value={{slots: {test: {'aria-label': 'test'}}}}>
<Button slot="test">Test</Button>
</ButtonContext.Provider>
);
let button = getByRole('button');
expect(button).toHaveAttribute('slot', 'test');
expect(button).toHaveAttribute('aria-label', 'test');
});
it('should support hover', async () => {
let hoverStartSpy = jest.fn();
let hoverChangeSpy = jest.fn();
let hoverEndSpy = jest.fn();
let {getByRole} = render(<Button className={({isHovered}) => isHovered ? 'hover' : ''} onHoverStart={hoverStartSpy} onHoverChange={hoverChangeSpy} onHoverEnd={hoverEndSpy}>Test</Button>);
let button = getByRole('button');
expect(button).not.toHaveAttribute('data-hovered');
expect(button).not.toHaveClass('hover');
await user.hover(button);
expect(button).toHaveAttribute('data-hovered', 'true');
expect(button).toHaveClass('hover');
expect(hoverStartSpy).toHaveBeenCalledTimes(1);
expect(hoverChangeSpy).toHaveBeenCalledTimes(1);
await user.unhover(button);
expect(button).not.toHaveAttribute('data-hovered');
expect(button).not.toHaveClass('hover');
expect(hoverEndSpy).toHaveBeenCalledTimes(1);
expect(hoverChangeSpy).toHaveBeenCalledTimes(2);
});
it('should support focus ring', async () => {
let {getByRole} = render(<Button className={({isFocusVisible}) => isFocusVisible ? 'focus' : ''}>Test</Button>);
let button = getByRole('button');
expect(button).not.toHaveAttribute('data-focus-visible');
expect(button).not.toHaveClass('focus');
await user.tab();
expect(document.activeElement).toBe(button);
expect(button).toHaveAttribute('data-focus-visible', 'true');
expect(button).toHaveClass('focus');
await user.tab();
expect(button).not.toHaveAttribute('data-focus-visible');
expect(button).not.toHaveClass('focus');
});
it('should support press state', async () => {
let onPress = jest.fn();
let onClick = jest.fn();
let onClickCapture = jest.fn();
let {getByRole} = render(<Button className={({isPressed}) => isPressed ? 'pressed' : ''} onPress={onPress} onClick={onClick} onClickCapture={onClickCapture}>Test</Button>);
let button = getByRole('button');
expect(button).not.toHaveAttribute('data-pressed');
expect(button).not.toHaveClass('pressed');
await user.pointer({target: button, keys: '[MouseLeft>]'});
expect(button).toHaveAttribute('data-pressed', 'true');
expect(button).toHaveClass('pressed');
await user.pointer({target: button, keys: '[/MouseLeft]'});
expect(button).not.toHaveAttribute('data-pressed');
expect(button).not.toHaveClass('pressed');
expect(onPress).toHaveBeenCalledTimes(1);
expect(onClick).toHaveBeenCalledTimes(1);
expect(onClickCapture).toHaveBeenCalledTimes(1);
});
it('should support disabled state', () => {
let {getByRole} = render(<Button isDisabled className={({isDisabled}) => isDisabled ? 'disabled' : ''}>Test</Button>);
let button = getByRole('button');
expect(button).toBeDisabled();
expect(button).toHaveClass('disabled');
});
it('should support render props', async () => {
let {getByRole} = render(<Button>{({isPressed}) => isPressed ? 'Pressed' : 'Test'}</Button>);
let button = getByRole('button');
expect(button).toHaveTextContent('Test');
await user.pointer({target: button, keys: '[MouseLeft>]'});
expect(button).toHaveTextContent('Pressed');
await user.pointer({target: button, keys: '[/MouseLeft]'});
expect(button).toHaveTextContent('Test');
});
// isPending state
it('displays a spinner when isPending prop is true', async function () {
let onPressSpy = jest.fn();
function TestComponent() {
let [pending, setPending] = useState(false);
return (
<Button
onPress={() => {
setPending(true);
onPressSpy();
}}
isPending={pending}>
{({isPending}) => (
<>
<Text style={{opacity: isPending ? '0' : undefined}}>Test</Text>
<ProgressBar
aria-label="loading"
style={{opacity: isPending ? undefined : '0'}}
isIndeterminate>
loading
</ProgressBar>
</>
)}
</Button>
);
}
let {getByRole} = render(<TestComponent />);
let button = getByRole('button');
expect(button).not.toHaveAttribute('aria-disabled');
await user.click(button);
// Button is disabled immediately, but spinner visibility is delayed
expect(button).toHaveAttribute('aria-disabled', 'true');
// Multiple clicks shouldn't call onPressSpy
await user.click(button);
expect(button).toHaveAttribute('aria-disabled', 'true');
expect(onPressSpy).toHaveBeenCalledTimes(1);
});
// isPending anchor element
it('removes href attribute from anchor element when isPending is true', () => {
let {getByRole} = render(
<Button href="//example.com" isPending>
{({isPending}) => (
<>
<Text style={{visibility: isPending ? 'hidden' : undefined}}>Click me</Text>
<ProgressBar
aria-label="loading"
style={{visibility: isPending ? undefined : 'hidden'}}
isIndeterminate>
loading
</ProgressBar>
</>
)}
</Button>
);
let button = getByRole('button');
expect(button).not.toHaveAttribute('href');
});
it('should prevent explicit mouse form submission when isPending', async function () {
let onSubmitSpy = jest.fn(e => e.preventDefault());
function TestComponent() {
let [pending, setPending] = useState(false);
return (
<Button
type="submit"
onPress={() => {
// immediately setting pending to true will remove the click handler before the form is submitted
setTimeout(() => {
setPending(true);
}, 0);
}}
isPending={pending}>
{({isPending}) => (
<>
<Text style={{opacity: isPending ? '0' : undefined}}>Test</Text>
<ProgressBar
aria-label="loading"
style={{opacity: isPending ? undefined : '0'}}
isIndeterminate>
loading
</ProgressBar>
</>
)}
</Button>
);
}
let {getByRole} = render(
<form onSubmit={onSubmitSpy}>
<TestComponent />
</form>
);
let button = getByRole('button');
expect(button).not.toHaveAttribute('aria-disabled');
await user.click(button);
expect(onSubmitSpy).toHaveBeenCalled();
onSubmitSpy.mockClear();
// run timer to set pending
act(() => jest.runAllTimers());
await user.click(button);
expect(onSubmitSpy).not.toHaveBeenCalled();
});
it('should prevent explicit keyboard form submission when isPending', async function () {
let onSubmitSpy = jest.fn(e => e.preventDefault());
function TestComponent() {
let [pending, setPending] = useState(false);
return (
<Button
type="submit"
onPress={() => {
// immediately setting pending to true will remove the click handler before the form is submitted
setTimeout(() => {
setPending(true);
}, 0);
}}
isPending={pending}>
{({isPending}) => (
<>
<Text style={{opacity: isPending ? '0' : undefined}}>Test</Text>
<ProgressBar
aria-label="loading"
style={{opacity: isPending ? undefined : '0'}}
isIndeterminate>
loading
</ProgressBar>
</>
)}
</Button>
);
}
render(
<form onSubmit={onSubmitSpy}>
<TestComponent />
</form>
);
await user.tab();
await user.keyboard('{Enter}');
expect(onSubmitSpy).toHaveBeenCalled();
onSubmitSpy.mockClear();
act(() => jest.runAllTimers());
await user.keyboard('{Enter}');
expect(onSubmitSpy).not.toHaveBeenCalled();
});
// Note: two inputs are needed, otherwise https://www.w3.org/TR/2011/WD-html5-20110525/association-of-controls-and-forms.html#implicit-submission
// Implicit form submission can happen if there's only one.
it('should prevent implicit form submission when isPending', async function () {
let onSubmitSpy = jest.fn(e => e.preventDefault());
function TestComponent(props) {
let [pending, setPending] = useState(false);
return (
<form
onSubmit={(e) => {
// forms are submitted implicitly on keydown, so we need to wait to set pending until after to set pending
props.onSubmit(e);
}}
onKeyDown={(e) => {
if (e.key === 'Enter') {
// keyup could theoretically happen elsewhere if focus is moved during submission
document.body.addEventListener('keyup', () => {
setPending(true);
}, {capture: true, once: true});
}
}}>
<label htmlFor="foo">Test</label>
<input id="foo" type="text" />
<input id="bar" type="text" />
<Button
type="submit"
isPending={pending}>
{({isPending}) => (
<>
<Text style={{opacity: isPending ? '0' : undefined}}>Test</Text>
<ProgressBar
aria-label="loading"
style={{opacity: isPending ? undefined : '0'}}
isIndeterminate>
loading
</ProgressBar>
</>
)}
</Button>
</form>
);
}
render(
<TestComponent onSubmit={onSubmitSpy} />
);
await user.tab();
await user.keyboard('{Enter}');
expect(onSubmitSpy).toHaveBeenCalled();
onSubmitSpy.mockClear();
await user.keyboard('{Enter}');
expect(onSubmitSpy).not.toHaveBeenCalled();
});
it('disables press when in pending state for context', async function () {
let onFocusSpy = jest.fn();
let onBlurSpy = jest.fn();
let {getByRole, queryByRole} = render(
<DialogTrigger>
<Button isPending onFocus={onFocusSpy} onBlur={onBlurSpy}>Delete…</Button>
<Modal data-test="modal">
<Dialog role="alertdialog" data-test="dialog">
{({close}) => (
<>
<Heading slot="title">Alert</Heading>
<Button onPress={close}>Close</Button>
</>
)}
</Dialog>
</Modal>
</DialogTrigger>
);
let button = getByRole('button');
await user.click(button);
expect(onFocusSpy).toHaveBeenCalled();
expect(onBlurSpy).not.toHaveBeenCalled();
let dialog = queryByRole('alertdialog');
expect(dialog).toBeNull();
await user.click(document.body);
expect(onBlurSpy).toHaveBeenCalled();
});
});