@@ -28,6 +28,16 @@ const mockedKVs = [{
2828} , {
2929 key : "KeyForEmptyValue" ,
3030 value : "" ,
31+ } , {
32+ key : "app2.settings" ,
33+ value : JSON . stringify ( { fontColor : "blue" , fontSize : 20 } ) ,
34+ contentType : "application/json"
35+ } , {
36+ key : "app3.settings" ,
37+ value : "placeholder"
38+ } , {
39+ key : "app3.settings.fontColor" ,
40+ value : "yellow"
3141} ] . map ( createMockedKeyValue ) ;
3242
3343describe ( "load" , function ( ) {
@@ -83,11 +93,8 @@ describe("load", function () {
8393 trimKeyPrefixes : [ "app.settings." ]
8494 } ) ;
8595 expect ( settings ) . not . undefined ;
86- expect ( settings . has ( "fontColor" ) ) . eq ( true ) ;
8796 expect ( settings . get ( "fontColor" ) ) . eq ( "red" ) ;
88- expect ( settings . has ( "fontSize" ) ) . eq ( true ) ;
8997 expect ( settings . get ( "fontSize" ) ) . eq ( "40" ) ;
90- expect ( settings . has ( "TestKey" ) ) . eq ( false ) ;
9198 } ) ;
9299
93100 it ( "should trim longest key prefix first" , async ( ) => {
@@ -100,20 +107,15 @@ describe("load", function () {
100107 trimKeyPrefixes : [ "app." , "app.settings." , "Test" ]
101108 } ) ;
102109 expect ( settings ) . not . undefined ;
103- expect ( settings . has ( "fontColor" ) ) . eq ( true ) ;
104110 expect ( settings . get ( "fontColor" ) ) . eq ( "red" ) ;
105- expect ( settings . has ( "fontSize" ) ) . eq ( true ) ;
106111 expect ( settings . get ( "fontSize" ) ) . eq ( "40" ) ;
107- expect ( settings . has ( "TestKey" ) ) . eq ( false ) ;
108112 } ) ;
109113
110114 it ( "should support null/empty value" , async ( ) => {
111115 const connectionString = createMockedConnectionString ( ) ;
112116 const settings = await load ( connectionString ) ;
113117 expect ( settings ) . not . undefined ;
114- expect ( settings . has ( "KeyForNullValue" ) ) . eq ( true ) ;
115118 expect ( settings . get ( "KeyForNullValue" ) ) . eq ( null ) ;
116- expect ( settings . has ( "KeyForEmptyValue" ) ) . eq ( true ) ;
117119 expect ( settings . get ( "KeyForEmptyValue" ) ) . eq ( "" ) ;
118120 } ) ;
119121
@@ -148,7 +150,6 @@ describe("load", function () {
148150 } ]
149151 } ) ;
150152 expect ( settings ) . not . undefined ;
151- expect ( settings . has ( "TestKey" ) ) . eq ( true ) ;
152153 expect ( settings . get ( "TestKey" ) ) . eq ( "TestValueForProd" ) ;
153154 } ) ;
154155
@@ -167,8 +168,54 @@ describe("load", function () {
167168 } ]
168169 } ) ;
169170 expect ( settings ) . not . undefined ;
170- expect ( settings . has ( "TestKey" ) ) . eq ( true ) ;
171171 expect ( settings . get ( "TestKey" ) ) . eq ( "TestValueForProd" ) ;
172172 } ) ;
173173
174+ // access data property
175+ it ( "should directly access data property" , async ( ) => {
176+ const connectionString = createMockedConnectionString ( ) ;
177+ const settings = await load ( connectionString ) ;
178+ expect ( settings ) . not . undefined ;
179+ expect ( settings . data ) . not . undefined ;
180+ expect ( settings . data . app . settings . fontColor ) . eq ( "red" ) ;
181+ expect ( settings . data . app . settings . fontSize ) . eq ( "40" ) ;
182+ } ) ;
183+
184+ it ( "should access property of JSON object content-type with data accessor" , async ( ) => {
185+ const connectionString = createMockedConnectionString ( ) ;
186+ const settings = await load ( connectionString ) ;
187+ expect ( settings ) . not . undefined ;
188+ expect ( settings . data ) . not . undefined ;
189+ expect ( settings . data . app2 . settings . fontColor ) . eq ( "blue" ) ;
190+ expect ( settings . data . app2 . settings . fontSize ) . eq ( 20 ) ;
191+ } ) ;
192+
193+ it ( "should not access property of JSON content-type object with get()" , async ( ) => {
194+ const connectionString = createMockedConnectionString ( ) ;
195+ const settings = await load ( connectionString ) ;
196+ expect ( settings ) . not . undefined ;
197+ expect ( settings . get ( "app2.settings" ) ) . not . undefined ; // JSON object accessed as a whole
198+ expect ( settings . get ( "app2.settings.fontColor" ) ) . undefined ;
199+ expect ( settings . get ( "app2.settings.fontSize" ) ) . undefined ;
200+ } ) ;
201+
202+ /**
203+ * Edge case: Hierarchical key-value pairs with overlapped key prefix.
204+ * key: "app3.settings" => value: "placeholder"
205+ * key: "app3.settings.fontColor" => value: "yellow"
206+ *
207+ * get() will return "placeholder" for "app3.settings" and "yellow" for "app3.settings.fontColor", as expected.
208+ * data.app3.settings will return "placeholder" as a whole JSON object, which is not guarenteed to be correct.
209+ */
210+ it ( "Edge case: Hierarchical key-value pairs with overlapped key prefix." , async ( ) => {
211+ const connectionString = createMockedConnectionString ( ) ;
212+ const settings = await load ( connectionString ) ;
213+ expect ( settings ) . not . undefined ;
214+ // use get() method
215+ expect ( settings . get ( "app3.settings" ) ) . eq ( "placeholder" ) ;
216+ expect ( settings . get ( "app3.settings.fontColor" ) ) . eq ( "yellow" ) ;
217+ // use data property
218+ expect ( settings . data . app3 . settings ) . not . eq ( "placeholder" ) ; // not as expected.
219+ expect ( settings . data . app3 . settings . fontColor ) . eq ( "yellow" ) ;
220+ } ) ;
174221} ) ;
0 commit comments