@@ -574,11 +574,12 @@ describe('RemoteConfig', () => {
574
574
575
575
return remoteConfig . getServerTemplate ( )
576
576
. then ( ( template ) => {
577
- expect ( template . cache . conditions . length ) . to . equal ( 1 ) ;
578
- expect ( template . cache . conditions [ 0 ] . name ) . to . equal ( 'ios' ) ;
579
- expect ( template . cache . etag ) . to . equal ( 'etag-123456789012-5' ) ;
577
+ const parsedTemplate = template . toJSON ( ) ;
578
+ expect ( parsedTemplate . conditions . length ) . to . equal ( 1 ) ;
579
+ expect ( parsedTemplate . conditions [ 0 ] . name ) . to . equal ( 'ios' ) ;
580
+ expect ( parsedTemplate . etag ) . to . equal ( 'etag-123456789012-5' ) ;
580
581
581
- const version = template . cache . version ! ;
582
+ const version = parsedTemplate . version ! ;
582
583
expect ( version . versionNumber ) . to . equal ( '86' ) ;
583
584
expect ( version . updateOrigin ) . to . equal ( 'ADMIN_SDK_NODE' ) ;
584
585
expect ( version . updateType ) . to . equal ( 'INCREMENTAL_UPDATE' ) ;
@@ -589,23 +590,22 @@ describe('RemoteConfig', () => {
589
590
expect ( version . updateTime ) . to . equal ( 'Mon, 15 Jun 2020 16:45:03 GMT' ) ;
590
591
591
592
const key = 'holiday_promo_enabled' ;
592
- const p1 = template . cache . parameters [ key ] ;
593
+ const p1 = parsedTemplate . parameters [ key ] ;
593
594
expect ( p1 . defaultValue ) . deep . equals ( { value : 'true' } ) ;
594
595
expect ( p1 . conditionalValues ) . deep . equals ( { ios : { useInAppDefault : true } } ) ;
595
596
expect ( p1 . description ) . equals ( 'this is a promo' ) ;
596
597
expect ( p1 . valueType ) . equals ( 'BOOLEAN' ) ;
597
598
598
- const c = template . cache . conditions . find ( ( c ) => c . name === 'ios' ) ;
599
+ const c = parsedTemplate . conditions . find ( ( c ) => c . name === 'ios' ) ;
599
600
expect ( c ) . to . be . not . undefined ;
600
601
const cond = c as NamedCondition ;
601
602
expect ( cond . name ) . to . equal ( 'ios' ) ;
602
603
603
- const parsed = JSON . parse ( JSON . stringify ( template . cache ) ) ;
604
604
const expectedTemplate = deepCopy ( SERVER_REMOTE_CONFIG_RESPONSE ) ;
605
605
const expectedVersion = deepCopy ( VERSION_INFO ) ;
606
606
expectedVersion . updateTime = new Date ( expectedVersion . updateTime ) . toUTCString ( ) ;
607
607
expectedTemplate . version = expectedVersion ;
608
- expect ( parsed ) . deep . equals ( expectedTemplate ) ;
608
+ expect ( parsedTemplate ) . deep . equals ( expectedTemplate ) ;
609
609
} ) ;
610
610
} ) ;
611
611
@@ -649,7 +649,10 @@ describe('RemoteConfig', () => {
649
649
}
650
650
} ;
651
651
const initializedTemplate = remoteConfig . initServerTemplate ( { template } ) ;
652
- const parsed = JSON . parse ( JSON . stringify ( initializedTemplate . cache ) ) ;
652
+ const parsed = initializedTemplate . toJSON ( ) ;
653
+ const expectedVersion = deepCopy ( VERSION_INFO ) ;
654
+ expectedVersion . updateTime = new Date ( expectedVersion . updateTime ) . toUTCString ( ) ;
655
+ template . version = expectedVersion as Version ;
653
656
expect ( parsed ) . deep . equals ( deepCopy ( template ) ) ;
654
657
} ) ;
655
658
@@ -666,7 +669,7 @@ describe('RemoteConfig', () => {
666
669
} ;
667
670
const templateJson = JSON . stringify ( template ) ;
668
671
const initializedTemplate = remoteConfig . initServerTemplate ( { template : templateJson } ) ;
669
- const parsed = JSON . parse ( JSON . stringify ( initializedTemplate . cache ) ) ;
672
+ const parsed = initializedTemplate . toJSON ( ) ;
670
673
const expectedVersion = deepCopy ( VERSION_INFO ) ;
671
674
expectedVersion . updateTime = new Date ( expectedVersion . updateTime ) . toUTCString ( ) ;
672
675
template . version = expectedVersion as Version ;
@@ -751,6 +754,78 @@ describe('RemoteConfig', () => {
751
754
752
755
describe ( 'load' , ( ) => {
753
756
const operationName = 'getServerTemplate' ;
757
+ const INVALID_PARAMETERS : any [ ] = [ null , '' , 'abc' , 1 , true , [ ] ] ;
758
+ const INVALID_CONDITIONS : any [ ] = [ null , '' , 'abc' , 1 , true , { } ] ;
759
+ let sourceTemplate = deepCopy ( SERVER_REMOTE_CONFIG_RESPONSE ) ;
760
+
761
+ it ( 'should set template when passed' , ( ) => {
762
+ const template = deepCopy ( SERVER_REMOTE_CONFIG_RESPONSE ) as ServerTemplateData ;
763
+ template . parameters = {
764
+ dog_type : {
765
+ defaultValue : {
766
+ value : 'shiba'
767
+ } ,
768
+ description : 'Type of dog breed' ,
769
+ valueType : 'STRING'
770
+ }
771
+ } ;
772
+ const initializedTemplate = remoteConfig . initServerTemplate ( ) ;
773
+ initializedTemplate . load ( template ) ;
774
+ const parsed = initializedTemplate . toJSON ( ) ;
775
+ const expectedVersion = deepCopy ( VERSION_INFO ) ;
776
+ expectedVersion . updateTime = new Date ( expectedVersion . updateTime ) . toUTCString ( ) ;
777
+ template . version = expectedVersion as Version ;
778
+ expect ( parsed ) . deep . equals ( deepCopy ( template ) ) ;
779
+ } ) ;
780
+
781
+ it ( 'should set and instantiates template when json string is passed' , ( ) => {
782
+ const template = deepCopy ( SERVER_REMOTE_CONFIG_RESPONSE ) as ServerTemplateData ;
783
+ template . parameters = {
784
+ dog_type : {
785
+ defaultValue : {
786
+ value : 'shiba'
787
+ } ,
788
+ description : 'Type of dog breed' ,
789
+ valueType : 'STRING'
790
+ }
791
+ } ;
792
+ const templateJson = JSON . stringify ( template ) ;
793
+ const initializedTemplate = remoteConfig . initServerTemplate ( ) ;
794
+ initializedTemplate . load ( templateJson ) ;
795
+ const parsed = initializedTemplate . toJSON ( ) ;
796
+ const expectedVersion = deepCopy ( VERSION_INFO ) ;
797
+ expectedVersion . updateTime = new Date ( expectedVersion . updateTime ) . toUTCString ( ) ;
798
+ template . version = expectedVersion as Version ;
799
+ expect ( parsed ) . deep . equals ( deepCopy ( template ) ) ;
800
+ } ) ;
801
+
802
+ it ( 'should throw if template is an invalid JSON' , ( ) => {
803
+ const jsonString = '{invalidJson: null}' ;
804
+ const initializedTemplate = remoteConfig . initServerTemplate ( ) ;
805
+ expect ( ( ) => initializedTemplate . load ( jsonString ) )
806
+ . to . throw ( / F a i l e d t o p a r s e t h e J S O N s t r i n g : ( [ \D \w ] * ) \. / ) ;
807
+ } ) ;
808
+
809
+ INVALID_PARAMETERS . forEach ( ( invalidParameter ) => {
810
+ sourceTemplate . parameters = invalidParameter ;
811
+ it ( `should throw if the parameters is ${ JSON . stringify ( invalidParameter ) } ` , ( ) => {
812
+ const jsonString = JSON . stringify ( sourceTemplate ) ;
813
+ const initializedTemplate = remoteConfig . initServerTemplate ( ) ;
814
+ expect ( ( ) => initializedTemplate . load ( jsonString ) )
815
+ . to . throw ( / F a i l e d t o p a r s e t h e J S O N s t r i n g : ( [ \D \w ] * ) \. / ) ;
816
+ } ) ;
817
+ } ) ;
818
+
819
+ sourceTemplate = deepCopy ( SERVER_REMOTE_CONFIG_RESPONSE ) ;
820
+ INVALID_CONDITIONS . forEach ( ( invalidConditions ) => {
821
+ sourceTemplate . conditions = invalidConditions ;
822
+ it ( `should throw if the conditions is ${ JSON . stringify ( invalidConditions ) } ` , ( ) => {
823
+ const jsonString = JSON . stringify ( sourceTemplate ) ;
824
+ const initializedTemplate = remoteConfig . initServerTemplate ( ) ;
825
+ expect ( ( ) => initializedTemplate . load ( jsonString ) )
826
+ . to . throw ( / F a i l e d t o p a r s e t h e J S O N s t r i n g : ( [ \D \w ] * ) \. / ) ;
827
+ } ) ;
828
+ } ) ;
754
829
755
830
it ( 'should propagate API errors' , ( ) => {
756
831
const stub = sinon
@@ -816,7 +891,7 @@ describe('RemoteConfig', () => {
816
891
return remoteConfig . getServerTemplate ( )
817
892
. then ( ( template ) => {
818
893
// If parameters are not present in the response, we set it to an empty object.
819
- expect ( template . cache . parameters ) . deep . equals ( { } ) ;
894
+ expect ( template . toJSON ( ) . parameters ) . deep . equals ( { } ) ;
820
895
} ) ;
821
896
} ) ;
822
897
@@ -830,7 +905,7 @@ describe('RemoteConfig', () => {
830
905
return remoteConfig . getServerTemplate ( )
831
906
. then ( ( template ) => {
832
907
// If conditions are not present in the response, we set it to an empty array.
833
- expect ( template . cache . conditions ) . deep . equals ( [ ] ) ;
908
+ expect ( template . toJSON ( ) . conditions ) . deep . equals ( [ ] ) ;
834
909
} ) ;
835
910
} ) ;
836
911
@@ -842,11 +917,11 @@ describe('RemoteConfig', () => {
842
917
843
918
return remoteConfig . getServerTemplate ( )
844
919
. then ( ( template ) => {
845
- expect ( template . cache . conditions . length ) . to . equal ( 1 ) ;
846
- expect ( template . cache . conditions [ 0 ] . name ) . to . equal ( 'ios' ) ;
847
- expect ( template . cache . etag ) . to . equal ( 'etag-123456789012-5' ) ;
920
+ expect ( template . toJSON ( ) . conditions . length ) . to . equal ( 1 ) ;
921
+ expect ( template . toJSON ( ) . conditions [ 0 ] . name ) . to . equal ( 'ios' ) ;
922
+ expect ( template . toJSON ( ) . etag ) . to . equal ( 'etag-123456789012-5' ) ;
848
923
849
- const version = template . cache . version ! ;
924
+ const version = template . toJSON ( ) . version ! ;
850
925
expect ( version . versionNumber ) . to . equal ( '86' ) ;
851
926
expect ( version . updateOrigin ) . to . equal ( 'ADMIN_SDK_NODE' ) ;
852
927
expect ( version . updateType ) . to . equal ( 'INCREMENTAL_UPDATE' ) ;
@@ -857,13 +932,13 @@ describe('RemoteConfig', () => {
857
932
expect ( version . updateTime ) . to . equal ( 'Mon, 15 Jun 2020 16:45:03 GMT' ) ;
858
933
859
934
const key = 'holiday_promo_enabled' ;
860
- const p1 = template . cache . parameters [ key ] ;
935
+ const p1 = template . toJSON ( ) . parameters [ key ] ;
861
936
expect ( p1 . defaultValue ) . deep . equals ( { value : 'true' } ) ;
862
937
expect ( p1 . conditionalValues ) . deep . equals ( { ios : { useInAppDefault : true } } ) ;
863
938
expect ( p1 . description ) . equals ( 'this is a promo' ) ;
864
939
expect ( p1 . valueType ) . equals ( 'BOOLEAN' ) ;
865
940
866
- const c = template . cache . conditions . find ( ( c ) => c . name === 'ios' ) ;
941
+ const c = template . toJSON ( ) . conditions . find ( ( c ) => c . name === 'ios' ) ;
867
942
expect ( c ) . to . be . not . undefined ;
868
943
const cond = c as NamedCondition ;
869
944
expect ( cond . name ) . to . equal ( 'ios' ) ;
@@ -883,7 +958,7 @@ describe('RemoteConfig', () => {
883
958
}
884
959
} ) ;
885
960
886
- const parsed = JSON . parse ( JSON . stringify ( template . cache ) ) ;
961
+ const parsed = template . toJSON ( ) ;
887
962
const expectedTemplate = deepCopy ( SERVER_REMOTE_CONFIG_RESPONSE ) ;
888
963
const expectedVersion = deepCopy ( VERSION_INFO ) ;
889
964
expectedVersion . updateTime = new Date ( expectedVersion . updateTime ) . toUTCString ( ) ;
@@ -904,9 +979,9 @@ describe('RemoteConfig', () => {
904
979
905
980
return remoteConfig . getServerTemplate ( )
906
981
. then ( ( template ) => {
907
- expect ( template . cache . etag ) . to . equal ( 'etag-123456789012-5' ) ;
982
+ expect ( template . toJSON ( ) . etag ) . to . equal ( 'etag-123456789012-5' ) ;
908
983
909
- const version = template . cache . version ! ;
984
+ const version = template . toJSON ( ) . version ! ;
910
985
expect ( version . versionNumber ) . to . equal ( '86' ) ;
911
986
expect ( version . updateOrigin ) . to . equal ( 'ADMIN_SDK_NODE' ) ;
912
987
expect ( version . updateType ) . to . equal ( 'INCREMENTAL_UPDATE' ) ;
@@ -930,9 +1005,9 @@ describe('RemoteConfig', () => {
930
1005
931
1006
return remoteConfig . getServerTemplate ( )
932
1007
. then ( ( template ) => {
933
- expect ( template . cache . etag ) . to . equal ( 'etag-123456789012-5' ) ;
1008
+ expect ( template . toJSON ( ) . etag ) . to . equal ( 'etag-123456789012-5' ) ;
934
1009
935
- const version = template . cache . version ! ;
1010
+ const version = template . toJSON ( ) . version ! ;
936
1011
expect ( version . versionNumber ) . to . equal ( '86' ) ;
937
1012
expect ( version . updateOrigin ) . to . equal ( 'ADMIN_SDK_NODE' ) ;
938
1013
expect ( version . updateType ) . to . equal ( 'INCREMENTAL_UPDATE' ) ;
@@ -956,9 +1031,9 @@ describe('RemoteConfig', () => {
956
1031
957
1032
return remoteConfig . getServerTemplate ( )
958
1033
. then ( ( template ) => {
959
- expect ( template . cache . etag ) . to . equal ( 'etag-123456789012-5' ) ;
1034
+ expect ( template . toJSON ( ) . etag ) . to . equal ( 'etag-123456789012-5' ) ;
960
1035
961
- const version = template . cache . version ! ;
1036
+ const version = template . toJSON ( ) . version ! ;
962
1037
expect ( version . versionNumber ) . to . equal ( '86' ) ;
963
1038
expect ( version . updateOrigin ) . to . equal ( 'ADMIN_SDK_NODE' ) ;
964
1039
expect ( version . updateType ) . to . equal ( 'INCREMENTAL_UPDATE' ) ;
@@ -1173,7 +1248,7 @@ describe('RemoteConfig', () => {
1173
1248
} ,
1174
1249
}
1175
1250
1176
- template . cache = response as ServerTemplateData ;
1251
+ template . load ( response as ServerTemplateData ) ;
1177
1252
1178
1253
let config = template . evaluate ( ) ;
1179
1254
@@ -1188,7 +1263,7 @@ describe('RemoteConfig', () => {
1188
1263
} ,
1189
1264
}
1190
1265
1191
- template . cache = response as ServerTemplateData ;
1266
+ template . load ( response as ServerTemplateData ) ;
1192
1267
1193
1268
config = template . evaluate ( ) ;
1194
1269
0 commit comments