1
1
import React from "react" ;
2
- import { render } from "@testing-library/react" ;
2
+ import { render , fireEvent } from "@testing-library/react" ;
3
3
4
4
import Button from "../Button" ;
5
5
import {
@@ -16,6 +16,7 @@ const themes: ButtonTheme[] = [
16
16
"secondary" ,
17
17
"warning" ,
18
18
"error" ,
19
+ "disabled" ,
19
20
] ;
20
21
const themeTypes : ButtonThemeType [ ] = [ "flat" , "contained" , "outline" ] ;
21
22
const buttonTypes : ButtonType [ ] = [ "text" , "icon" ] ;
@@ -98,4 +99,110 @@ describe("Button", () => {
98
99
expect ( button . className ) . toContain ( "rmd-button--contained" ) ;
99
100
expect ( button . className ) . toContain ( "rmd-button--primary" ) ;
100
101
} ) ;
102
+
103
+ it ( "should correctly pass the event handlers down and fire them as expected" , ( ) => {
104
+ const onClick = jest . fn ( ) ;
105
+ const onKeyUp = jest . fn ( ) ;
106
+ const onKeyDown = jest . fn ( ) ;
107
+ const onMouseUp = jest . fn ( ) ;
108
+ const onMouseDown = jest . fn ( ) ;
109
+ const onMouseLeave = jest . fn ( ) ;
110
+ const onTouchStart = jest . fn ( ) ;
111
+ const onTouchMove = jest . fn ( ) ;
112
+ const onTouchEnd = jest . fn ( ) ;
113
+ const handlers = {
114
+ onClick,
115
+ onKeyUp,
116
+ onKeyDown,
117
+ onMouseUp,
118
+ onMouseDown,
119
+ onMouseLeave,
120
+ onTouchStart,
121
+ onTouchMove,
122
+ onTouchEnd,
123
+ } ;
124
+
125
+ const { getByRole } = render (
126
+ < Button
127
+ { ...handlers }
128
+ disableRipple
129
+ disablePressedFallback
130
+ enablePressedAndRipple = { false }
131
+ >
132
+ Button
133
+ </ Button >
134
+ ) ;
135
+ const button = getByRole ( "button" ) ;
136
+
137
+ fireEvent . keyDown ( button ) ;
138
+ fireEvent . keyUp ( button ) ;
139
+ fireEvent . mouseDown ( button ) ;
140
+ fireEvent . mouseUp ( button ) ;
141
+ fireEvent . mouseLeave ( button ) ;
142
+ fireEvent . click ( button ) ;
143
+ fireEvent . touchStart ( button ) ;
144
+ fireEvent . touchMove ( button ) ;
145
+ fireEvent . touchEnd ( button ) ;
146
+
147
+ expect ( onClick ) . toBeCalled ( ) ;
148
+ expect ( onKeyUp ) . toBeCalled ( ) ;
149
+ expect ( onKeyDown ) . toBeCalled ( ) ;
150
+ expect ( onMouseUp ) . toBeCalled ( ) ;
151
+ expect ( onMouseDown ) . toBeCalled ( ) ;
152
+ expect ( onMouseLeave ) . toBeCalled ( ) ;
153
+ expect ( onTouchStart ) . toBeCalled ( ) ;
154
+ expect ( onTouchMove ) . toBeCalled ( ) ;
155
+ expect ( onTouchEnd ) . toBeCalled ( ) ;
156
+ } ) ;
157
+
158
+ it ( "should not allow for any of the interaction state handlers to be called while the theme is disabled" , ( ) => {
159
+ const onClick = jest . fn ( ) ;
160
+ const onKeyUp = jest . fn ( ) ;
161
+ const onKeyDown = jest . fn ( ) ;
162
+ const onMouseUp = jest . fn ( ) ;
163
+ const onMouseDown = jest . fn ( ) ;
164
+ const onMouseLeave = jest . fn ( ) ;
165
+ const onTouchStart = jest . fn ( ) ;
166
+ const onTouchMove = jest . fn ( ) ;
167
+ const onTouchEnd = jest . fn ( ) ;
168
+ const handlers = {
169
+ onClick,
170
+ onKeyUp,
171
+ onKeyDown,
172
+ onMouseUp,
173
+ onMouseDown,
174
+ onMouseLeave,
175
+ onTouchStart,
176
+ onTouchMove,
177
+ onTouchEnd,
178
+ } ;
179
+
180
+ const { getByRole } = render (
181
+ < Button { ...handlers } theme = "disabled" >
182
+ Button
183
+ </ Button >
184
+ ) ;
185
+ const button = getByRole ( "button" ) ;
186
+
187
+ fireEvent . keyDown ( button ) ;
188
+ fireEvent . keyUp ( button ) ;
189
+ fireEvent . mouseDown ( button ) ;
190
+ fireEvent . mouseUp ( button ) ;
191
+ fireEvent . click ( button ) ;
192
+ fireEvent . mouseLeave ( button ) ;
193
+ fireEvent . touchStart ( button ) ;
194
+ fireEvent . touchMove ( button ) ;
195
+ fireEvent . touchEnd ( button ) ;
196
+
197
+ expect ( onClick ) . not . toBeCalled ( ) ;
198
+ expect ( onKeyUp ) . not . toBeCalled ( ) ;
199
+ expect ( onKeyDown ) . not . toBeCalled ( ) ;
200
+ expect ( onMouseUp ) . not . toBeCalled ( ) ;
201
+ expect ( onMouseDown ) . not . toBeCalled ( ) ;
202
+ expect ( onMouseLeave ) . not . toBeCalled ( ) ;
203
+ expect ( onTouchStart ) . not . toBeCalled ( ) ;
204
+ expect ( onTouchMove ) . not . toBeCalled ( ) ;
205
+ expect ( onTouchEnd ) . not . toBeCalled ( ) ;
206
+ expect ( button ) . toHaveAttribute ( "aria-disabled" , "true" ) ;
207
+ } ) ;
101
208
} ) ;
0 commit comments