1
1
import { MatDialogModule , MatDialogRef } from '@angular/material/dialog' ;
2
- import { render , screen , fireEvent } from '@testing-library/angular' ;
2
+ import { render , screen } from '@testing-library/angular' ;
3
3
import userEvent from '@testing-library/user-event' ;
4
4
5
5
import { DialogComponent , DialogContentComponent , DialogContentComponentModule } from './15-dialog.component' ;
6
6
7
7
test ( 'dialog closes' , async ( ) => {
8
+ const user = userEvent . setup ( ) ;
9
+
8
10
const closeFn = jest . fn ( ) ;
9
11
await render ( DialogContentComponent , {
10
12
imports : [ MatDialogModule ] ,
@@ -19,28 +21,28 @@ test('dialog closes', async () => {
19
21
} ) ;
20
22
21
23
const cancelButton = await screen . findByRole ( 'button' , { name : / c a n c e l / i } ) ;
22
- userEvent . click ( cancelButton ) ;
24
+ await user . click ( cancelButton ) ;
23
25
24
26
expect ( closeFn ) . toHaveBeenCalledTimes ( 1 ) ;
25
27
} ) ;
26
28
27
29
test ( 'closes the dialog via the backdrop' , async ( ) => {
30
+ const user = userEvent . setup ( ) ;
31
+
28
32
await render ( DialogComponent , {
29
33
imports : [ MatDialogModule , DialogContentComponentModule ] ,
30
34
} ) ;
31
35
32
36
const openDialogButton = await screen . findByRole ( 'button' , { name : / o p e n d i a l o g / i } ) ;
33
- userEvent . click ( openDialogButton ) ;
37
+ await user . click ( openDialogButton ) ;
34
38
35
39
const dialogControl = await screen . findByRole ( 'dialog' ) ;
36
40
expect ( dialogControl ) . toBeInTheDocument ( ) ;
37
41
const dialogTitleControl = await screen . findByRole ( 'heading' , { name : / d i a l o g t i t l e / i } ) ;
38
42
expect ( dialogTitleControl ) . toBeInTheDocument ( ) ;
39
43
40
- // using fireEvent because of:
41
- // unable to click element as it has or inherits pointer-events set to "none"
42
- // eslint-disable-next-line testing-library/no-node-access, @typescript-eslint/no-non-null-assertion
43
- fireEvent . click ( document . querySelector ( '.cdk-overlay-backdrop' ) ! ) ;
44
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion, testing-library/no-node-access
45
+ await user . click ( document . querySelector ( '.cdk-overlay-backdrop' ) ! ) ;
44
46
45
47
expect ( screen . queryByRole ( 'dialog' ) ) . not . toBeInTheDocument ( ) ;
46
48
@@ -49,20 +51,22 @@ test('closes the dialog via the backdrop', async () => {
49
51
} ) ;
50
52
51
53
test ( 'opens and closes the dialog with buttons' , async ( ) => {
54
+ const user = userEvent . setup ( ) ;
55
+
52
56
await render ( DialogComponent , {
53
57
imports : [ MatDialogModule , DialogContentComponentModule ] ,
54
58
} ) ;
55
59
56
60
const openDialogButton = await screen . findByRole ( 'button' , { name : / o p e n d i a l o g / i } ) ;
57
- userEvent . click ( openDialogButton ) ;
61
+ await user . click ( openDialogButton ) ;
58
62
59
63
const dialogControl = await screen . findByRole ( 'dialog' ) ;
60
64
expect ( dialogControl ) . toBeInTheDocument ( ) ;
61
65
const dialogTitleControl = await screen . findByRole ( 'heading' , { name : / d i a l o g t i t l e / i } ) ;
62
66
expect ( dialogTitleControl ) . toBeInTheDocument ( ) ;
63
67
64
68
const cancelButton = await screen . findByRole ( 'button' , { name : / c a n c e l / i } ) ;
65
- userEvent . click ( cancelButton ) ;
69
+ await user . click ( cancelButton ) ;
66
70
67
71
expect ( screen . queryByRole ( 'dialog' ) ) . not . toBeInTheDocument ( ) ;
68
72
0 commit comments