From e3f3d5fbf900fd2348818abaf85e0a76d00b703f Mon Sep 17 00:00:00 2001 From: Pierre Gayvallet Date: Thu, 17 Sep 2020 13:36:58 +0200 Subject: [PATCH] use App updater API instead of deprecated chrome.navLinks.update (#77708) --- x-pack/plugins/observability/public/plugin.ts | 9 ++- .../toggle_overview_link_in_nav.test.tsx | 75 +++++++++++-------- .../public/toggle_overview_link_in_nav.tsx | 14 +++- 3 files changed, 60 insertions(+), 38 deletions(-) diff --git a/x-pack/plugins/observability/public/plugin.ts b/x-pack/plugins/observability/public/plugin.ts index 80173cd1560d7..0a82f37d10a7b 100644 --- a/x-pack/plugins/observability/public/plugin.ts +++ b/x-pack/plugins/observability/public/plugin.ts @@ -4,9 +4,11 @@ * you may not use this file except in compliance with the Elastic License. */ +import { BehaviorSubject } from 'rxjs'; import { i18n } from '@kbn/i18n'; import { AppMountParameters, + AppUpdater, CoreSetup, DEFAULT_APP_CATEGORIES, Plugin as PluginClass, @@ -28,6 +30,8 @@ interface SetupPlugins { export type ObservabilityPluginStart = void; export class Plugin implements PluginClass { + private readonly appUpdater$ = new BehaviorSubject(() => ({})); + constructor(context: PluginInitializerContext) {} public setup(core: CoreSetup, plugins: SetupPlugins) { @@ -37,6 +41,7 @@ export class Plugin implements PluginClass) => { @@ -79,7 +84,7 @@ export class Plugin implements PluginClass { - const update = jest.fn(); - afterEach(() => { - update.mockClear(); + let applicationStart: ReturnType; + let subjectMock: jest.Mocked>; + + beforeEach(() => { + applicationStart = applicationServiceMock.createStartContract(); + subjectMock = { + next: jest.fn(), + } as any; }); + it('hides overview menu', () => { - const core = ({ - application: { - capabilities: { - navLinks: { - apm: false, - logs: false, - metrics: false, - uptime: false, - }, - }, + applicationStart.capabilities = { + management: {}, + catalogue: {}, + navLinks: { + apm: false, + logs: false, + metrics: false, + uptime: false, }, - chrome: { navLinks: { update } }, - } as unknown) as CoreStart; - toggleOverviewLinkInNav(core); - expect(update).toHaveBeenCalledWith('observability-overview', { hidden: true }); + }; + + toggleOverviewLinkInNav(subjectMock, applicationStart); + + expect(subjectMock.next).toHaveBeenCalledTimes(1); + const updater = subjectMock.next.mock.calls[0][0]!; + expect(updater({} as any)).toEqual({ + navLinkStatus: AppNavLinkStatus.hidden, + }); }); it('shows overview menu', () => { - const core = ({ - application: { - capabilities: { - navLinks: { - apm: true, - logs: false, - metrics: false, - uptime: false, - }, - }, + applicationStart.capabilities = { + management: {}, + catalogue: {}, + navLinks: { + apm: true, + logs: false, + metrics: false, + uptime: false, }, - chrome: { navLinks: { update } }, - } as unknown) as CoreStart; - toggleOverviewLinkInNav(core); - expect(update).not.toHaveBeenCalled(); + }; + + toggleOverviewLinkInNav(subjectMock, applicationStart); + + expect(subjectMock.next).not.toHaveBeenCalled(); }); }); diff --git a/x-pack/plugins/observability/public/toggle_overview_link_in_nav.tsx b/x-pack/plugins/observability/public/toggle_overview_link_in_nav.tsx index c33ca45e4fcd8..ad8b6a323a897 100644 --- a/x-pack/plugins/observability/public/toggle_overview_link_in_nav.tsx +++ b/x-pack/plugins/observability/public/toggle_overview_link_in_nav.tsx @@ -4,12 +4,18 @@ * you may not use this file except in compliance with the Elastic License. */ -import { CoreStart } from 'kibana/public'; +import { Subject } from 'rxjs'; +import { AppNavLinkStatus, AppUpdater, ApplicationStart } from '../../../../src/core/public'; -export function toggleOverviewLinkInNav(core: CoreStart) { - const { apm, logs, metrics, uptime } = core.application.capabilities.navLinks; +export function toggleOverviewLinkInNav( + updater$: Subject, + { capabilities }: ApplicationStart +) { + const { apm, logs, metrics, uptime } = capabilities.navLinks; const someVisible = Object.values({ apm, logs, metrics, uptime }).some((visible) => visible); if (!someVisible) { - core.chrome.navLinks.update('observability-overview', { hidden: true }); + updater$.next(() => ({ + navLinkStatus: AppNavLinkStatus.hidden, + })); } }