Skip to content

Commit

Permalink
KibanaLogic: Update navigateToUrl to use createHref helper
Browse files Browse the repository at this point in the history
+ add param.history value, since we're technically supposed to be using Kibana's passed history over anything from useHistory
  • Loading branch information
cee-chen committed Sep 29, 2020
1 parent e329ed4 commit 9bee2e9
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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');
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>;
}

export const KibanaLogic = kea<MakeLogicType<IKibanaValues>>({
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;
Expand Down

0 comments on commit 9bee2e9

Please sign in to comment.