@@ -619,6 +619,44 @@ module('Unit | Model | fake-model', function (hooks) {
619
619
620
620
assert . false ( model . validate ( { only : [ 'otherFakes' ] } ) ) ;
621
621
} ) ;
622
+
623
+ module ( 'conditional option' , function ( ) {
624
+ test ( 'it validates only if `if function` returns true' , async function ( assert ) {
625
+ assert . expect ( 1 ) ;
626
+
627
+ const store = this . owner . lookup ( 'service:store' ) ;
628
+ const model = store . createRecord ( 'fake-model' , { condType : 'gallery' } ) ;
629
+
630
+ let ifOtherFakes = await model . ifOtherFakes ;
631
+ const otherFake = store . createRecord ( 'other-model' ) ;
632
+ if ( ifOtherFakes . push ) {
633
+ ifOtherFakes . push ( otherFake ) ;
634
+ } else {
635
+ ifOtherFakes . pushObject ( otherFake ) ;
636
+ }
637
+
638
+ assert . false ( model . validate ( { only : [ 'ifOtherFakes' ] } ) ) ;
639
+ } ) ;
640
+
641
+ test ( 'skips validation if `if function` returns false' , async function ( assert ) {
642
+ assert . expect ( 1 ) ;
643
+
644
+ const store = this . owner . lookup ( 'service:store' ) ;
645
+ const model = store . createRecord ( 'fake-model' , { condType : 'chancuncha' } ) ;
646
+
647
+ let ifOtherFakes = await model . ifOtherFakes ;
648
+ const otherFake = store . createRecord ( 'other-model' ) ;
649
+ if ( ifOtherFakes . push ) {
650
+ ifOtherFakes . push ( otherFake ) ;
651
+ } else {
652
+ ifOtherFakes . pushObject ( otherFake ) ;
653
+ }
654
+
655
+ model . validate ( ) ;
656
+
657
+ assert . strictEqual ( model . get ( 'errors' ) . errorsFor ( 'ifOtherFakes' ) . length , 0 ) ;
658
+ } ) ;
659
+ } ) ;
622
660
} ) ;
623
661
624
662
module ( '`belongsTo` relations' , function ( ) {
@@ -635,6 +673,32 @@ module('Unit | Model | fake-model', function (hooks) {
635
673
Messages . presenceMessage
636
674
) ;
637
675
} ) ;
676
+
677
+ module ( 'conditional option' , function ( ) {
678
+ test ( 'it validates only if `if function` returns true' , async function ( assert ) {
679
+ assert . expect ( 1 ) ;
680
+
681
+ const store = this . owner . lookup ( 'service:store' ) ;
682
+ const model = store . createRecord ( 'fake-model' , { condType : 'gallery' } ) ;
683
+
684
+ model . set ( 'ifOtherFake' , store . createRecord ( 'other-model' ) ) ;
685
+
686
+ assert . false ( model . validate ( { only : [ 'ifOtherFake' ] } ) ) ;
687
+ } ) ;
688
+
689
+ test ( 'skips validation if `if function` returns false' , async function ( assert ) {
690
+ assert . expect ( 1 ) ;
691
+
692
+ const store = this . owner . lookup ( 'service:store' ) ;
693
+ const model = store . createRecord ( 'fake-model' , { condType : 'chancuncha' } ) ;
694
+
695
+ model . set ( 'ifOtherFake' , store . createRecord ( 'other-model' ) ) ;
696
+
697
+ model . validate ( ) ;
698
+
699
+ assert . strictEqual ( model . get ( 'errors' ) . errorsFor ( 'ifOtherFake' ) . length , 0 ) ;
700
+ } ) ;
701
+ } ) ;
638
702
} ) ;
639
703
} ) ;
640
704
0 commit comments