Skip to content

Commit

Permalink
Add new getEngineBreadcrumbs utility helper
Browse files Browse the repository at this point in the history
  • Loading branch information
cee-chen committed Apr 13, 2021
1 parent 4f6bd31 commit 6044cf3
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import { EngineDetails } from '../components/engine/types';
import { ENGINES_TITLE } from '../components/engines';
import { generateEncodedPath } from '../utils/encode_path_params';

export const mockEngineValues = {
Expand All @@ -20,11 +21,17 @@ export const mockEngineActions = {
export const mockGenerateEnginePath = jest.fn((path, pathParams = {}) =>
generateEncodedPath(path, { engineName: mockEngineValues.engineName, ...pathParams })
);
export const mockGetEngineBreadcrumbs = jest.fn((breadcrumbs = []) => [
ENGINES_TITLE,
mockEngineValues.engineName,
...breadcrumbs,
]);

jest.mock('../components/engine', () => ({
EngineLogic: {
values: mockEngineValues,
actions: mockEngineActions,
},
generateEnginePath: mockGenerateEnginePath,
getEngineBreadcrumbs: mockGetEngineBreadcrumbs,
}));
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
export { EngineRouter } from './engine_router';
export { EngineNav } from './engine_nav';
export { EngineLogic } from './engine_logic';
export { generateEnginePath } from './utils';
export { generateEnginePath, getEngineBreadcrumbs } from './utils';
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@

import { mockEngineValues } from '../../__mocks__';

import { generateEnginePath } from './utils';
import { generateEnginePath, getEngineBreadcrumbs } from './utils';

describe('generateEnginePath', () => {
mockEngineValues.engineName = 'hello-world';
beforeEach(() => {
mockEngineValues.engineName = 'hello-world';
});

it('generates paths with engineName filled from state', () => {
expect(generateEnginePath('/engines/:engineName/example')).toEqual(
Expand All @@ -27,3 +29,15 @@ describe('generateEnginePath', () => {
).toEqual('/engines/override/foo/baz');
});
});

describe('getEngineBreadcrumbs', () => {
beforeEach(() => {
mockEngineValues.engineName = 'foo';
});

it('generates breadcrumbs with engineName filled from state', () => {
expect(getEngineBreadcrumbs(['bar', 'baz'])).toEqual(['Engines', 'foo', 'bar', 'baz']);
expect(getEngineBreadcrumbs(['bar'])).toEqual(['Engines', 'foo', 'bar']);
expect(getEngineBreadcrumbs()).toEqual(['Engines', 'foo']);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
* 2.0.
*/

import { BreadcrumbTrail } from '../../../shared/kibana_chrome/generate_breadcrumbs';
import { generateEncodedPath } from '../../utils/encode_path_params';

import { ENGINES_TITLE } from '../engines';

import { EngineLogic } from './';

/**
Expand All @@ -16,3 +19,11 @@ export const generateEnginePath = (path: string, pathParams: object = {}) => {
const { engineName } = EngineLogic.values;
return generateEncodedPath(path, { engineName, ...pathParams });
};

/**
* Generate a breadcrumb trail with engineName automatically filled from EngineLogic state
*/
export const getEngineBreadcrumbs = (breadcrumbs: BreadcrumbTrail = []) => {
const { engineName } = EngineLogic.values;
return [ENGINES_TITLE, engineName, ...breadcrumbs];
};

0 comments on commit 6044cf3

Please sign in to comment.