From 6044cf328073e1423dec9c626b359b5a4d84e5cf Mon Sep 17 00:00:00 2001 From: Constance Chen Date: Mon, 12 Apr 2021 18:10:08 -0700 Subject: [PATCH] Add new getEngineBreadcrumbs utility helper --- .../app_search/__mocks__/engine_logic.mock.ts | 7 +++++++ .../app_search/components/engine/index.ts | 2 +- .../app_search/components/engine/utils.test.ts | 18 ++++++++++++++++-- .../app_search/components/engine/utils.ts | 11 +++++++++++ 4 files changed, 35 insertions(+), 3 deletions(-) diff --git a/x-pack/plugins/enterprise_search/public/applications/app_search/__mocks__/engine_logic.mock.ts b/x-pack/plugins/enterprise_search/public/applications/app_search/__mocks__/engine_logic.mock.ts index 485ac19f2eb82..d16391089120a 100644 --- a/x-pack/plugins/enterprise_search/public/applications/app_search/__mocks__/engine_logic.mock.ts +++ b/x-pack/plugins/enterprise_search/public/applications/app_search/__mocks__/engine_logic.mock.ts @@ -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 = { @@ -20,6 +21,11 @@ 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: { @@ -27,4 +33,5 @@ jest.mock('../components/engine', () => ({ actions: mockEngineActions, }, generateEnginePath: mockGenerateEnginePath, + getEngineBreadcrumbs: mockGetEngineBreadcrumbs, })); diff --git a/x-pack/plugins/enterprise_search/public/applications/app_search/components/engine/index.ts b/x-pack/plugins/enterprise_search/public/applications/app_search/components/engine/index.ts index 80c36822ccde0..2a5b3351f41f7 100644 --- a/x-pack/plugins/enterprise_search/public/applications/app_search/components/engine/index.ts +++ b/x-pack/plugins/enterprise_search/public/applications/app_search/components/engine/index.ts @@ -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'; diff --git a/x-pack/plugins/enterprise_search/public/applications/app_search/components/engine/utils.test.ts b/x-pack/plugins/enterprise_search/public/applications/app_search/components/engine/utils.test.ts index 867ed14fcc052..be6b9a53bd0d5 100644 --- a/x-pack/plugins/enterprise_search/public/applications/app_search/components/engine/utils.test.ts +++ b/x-pack/plugins/enterprise_search/public/applications/app_search/components/engine/utils.test.ts @@ -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( @@ -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']); + }); +}); diff --git a/x-pack/plugins/enterprise_search/public/applications/app_search/components/engine/utils.ts b/x-pack/plugins/enterprise_search/public/applications/app_search/components/engine/utils.ts index 7b8521105875c..820d89e473922 100644 --- a/x-pack/plugins/enterprise_search/public/applications/app_search/components/engine/utils.ts +++ b/x-pack/plugins/enterprise_search/public/applications/app_search/components/engine/utils.ts @@ -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 './'; /** @@ -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]; +};