@@ -6,22 +6,64 @@ import userEvent from "../src";
6
6
afterEach ( cleanup ) ;
7
7
8
8
describe ( "fireEvent.click" , ( ) => {
9
- it ( "should fire the correct events for <input>" , ( ) => {
9
+ it . each ( [ "input" , "textarea" ] ) (
10
+ "should fire the correct events for <%s>" ,
11
+ type => {
12
+ const onMouseOver = jest . fn ( ) ;
13
+ const onMouseMove = jest . fn ( ) ;
14
+ const onMouseDown = jest . fn ( ) ;
15
+ const onFocus = jest . fn ( ) ;
16
+ const onMouseUp = jest . fn ( ) ;
17
+ const onClick = jest . fn ( ) ;
18
+ const { getByTestId } = render (
19
+ React . createElement ( type , {
20
+ "data-testid" : "element" ,
21
+ onMouseOver : onMouseOver ,
22
+ onMouseMove : onMouseMove ,
23
+ onMouseDown : onMouseDown ,
24
+ onFocus : onFocus ,
25
+ onMouseUp : onMouseUp ,
26
+ onClick : onClick
27
+ } )
28
+ ) ;
29
+
30
+ expect ( onMouseOver ) . not . toHaveBeenCalled ( ) ;
31
+ expect ( onMouseMove ) . not . toHaveBeenCalled ( ) ;
32
+ expect ( onMouseDown ) . not . toHaveBeenCalled ( ) ;
33
+ expect ( onFocus ) . not . toHaveBeenCalled ( ) ;
34
+ expect ( onMouseUp ) . not . toHaveBeenCalled ( ) ;
35
+ expect ( onClick ) . not . toHaveBeenCalled ( ) ;
36
+
37
+ userEvent . click ( getByTestId ( "element" ) ) ;
38
+
39
+ expect ( onMouseOver ) . toHaveBeenCalledTimes ( 1 ) ;
40
+ expect ( onMouseMove ) . toHaveBeenCalledTimes ( 1 ) ;
41
+ expect ( onMouseDown ) . toHaveBeenCalledTimes ( 1 ) ;
42
+ expect ( onFocus ) . toHaveBeenCalledTimes ( 1 ) ;
43
+ expect ( onMouseUp ) . toHaveBeenCalledTimes ( 1 ) ;
44
+ expect ( onClick ) . toHaveBeenCalledTimes ( 1 ) ;
45
+ }
46
+ ) ;
47
+
48
+ it ( 'should fire the correct events for <input type="checkbox">' , ( ) => {
10
49
const onMouseOver = jest . fn ( ) ;
11
50
const onMouseMove = jest . fn ( ) ;
12
51
const onMouseDown = jest . fn ( ) ;
13
52
const onFocus = jest . fn ( ) ;
14
53
const onMouseUp = jest . fn ( ) ;
15
54
const onClick = jest . fn ( ) ;
55
+ const onChange = jest . fn ( ) ;
16
56
const { getByTestId } = render (
17
57
< input
18
- data-testid = "input"
58
+ data-testid = "element"
59
+ type = "checkbox"
19
60
onMouseOver = { onMouseOver }
20
61
onMouseMove = { onMouseMove }
21
62
onMouseDown = { onMouseDown }
22
63
onFocus = { onFocus }
23
64
onMouseUp = { onMouseUp }
24
65
onClick = { onClick }
66
+ onChange = { onChange }
25
67
/>
26
68
) ;
27
69
@@ -31,15 +73,18 @@ describe("fireEvent.click", () => {
31
73
expect ( onFocus ) . not . toHaveBeenCalled ( ) ;
32
74
expect ( onMouseUp ) . not . toHaveBeenCalled ( ) ;
33
75
expect ( onClick ) . not . toHaveBeenCalled ( ) ;
76
+ expect ( onChange ) . not . toHaveBeenCalled ( ) ;
34
77
35
- userEvent . click ( getByTestId ( "input " ) ) ;
78
+ userEvent . click ( getByTestId ( "element " ) ) ;
36
79
37
80
expect ( onMouseOver ) . toHaveBeenCalledTimes ( 1 ) ;
38
81
expect ( onMouseMove ) . toHaveBeenCalledTimes ( 1 ) ;
39
82
expect ( onMouseDown ) . toHaveBeenCalledTimes ( 1 ) ;
40
- expect ( onFocus ) . toHaveBeenCalledTimes ( 1 ) ;
83
+ expect ( onFocus ) . not . toHaveBeenCalledTimes ( 1 ) ;
41
84
expect ( onMouseUp ) . toHaveBeenCalledTimes ( 1 ) ;
42
85
expect ( onClick ) . toHaveBeenCalledTimes ( 1 ) ;
86
+ expect ( onChange ) . toHaveBeenCalledTimes ( 1 ) ;
87
+ expect ( getByTestId ( "element" ) ) . toHaveProperty ( "checked" , true ) ;
43
88
} ) ;
44
89
45
90
it ( "should fire the correct events for <div>" , ( ) => {
@@ -101,42 +146,51 @@ describe("fireEvent.click", () => {
101
146
expect ( a ) . not . toHaveFocus ( ) ;
102
147
} ) ;
103
148
104
- it ( "gives focus when clicking a <label> with htmlFor" , ( ) => {
105
- const { getByTestId } = render (
106
- < React . Fragment >
107
- < label htmlFor = "input" data-testid = "label" >
108
- Label
109
- </ label >
110
- < input id = "input" data-testid = "input" />
111
- </ React . Fragment >
112
- ) ;
113
- userEvent . click ( getByTestId ( "label" ) ) ;
114
- expect ( getByTestId ( "input" ) ) . toHaveFocus ( ) ;
115
- } ) ;
116
-
117
- it ( "gives focus when clicking a <label> without htmlFor" , ( ) => {
118
- const { getByTestId } = render (
119
- < React . Fragment >
120
- < label data-testid = "label" >
121
- Label
122
- < input data-testid = "input" />
123
- </ label >
124
- </ React . Fragment >
125
- ) ;
126
- userEvent . click ( getByTestId ( "label" ) ) ;
127
- expect ( getByTestId ( "input" ) ) . toHaveFocus ( ) ;
128
- } ) ;
129
-
130
- it ( "gives focus when clicking on an element contained within a <label>" , ( ) => {
131
- const { getByText, getByTestId } = render (
132
- < React . Fragment >
133
- < label htmlFor = "input" data-testid = "label" >
134
- < span > Label</ span >
135
- </ label >
136
- < input id = "input" data-testid = "input" />
137
- </ React . Fragment >
138
- ) ;
139
- userEvent . click ( getByText ( "Label" ) ) ;
140
- //expect(getByTestId('input')).toHaveFocus()
141
- } ) ;
149
+ it . each ( [ "input" , "textarea" ] ) (
150
+ "gives focus to <%s> when clicking a <label> with htmlFor" ,
151
+ type => {
152
+ const { getByTestId } = render (
153
+ < React . Fragment >
154
+ < label htmlFor = "input" data-testid = "label" >
155
+ Label
156
+ </ label >
157
+ { React . createElement ( type , { id : "input" , "data-testid" : "input" } ) }
158
+ </ React . Fragment >
159
+ ) ;
160
+ userEvent . click ( getByTestId ( "label" ) ) ;
161
+ expect ( getByTestId ( "input" ) ) . toHaveFocus ( ) ;
162
+ }
163
+ ) ;
164
+
165
+ it . each ( [ "input" , "textarea" ] ) (
166
+ "gives focus to <%s> when clicking a <label> without htmlFor" ,
167
+ type => {
168
+ const { getByTestId } = render (
169
+ < React . Fragment >
170
+ < label data-testid = "label" >
171
+ Label
172
+ { React . createElement ( type , { "data-testid" : "input" } ) }
173
+ </ label >
174
+ </ React . Fragment >
175
+ ) ;
176
+ userEvent . click ( getByTestId ( "label" ) ) ;
177
+ expect ( getByTestId ( "input" ) ) . toHaveFocus ( ) ;
178
+ }
179
+ ) ;
180
+
181
+ it . each ( [ "input" , "textarea" ] ) (
182
+ "gives focus to <%s> when clicking on an element contained within a <label>" ,
183
+ type => {
184
+ const { getByText, getByTestId } = render (
185
+ < React . Fragment >
186
+ < label htmlFor = "input" data-testid = "label" >
187
+ < span > Label</ span >
188
+ </ label >
189
+ { React . createElement ( type , { id : "input" , "data-testid" : "input" } ) }
190
+ </ React . Fragment >
191
+ ) ;
192
+ userEvent . click ( getByText ( "Label" ) ) ;
193
+ expect ( getByTestId ( "input" ) ) . toHaveFocus ( ) ;
194
+ }
195
+ ) ;
142
196
} ) ;
0 commit comments