1
- import { act } from 'react-dom/test-utils'
2
1
import React from 'react'
2
+ import { fireEvent , screen , within } from '@testing-library/react'
3
+ import { renderAndCheckA11Y } from '@hazelcast/test-helpers'
3
4
4
- import { mountAndCheckA11Y } from '@hazelcast/test-helpers'
5
-
6
- import { AlertCircle , AlertTriangle , CheckCircle , Clipboard , Info } from 'react-feather'
7
- import { ToastIconDescriptor , ToastType } from '../src/Toast'
5
+ import { Clipboard } from 'react-feather'
6
+ import { ToastType } from '../src/Toast'
8
7
import { Alert , AlertActionButton , AlertActionLink } from '../src/Alert'
9
8
10
9
import styles from '../src/Alert.module.scss'
10
+ import userEvent from '@testing-library/user-event'
11
11
12
12
const title = 'Alert Title'
13
13
const content = 'Alert Content'
@@ -24,98 +24,54 @@ const AlertAction2: AlertActionLink = {
24
24
text : 'Link' ,
25
25
href : '#' ,
26
26
target : '_blank' ,
27
- rel : [ 'nofollow' ] ,
27
+ rel : 'nofollow' ,
28
28
}
29
29
30
30
describe ( 'Alert' , ( ) => {
31
- const alertBasicTestData : [ ToastType , ToastIconDescriptor , string ] [ ] = [
32
- [
33
- 'success' ,
34
- {
35
- icon : CheckCircle ,
36
- ariaLabel : 'Check circle icon' ,
37
- } ,
38
- styles . success ,
39
- ] ,
40
- [
41
- 'info' ,
42
- {
43
- icon : Info ,
44
- ariaLabel : 'Info circle icon' ,
45
- } ,
46
- styles . info ,
47
- ] ,
48
- [
49
- 'warning' ,
50
- {
51
- icon : AlertTriangle ,
52
- ariaLabel : 'Warning triangle icon' ,
53
- } ,
54
- styles . warning ,
55
- ] ,
56
- [
57
- 'critical' ,
58
- {
59
- icon : AlertCircle ,
60
- ariaLabel : 'Info critical circle icon' ,
61
- } ,
62
- styles . critical ,
63
- ] ,
31
+ const alertBasicTestData : [ ToastType , string , string ] [ ] = [
32
+ [ 'success' , 'Check circle icon' , styles . success ] ,
33
+ [ 'info' , 'Info circle icon' , styles . info ] ,
34
+ [ 'warning' , 'Warning triangle icon' , styles . warning ] ,
35
+ [ 'critical' , 'Info critical circle icon' , styles . critical ] ,
64
36
]
65
37
66
- it . each ( alertBasicTestData ) (
67
- 'Renders %s Alert with correct className, icon, title and content' ,
68
- async ( type , { icon, ariaLabel } , className ) => {
69
- const wrapper = await mountAndCheckA11Y ( < Alert type = { type } title = { title } content = { content } closeToast = { noOp } /> )
70
-
71
- const AlertElement = wrapper . find ( Alert )
72
-
73
- expect ( AlertElement . findDataTest ( 'alert' ) . prop ( 'className' ) ) . toBe ( `alert ${ className } ` )
74
- expect ( AlertElement . findDataTest ( 'alert-title' ) . text ( ) ) . toBe ( title )
75
- expect ( AlertElement . findDataTest ( 'alert-content' ) . props ( ) ) . toMatchObject ( {
76
- children : content ,
77
- } )
78
- expect ( wrapper . findDataTestFirst ( 'alert-icon' ) . props ( ) ) . toMatchObject ( {
79
- icon,
80
- ariaLabel,
81
- } )
82
- expect ( wrapper . findDataTest ( 'alert-close' ) . exists ( ) ) . toBeTruthy ( )
83
- expect ( wrapper . findDataTest ( 'alert-actions' ) . exists ( ) ) . toBeFalsy ( )
84
- } ,
85
- )
38
+ it . each ( alertBasicTestData ) ( 'Renders %s Alert with correct className, icon, title and content' , async ( type , ariaLabel , className ) => {
39
+ await renderAndCheckA11Y ( < Alert type = { type } title = { title } content = { content } closeToast = { noOp } /> )
40
+
41
+ expect ( screen . getByTestId ( 'alert' ) ) . toHaveClass ( `alert ${ className } ` )
42
+ expect ( screen . getByTestId ( 'alert-title' ) ) . toHaveTextContent ( title )
43
+ expect ( screen . queryByText ( content ) ) . toBeInTheDocument ( )
44
+ expect ( within ( screen . getByTestId ( 'alert-icon' ) ) . queryByLabelText ( ariaLabel ) ) . toBeInTheDocument ( )
45
+ expect ( screen . queryByTestId ( 'alert-close' ) ) . toBeInTheDocument ( )
46
+ expect ( screen . queryByTestId ( 'alert-actions' ) ) . not . toBeInTheDocument ( )
47
+ } )
86
48
87
49
it ( 'Correct props are passed to an underlying Link Action component' , async ( ) => {
88
- const wrapper = await mountAndCheckA11Y ( < Alert type = "success" title = { title } content = { content } actions = { [ AlertAction2 ] } /> )
50
+ await renderAndCheckA11Y ( < Alert type = "success" title = { title } content = { content } actions = { [ AlertAction2 ] } /> )
89
51
90
- expect ( wrapper . findDataTest ( 'alert-actions' ) . children ( ) . props ( ) ) . toMatchObject ( {
91
- href : AlertAction2 . href ,
92
- target : AlertAction2 . target ,
93
- rel : AlertAction2 . rel ,
94
- } )
52
+ const link = screen . getByTestId ( 'alert-actions' ) . querySelector ( 'a' )
53
+ expect ( link ) . toBeInTheDocument ( )
54
+ expect ( link ) . toHaveAttribute ( 'href' , AlertAction2 . href )
55
+ expect ( link ) . toHaveAttribute ( ' rel' , AlertAction2 . rel )
56
+ expect ( link ) . toHaveAttribute ( 'target' , AlertAction2 . target )
95
57
} )
96
58
97
59
it ( 'Correct props are passed to an underlying Button Action component' , async ( ) => {
98
- const wrapper = await mountAndCheckA11Y ( < Alert type = "success" title = { title } content = { content } actions = { [ AlertAction1 ] } /> )
60
+ await renderAndCheckA11Y ( < Alert type = "success" title = { title } content = { content } actions = { [ AlertAction1 ] } /> )
99
61
100
- expect ( wrapper . findDataTest ( 'alert-actions' ) . children ( ) . props ( ) ) . toMatchObject ( {
101
- children : AlertAction1 . text ,
102
- onClick : AlertAction1 . onClick ,
103
- iconLeft : AlertAction1 . icon ,
104
- iconLeftAriaLabel : AlertAction1 . ariaLabel ,
105
- } )
62
+ const actions = screen . getByTestId ( 'alert-actions' )
63
+ expect ( within ( actions ) . queryByText ( AlertAction1 . text ) ) . toBeInTheDocument ( )
64
+ expect ( within ( actions ) . queryByLabelText ( AlertAction1 . ariaLabel ) ) . toBeInTheDocument ( )
106
65
} )
107
66
108
67
it ( 'Close button calls closeToast prop handler' , async ( ) => {
109
68
const closeToast = jest . fn ( )
110
69
111
- const wrapper = await mountAndCheckA11Y ( < Alert type = "success" title = { title } content = { content } closeToast = { closeToast } /> )
70
+ await renderAndCheckA11Y ( < Alert type = "success" title = { title } content = { content } closeToast = { closeToast } /> )
112
71
113
72
expect ( closeToast ) . toHaveBeenCalledTimes ( 0 )
114
73
115
- act ( ( ) => {
116
- wrapper . findDataTest ( 'alert-close' ) . at ( 1 ) . simulate ( 'click' )
117
- } )
118
- wrapper . update ( )
74
+ await userEvent . click ( screen . getByTestId ( 'alert-close' ) )
119
75
120
76
expect ( closeToast ) . toHaveBeenCalledTimes ( 1 )
121
77
} )
@@ -126,52 +82,39 @@ describe('Alert', () => {
126
82
]
127
83
128
84
it . each ( alertActionsTestData ) ( 'Renders correct text properties for Alert with %s' , async ( _ , actions ) => {
129
- const wrapper = await mountAndCheckA11Y ( < Alert type = "success" title = { title } content = { content } closeToast = { noOp } actions = { actions } /> )
130
-
131
- wrapper
132
- . findDataTest ( 'alert-actions' )
133
- . children ( )
134
- . forEach ( ( child , cI ) => {
135
- expect ( child . props ( ) ) . toHaveProperty ( 'children' , actions [ cI ] . text )
136
- } )
85
+ await renderAndCheckA11Y ( < Alert type = "success" title = { title } content = { content } closeToast = { noOp } actions = { actions } /> )
86
+
87
+ actions . forEach ( ( action ) => {
88
+ expect ( screen . queryByText ( action . text ) ) . toBeInTheDocument ( )
89
+ } )
137
90
} )
138
91
139
92
it ( 'Renders only text content with an empty array passed as "actions" property' , async ( ) => {
140
- const wrapper = await mountAndCheckA11Y ( < Alert type = "success" title = { title } content = { content } closeToast = { noOp } actions = { [ ] } /> )
141
- const textContent = wrapper . findDataTest ( 'alert-body' ) . text ( )
142
- expect ( textContent ) . toBe ( content )
93
+ await renderAndCheckA11Y ( < Alert type = "success" title = { title } content = { content } closeToast = { noOp } actions = { [ ] } /> )
94
+
95
+ expect ( screen . getByTestId ( 'alert-body' ) ) . toHaveTextContent ( content )
143
96
} )
144
97
145
98
it ( 'Alert.closeToast called after simulating Escape key' , async ( ) => {
146
99
const closeToast = jest . fn ( )
147
100
148
- const wrapper = await mountAndCheckA11Y ( < Alert type = "success" title = { title } content = { content } closeToast = { closeToast } /> )
101
+ await renderAndCheckA11Y ( < Alert type = "success" title = { title } content = { content } closeToast = { closeToast } /> )
149
102
150
103
expect ( closeToast ) . toHaveBeenCalledTimes ( 0 )
151
104
152
- act ( ( ) => {
153
- const event = new KeyboardEvent ( 'keydown' , { key : 'Escape' , bubbles : true } )
154
- document . dispatchEvent ( event )
155
- } )
156
- wrapper . update ( )
105
+ fireEvent . keyDown ( document . body , { key : 'Escape' , charCode : 13 } )
157
106
158
107
expect ( closeToast ) . toHaveBeenCalledTimes ( 1 )
159
108
} )
160
109
161
- it ( 'Alert.dismissableByEscKey = false means no Esc key handling' , async ( ) => {
110
+ it ( 'Alert.dismissibleByEscKey = false means no Esc key handling' , async ( ) => {
162
111
const closeToast = jest . fn ( )
163
112
164
- const wrapper = await mountAndCheckA11Y (
165
- < Alert type = "success" dismissableByEscKey = { false } title = { title } content = { content } closeToast = { closeToast } /> ,
166
- )
113
+ await renderAndCheckA11Y ( < Alert type = "success" dismissableByEscKey = { false } title = { title } content = { content } closeToast = { closeToast } /> )
167
114
168
115
expect ( closeToast ) . toHaveBeenCalledTimes ( 0 )
169
116
170
- act ( ( ) => {
171
- const event = new KeyboardEvent ( 'keydown' , { key : 'Escape' , bubbles : true } )
172
- document . dispatchEvent ( event )
173
- } )
174
- wrapper . update ( )
117
+ fireEvent . keyDown ( document . body , { key : 'Escape' , charCode : 13 } )
175
118
176
119
expect ( closeToast ) . toHaveBeenCalledTimes ( 0 )
177
120
} )
0 commit comments