@@ -17,13 +17,30 @@ describe('test ValidatorProvider', () => {
17
17
return 'not passed' ;
18
18
}
19
19
} ) ;
20
- Validator . extend ( 'required' , required )
20
+ Validator . extend ( 'required' , required ) ;
21
+
22
+ Validator . extend ( 'long_wait' , {
23
+ async passed ( ) : Promise < boolean > {
24
+ return new Promise ( ( resolve : ( value : boolean ) => void ) : void => {
25
+ setTimeout ( ( ) => {
26
+ resolve ( true ) ;
27
+ } , 100 ) ;
28
+ } )
29
+ } ,
30
+ message ( ) : string {
31
+ return 'test' ;
32
+ }
33
+ } ) ;
34
+ } ) ;
35
+
36
+ afterEach ( ( ) => {
37
+ jest . useRealTimers ( ) ;
21
38
} ) ;
22
39
23
40
it ( 'should render input' , ( ) => {
24
41
const area = mount < ValidatorArea , ValidatorAreaProps > (
25
42
< ValidatorArea >
26
- < input name = "test" />
43
+ < input name = "test" />
27
44
</ ValidatorArea >
28
45
) ;
29
46
@@ -34,7 +51,7 @@ describe('test ValidatorProvider', () => {
34
51
const area = mount < ValidatorArea , ValidatorAreaProps > (
35
52
< ValidatorArea >
36
53
{ ( ) => (
37
- < input name = "test" />
54
+ < input name = "test" />
38
55
) }
39
56
</ ValidatorArea >
40
57
) ;
@@ -65,8 +82,8 @@ describe('test ValidatorProvider', () => {
65
82
< >
66
83
< > < input /> </ >
67
84
< div >
68
- < input />
69
- < input />
85
+ < input />
86
+ < input />
70
87
< > < input /> </ >
71
88
</ div >
72
89
</ >
@@ -80,7 +97,7 @@ describe('test ValidatorProvider', () => {
80
97
it ( 'should apply rules on blur' , async ( ) => {
81
98
const area = mount < ValidatorArea , ValidatorAreaProps > (
82
99
< ValidatorArea rules = "passes_not" >
83
- < input name = "test" value = "test" />
100
+ < input name = "test" value = "test" />
84
101
</ ValidatorArea >
85
102
) ;
86
103
@@ -92,7 +109,7 @@ describe('test ValidatorProvider', () => {
92
109
it ( 'should not apply rules on blur when non-blurrable element' , ( ) => {
93
110
const area = mount < ValidatorArea , ValidatorAreaProps > (
94
111
< ValidatorArea rules = "passes_not" name = "test" >
95
- < canvas />
112
+ < canvas />
96
113
</ ValidatorArea >
97
114
) ;
98
115
@@ -103,10 +120,10 @@ describe('test ValidatorProvider', () => {
103
120
it ( 'should render error when area dirty' , async ( ) => {
104
121
const area = mount < ValidatorArea , ValidatorAreaProps > (
105
122
< ValidatorArea rules = "passes_not" >
106
- { ( { errors } ) => {
123
+ { ( { errors} ) => {
107
124
return (
108
125
< >
109
- < input name = "test" value = "test" />
126
+ < input name = "test" value = "test" />
110
127
{ ! ! errors . length && < div > { errors [ 0 ] } </ div > }
111
128
</ >
112
129
) ;
@@ -125,14 +142,27 @@ describe('test ValidatorProvider', () => {
125
142
126
143
const area = mount < ValidatorArea , ValidatorAreaProps > (
127
144
< ValidatorArea rules = "passes_not" >
128
- < input name = "test" onBlur = { mockFn } value = "test" />
145
+ < input name = "test" onBlur = { mockFn } value = "test" />
129
146
</ ValidatorArea >
130
147
) ;
131
148
132
149
area . find ( 'input' ) . simulate ( 'blur' ) ;
133
150
expect ( mockFn ) . toBeCalled ( ) ;
134
151
} ) ;
135
152
153
+ it ( 'should call element\'s provided onChange along validator onChange' , ( ) => {
154
+ const mockFn = jest . fn ( ) ;
155
+
156
+ const area = mount < ValidatorArea , ValidatorAreaProps > (
157
+ < ValidatorArea rules = "passes_not" >
158
+ < input name = "test" onChange = { mockFn } value = "test" />
159
+ </ ValidatorArea >
160
+ ) ;
161
+
162
+ area . find ( 'input' ) . simulate ( 'change' ) ;
163
+ expect ( mockFn ) . toBeCalled ( ) ;
164
+ } ) ;
165
+
136
166
it ( 'should get all input refs from the provider' , async ( ) => {
137
167
Validator . extend ( 'test_all' , ( validator : Validator ) => ( {
138
168
passed ( ) : boolean {
@@ -146,15 +176,15 @@ describe('test ValidatorProvider', () => {
146
176
147
177
const provider = mount < ValidatorProvider , ValidatorProviderProps > (
148
178
< ValidatorProvider rules = "test_all" >
149
- { ( { validate } : ProviderScope ) => (
179
+ { ( { validate} : ProviderScope ) => (
150
180
< >
151
181
< ValidatorArea name = "test1" >
152
- < input value = "test" />
182
+ < input value = "test" />
153
183
</ ValidatorArea >
154
184
< ValidatorArea >
155
- < input value = "test" name = "test2" />
185
+ < input value = "test" name = "test2" />
156
186
</ ValidatorArea >
157
- < button onClick = { ( ) => validate ( mockFn ) } />
187
+ < button onClick = { ( ) => validate ( mockFn ) } />
158
188
</ >
159
189
) }
160
190
</ ValidatorProvider >
@@ -169,7 +199,7 @@ describe('test ValidatorProvider', () => {
169
199
Validator . extend ( 'test_specific' , ( validator : Validator ) => ( {
170
200
passed ( ) : boolean {
171
201
return validator . refs ( 'test1' ) . length === 2
172
- && validator . refs ( 'test2' ) . length === 1 ;
202
+ && validator . refs ( 'test2' ) . length === 1 ;
173
203
} ,
174
204
message ( ) : string {
175
205
return 'test' ;
@@ -179,16 +209,16 @@ describe('test ValidatorProvider', () => {
179
209
180
210
const provider = mount < ValidatorProvider , ValidatorProviderProps > (
181
211
< ValidatorProvider rules = "test_specific" >
182
- { ( { validate } : ProviderScope ) => (
212
+ { ( { validate} : ProviderScope ) => (
183
213
< >
184
214
< ValidatorArea name = "test1" >
185
- < input value = "test" />
186
- < input value = "test" />
215
+ < input value = "test" />
216
+ < input value = "test" />
187
217
</ ValidatorArea >
188
218
< ValidatorArea >
189
- < input value = "test" name = "test2" />
219
+ < input value = "test" name = "test2" />
190
220
</ ValidatorArea >
191
- < button onClick = { ( ) => validate ( mockFn ) } />
221
+ < button onClick = { ( ) => validate ( mockFn ) } />
192
222
</ >
193
223
) }
194
224
</ ValidatorProvider >
@@ -212,15 +242,15 @@ describe('test ValidatorProvider', () => {
212
242
213
243
const provider = mount < ValidatorProvider , ValidatorProviderProps > (
214
244
< ValidatorProvider rules = "test_not_existing" >
215
- { ( { validate } : ProviderScope ) => (
245
+ { ( { validate} : ProviderScope ) => (
216
246
< >
217
247
< ValidatorArea name = "test1" >
218
- < input value = "test" />
248
+ < input value = "test" />
219
249
</ ValidatorArea >
220
250
< ValidatorArea >
221
- < input value = "test" name = "test2" />
251
+ < input value = "test" name = "test2" />
222
252
</ ValidatorArea >
223
- < button onClick = { ( ) => validate ( mockFn ) } />
253
+ < button onClick = { ( ) => validate ( mockFn ) } />
224
254
</ >
225
255
) }
226
256
</ ValidatorProvider >
@@ -245,7 +275,7 @@ describe('test ValidatorProvider', () => {
245
275
246
276
const area = mount < ValidatorArea , ValidatorAreaProps > (
247
277
< ValidatorArea rules = "no_other_areas" >
248
- < input name = "test" value = "test" onBlur = { mockFn } />
278
+ < input name = "test" value = "test" onBlur = { mockFn } />
249
279
</ ValidatorArea >
250
280
) ;
251
281
@@ -269,15 +299,15 @@ describe('test ValidatorProvider', () => {
269
299
270
300
const provider = mount < ValidatorProvider , ValidatorProviderProps > (
271
301
< ValidatorProvider rules = "test_types" >
272
- { ( { validate } : ProviderScope ) => (
302
+ { ( { validate} : ProviderScope ) => (
273
303
< >
274
304
< ValidatorArea name = "test1" >
275
- < textarea value = "test" />
305
+ < textarea value = "test" />
276
306
</ ValidatorArea >
277
307
< ValidatorArea >
278
- < input value = "test" name = "test2" />
308
+ < input value = "test" name = "test2" />
279
309
</ ValidatorArea >
280
- < button onClick = { ( ) => validate ( mockFn ) } />
310
+ < button onClick = { ( ) => validate ( mockFn ) } />
281
311
</ >
282
312
) }
283
313
</ ValidatorProvider >
@@ -300,7 +330,7 @@ describe('test ValidatorProvider', () => {
300
330
301
331
const area = mount < ValidatorArea , ValidatorAreaProps > (
302
332
< ValidatorArea validationName = "Foo" rules = "passes_not" >
303
- < input name = "test" value = "test" />
333
+ < input name = "test" value = "test" />
304
334
</ ValidatorArea >
305
335
) ;
306
336
@@ -313,12 +343,86 @@ describe('test ValidatorProvider', () => {
313
343
const logFn = jest . spyOn ( console , 'error' ) ;
314
344
const area = mount (
315
345
< ValidatorArea rules = "min:foo" >
316
- < input name = "test" value = "test" />
346
+ < input name = "test" value = "test" />
317
347
</ ValidatorArea >
318
348
) ;
319
349
320
350
area . find ( 'input' ) . at ( 0 ) . simulate ( 'blur' ) ;
321
351
await tick ( ) ;
322
352
expect ( logFn ) . toHaveBeenCalled ( ) ;
323
- } )
353
+ } ) ;
354
+
355
+ it ( 'should indicate whether the area is valid' , async ( ) => {
356
+ const area = mount (
357
+ < ValidatorArea rules = "required" >
358
+ { ( { valid} ) => (
359
+ < >
360
+ < input name = "test" value = "" />
361
+ < div > { valid ? 'yes' : 'no' } </ div >
362
+ </ >
363
+ ) }
364
+ </ ValidatorArea >
365
+ ) ;
366
+
367
+ area . find ( 'input' ) . at ( 0 ) . simulate ( 'blur' ) ;
368
+ await tick ( ) ;
369
+ expect ( area . find ( 'div' ) . text ( ) ) . toBe ( 'no' ) ;
370
+ } ) ;
371
+
372
+ it ( 'should indicate pending while validation is ongoing' , async ( ) => {
373
+ jest . useFakeTimers ( ) ;
374
+
375
+ const area = mount (
376
+ < ValidatorArea rules = "long_wait" >
377
+ { ( { pending} ) => (
378
+ < >
379
+ < input name = "test" value = "" />
380
+ < div > { pending ? 'yes' : 'no' } </ div >
381
+ </ >
382
+ ) }
383
+ </ ValidatorArea >
384
+ ) ;
385
+
386
+ area . find ( 'input' ) . at ( 0 ) . simulate ( 'blur' ) ;
387
+ jest . advanceTimersByTime ( 90 ) ;
388
+ expect ( area . find ( 'div' ) . text ( ) ) . toBe ( 'yes' ) ;
389
+ await Promise . resolve ( ) ;
390
+ jest . advanceTimersByTime ( 10 ) ;
391
+ await Promise . resolve ( ) ;
392
+ expect ( area . find ( 'div' ) . text ( ) ) . toBe ( 'no' ) ;
393
+ } ) ;
394
+
395
+ it ( 'should indicate dirty when input changed' , async ( ) => {
396
+ const area = mount (
397
+ < ValidatorArea >
398
+ { ( { dirty} ) => (
399
+ < >
400
+ < input name = "test" value = "" />
401
+ < div > { dirty ? 'yes' : 'no' } </ div >
402
+ </ >
403
+ ) }
404
+ </ ValidatorArea >
405
+ ) ;
406
+
407
+ area . find ( 'input' ) . at ( 0 ) . simulate ( 'change' , { target : { value : 'a' } } ) ;
408
+ await tick ( ) ;
409
+ expect ( area . find ( 'div' ) . text ( ) ) . toBe ( 'yes' )
410
+ } ) ;
411
+
412
+ it ( 'should indicate touched when input blurred' , async ( ) => {
413
+ const area = mount (
414
+ < ValidatorArea rules = "long_wait" >
415
+ { ( { touched} ) => (
416
+ < >
417
+ < input name = "test" value = "" />
418
+ < div > { touched ? 'yes' : 'no' } </ div >
419
+ </ >
420
+ ) }
421
+ </ ValidatorArea >
422
+ ) ;
423
+
424
+ area . find ( 'input' ) . at ( 0 ) . simulate ( 'blur' ) ;
425
+ await tick ( ) ;
426
+ expect ( area . find ( 'div' ) . text ( ) ) . toBe ( 'yes' )
427
+ } ) ;
324
428
} )
0 commit comments