Skip to content

Commit

Permalink
Added unit tests for public (#4)
Browse files Browse the repository at this point in the history
* application.test.ts

* Added Unit Test for EngineOverviewHeader

* Added Unit Test for generate_breadcrumbs

* Added Unit Test for set_breadcrumb.tsx

* Added a unit test for link_events

- Also changed link_events.tsx to link_events.ts since it's just TS, no
React
- Modified letBrowserHandleEvent so it will still return a false
boolean when target is blank

* Betterize these tests

Co-Authored-By: Constance <constancecchen@users.noreply.github.com>

Co-authored-by: Constance <constancecchen@users.noreply.github.com>
  • Loading branch information
2 people authored and cee-chen committed May 7, 2020
1 parent 38703ce commit 8df6d3e
Show file tree
Hide file tree
Showing 8 changed files with 431 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import React from 'react';

import { EngineOverviewHeader } from '../engine_overview_header';
import { mountWithKibanaContext } from '../../../test_utils/helpers';

describe('EngineOverviewHeader', () => {
describe('when enterpriseSearchUrl is set', () => {
let wrapper;

beforeEach(() => {
wrapper = mountWithKibanaContext(<EngineOverviewHeader />, {
enterpriseSearchUrl: 'http://localhost:3002',
});
});

describe('the Launch App Search button', () => {
const subject = () => wrapper.find('EuiButton[data-test-subj="launchButton"]');

it('should not be disabled', () => {
expect(subject().props().isDisabled).toBeFalsy();
});

it('should use the enterpriseSearchUrl as the base path for its href', () => {
expect(subject().props().href).toBe('http://localhost:3002/as');
});
});
});

describe('when enterpriseSearchUrl is not set', () => {
let wrapper;

beforeEach(() => {
wrapper = mountWithKibanaContext(<EngineOverviewHeader />, {
enterpriseSearchUrl: undefined,
});
});

describe('the Launch App Search button', () => {
const subject = () => wrapper.find('EuiButton[data-test-subj="launchButton"]');

it('should be disabled', () => {
expect(subject().props().isDisabled).toBe(true);
});

it('should not have an href', () => {
expect(subject().props().href).toBeUndefined();
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export const EngineOverviewHeader: React.FC<> = () => {
const buttonProps = {
fill: true,
iconType: 'popout',
['data-test-subj']: 'launchButton',
};
if (enterpriseSearchUrl) {
buttonProps.href = `${enterpriseSearchUrl}/as`;
Expand Down
20 changes: 20 additions & 0 deletions x-pack/plugins/enterprise_search/public/applications/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { coreMock } from 'src/core/public/mocks';
import { renderApp } from '../applications';

describe('renderApp', () => {
it('mounts and unmounts UI', () => {
const params = coreMock.createAppMountParamters();
const core = coreMock.createStart();

const unmount = renderApp(core, params, {});
expect(params.element.querySelector('.setup-guide')).not.toBeNull();
unmount();
expect(params.element.innerHTML).toEqual('');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { appSearchBreadcrumbs, enterpriseSearchBreadcrumbs } from '../kibana_breadcrumbs';

jest.mock('../react_router_helpers', () => ({
letBrowserHandleEvent: () => false,
}));

describe('appSearchBreadcrumbs', () => {
const historyMock = {
createHref: jest.fn().mockImplementation(path => path.pathname),
push: jest.fn(),
};

const breadCrumbs = [
{
text: 'Page 1',
path: '/page1',
},
{
text: 'Page 2',
path: '/page2',
},
];

afterEach(() => {
jest.clearAllMocks();
});

const subject = () => appSearchBreadcrumbs(historyMock)(breadCrumbs);

it('Builds a chain of breadcrumbs with Enterprise Search and App Search at the root', () => {
expect(subject()).toEqual([
{
href: '/',
onClick: expect.any(Function),
text: 'Enterprise Search',
},
{
href: '/app_search',
onClick: expect.any(Function),
text: 'App Search',
},
{
href: '/page1',
onClick: expect.any(Function),
text: 'Page 1',
},
{
href: '/page2',
onClick: expect.any(Function),
text: 'Page 2',
},
]);
});

describe('links', () => {
const eventMock = {
preventDefault: jest.fn(),
};

it('has a link to Enterprise Search Home page first', () => {
subject()[0].onClick(eventMock);
expect(historyMock.push).toHaveBeenCalledWith('/');
});

it('has a link to App Search second', () => {
subject()[1].onClick(eventMock);
expect(historyMock.push).toHaveBeenCalledWith('/app_search');
});

it('has a link to page 1 third', () => {
subject()[2].onClick(eventMock);
expect(historyMock.push).toHaveBeenCalledWith('/page1');
});

it('has a link to page 2 last', () => {
subject()[3].onClick(eventMock);
expect(historyMock.push).toHaveBeenCalledWith('/page2');
});
});
});

describe('enterpriseSearchBreadcrumbs', () => {
const historyMock = {
createHref: jest.fn(),
push: jest.fn(),
};

const breadCrumbs = [
{
text: 'Page 1',
path: '/page1',
},
{
text: 'Page 2',
path: '/page2',
},
];

afterEach(() => {
jest.clearAllMocks();
});

const subject = () => enterpriseSearchBreadcrumbs(historyMock)(breadCrumbs);

it('Builds a chain of breadcrumbs with Enterprise Search at the root', () => {
expect(subject()).toEqual([
{
href: undefined,
onClick: expect.any(Function),
text: 'Enterprise Search',
},
{
href: undefined,
onClick: expect.any(Function),
text: 'Page 1',
},
{
href: undefined,
onClick: expect.any(Function),
text: 'Page 2',
},
]);
});

describe('links', () => {
const eventMock = {
preventDefault: jest.fn(),
};

it('has a link to Enterprise Search Home page first', () => {
subject()[0].onClick(eventMock);
expect(historyMock.push).toHaveBeenCalledWith('/');
});

it('has a link to page 1 second', () => {
subject()[1].onClick(eventMock);
expect(historyMock.push).toHaveBeenCalledWith('/page1');
});

it('has a link to page 2 last', () => {
subject()[2].onClick(eventMock);
expect(historyMock.push).toHaveBeenCalledWith('/page2');
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import React from 'react';

import { SetAppSearchBreadcrumbs } from '../kibana_breadcrumbs';
import { mountWithKibanaContext } from '../../test_utils/helpers';

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

jest.mock('react-router-dom', () => ({
useHistory: () => ({
createHref: jest.fn(),
push: jest.fn(),
location: {
pathname: '/current-path',
},
}),
}));

describe('SetAppSearchBreadcrumbs', () => {
const setBreadcrumbs = jest.fn();
const builtBreadcrumbs = [];
const appSearchBreadCrumbsInnerCall = jest.fn().mockReturnValue(builtBreadcrumbs);
const appSearchBreadCrumbsOuterCall = jest.fn().mockReturnValue(appSearchBreadCrumbsInnerCall);
appSearchBreadcrumbs.mockImplementation(appSearchBreadCrumbsOuterCall);

afterEach(() => {
jest.clearAllMocks();
});

const mountSetAppSearchBreadcrumbs = props => {
return mountWithKibanaContext(<SetAppSearchBreadcrumbs {...props} />, {
http: {},
enterpriseSearchUrl: 'http://localhost:3002',
setBreadcrumbs,
});
};

describe('when isRoot is false', () => {
const subject = () => mountSetAppSearchBreadcrumbs({ text: 'Page 1', isRoot: false });

it('calls appSearchBreadcrumbs to build breadcrumbs, then registers them with Kibana', () => {
subject();

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

// then registers them with Kibana
expect(setBreadcrumbs).toHaveBeenCalledWith(builtBreadcrumbs);
});
});

describe('when isRoot is true', () => {
const subject = () => mountSetAppSearchBreadcrumbs({ text: 'Page 1', isRoot: true });

it('calls appSearchBreadcrumbs to build breadcrumbs with an empty breadcrumb, then registers them with Kibana', () => {
subject();

// uses an empty bredcrumb
expect(appSearchBreadCrumbsInnerCall).toHaveBeenCalledWith([]);

// then registers them with Kibana
expect(setBreadcrumbs).toHaveBeenCalledWith(builtBreadcrumbs);
});
});
});
Loading

0 comments on commit 8df6d3e

Please sign in to comment.