1
1
import { AvBaseInput } from 'availity-reactstrap-validation' ;
2
2
3
3
describe ( 'BaseInput' , function ( ) {
4
+ let touched ;
5
+ let dirty ;
6
+ let bad ;
7
+ let error ;
8
+
4
9
beforeEach ( ( ) => {
10
+ touched = false ;
11
+ dirty = false ;
12
+ bad = false ;
13
+ error = false ;
5
14
this . inputState = 'danger' ;
6
15
this . props = {
7
16
name : 'fieldName' ,
@@ -17,17 +26,17 @@ describe('BaseInput', function () {
17
26
inputs : { } ,
18
27
getDefaultValue : sinon . stub ( ) ,
19
28
getInputState : sinon . stub ( ) . returns ( this . inputState ) ,
20
- hasError : { } ,
21
- isDirty : { } ,
22
- isTouched : { } ,
23
- isBad : { } ,
29
+ hasError : ( ) => error ,
30
+ isDirty : ( ) => dirty ,
31
+ isTouched : ( ) => touched ,
32
+ isBad : ( ) => bad ,
24
33
setDirty : sinon . spy ( ) ,
25
34
setTouched : sinon . spy ( ) ,
26
35
setBad : sinon . spy ( ) ,
27
36
register : sinon . spy ( ) ,
28
37
unregister : sinon . spy ( ) ,
29
38
validate : sinon . spy ( ) ,
30
- validationEvent : 'formCtrlValidationEvent' ,
39
+ getValidationEvent : ( ) => 'formCtrlValidationEvent' ,
31
40
parent : null ,
32
41
} ,
33
42
} ;
@@ -361,17 +370,17 @@ describe('BaseInput', function () {
361
370
} ) ;
362
371
363
372
it ( 'should not call setBadInput if it has not changed' , ( ) => {
364
- this . context . FormCtrl . isBad [ this . props . name ] = true ;
373
+ bad = true ;
365
374
this . component . onKeyUpHandler ( { target : { validity : { badInput : true } } } ) ;
366
375
expect ( this . context . FormCtrl . setBad ) . to . not . have . been . called ;
367
376
} ) ;
368
377
369
378
it ( 'should call setBadInput if it has changed' , ( ) => {
370
- this . context . FormCtrl . isBad [ this . props . name ] = true ;
379
+ bad = true ;
371
380
this . component . onKeyUpHandler ( { target : { validity : { badInput : false } } } ) ;
372
381
expect ( this . context . FormCtrl . setBad ) . to . have . been . calledWith ( this . props . name , false ) ;
373
382
374
- this . context . FormCtrl . isBad [ this . props . name ] = false ;
383
+ bad = false ;
375
384
this . component . onKeyUpHandler ( { target : { validity : { badInput : true } } } ) ;
376
385
expect ( this . context . FormCtrl . setBad ) . to . have . been . calledWith ( this . props . name , true ) ;
377
386
} ) ;
@@ -404,13 +413,13 @@ describe('BaseInput', function () {
404
413
} ) ;
405
414
406
415
it ( 'should call setDirty if the control was not previously dirty' , ( ) => {
407
- this . context . FormCtrl . isDirty [ this . props . name ] = false ;
416
+ dirty = false ;
408
417
this . component . onInputHandler ( 'something' ) ;
409
418
expect ( this . context . FormCtrl . setDirty ) . to . have . been . calledWith ( this . props . name ) ;
410
419
} ) ;
411
420
412
421
it ( 'should not call setDirty if the control was previously dirty' , ( ) => {
413
- this . context . FormCtrl . isDirty [ this . props . name ] = true ;
422
+ dirty = true ;
414
423
this . component . onInputHandler ( 'something' ) ;
415
424
expect ( this . context . FormCtrl . setDirty ) . to . not . have . been . called ;
416
425
} ) ;
@@ -436,13 +445,13 @@ describe('BaseInput', function () {
436
445
} ) ;
437
446
438
447
it ( 'should call setTouched if the control was not previously touched' , ( ) => {
439
- this . context . FormCtrl . isTouched [ this . props . name ] = false ;
448
+ touched = false ;
440
449
this . component . onBlurHandler ( 'something' ) ;
441
450
expect ( this . context . FormCtrl . setTouched ) . to . have . been . calledWith ( this . props . name ) ;
442
451
} ) ;
443
452
444
453
it ( 'should not call setTouched if the control was previously touched' , ( ) => {
445
- this . context . FormCtrl . isTouched [ this . props . name ] = true ;
454
+ touched = true ;
446
455
this . component . onBlurHandler ( 'something' ) ;
447
456
expect ( this . context . FormCtrl . setTouched ) . to . not . have . been . called ;
448
457
} ) ;
@@ -488,13 +497,13 @@ describe('BaseInput', function () {
488
497
} ) ;
489
498
490
499
it ( 'should call setDirty if the control was not previously touched' , ( ) => {
491
- this . context . FormCtrl . isDirty [ this . props . name ] = false ;
500
+ dirty = false ;
492
501
this . component . onChangeHandler ( 'something' ) ;
493
502
expect ( this . context . FormCtrl . setDirty ) . to . have . been . calledWith ( this . props . name ) ;
494
503
} ) ;
495
504
496
505
it ( 'should not call setDirty if the control was previously touched' , ( ) => {
497
- this . context . FormCtrl . isDirty [ this . props . name ] = true ;
506
+ dirty = true ;
498
507
this . component . onChangeHandler ( 'something' ) ;
499
508
expect ( this . context . FormCtrl . setDirty ) . to . not . have . been . called ;
500
509
} ) ;
@@ -573,9 +582,9 @@ describe('BaseInput', function () {
573
582
this . props . validationEvent = 'noMatch' ;
574
583
} ) ;
575
584
576
- it ( 'should not set the state value' , ( ) => {
585
+ it ( 'should set the state value' , ( ) => {
577
586
this . component . validateEvent ( 'onChange' ) ;
578
- expect ( this . setStateSpy ) . to . not . have . been . called ;
587
+ expect ( this . setStateSpy ) . to . have . been . called ;
579
588
} ) ;
580
589
581
590
it ( 'should not try to validate' , ( ) => {
@@ -683,7 +692,7 @@ describe('BaseInput', function () {
683
692
684
693
it ( 'should return the validation event from the form control if not from props' , ( ) => {
685
694
this . props . validationEvent = '' ;
686
- expect ( this . component . getValidationEvent ( ) ) . to . eql ( [ this . context . FormCtrl . validationEvent ] ) ;
695
+ expect ( this . component . getValidationEvent ( ) ) . to . eql ( [ this . context . FormCtrl . getValidationEvent ( ) ] ) ;
687
696
} ) ;
688
697
} ) ;
689
698
0 commit comments