@@ -536,26 +536,85 @@ describe('formly-form', () => {
536
536
beforeEach ( ( ) => {
537
537
scope . model = { }
538
538
scope . fieldModel = { }
539
-
540
- scope . fields = [
541
- { template : input , key : 'foo' , model : scope . fieldModel } ,
542
- { template : input , key : 'bar' , model : scope . fieldModel , hideExpression : ( ) => ! ! scope . fieldModel . foo } ,
543
- ]
544
539
} )
540
+ describe ( 'behaviour when model changes' , ( ) => {
541
+
542
+ describe ( 'when a string is passed to hideExpression' , ( ) => {
543
+ beforeEach ( ( ) => {
544
+ scope . fields = [
545
+ { template : input , key : 'foo' , model : scope . fieldModel } ,
546
+ { template : input , key : 'bar' , model : scope . fieldModel , hideExpression : ( ) => ! ! scope . fieldModel . foo } ,
547
+ ]
548
+ } )
545
549
546
- it ( 'should be called and resolve to true when field model changes' , ( ) => {
547
- compileAndDigest ( )
548
- expect ( scope . fields [ 1 ] . hide ) . be . false
549
- scope . fields [ 0 ] . formControl . $setViewValue ( 'value' )
550
- expect ( scope . fields [ 1 ] . hide ) . be . true
550
+ it ( 'should be called and should resolve to true when field model changes' , ( ) => {
551
+ compileAndDigest ( )
552
+ expect ( scope . fields [ 1 ] . hide ) . be . false
553
+ scope . fields [ 0 ] . formControl . $setViewValue ( 'value' )
554
+ expect ( scope . fields [ 1 ] . hide ) . be . true
555
+ } )
556
+
557
+ it ( 'should be called and should resolve to false when field model changes' , ( ) => {
558
+ scope . fieldModel . foo = 'value'
559
+ compileAndDigest ( )
560
+ expect ( scope . fields [ 1 ] . hide ) . be . true
561
+ scope . fields [ 0 ] . formControl . $setViewValue ( '' )
562
+ expect ( scope . fields [ 1 ] . hide ) . be . false
563
+ } )
564
+ } )
565
+ describe ( 'when a function is passed to hideExpression' , ( ) => {
566
+ beforeEach ( ( ) => {
567
+ scope . fields = [
568
+ { template : input , key : 'foo' , model : scope . fieldModel } ,
569
+ {
570
+ template : input , key : 'bar' ,
571
+ model : scope . fieldModel ,
572
+ hideExpression : ( $viewValue , $modelValue , scope ) => {
573
+ return ! ! scope . fields [ 1 ] . data . parentScope . fieldModel . foo //since the scope passed to the function belongs to the form,
574
+ } , //we store the outer(parent) scope in 'data' property to access
575
+ data : { //the template named 'foo' stored in the fields array
576
+ parentScope : scope , //the parent scope(one used to compile the form)
577
+ } ,
578
+ } ,
579
+ ]
580
+ } )
581
+
582
+ it ( 'should be called and should resolve to true when field model changes' , ( ) => {
583
+ compileAndDigest ( )
584
+ expect ( scope . fields [ 1 ] . hide ) . be . false
585
+ scope . fields [ 0 ] . formControl . $setViewValue ( 'value' )
586
+ expect ( scope . fields [ 1 ] . hide ) . be . true
587
+ } )
588
+
589
+ it ( 'should be called and should resolve to false when field model changes' , ( ) => {
590
+ scope . fieldModel . foo = 'value'
591
+ compileAndDigest ( )
592
+ expect ( scope . fields [ 1 ] . hide ) . be . true
593
+ scope . fields [ 0 ] . formControl . $setViewValue ( '' )
594
+ expect ( scope . fields [ 1 ] . hide ) . be . false
595
+ } )
596
+ } )
551
597
} )
552
598
553
- it ( 'should be called and resolve to false when field model changes' , ( ) => {
554
- scope . fieldModel . foo = 'value'
599
+ it ( 'ensures that hideExpression has all the expressionProperties values' , ( ) => {
600
+ scope . options = { formState : { } }
601
+ scope . fields = [ {
602
+ template : input ,
603
+ key : 'test' ,
604
+ hideExpression : `
605
+ options === options.data.field &&
606
+ index === 0 &&
607
+ formState === options.data.formOptions.formState &&
608
+ originalModel === options.data.originalModel &&
609
+ formOptions === options.data.formOptions` ,
610
+ data : {
611
+ originalModel : scope . model ,
612
+ formOptions : scope . options ,
613
+ } ,
614
+ } ]
615
+ scope . fields [ 0 ] . data . field = scope . fields [ 0 ]
555
616
compileAndDigest ( )
556
- expect ( scope . fields [ 1 ] . hide ) . be . true
557
- scope . fields [ 0 ] . formControl . $setViewValue ( '' )
558
- expect ( scope . fields [ 1 ] . hide ) . be . false
617
+ expect ( scope . fields [ 0 ] . hide ) . be . true
559
618
} )
560
619
} )
561
620
0 commit comments