@@ -104,4 +104,100 @@ describe('Configuration class', () => {
104
104
const test = configInstance . get ( 'test' ) ;
105
105
expect ( test ) . toEqual ( 'dev2' ) ;
106
106
} ) ;
107
+
108
+ it ( 'should get nested values from dicts' , ( ) => {
109
+ let nestedDict = {
110
+ 'level1' : 'level1' ,
111
+ 'nested1' : {
112
+ 'nested12' : 'nested12' ,
113
+ 'nested2' : {
114
+ 'nested21' : 'nested21'
115
+ }
116
+ }
117
+ } ;
118
+ configInstance . setAll ( nestedDict ) ;
119
+
120
+ expect ( configInstance . getDictValue ( nestedDict , 'level1' ) ) . toEqual ( 'level1' ) ;
121
+ expect ( configInstance . getDictValue ( nestedDict , 'nested1.nested12' ) ) . toEqual ( 'nested12' ) ;
122
+ expect ( configInstance . getDictValue ( nestedDict , 'nested1.nested2.nested21' ) ) . toEqual ( 'nested21' ) ;
123
+
124
+ expect (
125
+ configInstance . getDictValue ( nestedDict [ 'nested1' ] , 'nested2.nested21' ) ,
126
+ ) . toEqual ( 'nested21' ) ;
127
+
128
+ expect (
129
+ configInstance . getDictValue ( nestedDict [ 'nested1' ] [ 'nested2' ] , 'nested21' ) ,
130
+ ) . toEqual ( 'nested21' ) ;
131
+
132
+ expect (
133
+ function ( ) { configInstance . getDictValue ( nestedDict , 'nonExisting' ) }
134
+ ) . toThrow ( ) ;
135
+ } ) ;
136
+
137
+ it ( 'should get nested values from configs' , ( ) => {
138
+ let nestedDict = {
139
+ 'level1' : 'level1' ,
140
+ 'nested1' : {
141
+ 'nested12' : 'nested12' ,
142
+ 'nested2' : {
143
+ 'nested21' : 'nested21'
144
+ }
145
+ }
146
+ } ;
147
+ configInstance . setAll ( nestedDict ) ;
148
+
149
+ expect ( configInstance . get ( 'level1' ) ) . toEqual ( 'level1' ) ;
150
+ expect ( configInstance . get ( 'nested1.nested12' ) ) . toEqual ( 'nested12' ) ;
151
+ expect ( configInstance . get ( 'nested1.nested2.nested21' ) ) . toEqual ( 'nested21' ) ;
152
+ expect (
153
+ configInstance . get ( 'nested1.nested2' )
154
+ ) . toEqual ( { 'nested21' : 'nested21' } ) ;
155
+ expect (
156
+ configInstance . get ( 'nested1.nested2.nested21' )
157
+ ) . toEqual ( 'nested21' ) ;
158
+ expect (
159
+ function ( ) { configInstance . getDictValue ( nestedDict , 'nonExisting' ) }
160
+ ) . toThrow ( ) ;
161
+ } ) ;
162
+
163
+ it ( 'should prefer environment values from configs' , ( ) => {
164
+ let nestedDict = {
165
+ 'level1' : 'level1' ,
166
+ 'nested1' : {
167
+ 'nested12' : 'nested12' ,
168
+ 'nested2' : {
169
+ 'nested21' : 'nested21'
170
+ } ,
171
+ 'nested13' : 'nested13'
172
+ } ,
173
+ 'dev2' : {
174
+ 'level1' : 'level1e' ,
175
+ 'nested1' : {
176
+ 'nested12' : 'nested12e' ,
177
+ 'nested2' : {
178
+ 'nested21' : 'nested21e'
179
+ }
180
+ }
181
+ }
182
+ } ;
183
+ configInstance . setAll ( nestedDict ) ;
184
+ configInstance . setEnvironment ( 'dev2' ) ;
185
+
186
+ expect ( configInstance . get ( 'level1' ) ) . toEqual ( 'level1e' ) ;
187
+ expect ( configInstance . get ( 'nested1.nested12' ) ) . toEqual ( 'nested12e' ) ;
188
+ expect ( configInstance . get ( 'nested1.nested2.nested21' ) ) . toEqual ( 'nested21e' ) ;
189
+ expect (
190
+ configInstance . get ( 'nested1.nested2' )
191
+ ) . toEqual ( { 'nested21' : 'nested21e' } ) ;
192
+ expect (
193
+ configInstance . get ( 'nested1.nested2.nested21' )
194
+ ) . toEqual ( 'nested21e' ) ;
195
+ expect (
196
+ configInstance . get ( 'nested1.nested13' )
197
+ ) . toEqual ( 'nested13' ) ;
198
+ expect ( configInstance . get ( 'nonExisting' , 'default' ) ) . toEqual ( 'default' ) ;
199
+ expect (
200
+ function ( ) { configInstance . getDictValue ( nestedDict , 'nonExisting' ) }
201
+ ) . toThrow ( ) ;
202
+ } ) ;
107
203
} ) ;
0 commit comments