diff --git a/runtime/config/index.test.ts b/runtime/config/index.test.ts index f9d537cf..68568dbe 100644 --- a/runtime/config/index.test.ts +++ b/runtime/config/index.test.ts @@ -1,6 +1,8 @@ import { CONFIG_CHANGED } from '../constants'; import * as subscriptions from '../subscriptions'; import { + addAppConfigs, + getAppConfig, getSiteConfig, mergeSiteConfig, setSiteConfig, @@ -295,4 +297,57 @@ describe('mergeSiteConfig', () => { expect(getSiteConfig().apps![0].config!.ORIGINAL).toBe('value'); }); }); + + describe('getAppConfig with commonAppConfig', () => { + it('should return commonAppConfig values for an app with no config', () => { + setSiteConfig({ + ...defaultSiteConfig, + commonAppConfig: { COMMON_KEY: 'common-value' }, + apps: [{ appId: 'test-app' }], + }); + addAppConfigs(); + + const config = getAppConfig('test-app'); + expect(config).toEqual({ COMMON_KEY: 'common-value' }); + }); + + it('should let app-specific config override commonAppConfig', () => { + setSiteConfig({ + ...defaultSiteConfig, + commonAppConfig: { SHARED: 'common', COMMON_ONLY: 'yes' }, + apps: [{ appId: 'test-app', config: { SHARED: 'app-specific', APP_ONLY: 'yes' } }], + }); + addAppConfigs(); + + const config = getAppConfig('test-app'); + expect(config).toEqual({ + SHARED: 'app-specific', + COMMON_ONLY: 'yes', + APP_ONLY: 'yes', + }); + }); + + it('should return app config as-is when commonAppConfig is not set', () => { + setSiteConfig({ + ...defaultSiteConfig, + apps: [{ appId: 'test-app', config: { VALUE: 'test' } }], + }); + addAppConfigs(); + + const config = getAppConfig('test-app'); + expect(config).toEqual({ VALUE: 'test' }); + }); + + it('should deep merge commonAppConfig with app config', () => { + setSiteConfig({ + ...defaultSiteConfig, + commonAppConfig: { NESTED: { a: 1, b: 2 } }, + apps: [{ appId: 'test-app', config: { NESTED: { b: 3, c: 4 } } }], + }); + addAppConfigs(); + + const config = getAppConfig('test-app'); + expect(config).toEqual({ NESTED: { a: 1, b: 3, c: 4 } }); + }); + }); }); diff --git a/runtime/config/index.ts b/runtime/config/index.ts index 1b343764..ca1a49f4 100644 --- a/runtime/config/index.ts +++ b/runtime/config/index.ts @@ -272,7 +272,11 @@ export function addAppConfigs() { } export function getAppConfig(id: string) { - return appConfigs[id]; + const { commonAppConfig } = getSiteConfig(); + if (commonAppConfig === undefined) { + return appConfigs[id]; + } + return merge({}, commonAppConfig, appConfigs[id]); } export function mergeAppConfig(id: string, newAppConfig: AppConfig) { diff --git a/types.ts b/types.ts index 772fe785..869007a9 100644 --- a/types.ts +++ b/types.ts @@ -59,6 +59,7 @@ export interface OptionalSiteConfig { externalRoutes: ExternalRoute[], externalLinkUrlOverrides: string[], runtimeConfigJsonUrl: string | null, + commonAppConfig: AppConfig, headerLogoImageUrl: string, // Theme