Skip to content

Commit

Permalink
fix app updater for deepLinks
Browse files Browse the repository at this point in the history
  • Loading branch information
pgayvallet committed Jun 22, 2021
1 parent d7f2325 commit f1eb552
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 6 deletions.
83 changes: 81 additions & 2 deletions src/core/public/application/application_service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ import {
import { createElement } from 'react';
import { BehaviorSubject, Subject } from 'rxjs';
import { bufferCount, take, takeUntil } from 'rxjs/operators';
import { shallow, mount } from 'enzyme';
import { mount, shallow } from 'enzyme';

import { httpServiceMock } from '../http/http_service.mock';
import { overlayServiceMock } from '../overlays/overlay_service.mock';
import { MockLifecycle } from './test_types';
import { ApplicationService } from './application_service';
import { App, PublicAppInfo, AppNavLinkStatus, AppStatus, AppUpdater } from './types';
import { App, AppDeepLink, AppNavLinkStatus, AppStatus, AppUpdater, PublicAppInfo } from './types';
import { act } from 'react-dom/test-utils';

const createApp = (props: Partial<App>): App => {
Expand Down Expand Up @@ -365,6 +365,85 @@ describe('#setup()', () => {
expect(MockHistory.push).toHaveBeenCalledWith('/app/app1', undefined);
MockHistory.push.mockClear();
});

it('preserves the deep links if the update does not modify them', async () => {
const setup = service.setup(setupDeps);

const pluginId = Symbol('plugin');
const updater$ = new BehaviorSubject<AppUpdater>((app) => ({}));

const deepLinks: AppDeepLink[] = [
{
id: 'foo',
title: 'Foo',
searchable: true,
navLinkStatus: AppNavLinkStatus.visible,
path: '/foo',
},
{
id: 'bar',
title: 'Bar',
searchable: false,
navLinkStatus: AppNavLinkStatus.hidden,
path: '/bar',
},
];

setup.register(pluginId, createApp({ id: 'app1', deepLinks, updater$ }));

const { applications$ } = await service.start(startDeps);

updater$.next((app) => ({ defaultPath: '/foo' }));

let appInfos = await applications$.pipe(take(1)).toPromise();

expect(appInfos.get('app1')!.deepLinks).toEqual([
{
deepLinks: [],
id: 'foo',
keywords: [],
navLinkStatus: 1,
path: '/foo',
searchable: true,
title: 'Foo',
},
{
deepLinks: [],
id: 'bar',
keywords: [],
navLinkStatus: 3,
path: '/bar',
searchable: false,
title: 'Bar',
},
]);

updater$.next((app) => ({
deepLinks: [
{
id: 'bar',
title: 'Bar',
searchable: false,
navLinkStatus: AppNavLinkStatus.hidden,
path: '/bar',
},
],
}));

appInfos = await applications$.pipe(take(1)).toPromise();

expect(appInfos.get('app1')!.deepLinks).toEqual([
{
deepLinks: [],
id: 'bar',
keywords: [],
navLinkStatus: 3,
path: '/bar',
searchable: false,
title: 'Bar',
},
]);
});
});
});

Expand Down
10 changes: 6 additions & 4 deletions src/core/public/application/application_service.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ function filterAvailable<T>(m: Map<string, T>, capabilities: Capabilities) {
)
);
}

const findMounter = (mounters: Map<string, Mounter>, appRoute?: string) =>
[...mounters].find(([, mounter]) => mounter.appRoute === appRoute);

Expand Down Expand Up @@ -401,6 +402,9 @@ const updateStatus = (app: App, statusUpdaters: AppUpdaterWrapper[]): App => {
}
const fields = wrapper.updater(app);
if (fields) {
const deepLinks = fields.deepLinks
? populateDeepLinkDefaults(fields.deepLinks)
: changes.deepLinks;
changes = {
...changes,
...fields,
Expand All @@ -414,13 +418,11 @@ const updateStatus = (app: App, statusUpdaters: AppUpdaterWrapper[]): App => {
changes.navLinkStatus ?? AppNavLinkStatus.default,
fields.navLinkStatus ?? AppNavLinkStatus.default
),
// deepLinks take the last defined update
deepLinks: fields.deepLinks
? populateDeepLinkDefaults(fields.deepLinks)
: changes.deepLinks,
...(deepLinks ? { deepLinks } : {}),
};
}
});

return {
...app,
...changes,
Expand Down

0 comments on commit f1eb552

Please sign in to comment.