@@ -6,22 +6,64 @@ import userEvent from "../src";
66afterEach ( cleanup ) ;
77
88describe ( "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">' , ( ) => {
1049 const onMouseOver = jest . fn ( ) ;
1150 const onMouseMove = jest . fn ( ) ;
1251 const onMouseDown = jest . fn ( ) ;
1352 const onFocus = jest . fn ( ) ;
1453 const onMouseUp = jest . fn ( ) ;
1554 const onClick = jest . fn ( ) ;
55+ const onChange = jest . fn ( ) ;
1656 const { getByTestId } = render (
1757 < input
18- data-testid = "input"
58+ data-testid = "element"
59+ type = "checkbox"
1960 onMouseOver = { onMouseOver }
2061 onMouseMove = { onMouseMove }
2162 onMouseDown = { onMouseDown }
2263 onFocus = { onFocus }
2364 onMouseUp = { onMouseUp }
2465 onClick = { onClick }
66+ onChange = { onChange }
2567 />
2668 ) ;
2769
@@ -31,15 +73,18 @@ describe("fireEvent.click", () => {
3173 expect ( onFocus ) . not . toHaveBeenCalled ( ) ;
3274 expect ( onMouseUp ) . not . toHaveBeenCalled ( ) ;
3375 expect ( onClick ) . not . toHaveBeenCalled ( ) ;
76+ expect ( onChange ) . not . toHaveBeenCalled ( ) ;
3477
35- userEvent . click ( getByTestId ( "input " ) ) ;
78+ userEvent . click ( getByTestId ( "element " ) ) ;
3679
3780 expect ( onMouseOver ) . toHaveBeenCalledTimes ( 1 ) ;
3881 expect ( onMouseMove ) . toHaveBeenCalledTimes ( 1 ) ;
3982 expect ( onMouseDown ) . toHaveBeenCalledTimes ( 1 ) ;
40- expect ( onFocus ) . toHaveBeenCalledTimes ( 1 ) ;
83+ expect ( onFocus ) . not . toHaveBeenCalledTimes ( 1 ) ;
4184 expect ( onMouseUp ) . toHaveBeenCalledTimes ( 1 ) ;
4285 expect ( onClick ) . toHaveBeenCalledTimes ( 1 ) ;
86+ expect ( onChange ) . toHaveBeenCalledTimes ( 1 ) ;
87+ expect ( getByTestId ( "element" ) ) . toHaveProperty ( "checked" , true ) ;
4388 } ) ;
4489
4590 it ( "should fire the correct events for <div>" , ( ) => {
@@ -101,42 +146,51 @@ describe("fireEvent.click", () => {
101146 expect ( a ) . not . toHaveFocus ( ) ;
102147 } ) ;
103148
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+ ) ;
142196} ) ;
0 commit comments