Skip to content

Commit

Permalink
Update breadcrumbs & eui link helpers w/ new KibanaLogic changes
Browse files Browse the repository at this point in the history
- navigateToUrl is now double-dipping createHref, so we update it to not do so
- remove useHistory in favor of grabbing history from KibanaLogic
  • Loading branch information
cee-chen committed Sep 29, 2020
1 parent 9bee2e9 commit a60020b
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/

import '../../__mocks__/kea.mock';
import '../../__mocks__/react_router_history.mock';
import { mockKibanaValues, mockHistory } from '../../__mocks__';

jest.mock('../react_router_helpers', () => ({
Expand Down Expand Up @@ -53,21 +52,23 @@ describe('useBreadcrumbs', () => {

it('prevents default navigation and uses React Router history on click', () => {
const breadcrumb = useBreadcrumbs([{ text: '', path: '/test' }])[0] as any;

expect(breadcrumb.href).toEqual('/app/enterprise_search/test');
expect(mockHistory.createHref).toHaveBeenCalled();

const event = { preventDefault: jest.fn() };
breadcrumb.onClick(event);

expect(mockKibanaValues.navigateToUrl).toHaveBeenCalledWith('/app/enterprise_search/test');
expect(mockHistory.createHref).toHaveBeenCalled();
expect(event.preventDefault).toHaveBeenCalled();
expect(mockKibanaValues.navigateToUrl).toHaveBeenCalled();
});

it('does not call createHref if shouldNotCreateHref is passed', () => {
const breadcrumb = useBreadcrumbs([
{ text: '', path: '/test', shouldNotCreateHref: true },
])[0] as any;
breadcrumb.onClick({ preventDefault: () => null });

expect(mockKibanaValues.navigateToUrl).toHaveBeenCalledWith('/test');
expect(breadcrumb.href).toEqual('/test');
expect(mockHistory.createHref).not.toHaveBeenCalled();
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/

import { useValues } from 'kea';
import { useHistory } from 'react-router-dom';
import { EuiBreadcrumb } from '@elastic/eui';

import { KibanaLogic } from '../../shared/kibana';
Expand Down Expand Up @@ -33,20 +32,17 @@ interface IBreadcrumb {
export type TBreadcrumbs = IBreadcrumb[];

export const useBreadcrumbs = (breadcrumbs: TBreadcrumbs) => {
const history = useHistory();
const { navigateToUrl } = useValues(KibanaLogic);
const { navigateToUrl, history } = useValues(KibanaLogic);

return breadcrumbs.map(({ text, path, shouldNotCreateHref }) => {
const breadcrumb = { text } as EuiBreadcrumb;

if (path) {
const href = createHref(path, history, { shouldNotCreateHref });

breadcrumb.href = href;
breadcrumb.href = createHref(path, history, { shouldNotCreateHref });
breadcrumb.onClick = (event) => {
if (letBrowserHandleEvent(event)) return;
event.preventDefault();
navigateToUrl(href);
navigateToUrl(path, { shouldNotCreateHref });
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/

import '../../__mocks__/kea.mock';
import '../../__mocks__/react_router_history.mock';

import React from 'react';
import { shallow, mount } from 'enzyme';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import React from 'react';
import { useValues } from 'kea';
import { useHistory } from 'react-router-dom';
import { EuiLink, EuiButton, EuiButtonProps, EuiLinkAnchorProps } from '@elastic/eui';

import { KibanaLogic } from '../../shared/kibana';
Expand All @@ -33,8 +32,7 @@ export const EuiReactRouterHelper: React.FC<IEuiReactRouterProps> = ({
shouldNotCreateHref,
children,
}) => {
const history = useHistory();
const { navigateToUrl } = useValues(KibanaLogic);
const { navigateToUrl, history } = useValues(KibanaLogic);

// Generate the correct link href (with basename etc. accounted for)
const href = createHref(to, history, { shouldNotCreateHref });
Expand All @@ -47,7 +45,7 @@ export const EuiReactRouterHelper: React.FC<IEuiReactRouterProps> = ({
event.preventDefault();

// Perform SPA navigation.
navigateToUrl(href);
navigateToUrl(to, { shouldNotCreateHref });
};

const reactRouterProps = { href, onClick: reactRouterLinkClick };
Expand Down

0 comments on commit a60020b

Please sign in to comment.