Skip to content

Commit

Permalink
[AppService] fix deepLinks being lost when updating the app with othe…
Browse files Browse the repository at this point in the history
…r fields (#102895)

* fix app updater for deepLinks

* improve implem
  • Loading branch information
pgayvallet committed Jun 22, 2021
1 parent 69a5d01 commit 016259d
Show file tree
Hide file tree
Showing 2 changed files with 84 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
7 changes: 3 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 @@ -414,13 +415,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,
...(fields.deepLinks ? { deepLinks: populateDeepLinkDefaults(fields.deepLinks) } : {}),
};
}
});

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

0 comments on commit 016259d

Please sign in to comment.