Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions runtime/config/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
10 changes: 4 additions & 6 deletions runtime/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
19 changes: 19 additions & 0 deletions runtime/react/hooks/useAppConfig.test.tsx
Original file line number Diff line number Diff line change
@@ -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 }) => (
<CurrentAppProvider appId="bare-app">{children}</CurrentAppProvider>
);
const { result } = renderHook(() => useAppConfig(), { wrapper });
expect(result.current).toEqual({});
});
});
2 changes: 1 addition & 1 deletion runtime/scripts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Loading