@instructure/ui-buttons 8.55.0 → 8.55.1-snapshot-3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (89) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/es/BaseButton/__new-tests__/BaseButton.test.js +361 -0
  3. package/es/Button/__new-tests__/Button.test.js +459 -0
  4. package/es/CloseButton/__new-tests__/CloseButton.test.js +63 -0
  5. package/es/CondensedButton/__new-tests__/CondensedButton.test.js +181 -0
  6. package/es/IconButton/__new-tests__/IconButton.test.js +185 -0
  7. package/es/ToggleButton/__new-tests__/ToggleButton.test.js +163 -0
  8. package/lib/BaseButton/__new-tests__/BaseButton.test.js +363 -0
  9. package/lib/Button/__new-tests__/Button.test.js +461 -0
  10. package/lib/CloseButton/__new-tests__/CloseButton.test.js +65 -0
  11. package/lib/CondensedButton/__new-tests__/CondensedButton.test.js +183 -0
  12. package/lib/IconButton/__new-tests__/IconButton.test.js +187 -0
  13. package/lib/ToggleButton/__new-tests__/ToggleButton.test.js +165 -0
  14. package/package.json +22 -20
  15. package/src/BaseButton/__new-tests__/BaseButton.test.tsx +407 -0
  16. package/src/Button/__new-tests__/Button.test.tsx +500 -0
  17. package/src/CloseButton/__new-tests__/CloseButton.test.tsx +70 -0
  18. package/src/CondensedButton/__new-tests__/CondensedButton.test.tsx +186 -0
  19. package/src/IconButton/__new-tests__/IconButton.test.tsx +214 -0
  20. package/src/ToggleButton/__new-tests__/ToggleButton.test.tsx +200 -0
  21. package/tsconfig.build.json +2 -3
  22. package/tsconfig.build.tsbuildinfo +1 -1
  23. package/types/BaseButton/__new-tests__/BaseButton.test.d.ts +2 -0
  24. package/types/BaseButton/__new-tests__/BaseButton.test.d.ts.map +1 -0
  25. package/types/Button/__new-tests__/Button.test.d.ts +2 -0
  26. package/types/Button/__new-tests__/Button.test.d.ts.map +1 -0
  27. package/types/CloseButton/__new-tests__/CloseButton.test.d.ts +2 -0
  28. package/types/CloseButton/__new-tests__/CloseButton.test.d.ts.map +1 -0
  29. package/types/CondensedButton/__new-tests__/CondensedButton.test.d.ts +2 -0
  30. package/types/CondensedButton/__new-tests__/CondensedButton.test.d.ts.map +1 -0
  31. package/types/IconButton/__new-tests__/IconButton.test.d.ts +2 -0
  32. package/types/IconButton/__new-tests__/IconButton.test.d.ts.map +1 -0
  33. package/types/ToggleButton/__new-tests__/ToggleButton.test.d.ts +2 -0
  34. package/types/ToggleButton/__new-tests__/ToggleButton.test.d.ts.map +1 -0
  35. package/es/BaseButton/BaseButtonLocator.js +0 -34
  36. package/es/BaseButton/locator.js +0 -27
  37. package/es/Button/ButtonLocator.js +0 -33
  38. package/es/Button/locator.js +0 -28
  39. package/es/CloseButton/CloseButtonLocator.js +0 -29
  40. package/es/CloseButton/locator.js +0 -27
  41. package/es/CondensedButton/CondensedButtonLocator.js +0 -33
  42. package/es/CondensedButton/locator.js +0 -26
  43. package/es/IconButton/IconButtonLocator.js +0 -33
  44. package/es/IconButton/locator.js +0 -26
  45. package/es/ToggleButton/ToggleButtonLocator.js +0 -46
  46. package/lib/BaseButton/BaseButtonLocator.js +0 -39
  47. package/lib/BaseButton/locator.js +0 -37
  48. package/lib/Button/ButtonLocator.js +0 -39
  49. package/lib/Button/locator.js +0 -39
  50. package/lib/CloseButton/CloseButtonLocator.js +0 -34
  51. package/lib/CloseButton/locator.js +0 -37
  52. package/lib/CondensedButton/CondensedButtonLocator.js +0 -39
  53. package/lib/CondensedButton/locator.js +0 -37
  54. package/lib/IconButton/IconButtonLocator.js +0 -39
  55. package/lib/IconButton/locator.js +0 -37
  56. package/lib/ToggleButton/ToggleButtonLocator.js +0 -51
  57. package/src/BaseButton/BaseButtonLocator.ts +0 -35
  58. package/src/BaseButton/locator.ts +0 -28
  59. package/src/Button/ButtonLocator.ts +0 -34
  60. package/src/Button/locator.ts +0 -27
  61. package/src/CloseButton/CloseButtonLocator.ts +0 -29
  62. package/src/CloseButton/locator.ts +0 -28
  63. package/src/CondensedButton/CondensedButtonLocator.ts +0 -34
  64. package/src/CondensedButton/locator.ts +0 -27
  65. package/src/IconButton/IconButtonLocator.ts +0 -34
  66. package/src/IconButton/locator.ts +0 -27
  67. package/src/ToggleButton/ToggleButtonLocator.ts +0 -44
  68. package/types/BaseButton/BaseButtonLocator.d.ts +0 -577
  69. package/types/BaseButton/BaseButtonLocator.d.ts.map +0 -1
  70. package/types/BaseButton/locator.d.ts +0 -4
  71. package/types/BaseButton/locator.d.ts.map +0 -1
  72. package/types/Button/ButtonLocator.d.ts +0 -577
  73. package/types/Button/ButtonLocator.d.ts.map +0 -1
  74. package/types/Button/locator.d.ts +0 -581
  75. package/types/Button/locator.d.ts.map +0 -1
  76. package/types/CloseButton/CloseButtonLocator.d.ts +0 -566
  77. package/types/CloseButton/CloseButtonLocator.d.ts.map +0 -1
  78. package/types/CloseButton/locator.d.ts +0 -4
  79. package/types/CloseButton/locator.d.ts.map +0 -1
  80. package/types/CondensedButton/CondensedButtonLocator.d.ts +0 -577
  81. package/types/CondensedButton/CondensedButtonLocator.d.ts.map +0 -1
  82. package/types/CondensedButton/locator.d.ts +0 -4
  83. package/types/CondensedButton/locator.d.ts.map +0 -1
  84. package/types/IconButton/IconButtonLocator.d.ts +0 -577
  85. package/types/IconButton/IconButtonLocator.d.ts.map +0 -1
  86. package/types/IconButton/locator.d.ts +0 -4
  87. package/types/IconButton/locator.d.ts.map +0 -1
  88. package/types/ToggleButton/ToggleButtonLocator.d.ts +0 -582
  89. package/types/ToggleButton/ToggleButtonLocator.d.ts.map +0 -1
