3
3
import { h } from 'preact' ;
4
4
import { shallow } from 'enzyme' ;
5
5
import { render , fireEvent } from '@testing-library/preact' ;
6
- import RangeInput from '../RangeInput' ;
6
+ import RangeInput , { RangeInputProps } from '../RangeInput' ;
7
+ import { ReactElementLike } from 'prop-types' ;
7
8
8
9
describe ( 'RangeInput' , ( ) => {
9
- const defaultProps = {
10
+ const defaultProps : RangeInputProps = {
10
11
min : 0 ,
11
12
max : 500 ,
12
13
step : 1 ,
@@ -31,12 +32,11 @@ describe('RangeInput', () => {
31
32
refine : ( ) => { } ,
32
33
} ;
33
34
34
- const shallowRender = props =>
35
- shallow ( < RangeInput { ...defaultProps } { ...props } /> ) ;
35
+ const shallowRender = ( props ?: Partial < RangeInputProps > ) =>
36
+ shallow ( ( < RangeInput { ...defaultProps } { ...props } /> ) as ReactElementLike ) ;
36
37
37
38
it ( 'expect to render' , ( ) => {
38
- const props = { } ;
39
- const component = shallowRender ( props ) ;
39
+ const component = shallowRender ( ) ;
40
40
41
41
expect ( component ) . toMatchSnapshot ( ) ;
42
42
} ) ;
@@ -125,7 +125,9 @@ describe('RangeInput', () => {
125
125
it ( 'expect to update the state when min change' , ( ) => {
126
126
const props = { } ;
127
127
const { container } = render ( < RangeInput { ...defaultProps } { ...props } /> ) ;
128
- const [ minInput ] = container . querySelectorAll ( 'input[type="number"]' ) ;
128
+ const [ minInput ] = container . querySelectorAll < HTMLInputElement > (
129
+ 'input[type="number"]'
130
+ ) ;
129
131
130
132
fireEvent . input ( minInput , { target : { value : 20 } } ) ;
131
133
@@ -135,7 +137,9 @@ describe('RangeInput', () => {
135
137
it ( 'expect to update the state when max change' , ( ) => {
136
138
const props = { } ;
137
139
const { container } = render ( < RangeInput { ...defaultProps } { ...props } /> ) ;
138
- const [ , maxInput ] = container . querySelectorAll ( 'input[type="number"]' ) ;
140
+ const [ , maxInput ] = container . querySelectorAll < HTMLInputElement > (
141
+ 'input[type="number"]'
142
+ ) ;
139
143
140
144
fireEvent . input ( maxInput , { target : { value : 480 } } ) ;
141
145
@@ -150,7 +154,7 @@ describe('RangeInput', () => {
150
154
} ;
151
155
152
156
const { container } = render ( < RangeInput { ...defaultProps } { ...props } /> ) ;
153
- const [ minInput , maxInput ] = container . querySelectorAll (
157
+ const [ minInput , maxInput ] = container . querySelectorAll < HTMLInputElement > (
154
158
'input[type="number"]'
155
159
) ;
156
160
@@ -161,7 +165,11 @@ describe('RangeInput', () => {
161
165
target : { value : 480 } ,
162
166
} ) ;
163
167
164
- fireEvent . submit ( container . querySelector ( 'form' ) ) ;
168
+ const form = container . querySelector ( 'form' ) ;
169
+
170
+ if ( form ) {
171
+ fireEvent . submit ( form ) ;
172
+ }
165
173
166
174
expect ( props . refine ) . toHaveBeenCalledWith ( [ 20 , 480 ] ) ;
167
175
} ) ;
@@ -172,7 +180,7 @@ describe('RangeInput', () => {
172
180
} ;
173
181
174
182
const { container } = render ( < RangeInput { ...defaultProps } { ...props } /> ) ;
175
- const [ minInput , maxInput ] = container . querySelectorAll (
183
+ const [ minInput , maxInput ] = container . querySelectorAll < HTMLInputElement > (
176
184
'input[type="number"]'
177
185
) ;
178
186
@@ -183,7 +191,11 @@ describe('RangeInput', () => {
183
191
target : { value : 480.05 } ,
184
192
} ) ;
185
193
186
- fireEvent . submit ( container . querySelector ( 'form' ) ) ;
194
+ const form = container . querySelector ( 'form' ) ;
195
+
196
+ if ( form ) {
197
+ fireEvent . submit ( form ) ;
198
+ }
187
199
188
200
expect ( props . refine ) . toHaveBeenCalledWith ( [ 20.05 , 480.05 ] ) ;
189
201
} ) ;
@@ -194,13 +206,19 @@ describe('RangeInput', () => {
194
206
} ;
195
207
196
208
const { container } = render ( < RangeInput { ...defaultProps } { ...props } /> ) ;
197
- const [ minInput ] = container . querySelectorAll ( 'input[type="number"]' ) ;
209
+ const [ minInput ] = container . querySelectorAll < HTMLInputElement > (
210
+ 'input[type="number"]'
211
+ ) ;
198
212
199
213
fireEvent . input ( minInput , {
200
214
target : { value : 20 } ,
201
215
} ) ;
202
216
203
- fireEvent . submit ( container . querySelector ( 'form' ) ) ;
217
+ const form = container . querySelector ( 'form' ) ;
218
+
219
+ if ( form ) {
220
+ fireEvent . submit ( form ) ;
221
+ }
204
222
205
223
expect ( props . refine ) . toHaveBeenCalledWith ( [ 20 , undefined ] ) ;
206
224
} ) ;
@@ -211,13 +229,19 @@ describe('RangeInput', () => {
211
229
} ;
212
230
213
231
const { container } = render ( < RangeInput { ...defaultProps } { ...props } /> ) ;
214
- const [ , maxInput ] = container . querySelectorAll ( 'input[type="number"]' ) ;
232
+ const [ , maxInput ] = container . querySelectorAll < HTMLInputElement > (
233
+ 'input[type="number"]'
234
+ ) ;
215
235
216
236
fireEvent . input ( maxInput , {
217
237
target : { value : 480 } ,
218
238
} ) ;
219
239
220
- fireEvent . submit ( container . querySelector ( 'form' ) ) ;
240
+ const form = container . querySelector ( 'form' ) ;
241
+
242
+ if ( form ) {
243
+ fireEvent . submit ( form ) ;
244
+ }
221
245
222
246
expect ( props . refine ) . toHaveBeenCalledWith ( [ undefined , 480 ] ) ;
223
247
} ) ;
@@ -228,7 +252,11 @@ describe('RangeInput', () => {
228
252
} ;
229
253
230
254
const { container } = render ( < RangeInput { ...defaultProps } { ...props } /> ) ;
231
- fireEvent . submit ( container . querySelector ( 'form' ) ) ;
255
+ const form = container . querySelector ( 'form' ) ;
256
+
257
+ if ( form ) {
258
+ fireEvent . submit ( form ) ;
259
+ }
232
260
233
261
expect ( props . refine ) . toHaveBeenCalledWith ( [ undefined , undefined ] ) ;
234
262
} ) ;
0 commit comments