1
1
import React , { useState } from 'react' ;
2
- import {
3
- act ,
4
- fireEvent ,
5
- render ,
6
- waitForElementToBeRemoved ,
7
- } from '@testing-library/react' ;
2
+ import { act , render , waitFor } from '@testing-library/react' ;
8
3
import userEvent from '@testing-library/user-event' ;
9
4
import { axe } from 'jest-axe' ;
10
5
@@ -42,14 +37,35 @@ function renderModal(
42
37
}
43
38
44
39
describe ( 'packages/confirmation-modal' , ( ) => {
40
+ // Mock dialog methods for JSDOM environment
41
+ beforeAll ( ( ) => {
42
+ HTMLDialogElement . prototype . show = jest . fn ( function mock (
43
+ this : HTMLDialogElement ,
44
+ ) {
45
+ this . open = true ;
46
+ } ) ;
47
+
48
+ HTMLDialogElement . prototype . showModal = jest . fn ( function mock (
49
+ this : HTMLDialogElement ,
50
+ ) {
51
+ this . open = true ;
52
+ } ) ;
53
+
54
+ HTMLDialogElement . prototype . close = jest . fn ( function mock (
55
+ this : HTMLDialogElement ,
56
+ ) {
57
+ this . open = false ;
58
+ } ) ;
59
+ } ) ;
60
+
45
61
describe ( 'a11y' , ( ) => {
46
62
test ( 'does not have basic accessibility issues' , async ( ) => {
47
63
const { container, getByText } = renderModal ( { open : true } ) ;
48
64
const results = await axe ( container ) ;
49
65
expect ( results ) . toHaveNoViolations ( ) ;
50
66
51
67
let newResults = null as any ;
52
- act ( ( ) => void fireEvent . click ( getByText ( 'Confirm' ) ) ) ;
68
+ act ( ( ) => void userEvent . click ( getByText ( 'Confirm' ) ) ) ;
53
69
await act ( async ( ) => {
54
70
newResults = await axe ( container ) ;
55
71
} ) ;
@@ -58,8 +74,8 @@ describe('packages/confirmation-modal', () => {
58
74
} ) ;
59
75
60
76
test ( 'does not render if closed' , ( ) => {
61
- renderModal ( ) ;
62
- expect ( document . body . innerHTML ) . toEqual ( '<div></div>' ) ;
77
+ const { getByText } = renderModal ( ) ;
78
+ expect ( getByText ( 'Content text' ) ) . not . toBeVisible ( ) ;
63
79
} ) ;
64
80
65
81
test ( 'renders if open' , ( ) => {
@@ -122,7 +138,7 @@ describe('packages/confirmation-modal', () => {
122
138
const button = getByText ( 'Confirm' ) ;
123
139
expect ( button ) . toBeVisible ( ) ;
124
140
125
- fireEvent . click ( button ) ;
141
+ userEvent . click ( button ) ;
126
142
expect ( confirmSpy ) . toHaveBeenCalledTimes ( 1 ) ;
127
143
expect ( cancelSpy ) . not . toHaveBeenCalled ( ) ;
128
144
} ) ;
@@ -141,7 +157,7 @@ describe('packages/confirmation-modal', () => {
141
157
const button = getByText ( 'Confirm' ) ;
142
158
expect ( button ) . toBeVisible ( ) ;
143
159
144
- fireEvent . click ( button ) ;
160
+ userEvent . click ( button ) ;
145
161
expect ( confirmSpy ) . toHaveBeenCalledTimes ( 1 ) ;
146
162
} ) ;
147
163
} ) ;
@@ -161,7 +177,7 @@ describe('packages/confirmation-modal', () => {
161
177
const button = getByText ( 'Cancel' ) ;
162
178
expect ( button ) . toBeVisible ( ) ;
163
179
164
- fireEvent . click ( button ) ;
180
+ userEvent . click ( button ) ;
165
181
expect ( confirmSpy ) . not . toHaveBeenCalled ( ) ;
166
182
expect ( cancelSpy ) . toHaveBeenCalledTimes ( 1 ) ;
167
183
} ) ;
@@ -181,7 +197,7 @@ describe('packages/confirmation-modal', () => {
181
197
const button = getByText ( 'Cancel' ) ;
182
198
expect ( button ) . toBeVisible ( ) ;
183
199
184
- fireEvent . click ( button ) ;
200
+ userEvent . click ( button ) ;
185
201
expect ( confirmSpy ) . not . toHaveBeenCalled ( ) ;
186
202
expect ( cancelSpy ) . toHaveBeenCalledTimes ( 1 ) ;
187
203
} ) ;
@@ -192,19 +208,19 @@ describe('packages/confirmation-modal', () => {
192
208
const { getByRole } = renderModal ( { open : true } ) ;
193
209
const modal = getByRole ( 'dialog' ) ;
194
210
195
- fireEvent . keyDown ( document , { key : ' Escape' , keyCode : 27 } ) ;
211
+ userEvent . keyboard ( '{ Escape}' ) ;
196
212
197
- await waitForElementToBeRemoved ( modal ) ;
213
+ await waitFor ( ( ) => expect ( modal ) . not . toBeVisible ( ) ) ;
198
214
} ) ;
199
215
200
216
test ( 'x icon is clicked' , async ( ) => {
201
217
const { getByLabelText, getByRole } = renderModal ( { open : true } ) ;
202
218
const modal = getByRole ( 'dialog' ) ;
203
219
204
220
const x = getByLabelText ( 'Close modal' ) ;
205
- fireEvent . click ( x ) ;
221
+ userEvent . click ( x ) ;
206
222
207
- await waitForElementToBeRemoved ( modal ) ;
223
+ await waitFor ( ( ) => expect ( modal ) . not . toBeVisible ( ) ) ;
208
224
} ) ;
209
225
} ) ;
210
226
@@ -227,18 +243,22 @@ describe('packages/confirmation-modal', () => {
227
243
expect ( textInput ) . toBe ( document . activeElement ) ;
228
244
229
245
// Should still be disabled after partial entry
230
- fireEvent . change ( textInput , { target : { value : 'Confir' } } ) ;
246
+ userEvent . clear ( textInput ) ;
247
+ userEvent . type ( textInput , 'Confir' ) ;
231
248
expect ( confirmationButton ) . toHaveAttribute ( 'aria-disabled' , 'true' ) ;
232
249
233
- fireEvent . change ( textInput , { target : { value : 'Confirm' } } ) ;
250
+ userEvent . clear ( textInput ) ;
251
+ userEvent . type ( textInput , 'Confirm' ) ;
234
252
expect ( confirmationButton ) . not . toHaveAttribute ( 'aria-disabled' , 'true' ) ;
235
253
236
254
// Should be disabled again
237
- fireEvent . change ( textInput , { target : { value : 'Confirm?' } } ) ;
255
+ userEvent . clear ( textInput ) ;
256
+ userEvent . type ( textInput , 'Confirm?' ) ;
238
257
expect ( confirmationButton ) . toHaveAttribute ( 'aria-disabled' , 'true' ) ;
239
258
240
259
// Case matters
241
- fireEvent . change ( textInput , { target : { value : 'confirm' } } ) ;
260
+ userEvent . clear ( textInput ) ;
261
+ userEvent . type ( textInput , 'confirm' ) ;
242
262
expect ( confirmationButton ) . toHaveAttribute ( 'aria-disabled' , 'true' ) ;
243
263
} ) ;
244
264
@@ -287,9 +307,8 @@ describe('packages/confirmation-modal', () => {
287
307
textInput = getByLabelText (
288
308
`Type "${ requiredInputText } " to confirm your action` ,
289
309
) ;
290
- fireEvent . change ( textInput , {
291
- target : { value : requiredInputText } ,
292
- } ) ;
310
+ userEvent . clear ( textInput ) ;
311
+ userEvent . type ( textInput , requiredInputText ) ;
293
312
expect ( confirmationButton ) . not . toHaveAttribute (
294
313
'aria-disabled' ,
295
314
'true' ,
@@ -298,7 +317,7 @@ describe('packages/confirmation-modal', () => {
298
317
299
318
userEvent . click ( buttonToClick ) ;
300
319
301
- await waitForElementToBeRemoved ( modal ) ;
320
+ await waitFor ( ( ) => expect ( modal ) . not . toBeVisible ( ) ) ;
302
321
303
322
rerender (
304
323
< ConfirmationModal
@@ -354,8 +373,8 @@ describe('packages/confirmation-modal', () => {
354
373
expect ( button ) . toBeVisible ( ) ;
355
374
356
375
// Modal doesn't close when button is clicked
357
- fireEvent . click ( button ) ;
358
- await waitForElementToBeRemoved ( modal ) ;
376
+ userEvent . click ( button ) ;
377
+ await waitFor ( ( ) => expect ( modal ) . not . toBeVisible ( ) ) ;
359
378
} ) ;
360
379
361
380
test ( '"confirmButtonProps" has "disabled: false"' , async ( ) => {
@@ -374,8 +393,8 @@ describe('packages/confirmation-modal', () => {
374
393
expect ( button ) . toBeVisible ( ) ;
375
394
376
395
// Modal doesn't close when button is clicked
377
- fireEvent . click ( button ) ;
378
- await waitForElementToBeRemoved ( modal ) ;
396
+ userEvent . click ( button ) ;
397
+ await waitFor ( ( ) => expect ( modal ) . not . toBeVisible ( ) ) ;
379
398
} ) ;
380
399
} ) ;
381
400
@@ -395,7 +414,7 @@ describe('packages/confirmation-modal', () => {
395
414
expect ( button ) . toBeVisible ( ) ;
396
415
397
416
// Modal doesn't close when button is clicked
398
- fireEvent . click ( button ) ;
417
+ userEvent . click ( button ) ;
399
418
expect ( button ) . toBeVisible ( ) ;
400
419
} ) ;
401
420
@@ -413,7 +432,7 @@ describe('packages/confirmation-modal', () => {
413
432
expect ( button ) . toBeVisible ( ) ;
414
433
415
434
// Modal doesn't close when button is clicked
416
- fireEvent . click ( button ) ;
435
+ userEvent . click ( button ) ;
417
436
expect ( button ) . toBeVisible ( ) ;
418
437
} ) ;
419
438
@@ -434,7 +453,7 @@ describe('packages/confirmation-modal', () => {
434
453
expect ( button ) . toBeVisible ( ) ;
435
454
436
455
// Modal doesn't close when button is clicked
437
- fireEvent . click ( button ) ;
456
+ userEvent . click ( button ) ;
438
457
expect ( button ) . toBeVisible ( ) ;
439
458
} ) ;
440
459
@@ -453,7 +472,8 @@ describe('packages/confirmation-modal', () => {
453
472
const textInput = getByLabelText ( 'Type "Confirm" to confirm your action' ) ;
454
473
expect ( textInput ) . toBeVisible ( ) ;
455
474
456
- fireEvent . change ( textInput , { target : { value : 'Confirm' } } ) ;
475
+ userEvent . clear ( textInput ) ;
476
+ userEvent . type ( textInput , 'Confirm' ) ;
457
477
expect ( confirmationButton ) . toHaveAttribute ( 'aria-disabled' , 'true' ) ;
458
478
} ) ;
459
479
@@ -471,7 +491,8 @@ describe('packages/confirmation-modal', () => {
471
491
const textInput = getByLabelText ( 'Type "Confirm" to confirm your action' ) ;
472
492
expect ( textInput ) . toBeVisible ( ) ;
473
493
474
- fireEvent . change ( textInput , { target : { value : 'Confirm' } } ) ;
494
+ userEvent . clear ( textInput ) ;
495
+ userEvent . type ( textInput , 'Confirm' ) ;
475
496
expect ( confirmationButton ) . toHaveAttribute ( 'aria-disabled' , 'true' ) ;
476
497
} ) ;
477
498
} ) ;
0 commit comments