Skip to content

Commit

Permalink
Add setDocTitle context + behavior to set_chrome
Browse files Browse the repository at this point in the history
+ refactor set_chrome.test.tsx:
  - add title tests
  - add SetWorkplaceSearchChrome test to increase coverage
  - clean up inner/outer call in favor of simpler mocks/imports
  - simplify isRoot tests
  • Loading branch information
cee-chen committed Aug 5, 2020
1 parent dac16d4 commit b07866f
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ import { httpServiceMock } from 'src/core/public/mocks';
export const mockKibanaContext = {
http: httpServiceMock.createSetupContract(),
setBreadcrumbs: jest.fn(),
setDocTitle: jest.fn(),
enterpriseSearchUrl: 'http://localhost:3002',
};
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface IKibanaContext {
enterpriseSearchUrl?: string;
http: HttpSetup;
setBreadcrumbs(crumbs: ChromeBreadcrumb[]): void;
setDocTitle(title: string): void;
}

export const KibanaContext = React.createContext({});
Expand All @@ -41,6 +42,7 @@ export const renderApp = (
http: core.http,
enterpriseSearchUrl: config.host,
setBreadcrumbs: core.chrome.setBreadcrumbs,
setDocTitle: core.chrome.docTitle.change,
}}
>
<LicenseProvider license$={plugins.licensing.license$}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,58 +7,78 @@
import React from 'react';

import '../../__mocks__/react_router_history.mock';
import { mountWithKibanaContext } from '../../__mocks__';
import { mockKibanaContext, mountWithKibanaContext } from '../../__mocks__';

jest.mock('./generate_breadcrumbs', () => ({ appSearchBreadcrumbs: jest.fn() }));
import { appSearchBreadcrumbs } from './generate_breadcrumbs';
import { SetAppSearchChrome } from './';
jest.mock('./generate_breadcrumbs', () => ({
appSearchBreadcrumbs: jest.fn(() => (crumbs: any) => crumbs),
workplaceSearchBreadcrumbs: jest.fn(() => (crumbs: any) => crumbs),
}));
import { appSearchBreadcrumbs, workplaceSearchBreadcrumbs } from './generate_breadcrumbs';

jest.mock('./generate_title', () => ({
appSearchTitle: jest.fn((title: any) => title),
workplaceSearchTitle: jest.fn((title: any) => title),
}));
import { appSearchTitle, workplaceSearchTitle } from './generate_title';

import { SetAppSearchChrome, SetWorkplaceSearchChrome } from './';

