@@ -105,27 +105,6 @@ test('re-validate config when updated', async () => {
105105 ` ) ;
106106} ) ;
107107
108- test ( "returns undefined if fetching optional config at a path that doesn't exist" , async ( ) => {
109- const rawConfig = getRawConfigProvider ( { } ) ;
110- const configService = new ConfigService ( rawConfig , defaultEnv , logger ) ;
111-
112- const value$ = configService . optionalAtPath ( 'unique-name' ) ;
113- const value = await value$ . pipe ( first ( ) ) . toPromise ( ) ;
114-
115- expect ( value ) . toBeUndefined ( ) ;
116- } ) ;
117-
118- test ( 'returns observable config at optional path if it exists' , async ( ) => {
119- const rawConfig = getRawConfigProvider ( { value : 'bar' } ) ;
120- const configService = new ConfigService ( rawConfig , defaultEnv , logger ) ;
121- await configService . setSchema ( 'value' , schema . string ( ) ) ;
122-
123- const value$ = configService . optionalAtPath ( 'value' ) ;
124- const value : any = await value$ . pipe ( first ( ) ) . toPromise ( ) ;
125-
126- expect ( value ) . toBe ( 'bar' ) ;
127- } ) ;
128-
129108test ( "does not push new configs when reloading if config at path hasn't changed" , async ( ) => {
130109 const rawConfig$ = new BehaviorSubject < Record < string , any > > ( { key : 'value' } ) ;
131110 const rawConfigProvider = rawConfigServiceMock . create ( { rawConfig$ } ) ;
@@ -209,34 +188,38 @@ test('flags schema paths as handled when registering a schema', async () => {
209188
210189test ( 'tracks unhandled paths' , async ( ) => {
211190 const initialConfig = {
212- bar : {
213- deep1 : {
214- key : '123' ,
215- } ,
216- deep2 : {
217- key : '321' ,
218- } ,
191+ service : {
192+ string : 'str' ,
193+ number : 42 ,
219194 } ,
220- foo : 'value' ,
221- quux : {
222- deep1 : {
223- key : 'hello' ,
224- } ,
225- deep2 : {
226- key : 'world' ,
227- } ,
195+ plugin : {
196+ foo : 'bar' ,
197+ } ,
198+ unknown : {
199+ hello : 'dolly' ,
200+ number : 9000 ,
228201 } ,
229202 } ;
230203
231204 const rawConfigProvider = rawConfigServiceMock . create ( { rawConfig : initialConfig } ) ;
232205 const configService = new ConfigService ( rawConfigProvider , defaultEnv , logger ) ;
233-
234- configService . atPath ( 'foo' ) ;
235- configService . atPath ( [ 'bar' , 'deep2' ] ) ;
206+ await configService . setSchema (
207+ 'service' ,
208+ schema . object ( {
209+ string : schema . string ( ) ,
210+ number : schema . number ( ) ,
211+ } )
212+ ) ;
213+ await configService . setSchema (
214+ 'plugin' ,
215+ schema . object ( {
216+ foo : schema . string ( ) ,
217+ } )
218+ ) ;
236219
237220 const unused = await configService . getUnusedPaths ( ) ;
238221
239- expect ( unused ) . toEqual ( [ 'bar.deep1.key ' , 'quux.deep1.key' , 'quux.deep2.key '] ) ;
222+ expect ( unused ) . toEqual ( [ 'unknown.hello ' , 'unknown.number ' ] ) ;
240223} ) ;
241224
242225test ( 'correctly passes context' , async ( ) => {
@@ -339,22 +322,18 @@ test('does not throw if schema does not define "enabled" schema', async () => {
339322
340323 const rawConfigProvider = rawConfigServiceMock . create ( { rawConfig : initialConfig } ) ;
341324 const configService = new ConfigService ( rawConfigProvider , defaultEnv , logger ) ;
342- await expect (
325+ expect (
343326 configService . setSchema (
344327 'pid' ,
345328 schema . object ( {
346329 file : schema . string ( ) ,
347330 } )
348331 )
349- ) . resolves . toBeUndefined ( ) ;
332+ ) . toBeUndefined ( ) ;
350333
351334 const value$ = configService . atPath ( 'pid' ) ;
352335 const value : any = await value$ . pipe ( first ( ) ) . toPromise ( ) ;
353336 expect ( value . enabled ) . toBe ( undefined ) ;
354-
355- const valueOptional$ = configService . optionalAtPath ( 'pid' ) ;
356- const valueOptional : any = await valueOptional$ . pipe ( first ( ) ) . toPromise ( ) ;
357- expect ( valueOptional . enabled ) . toBe ( undefined ) ;
358337} ) ;
359338
360339test ( 'treats config as enabled if config path is not present in config' , async ( ) => {
@@ -457,3 +436,44 @@ test('logs deprecation warning during validation', async () => {
457436 ]
458437 ` ) ;
459438} ) ;
439+
440+ describe ( 'atPathSync' , ( ) => {
441+ test ( 'returns the value at path' , async ( ) => {
442+ const rawConfig = getRawConfigProvider ( { key : 'foo' } ) ;
443+ const configService = new ConfigService ( rawConfig , defaultEnv , logger ) ;
444+ const stringSchema = schema . string ( ) ;
445+ await configService . setSchema ( 'key' , stringSchema ) ;
446+
447+ await configService . validate ( ) ;
448+
449+ const value = configService . atPathSync ( 'key' ) ;
450+ expect ( value ) . toBe ( 'foo' ) ;
451+ } ) ;
452+
453+ test ( 'throws if called before `validate`' , async ( ) => {
454+ const rawConfig = getRawConfigProvider ( { key : 'foo' } ) ;
455+ const configService = new ConfigService ( rawConfig , defaultEnv , logger ) ;
456+ const stringSchema = schema . string ( ) ;
457+ await configService . setSchema ( 'key' , stringSchema ) ;
458+
459+ expect ( ( ) => configService . atPathSync ( 'key' ) ) . toThrowErrorMatchingInlineSnapshot (
460+ `"\`atPathSync\` called before config was validated"`
461+ ) ;
462+ } ) ;
463+
464+ test ( 'returns the last config value' , async ( ) => {
465+ const rawConfig$ = new BehaviorSubject < Record < string , any > > ( { key : 'value' } ) ;
466+ const rawConfigProvider = rawConfigServiceMock . create ( { rawConfig$ } ) ;
467+
468+ const configService = new ConfigService ( rawConfigProvider , defaultEnv , logger ) ;
469+ await configService . setSchema ( 'key' , schema . string ( ) ) ;
470+
471+ await configService . validate ( ) ;
472+
473+ expect ( configService . atPathSync ( 'key' ) ) . toEqual ( 'value' ) ;
474+
475+ rawConfig$ . next ( { key : 'new-value' } ) ;
476+
477+ expect ( configService . atPathSync ( 'key' ) ) . toEqual ( 'new-value' ) ;
478+ } ) ;
479+ } ) ;
0 commit comments