@@ -35,7 +35,7 @@ import {
35
35
} from '../../../src/remote-config/remote-config-api-client-internal' ;
36
36
import { deepCopy } from '../../../src/utils/deep-copy' ;
37
37
import {
38
- NamedCondition , ServerTemplate , ServerTemplateData
38
+ NamedCondition , ServerTemplate , ServerTemplateData , Version
39
39
} from '../../../src/remote-config/remote-config-api' ;
40
40
41
41
const expect = chai . expect ;
@@ -648,10 +648,61 @@ describe('RemoteConfig', () => {
648
648
valueType : 'STRING'
649
649
}
650
650
} ;
651
- const initializedTemplate = remoteConfig . initServerTemplate ( { template } ) . cache ;
652
- const parsed = JSON . parse ( JSON . stringify ( initializedTemplate ) ) ;
651
+ const initializedTemplate = remoteConfig . initServerTemplate ( { template } ) ;
652
+ const parsed = JSON . parse ( JSON . stringify ( initializedTemplate . cache ) ) ;
653
653
expect ( parsed ) . deep . equals ( deepCopy ( template ) ) ;
654
654
} ) ;
655
+
656
+ it ( 'should set and instantiates template when json string is passed' , ( ) => {
657
+ const template = deepCopy ( SERVER_REMOTE_CONFIG_RESPONSE ) as ServerTemplateData ;
658
+ template . parameters = {
659
+ dog_type : {
660
+ defaultValue : {
661
+ value : 'shiba'
662
+ } ,
663
+ description : 'Type of dog breed' ,
664
+ valueType : 'STRING'
665
+ }
666
+ } ;
667
+ const templateJson = JSON . stringify ( template ) ;
668
+ const initializedTemplate = remoteConfig . initServerTemplate ( { template : templateJson } ) ;
669
+ const parsed = JSON . parse ( JSON . stringify ( initializedTemplate . cache ) ) ;
670
+ const expectedVersion = deepCopy ( VERSION_INFO ) ;
671
+ expectedVersion . updateTime = new Date ( expectedVersion . updateTime ) . toUTCString ( ) ;
672
+ template . version = expectedVersion as Version ;
673
+ expect ( parsed ) . deep . equals ( deepCopy ( template ) ) ;
674
+ } ) ;
675
+
676
+ describe ( 'should throw error if invalid template JSON is passed' , ( ) => {
677
+ const INVALID_PARAMETERS : any [ ] = [ null , '' , 'abc' , 1 , true , [ ] ] ;
678
+ const INVALID_CONDITIONS : any [ ] = [ null , '' , 'abc' , 1 , true , { } ] ;
679
+
680
+ let sourceTemplate = deepCopy ( SERVER_REMOTE_CONFIG_RESPONSE ) ;
681
+ const jsonString = '{invalidJson: null}' ;
682
+ it ( 'should throw if template is an invalid JSON' , ( ) => {
683
+ expect ( ( ) => remoteConfig . initServerTemplate ( { template : jsonString } ) )
684
+ . 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 ] * ) \. / ) ;
685
+ } ) ;
686
+
687
+ INVALID_PARAMETERS . forEach ( ( invalidParameter ) => {
688
+ sourceTemplate . parameters = invalidParameter ;
689
+ const jsonString = JSON . stringify ( sourceTemplate ) ;
690
+ it ( `should throw if the parameters is ${ JSON . stringify ( invalidParameter ) } ` , ( ) => {
691
+ expect ( ( ) => remoteConfig . initServerTemplate ( { template : jsonString } ) )
692
+ . to . throw ( 'Remote Config parameters must be a non-null object' ) ;
693
+ } ) ;
694
+ } ) ;
695
+
696
+ sourceTemplate = deepCopy ( SERVER_REMOTE_CONFIG_RESPONSE ) ;
697
+ INVALID_CONDITIONS . forEach ( ( invalidConditions ) => {
698
+ sourceTemplate . conditions = invalidConditions ;
699
+ const jsonString = JSON . stringify ( sourceTemplate ) ;
700
+ it ( `should throw if the conditions is ${ JSON . stringify ( invalidConditions ) } ` , ( ) => {
701
+ expect ( ( ) => remoteConfig . initServerTemplate ( { template : jsonString } ) )
702
+ . to . throw ( 'Remote Config conditions must be an array' ) ;
703
+ } ) ;
704
+ } ) ;
705
+ } ) ;
655
706
} ) ;
656
707
657
708
describe ( 'RemoteConfigServerTemplate' , ( ) => {
@@ -1039,12 +1090,12 @@ describe('RemoteConfig', () => {
1039
1090
it ( 'uses local default if parameter not in template' , ( ) => {
1040
1091
const template = deepCopy ( SERVER_REMOTE_CONFIG_RESPONSE ) as ServerTemplateData ;
1041
1092
template . parameters = { } ;
1042
-
1093
+
1043
1094
const stub = sinon
1044
1095
. stub ( RemoteConfigApiClient . prototype , 'getServerTemplate' )
1045
1096
. resolves ( template ) ;
1046
1097
stubs . push ( stub ) ;
1047
-
1098
+
1048
1099
const defaultConfig = {
1049
1100
dog_coat : 'blue merle' ,
1050
1101
} ;
@@ -1061,12 +1112,12 @@ describe('RemoteConfig', () => {
1061
1112
template . parameters = {
1062
1113
dog_no_remote_default_value : { }
1063
1114
} ;
1064
-
1115
+
1065
1116
const stub = sinon
1066
1117
. stub ( RemoteConfigApiClient . prototype , 'getServerTemplate' )
1067
1118
. resolves ( template ) ;
1068
1119
stubs . push ( stub ) ;
1069
-
1120
+
1070
1121
const defaultConfig = {
1071
1122
dog_no_remote_default_value : 'local default'
1072
1123
} ;
@@ -1083,7 +1134,7 @@ describe('RemoteConfig', () => {
1083
1134
template . parameters = {
1084
1135
dog_no_remote_default_value : { }
1085
1136
} ;
1086
-
1137
+
1087
1138
const stub = sinon
1088
1139
. stub ( RemoteConfigApiClient . prototype , 'getServerTemplate' )
1089
1140
. resolves ( template ) ;
0 commit comments