diff --git a/runtime/config/index.test.ts b/runtime/config/index.test.ts index 3160f50a..086915c1 100644 --- a/runtime/config/index.test.ts +++ b/runtime/config/index.test.ts @@ -414,6 +414,25 @@ describe('mergeSiteConfig', () => { }); }); + describe('getAppConfig with no config at all', () => { + it('should return an empty object for an app with no config', () => { + setSiteConfig({ + ...defaultSiteConfig, + apps: [{ appId: 'bare-app' }], + }); + addAppConfigs(); + + expect(getAppConfig('bare-app')).toEqual({}); + }); + + it('should return an empty object for an unknown app', () => { + setSiteConfig({ ...defaultSiteConfig, apps: [] }); + addAppConfigs(); + + expect(getAppConfig('unknown-app')).toEqual({}); + }); + }); + describe('getAppConfig with defaultConfig', () => { it('should return defaultConfig values when nothing else is set', () => { setSiteConfig({ diff --git a/runtime/config/index.ts b/runtime/config/index.ts index 17784e9d..8461fa15 100644 --- a/runtime/config/index.ts +++ b/runtime/config/index.ts @@ -368,14 +368,12 @@ export function addAppConfigs() { * - `App.defaultConfig`, bundled by the app author * - `SiteConfig.commonAppConfig`, supplied site-wide by an operator * - `App.config`, supplied per-app by an operator + * + * Always returns an object: an app with none of the above gets `{}`. */ -export function getAppConfig(id: string) { +export function getAppConfig(id: string): AppConfig { const { commonAppConfig } = getSiteConfig(); - const defaultConfig = appDefaultConfigs[id]; - if (defaultConfig === undefined && commonAppConfig === undefined) { - return appConfigs[id]; - } - return merge({}, defaultConfig, commonAppConfig, appConfigs[id]); + return merge({}, appDefaultConfigs[id], commonAppConfig, appConfigs[id]); } export function mergeAppConfig(id: string, newAppConfig: AppConfig) { diff --git a/runtime/react/hooks/useAppConfig.test.tsx b/runtime/react/hooks/useAppConfig.test.tsx new file mode 100644 index 00000000..5717a810 --- /dev/null +++ b/runtime/react/hooks/useAppConfig.test.tsx @@ -0,0 +1,19 @@ +import { renderHook } from '@testing-library/react'; +import { ReactNode } from 'react'; + +import { addAppConfigs, mergeSiteConfig } from '../../config'; +import CurrentAppProvider from '../CurrentAppProvider'; +import useAppConfig from './useAppConfig'; + +describe('useAppConfig', () => { + it('returns an empty object for an app with no config', () => { + mergeSiteConfig({ apps: [{ appId: 'bare-app' }] }); + addAppConfigs(); + + const wrapper = ({ children }: { children: ReactNode }) => ( + {children} + ); + const { result } = renderHook(() => useAppConfig(), { wrapper }); + expect(result.current).toEqual({}); + }); +}); diff --git a/runtime/scripts/index.ts b/runtime/scripts/index.ts index f8e83307..e93bcbcb 100644 --- a/runtime/scripts/index.ts +++ b/runtime/scripts/index.ts @@ -3,7 +3,7 @@ import { getAppConfig, getSiteConfig } from '../config'; export function loadExternalScripts() { const apps = getSiteConfig().apps ?? []; apps.forEach(app => { - const config = getAppConfig(app.appId) ?? {}; + const config = getAppConfig(app.appId); (app.externalScripts ?? []).forEach(ExternalScript => { const script = new ExternalScript({ config }); script.loadScript();