Skip to content

Commit

Permalink
use App updater API instead of deprecated chrome.navLinks.update (#77708
Browse files Browse the repository at this point in the history
)
  • Loading branch information
pgayvallet authored Sep 17, 2020
1 parent 5be36b7 commit e3f3d5f
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 38 deletions.
9 changes: 7 additions & 2 deletions x-pack/plugins/observability/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -28,6 +30,8 @@ interface SetupPlugins {
export type ObservabilityPluginStart = void;

export class Plugin implements PluginClass<ObservabilityPluginSetup, ObservabilityPluginStart> {
private readonly appUpdater$ = new BehaviorSubject<AppUpdater>(() => ({}));

constructor(context: PluginInitializerContext) {}

public setup(core: CoreSetup, plugins: SetupPlugins) {
Expand All @@ -37,6 +41,7 @@ export class Plugin implements PluginClass<ObservabilityPluginSetup, Observabili
order: 8000,
euiIconType: 'logoObservability',
appRoute: '/app/observability',
updater$: this.appUpdater$,
category: DEFAULT_APP_CATEGORIES.observability,

mount: async (params: AppMountParameters<unknown>) => {
Expand Down Expand Up @@ -79,7 +84,7 @@ export class Plugin implements PluginClass<ObservabilityPluginSetup, Observabili
dashboard: { register: registerDataHandler },
};
}
public start(core: CoreStart) {
toggleOverviewLinkInNav(core);
public start({ application }: CoreStart) {
toggleOverviewLinkInNav(this.appUpdater$, application);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,58 @@
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { CoreStart } from 'kibana/public';

import { Subject } from 'rxjs';
import { AppUpdater, AppNavLinkStatus } from '../../../../src/core/public';
import { applicationServiceMock } from '../../../../src/core/public/mocks';

import { toggleOverviewLinkInNav } from './toggle_overview_link_in_nav';

describe('toggleOverviewLinkInNav', () => {
const update = jest.fn();
afterEach(() => {
update.mockClear();
let applicationStart: ReturnType<typeof applicationServiceMock.createStartContract>;
let subjectMock: jest.Mocked<Subject<AppUpdater>>;

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();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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<AppUpdater>,
{ 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,
}));
}
}

0 comments on commit e3f3d5f

Please sign in to comment.