@@ -0,0 +1,500 @@
1
+ /*
2
+ * The MIT License (MIT)
3
+ *
4
+ * Copyright (c) 2015 - present Instructure, Inc.
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in all
14
+ * copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ * SOFTWARE.
23
+ */
24
+
25
+ import React from 'react'
26
+ import { render, screen, waitFor } from '@testing-library/react'
27
+ import userEvent from '@testing-library/user-event'
28
+ import '@testing-library/jest-dom'
29
+
30
+ import { runAxeCheck } from '@instructure/ui-axe-check'
31
+ import { BaseButton } from '../../BaseButton'
32
+ import { Button } from '../index'
33
+
34
+ describe('<Button/>', () => {
35
+ let consoleWarningMock: jest.SpyInstance
36
+ const iconSelector = 'svg[data-title="myIcon"]'
37
+
38
+ const icon = (
39
+ <svg
40
+ data-title="myIcon"
41
+ height="1em"
42
+ width="1em"
43
+ style={{ fill: 'currentcolor' }}
44
+ >
45
+ <circle cx="0.5em" cy="0.5em" r="0.5em" />
46
+ </svg>
47
+ )
48
+
49
+ beforeEach(() => {
50
+ // Mocking console to prevent test output pollution
51
+ consoleWarningMock = jest.spyOn(console, 'warn').mockImplementation()
52
+ })
53
+
54
+ afterEach(() => {
55
+ consoleWarningMock.mockRestore()
56
+ })
57
+
58
+ it('should render children', () => {
59
+ const children = 'Hello world'
60
+
61
+ render(<Button>{children}</Button>)
62
+
63
+ const button = document.querySelector('button')
64
+
65
+ expect(button).toBeInTheDocument()
66
+ expect(button).toHaveTextContent(children)
67
+ })
68
+
69
+ it('should render a button', () => {
70
+ render(<Button>Hello World</Button>)
71
+
72
+ const button = screen.getByRole('button', { name: 'Hello World' })
73
+
74
+ expect(button).toBeInTheDocument()
75
+ expect(button).toHaveAttribute('type', 'button')
76
+ })
77
+
78
+ it('should provide a focused getter', () => {
79
+ let componentRef: BaseButton | undefined
80
+
81
+ render(
82
+ <Button
83
+ //@ts-expect-error TODO this is coming from ReactComponentWrapper
84
+ ref={(component: BaseButton) => {
85
+ componentRef = component
86
+ }}
87
+ >
88
+ Hello
89
+ </Button>
90
+ )
91
+ const button = screen.getByRole('button', { name: 'Hello' })
92
+
93
+ button.focus()
94
+
95
+ expect(componentRef?.focused).toBe(true)
96
+ })
97
+
98
+ it('should provide a focus function', () => {
99
+ let componentRef: BaseButton | undefined
100
+
101
+ render(
102
+ <Button
103
+ //@ts-expect-error TODO this is coming from ReactComponentWrapper
104
+ ref={(component: BaseButton) => {
105
+ componentRef = component
106
+ }}
107
+ >
108
+ Hello
109
+ </Button>
110
+ )
111
+ const button = screen.getByRole('button', { name: 'Hello' })
112
+
113
+ componentRef?.focus()
114
+
115
+ expect(document.activeElement).toBe(button)
116
+ })
117
+
118
+ it('should pass the type attribute', () => {
119
+ render(<Button type="submit">Hello</Button>)
120
+
121
+ const button = screen.getByRole('button', { name: 'Hello' })
122
+
123
+ expect(button).toBeInTheDocument()
124
+ expect(button).toHaveAttribute('type', 'submit')
125
+ })
126
+
127
+ it('should pass the `elementRef` prop', () => {
128
+ const elementRef = jest.fn()
129
+ render(<Button elementRef={elementRef}>Hello</Button>)
130
+
131
+ const button = screen.getByRole('button', { name: 'Hello' })
132
+
133
+ expect(elementRef).toHaveBeenCalledWith(button)
134
+ })
135
+
136
+ it('should pass the `as` prop', () => {
137
+ const { container } = render(<Button as="li">Hello</Button>)
138
+
139
+ const button = container.querySelector('[type="button"]')
140
+
141
+ expect(button).toBeInTheDocument()
142
+ expect(button).toHaveTextContent('Hello')
143
+ expect(button!.tagName).toBe('LI')
144
+ })
145
+
146
+ it('should set the disabled attribute when `interaction` is set to disabled', () => {
147
+ render(<Button interaction="disabled">Hello</Button>)
148
+
149
+ const button = screen.getByRole('button', { name: 'Hello' })
150
+
151
+ expect(button).toHaveAttribute('disabled')
152
+ })
153
+
154
+ it('should set the disabled attribute when `disabled` is set', () => {
155
+ render(<Button disabled>Hello</Button>)
156
+
157
+ const button = screen.getByRole('button', { name: 'Hello' })
158
+
159
+ expect(button).toHaveAttribute('disabled')
160
+ })
161
+
162
+ it('should set the disabled attribute when `interaction` is set to readonly', () => {
163
+ render(<Button interaction="readonly">Hello</Button>)
164
+
165
+ const button = screen.getByRole('button', { name: 'Hello' })
166
+
167
+ expect(button).toHaveAttribute('disabled')
168
+ })
169
+
170
+ it('should set the disabled attribute when `readOnly` is set', () => {
171
+ render(<Button readOnly>Hello</Button>)
172
+
173
+ const button = screen.getByRole('button', { name: 'Hello' })
174
+
175
+ expect(button).toHaveAttribute('disabled')
176
+ })
177
+
178
+ it('should pass the `href` prop', () => {
179
+ render(<Button href="#">Hello</Button>)
180
+
181
+ const linkButton = screen.getByRole('link', { name: 'Hello' })
182
+
183
+ expect(linkButton).toBeInTheDocument()
184
+ expect(linkButton).toHaveAttribute('href', '#')
185
+ })
186
+
187
+ it('should pass the `renderIcon` prop', () => {
188
+ render(<Button renderIcon={icon}>Hello</Button>)
189
+
190
+ const svgIcon = document.querySelector(iconSelector)
191
+ const button = screen.getByRole('button', { name: 'Hello' })
192
+
193
+ expect(button).toBeInTheDocument()
194
+ expect(svgIcon).toBeInTheDocument()
195
+ })
196
+
197
+ it('should pass the `onClick` prop', async () => {
198
+ const onClick = jest.fn()
199
+ render(<Button onClick={onClick}>Hello</Button>)
200
+
201
+ const button = screen.getByRole('button', { name: 'Hello' })
202
+
203
+ await userEvent.click(button)
204
+
205
+ await waitFor(() => {
206
+ expect(onClick).toHaveBeenCalledTimes(1)
207
+ })
208
+ })
209
+
210
+ it('should render the children as button text', () => {
211
+ render(<Button>Hello World</Button>)
212
+
213
+ const button = screen.getByRole('button')
214
+
215
+ expect(button).toBeInTheDocument()
216
+ expect(button).toHaveTextContent('Hello World')
217
+ })
218
+
219
+ it('should render a button', async () => {
220
+ render(<Button>Hello World</Button>)
221
+
222
+ const button = screen.getByRole('button')
223
+
224
+ expect(button).toBeInTheDocument()
225
+ expect(button).toHaveAttribute('type', 'button')
226
+ expect(button).toHaveTextContent('Hello World')
227
+ })
228
+
229
+ it('should not error with a null child', () => {
230
+ render(<Button>Hello World{null}</Button>)
231
+
232
+ const button = screen.getByRole('button', { name: 'Hello World' })
233
+
234
+ expect(button).toBeInTheDocument()
235
+ })
236
+
237
+ it('should render a link styled as a button if href is provided', () => {
238
+ render(<Button href="example.html">Hello World</Button>)
239
+
240
+ const button = screen.getAllByRole('link', { name: 'Hello World' })
241
+
242
+ expect(button).toHaveLength(1)
243
+ expect(button[0]).toHaveAttribute('href', 'example.html')
244
+ })
245
+
246
+ it('should render as a link when `to` prop is provided', () => {
247
+ const { container } = render(<Button to="/example">Test</Button>)
248
+
249
+ const linkButton = container.querySelector('a')
250
+
251
+ expect(linkButton).toBeInTheDocument()
252
+ expect(linkButton).toHaveAttribute('to', '/example')
253
+ })
254
+
255
+ it('should render designated tag if `as` prop is specified', () => {
256
+ const { container } = render(<Button as="span">Hello World</Button>)
257
+
258
+ const button = container.querySelector('[type="button"]')
259
+
260
+ expect(button).toBeInTheDocument()
261
+ expect(button).toHaveTextContent('Hello World')
262
+ expect(button!.tagName).toBe('SPAN')
263
+ })
264
+
265
+ it('should set role="button"', () => {
266
+ const onClick = jest.fn()
267
+ const { container } = render(
268
+ <Button as="span" onClick={onClick}>
269
+ Hello World
270
+ </Button>
271
+ )
272
+ const button = container.querySelector('[type="button"]')
273
+
274
+ expect(button).toBeInTheDocument()
275
+ expect(button).toHaveAttribute('role', 'button')
276
+ })
277
+
278
+ it('should set tabIndex="0"', () => {
279
+ const onClick = jest.fn()
280
+ render(
281
+ <Button as="span" onClick={onClick}>
282
+ Hello World
283
+ </Button>
284
+ )
285
+ const button = screen.getByRole('button')
286
+
287
+ expect(button).toBeInTheDocument()
288
+ expect(button).toHaveAttribute('tabIndex', '0')
289
+ })
290
+
291
+ it('should pass down the type prop to the button element', () => {
292
+ const onClick = jest.fn()
293
+ render(
294
+ <Button type="submit" onClick={onClick}>
295
+ Hello World
296
+ </Button>
297
+ )
298
+ const button = screen.getByRole('button')
299
+
300
+ expect(button).toBeInTheDocument()
301
+ expect(button).toHaveAttribute('type', 'submit')
302
+ })
303
+
304
+ it('focuses with the focus helper', () => {
305
+ const onFocus = jest.fn()
306
+ render(<Button onFocus={onFocus}>Hello World</Button>)
307
+
308
+ const button = screen.getByRole('button')
309
+
310
+ button.focus()
311
+
312
+ expect(onFocus).toHaveBeenCalled()
313
+ expect(document.activeElement).toBe(button)
314
+ })
315
+
316
+ describe('onClick', () => {
317
+ it('should call onClick when clicked', async () => {
318
+ const onClick = jest.fn()
319
+ render(<Button onClick={onClick}>Hello World</Button>)
320
+
321
+ const button = screen.getByRole('button', { name: 'Hello World' })
322
+
323
+ await userEvent.click(button)
324
+
325
+ await waitFor(() => {
326
+ expect(onClick).toHaveBeenCalled()
327
+ })
328
+ })
329
+
330
+ it('should not call onClick when button is disabled', async () => {
331
+ const onClick = jest.fn()
332
+
333
+ render(
334
+ <Button disabled onClick={onClick}>
335
+ Hello World
336
+ </Button>
337
+ )
338
+ const button = screen.getByRole('button', { name: 'Hello World' })
339
+
340
+ await userEvent.click(button)
341
+
342
+ await waitFor(() => {
343
+ expect(onClick).not.toHaveBeenCalled()
344
+ })
345
+ })
346
+
347
+ it('should not call onClick when button is readOnly', async () => {
348
+ const onClick = jest.fn()
349
+
350
+ render(
351
+ <Button readOnly onClick={onClick}>
352
+ Hello World
353
+ </Button>
354
+ )
355
+ const button = screen.getByRole('button', { name: 'Hello World' })
356
+
357
+ await userEvent.click(button)
358
+
359
+ await waitFor(() => {
360
+ expect(onClick).not.toHaveBeenCalled()
361
+ })
362
+ })
363
+
364
+ it('should not call onClick when button is disabled and an href prop is provided', async () => {
365
+ const onClick = jest.fn()
366
+
367
+ render(<Button href="#">Hello World</Button>)
368
+
369
+ const button = screen.getByRole('link', { name: 'Hello World' })
370
+
371
+ await userEvent.click(button)
372
+
373
+ await waitFor(() => {
374
+ expect(onClick).not.toHaveBeenCalled()
375
+ })
376
+ })
377
+
378
+ it('should not call onClick when button is readOnly and an href prop is provided', async () => {
379
+ const onClick = jest.fn()
380
+
381
+ render(
382
+ <Button readOnly onClick={onClick} href="#">
383
+ Hello World
384
+ </Button>
385
+ )
386
+ const button = screen.getByRole('link', { name: 'Hello World' })
387
+
388
+ await userEvent.click(button)
389
+
390
+ await waitFor(() => {
391
+ expect(onClick).not.toHaveBeenCalled()
392
+ })
393
+ })
394
+
395
+ it('should call onClick when space key is pressed if href is provided', async () => {
396
+ const onClick = jest.fn()
397
+
398
+ render(
399
+ <Button onClick={onClick} href="#">
400
+ Hello World
401
+ </Button>
402
+ )
403
+ const button = screen.getByRole('link', { name: 'Hello World' })
404
+
405
+ await userEvent.type(button, '{space}')
406
+
407
+ await waitFor(() => {
408
+ expect(onClick).toHaveBeenCalled()
409
+ })
410
+ })
411
+
412
+ it('should call onClick when enter key is pressed when not a button or link', async () => {
413
+ const onClick = jest.fn()
414
+
415
+ render(
416
+ <Button as="span" onClick={onClick}>
417
+ Hello World
418
+ </Button>
419
+ )
420
+ const button = screen.getByRole('button', { name: 'Hello World' })
421
+
422
+ await userEvent.type(button, '{enter}')
423
+
424
+ await waitFor(() => {
425
+ expect(onClick).toHaveBeenCalled()
426
+ })
427
+ })
428
+
429
+ it('should not call onClick when button is disabled and space key is pressed', async () => {
430
+ const onClick = jest.fn()
431
+
432
+ render(
433
+ <Button disabled onClick={onClick} href="#">
434
+ Hello World
435
+ </Button>
436
+ )
437
+ const button = screen.getByRole('link', { name: 'Hello World' })
438
+
439
+ await userEvent.type(button, '{spaec}')
440
+
441
+ await waitFor(() => {
442
+ expect(onClick).not.toHaveBeenCalled()
443
+ })
444
+ })
445
+
446
+ it('should not call onClick when button is readOnly and space key is pressed', async () => {
447
+ const onClick = jest.fn()
448
+
449
+ render(
450
+ <Button readOnly onClick={onClick} href="#">
451
+ Hello World
452
+ </Button>
453
+ )
454
+ const button = screen.getByRole('link', { name: 'Hello World' })
455
+
456
+ await userEvent.type(button, '{space}')
457
+
458
+ await waitFor(() => {
459
+ expect(onClick).not.toHaveBeenCalled()
460
+ })
461
+ })
462
+ })
463
+
464
+ describe('for a11y', () => {
465
+ it('should meet standards when onClick is given', async () => {
466
+ const onClick = jest.fn()
467
+ render(<Button onClick={onClick}>Hello World</Button>)
468
+
469
+ const button = screen.getByRole('button', { name: 'Hello World' })
470
+
471
+ await userEvent.click(button)
472
+
473
+ await waitFor(async () => {
474
+ const axeCheck = await runAxeCheck(button)
475
+
476
+ expect(axeCheck).toBe(true)
477
+ })
478
+ })
479
+
480
+ describe('when disabled', () => {
481
+ it('sets the disabled attribute so that the button is not in tab order', () => {
482
+ render(<Button disabled>Hello World</Button>)
483
+
484
+ const button = screen.getByRole('button', { name: 'Hello World' })
485
+
486
+ expect(button).toHaveAttribute('disabled')
487
+ })
488
+ })
489
+
490
+ describe('when readOnly', () => {
491
+ it('sets the disabled attribute so that the button is not in tab order', () => {
492
+ render(<Button readOnly>Hello World</Button>)
493
+
494
+ const button = screen.getByRole('button', { name: 'Hello World' })
495
+
496
+ expect(button).toHaveAttribute('disabled')
497
+ })
498
+ })
499
+ })
500
+ })
@@ -0,0 +1,70 @@
1
+ /*
2
+ * The MIT License (MIT)
3
+ *
4
+ * Copyright (c) 2015 - present Instructure, Inc.
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in all
14
+ * copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ * SOFTWARE.
23
+ */
24
+
25
+ import React from 'react'
26
+ import { render, screen, waitFor } from '@testing-library/react'
27
+ import userEvent from '@testing-library/user-event'
28
+ import '@testing-library/jest-dom'
29
+
30
+ import { CloseButton } from '../index'
31
+
32
+ describe('<CloseButton />', () => {
33
+ let consoleWarningMock: jest.SpyInstance
34
+
35
+ beforeEach(() => {
36
+ // Mocking console to prevent test output pollution
37
+ consoleWarningMock = jest.spyOn(console, 'warn').mockImplementation()
38
+ })
39
+
40
+ afterEach(() => {
41
+ consoleWarningMock.mockRestore()
42
+ })
43
+
44
+ it('should render with x icon', async () => {
45
+ render(<CloseButton screenReaderLabel="Close" />)
46
+
47
+ const button = screen.getByRole('button')
48
+
49
+ const icon = document.querySelector('svg')
50
+
51
+ expect(icon).toBeInTheDocument()
52
+ expect(icon).toHaveAttribute('name', 'IconX')
53
+ expect(button).toBeInTheDocument()
54
+ expect(button).toHaveTextContent('Close')
55
+ })
56
+
57
+ it('should pass the `onClick` prop', async () => {
58
+ const onClick = jest.fn()
59
+
60
+ render(<CloseButton onClick={onClick} screenReaderLabel="Hello" />)
61
+
62
+ const button = screen.getByRole('button')
63
+
64
+ await userEvent.click(button)
65
+
66
+ await waitFor(() => {
67
+ expect(onClick).toHaveBeenCalledTimes(1)
68
+ })
69
+ })
70
+ })