1
- import React from ' react'
2
- import { render , cleanup } from ' react-testing-library'
3
- import ' jest-dom/extend-expect'
4
- import userEvent from ' ../src'
5
-
6
- afterEach ( cleanup )
7
-
8
- describe ( ' fireEvent.click' , ( ) => {
9
- it ( ' should fire the correct events for <input>' , ( ) => {
10
- const onMouseOver = jest . fn ( )
11
- const onMouseMove = jest . fn ( )
12
- const onMouseDown = jest . fn ( )
13
- const onFocus = jest . fn ( )
14
- const onMouseUp = jest . fn ( )
15
- const onClick = jest . fn ( )
1
+ import React from " react" ;
2
+ import { render , cleanup } from " react-testing-library" ;
3
+ import " jest-dom/extend-expect" ;
4
+ import userEvent from " ../src" ;
5
+
6
+ afterEach ( cleanup ) ;
7
+
8
+ describe ( " fireEvent.click" , ( ) => {
9
+ it ( " should fire the correct events for <input>" , ( ) => {
10
+ const onMouseOver = jest . fn ( ) ;
11
+ const onMouseMove = jest . fn ( ) ;
12
+ const onMouseDown = jest . fn ( ) ;
13
+ const onFocus = jest . fn ( ) ;
14
+ const onMouseUp = jest . fn ( ) ;
15
+ const onClick = jest . fn ( ) ;
16
16
const { getByTestId } = render (
17
17
< input
18
18
data-testid = "input"
@@ -23,32 +23,32 @@ describe('fireEvent.click', () => {
23
23
onMouseUp = { onMouseUp }
24
24
onClick = { onClick }
25
25
/>
26
- )
27
-
28
- expect ( onMouseOver ) . not . toHaveBeenCalled ( )
29
- expect ( onMouseMove ) . not . toHaveBeenCalled ( )
30
- expect ( onMouseDown ) . not . toHaveBeenCalled ( )
31
- expect ( onFocus ) . not . toHaveBeenCalled ( )
32
- expect ( onMouseUp ) . not . toHaveBeenCalled ( )
33
- expect ( onClick ) . not . toHaveBeenCalled ( )
34
-
35
- userEvent . click ( getByTestId ( ' input' ) )
36
-
37
- expect ( onMouseOver ) . toHaveBeenCalledTimes ( 1 )
38
- expect ( onMouseMove ) . toHaveBeenCalledTimes ( 1 )
39
- expect ( onMouseDown ) . toHaveBeenCalledTimes ( 1 )
40
- expect ( onFocus ) . toHaveBeenCalledTimes ( 1 )
41
- expect ( onMouseUp ) . toHaveBeenCalledTimes ( 1 )
42
- expect ( onClick ) . toHaveBeenCalledTimes ( 1 )
43
- } )
44
-
45
- it ( ' should fire the correct events for <div>' , ( ) => {
46
- const onMouseOver = jest . fn ( )
47
- const onMouseMove = jest . fn ( )
48
- const onMouseDown = jest . fn ( )
49
- const onFocus = jest . fn ( )
50
- const onMouseUp = jest . fn ( )
51
- const onClick = jest . fn ( )
26
+ ) ;
27
+
28
+ expect ( onMouseOver ) . not . toHaveBeenCalled ( ) ;
29
+ expect ( onMouseMove ) . not . toHaveBeenCalled ( ) ;
30
+ expect ( onMouseDown ) . not . toHaveBeenCalled ( ) ;
31
+ expect ( onFocus ) . not . toHaveBeenCalled ( ) ;
32
+ expect ( onMouseUp ) . not . toHaveBeenCalled ( ) ;
33
+ expect ( onClick ) . not . toHaveBeenCalled ( ) ;
34
+
35
+ userEvent . click ( getByTestId ( " input" ) ) ;
36
+
37
+ expect ( onMouseOver ) . toHaveBeenCalledTimes ( 1 ) ;
38
+ expect ( onMouseMove ) . toHaveBeenCalledTimes ( 1 ) ;
39
+ expect ( onMouseDown ) . toHaveBeenCalledTimes ( 1 ) ;
40
+ expect ( onFocus ) . toHaveBeenCalledTimes ( 1 ) ;
41
+ expect ( onMouseUp ) . toHaveBeenCalledTimes ( 1 ) ;
42
+ expect ( onClick ) . toHaveBeenCalledTimes ( 1 ) ;
43
+ } ) ;
44
+
45
+ it ( " should fire the correct events for <div>" , ( ) => {
46
+ const onMouseOver = jest . fn ( ) ;
47
+ const onMouseMove = jest . fn ( ) ;
48
+ const onMouseDown = jest . fn ( ) ;
49
+ const onFocus = jest . fn ( ) ;
50
+ const onMouseUp = jest . fn ( ) ;
51
+ const onClick = jest . fn ( ) ;
52
52
const { getByTestId } = render (
53
53
< div
54
54
data-testid = "div"
@@ -59,84 +59,84 @@ describe('fireEvent.click', () => {
59
59
onMouseUp = { onMouseUp }
60
60
onClick = { onClick }
61
61
/>
62
- )
63
-
64
- expect ( onMouseOver ) . not . toHaveBeenCalled ( )
65
- expect ( onMouseMove ) . not . toHaveBeenCalled ( )
66
- expect ( onMouseDown ) . not . toHaveBeenCalled ( )
67
- expect ( onFocus ) . not . toHaveBeenCalled ( )
68
- expect ( onMouseUp ) . not . toHaveBeenCalled ( )
69
- expect ( onClick ) . not . toHaveBeenCalled ( )
70
-
71
- userEvent . click ( getByTestId ( ' div' ) )
72
-
73
- expect ( onMouseOver ) . toHaveBeenCalledTimes ( 1 )
74
- expect ( onMouseMove ) . toHaveBeenCalledTimes ( 1 )
75
- expect ( onMouseDown ) . toHaveBeenCalledTimes ( 1 )
76
- expect ( onFocus ) . not . toHaveBeenCalled ( )
77
- expect ( onMouseUp ) . toHaveBeenCalledTimes ( 1 )
78
- expect ( onClick ) . toHaveBeenCalledTimes ( 1 )
79
- } )
80
-
81
- it ( ' toggles the focus' , ( ) => {
62
+ ) ;
63
+
64
+ expect ( onMouseOver ) . not . toHaveBeenCalled ( ) ;
65
+ expect ( onMouseMove ) . not . toHaveBeenCalled ( ) ;
66
+ expect ( onMouseDown ) . not . toHaveBeenCalled ( ) ;
67
+ expect ( onFocus ) . not . toHaveBeenCalled ( ) ;
68
+ expect ( onMouseUp ) . not . toHaveBeenCalled ( ) ;
69
+ expect ( onClick ) . not . toHaveBeenCalled ( ) ;
70
+
71
+ userEvent . click ( getByTestId ( " div" ) ) ;
72
+
73
+ expect ( onMouseOver ) . toHaveBeenCalledTimes ( 1 ) ;
74
+ expect ( onMouseMove ) . toHaveBeenCalledTimes ( 1 ) ;
75
+ expect ( onMouseDown ) . toHaveBeenCalledTimes ( 1 ) ;
76
+ expect ( onFocus ) . not . toHaveBeenCalled ( ) ;
77
+ expect ( onMouseUp ) . toHaveBeenCalledTimes ( 1 ) ;
78
+ expect ( onClick ) . toHaveBeenCalledTimes ( 1 ) ;
79
+ } ) ;
80
+
81
+ it ( " toggles the focus" , ( ) => {
82
82
const { getByTestId } = render (
83
83
< React . Fragment >
84
84
< input data-testid = "A" />
85
85
< input data-testid = "B" />
86
86
</ React . Fragment >
87
- )
87
+ ) ;
88
88
89
- const a = getByTestId ( 'A' )
90
- const b = getByTestId ( 'B' )
89
+ const a = getByTestId ( "A" ) ;
90
+ const b = getByTestId ( "B" ) ;
91
91
92
- expect ( a ) . not . toHaveFocus ( )
93
- expect ( b ) . not . toHaveFocus ( )
92
+ expect ( a ) . not . toHaveFocus ( ) ;
93
+ expect ( b ) . not . toHaveFocus ( ) ;
94
94
95
- userEvent . click ( a )
96
- expect ( a ) . toHaveFocus ( )
97
- expect ( b ) . not . toHaveFocus ( )
95
+ userEvent . click ( a ) ;
96
+ expect ( a ) . toHaveFocus ( ) ;
97
+ expect ( b ) . not . toHaveFocus ( ) ;
98
98
99
- userEvent . click ( b )
100
- expect ( a ) . not . toHaveFocus ( )
101
- expect ( a ) . not . toHaveFocus ( )
102
- } )
99
+ userEvent . click ( b ) ;
100
+ expect ( a ) . not . toHaveFocus ( ) ;
101
+ expect ( a ) . not . toHaveFocus ( ) ;
102
+ } ) ;
103
103
104
- it ( ' gives focus when clicking a <label> with htmlFor' , ( ) => {
104
+ it ( " gives focus when clicking a <label> with htmlFor" , ( ) => {
105
105
const { getByTestId } = render (
106
106
< React . Fragment >
107
107
< label htmlFor = "input" data-testid = "label" >
108
108
Label
109
109
</ label >
110
110
< input id = "input" data-testid = "input" />
111
111
</ React . Fragment >
112
- )
113
- userEvent . click ( getByTestId ( ' label' ) )
114
- expect ( getByTestId ( ' input' ) ) . toHaveFocus ( )
115
- } )
112
+ ) ;
113
+ userEvent . click ( getByTestId ( " label" ) ) ;
114
+ expect ( getByTestId ( " input" ) ) . toHaveFocus ( ) ;
115
+ } ) ;
116
116
117
- it ( ' gives focus when clicking a <label> without htmlFor' , ( ) => {
117
+ it ( " gives focus when clicking a <label> without htmlFor" , ( ) => {
118
118
const { getByTestId } = render (
119
119
< React . Fragment >
120
120
< label data-testid = "label" >
121
121
Label
122
122
< input data-testid = "input" />
123
123
</ label >
124
124
</ React . Fragment >
125
- )
126
- userEvent . click ( getByTestId ( ' label' ) )
127
- expect ( getByTestId ( ' input' ) ) . toHaveFocus ( )
128
- } )
125
+ ) ;
126
+ userEvent . click ( getByTestId ( " label" ) ) ;
127
+ expect ( getByTestId ( " input" ) ) . toHaveFocus ( ) ;
128
+ } ) ;
129
129
130
- it ( ' gives focus when clicking on an element contained within a <label>' , ( ) => {
130
+ it ( " gives focus when clicking on an element contained within a <label>" , ( ) => {
131
131
const { getByText, getByTestId } = render (
132
132
< React . Fragment >
133
133
< label htmlFor = "input" data-testid = "label" >
134
134
< span > Label</ span >
135
135
</ label >
136
136
< input id = "input" data-testid = "input" />
137
137
</ React . Fragment >
138
- )
139
- userEvent . click ( getByText ( ' Label' ) )
138
+ ) ;
139
+ userEvent . click ( getByText ( " Label" ) ) ;
140
140
//expect(getByTestId('input')).toHaveFocus()
141
- } )
142
- } )
141
+ } ) ;
142
+ } ) ;
0 commit comments