From 9bee2e9bcf9f6303e43b18d4373d2ad8e47616e8 Mon Sep 17 00:00:00 2001 From: Constance Chen Date: Mon, 28 Sep 2020 20:19:45 -0700 Subject: [PATCH] KibanaLogic: Update navigateToUrl to use createHref helper + add param.history value, since we're technically supposed to be using Kibana's passed history over anything from useHistory --- .../__mocks__/kibana_logic.mock.ts | 3 +++ .../public/applications/index.tsx | 1 + .../shared/kibana/kibana_logic.test.ts | 21 ++++++++++++++++++- .../shared/kibana/kibana_logic.ts | 20 +++++++++++++++--- 4 files changed, 41 insertions(+), 4 deletions(-) diff --git a/x-pack/plugins/enterprise_search/public/applications/__mocks__/kibana_logic.mock.ts b/x-pack/plugins/enterprise_search/public/applications/__mocks__/kibana_logic.mock.ts index 9f3c2443bc9b8..419db86148984 100644 --- a/x-pack/plugins/enterprise_search/public/applications/__mocks__/kibana_logic.mock.ts +++ b/x-pack/plugins/enterprise_search/public/applications/__mocks__/kibana_logic.mock.ts @@ -4,8 +4,11 @@ * you may not use this file except in compliance with the Elastic License. */ +import { mockHistory } from './'; + export const mockKibanaValues = { config: { host: 'http://localhost:3002' }, + history: mockHistory, navigateToUrl: jest.fn(), setBreadcrumbs: jest.fn(), setDocTitle: jest.fn(), diff --git a/x-pack/plugins/enterprise_search/public/applications/index.tsx b/x-pack/plugins/enterprise_search/public/applications/index.tsx index 63be9b684e56f..4b5f1374814d5 100644 --- a/x-pack/plugins/enterprise_search/public/applications/index.tsx +++ b/x-pack/plugins/enterprise_search/public/applications/index.tsx @@ -41,6 +41,7 @@ export const renderApp = ( const unmountKibanaLogic = mountKibanaLogic({ config, + history: params.history, navigateToUrl: core.application.navigateToUrl, setBreadcrumbs: core.chrome.setBreadcrumbs, setDocTitle: core.chrome.docTitle.change, diff --git a/x-pack/plugins/enterprise_search/public/applications/shared/kibana/kibana_logic.test.ts b/x-pack/plugins/enterprise_search/public/applications/shared/kibana/kibana_logic.test.ts index 99262cca97ca8..4d51362a7e11b 100644 --- a/x-pack/plugins/enterprise_search/public/applications/shared/kibana/kibana_logic.test.ts +++ b/x-pack/plugins/enterprise_search/public/applications/shared/kibana/kibana_logic.test.ts @@ -20,7 +20,10 @@ describe('KibanaLogic', () => { it('sets values from props', () => { mountKibanaLogic(mockKibanaValues); - expect(KibanaLogic.values).toEqual(mockKibanaValues); + expect(KibanaLogic.values).toEqual({ + ...mockKibanaValues, + navigateToUrl: expect.any(Function), + }); }); it('gracefully handles missing configs', () => { @@ -29,4 +32,20 @@ describe('KibanaLogic', () => { expect(KibanaLogic.values.config).toEqual({}); }); }); + + describe('navigateToUrl()', () => { + beforeEach(() => mountKibanaLogic(mockKibanaValues)); + + it('runs paths through createHref before calling navigateToUrl', () => { + KibanaLogic.values.navigateToUrl('/test'); + + expect(mockKibanaValues.navigateToUrl).toHaveBeenCalledWith('/app/enterprise_search/test'); + }); + + it('does not run paths through createHref if the shouldNotCreateHref option is passed', () => { + KibanaLogic.values.navigateToUrl('/test', { shouldNotCreateHref: true }); + + expect(mockKibanaValues.navigateToUrl).toHaveBeenCalledWith('/test'); + }); + }); }); diff --git a/x-pack/plugins/enterprise_search/public/applications/shared/kibana/kibana_logic.ts b/x-pack/plugins/enterprise_search/public/applications/shared/kibana/kibana_logic.ts index a884acb02d10a..5904c6c89e39c 100644 --- a/x-pack/plugins/enterprise_search/public/applications/shared/kibana/kibana_logic.ts +++ b/x-pack/plugins/enterprise_search/public/applications/shared/kibana/kibana_logic.ts @@ -6,26 +6,40 @@ import { kea, MakeLogicType } from 'kea'; +import { History } from 'history'; import { ApplicationStart, ChromeBreadcrumb } from 'src/core/public'; -export interface IKibanaValues { +import { createHref, ICreateHrefOptions } from '../react_router_helpers'; + +interface IKibanaLogicProps { config: { host?: string }; + history: History; navigateToUrl: ApplicationStart['navigateToUrl']; setBreadcrumbs(crumbs: ChromeBreadcrumb[]): void; setDocTitle(title: string): void; } +export interface IKibanaValues extends IKibanaLogicProps { + navigateToUrl(path: string, options?: ICreateHrefOptions): Promise; +} export const KibanaLogic = kea>({ path: ['enterprise_search', 'kibana_logic'], reducers: ({ props }) => ({ config: [props.config || {}, {}], - navigateToUrl: [props.navigateToUrl, {}], + history: [props.history, {}], + navigateToUrl: [ + (url: string, options?: ICreateHrefOptions) => { + const href = createHref(url, props.history, options); + return props.navigateToUrl(href); + }, + {}, + ], setBreadcrumbs: [props.setBreadcrumbs, {}], setDocTitle: [props.setDocTitle, {}], }), }); -export const mountKibanaLogic = (props: IKibanaValues) => { +export const mountKibanaLogic = (props: IKibanaLogicProps) => { KibanaLogic(props); const unmount = KibanaLogic.mount(); return unmount;