Skip to content

Commit

Permalink
Reset chrome fields while switching an app (#73064) (#75953)
Browse files Browse the repository at this point in the history
* Reset chrome help extension while switching an app

* Reset other chrome fields

* Set docTitle in saved objects app

* Add unit tests

Co-authored-by: Elastic Machine <[email protected]>

Co-authored-by: Elastic Machine <[email protected]>
  • Loading branch information
sulemanof and elasticmachine authored Aug 27, 2020
1 parent 54dc88b commit 7c44674
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
53 changes: 53 additions & 0 deletions src/core/public/chrome/chrome_service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,59 @@ describe('start', () => {
`);
});
});

describe('erase chrome fields', () => {
it('while switching an app', async () => {
const startDeps = defaultStartDeps([new FakeApp('alpha')]);
const { navigateToApp } = startDeps.application;
const { chrome, service } = await start({ startDeps });

const helpExtensionPromise = chrome.getHelpExtension$().pipe(toArray()).toPromise();
const breadcrumbsPromise = chrome.getBreadcrumbs$().pipe(toArray()).toPromise();
const badgePromise = chrome.getBadge$().pipe(toArray()).toPromise();
const docTitleResetSpy = jest.spyOn(chrome.docTitle, 'reset');

const promises = Promise.all([helpExtensionPromise, breadcrumbsPromise, badgePromise]);

chrome.setHelpExtension({ appName: 'App name' });
chrome.setBreadcrumbs([{ text: 'App breadcrumb' }]);
chrome.setBadge({ text: 'App badge', tooltip: 'App tooltip' });

navigateToApp('alpha');

service.stop();

expect(docTitleResetSpy).toBeCalledTimes(1);
await expect(promises).resolves.toMatchInlineSnapshot(`
Array [
Array [
undefined,
Object {
"appName": "App name",
},
undefined,
],
Array [
Array [],
Array [
Object {
"text": "App breadcrumb",
},
],
Array [],
],
Array [
undefined,
Object {
"text": "App badge",
"tooltip": "App tooltip",
},
undefined,
],
]
`);
});
});
});

describe('stop', () => {
Expand Down
8 changes: 8 additions & 0 deletions src/core/public/chrome/chrome_service.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,14 @@ export class ChromeService {
const recentlyAccessed = await this.recentlyAccessed.start({ http });
const docTitle = this.docTitle.start({ document: window.document });

// erase chrome fields from a previous app while switching to a next app
application.currentAppId$.subscribe(() => {
helpExtension$.next(undefined);
breadcrumbs$.next([]);
badge$.next(undefined);
docTitle.reset();
});

const setIsNavDrawerLocked = (isLocked: boolean) => {
isNavDrawerLocked$.next(isLocked);
localStorage.setItem(IS_LOCKED_KEY, `${isLocked}`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import React, { lazy, Suspense } from 'react';
import ReactDOM from 'react-dom';
import { Router, Switch, Route } from 'react-router-dom';
import { I18nProvider } from '@kbn/i18n/react';
import { i18n } from '@kbn/i18n';
import { EuiLoadingSpinner } from '@elastic/eui';
import { CoreSetup } from 'src/core/public';
import { ManagementAppMountParams } from '../../../management/public';
Expand All @@ -36,6 +37,10 @@ interface MountParams {

let allowedObjectTypes: string[] | undefined;

const title = i18n.translate('savedObjectsManagement.objects.savedObjectsTitle', {
defaultMessage: 'Saved Objects',
});

const SavedObjectsEditionPage = lazy(() => import('./saved_objects_edition_page'));
const SavedObjectsTablePage = lazy(() => import('./saved_objects_table_page'));
export const mountManagementSection = async ({
Expand All @@ -49,6 +54,8 @@ export const mountManagementSection = async ({
allowedObjectTypes = await getAllowedTypes(coreStart.http);
}

coreStart.chrome.docTitle.change(title);

const capabilities = coreStart.application.capabilities;

const RedirectToHomeIfUnauthorized: React.FunctionComponent = ({ children }) => {
Expand Down

0 comments on commit 7c44674

Please sign in to comment.