describe('SetAppSearchChrome', () => {
const setBreadcrumbs = jest.fn();
const builtBreadcrumbs = [] as any;
const appSearchBreadCrumbsInnerCall = jest.fn().mockReturnValue(builtBreadcrumbs);
const appSearchBreadCrumbsOuterCall = jest.fn().mockReturnValue(appSearchBreadCrumbsInnerCall);
(appSearchBreadcrumbs as jest.Mock).mockImplementation(appSearchBreadCrumbsOuterCall);
beforeEach(() => {
jest.clearAllMocks();
});

afterEach(() => {
jest.clearAllMocks();
expect(appSearchBreadcrumbs).toHaveBeenCalled();
expect(appSearchTitle).toHaveBeenCalled();
});

const mountSetAppSearchChrome = (props: any) => {
return mountWithKibanaContext(<SetAppSearchChrome {...props} />, {
http: {},
enterpriseSearchUrl: 'http://localhost:3002',
setBreadcrumbs,
});
};
it('sets breadcrumbs and document title', () => {
mountWithKibanaContext(<SetAppSearchChrome text="Engines" />);

describe('when isRoot is false', () => {
const subject = () => mountSetAppSearchChrome({ text: 'Page 1', isRoot: false });
expect(mockKibanaContext.setBreadcrumbs).toHaveBeenCalledWith([
{
text: 'Engines',
path: '/current-path',
},
]);
expect(mockKibanaContext.setDocTitle).toHaveBeenCalledWith(['Engines']);
});

it('calls appSearchBreadcrumbs to build breadcrumbs, then registers them with Kibana', () => {
subject();
it('sets empty breadcrumbs and document title when isRoot is true', () => {
mountWithKibanaContext(<SetAppSearchChrome isRoot />);

// calls appSearchBreadcrumbs to build breadcrumbs with the target page and current location
expect(appSearchBreadCrumbsInnerCall).toHaveBeenCalledWith([
{ text: 'Page 1', path: '/current-path' },
]);
expect(mockKibanaContext.setBreadcrumbs).toHaveBeenCalledWith([]);
expect(mockKibanaContext.setDocTitle).toHaveBeenCalledWith([]);
});
});

describe('SetWorkplaceSearchChrome', () => {
beforeEach(() => {
jest.clearAllMocks();
});

// then registers them with Kibana
expect(setBreadcrumbs).toHaveBeenCalledWith(builtBreadcrumbs);
});
afterEach(() => {
expect(workplaceSearchBreadcrumbs).toHaveBeenCalled();
expect(workplaceSearchTitle).toHaveBeenCalled();
});

describe('when isRoot is true', () => {
const subject = () => mountSetAppSearchChrome({ text: 'Page 1', isRoot: true });
it('sets breadcrumbs and document title', () => {
mountWithKibanaContext(<SetWorkplaceSearchChrome text="Sources" />);

it('calls appSearchBreadcrumbs to build breadcrumbs with an empty breadcrumb, then registers them with Kibana', () => {
subject();
expect(mockKibanaContext.setBreadcrumbs).toHaveBeenCalledWith([
{
text: 'Sources',
path: '/current-path',
},
]);
expect(mockKibanaContext.setDocTitle).toHaveBeenCalledWith(['Sources']);
});

// uses an empty bredcrumb
expect(appSearchBreadCrumbsInnerCall).toHaveBeenCalledWith([]);
it('sets empty breadcrumbs and document title when isRoot is true', () => {
mountWithKibanaContext(<SetWorkplaceSearchChrome isRoot />);

// then registers them with Kibana
expect(setBreadcrumbs).toHaveBeenCalledWith(builtBreadcrumbs);
});
expect(mockKibanaContext.setBreadcrumbs).toHaveBeenCalledWith([]);
expect(mockKibanaContext.setDocTitle).toHaveBeenCalledWith([]);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
workplaceSearchBreadcrumbs,
TBreadcrumbs,
} from './generate_breadcrumbs';
import { appSearchTitle, workplaceSearchTitle, TTitle } from './generate_title';

/**
* Helpers for setting Kibana chrome (breadcrumbs, doc titles) on React view mount
Expand All @@ -33,25 +34,29 @@ type TBreadcrumbsProps = IBreadcrumbsProps | IRootBreadcrumbsProps;

export const SetAppSearchChrome: React.FC<TBreadcrumbsProps> = ({ text, isRoot }) => {
const history = useHistory();
const { setBreadcrumbs } = useContext(KibanaContext) as IKibanaContext;
const { setBreadcrumbs, setDocTitle } = useContext(KibanaContext) as IKibanaContext;

const crumb = isRoot ? [] : [{ text, path: history.location.pathname }];
const title = isRoot ? [] : [text];

useEffect(() => {
setBreadcrumbs(appSearchBreadcrumbs(history)(crumb as TBreadcrumbs | []));
setDocTitle(appSearchTitle(title as TTitle | []));
}, []);

return null;
};

export const SetWorkplaceSearchChrome: React.FC<TBreadcrumbsProps> = ({ text, isRoot }) => {
const history = useHistory();
const { setBreadcrumbs } = useContext(KibanaContext) as IKibanaContext;
const { setBreadcrumbs, setDocTitle } = useContext(KibanaContext) as IKibanaContext;

const crumb = isRoot ? [] : [{ text, path: history.location.pathname }];
const title = isRoot ? [] : [text];

useEffect(() => {
setBreadcrumbs(workplaceSearchBreadcrumbs(history)(crumb as TBreadcrumbs | []));
setDocTitle(workplaceSearchTitle(title as TTitle | []));
}, []);

return null;
Expand Down

0 comments on commit b07866f

Please sign in to comment.