Skip to content

Commit

Permalink
[Refactor] Pull out various Kibana context mocks into separate files
Browse files Browse the repository at this point in the history
- I'm creating a reusable useContext mock for shallow()ed enzyme components
+ add more documentation comments + examples
  • Loading branch information
cee-chen committed Apr 30, 2020
1 parent 500533e commit 634ae14
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import React from 'react';

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

describe('EngineOverviewHeader', () => {
describe('when enterpriseSearchUrl is set', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import React from 'react';

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

jest.mock('./generate_breadcrumbs', () => ({
appSearchBreadcrumbs: jest.fn(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import React from 'react';

import { httpServiceMock } from 'src/core/public/mocks';
import { mountWithKibanaContext } from '../../test_utils/helpers';
import { mountWithKibanaContext } from '../../test_utils';
import { sendTelemetry, SendAppSearchTelemetry } from './';

describe('Shared Telemetry Helpers', () => {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* 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.
*/

export { mockKibanaContext } from './mock_kibana_context';
export { mountWithKibanaContext } from './mount_with_context';

// Note: mock_shallow_usecontext must be imported directly as a file
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* 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 { httpServiceMock } from '../../../../../../src/core/public/mocks';

/**
* A set of default Kibana context values to use across component tests.
* @see enterprise_search/public/index.tsx for the KibanaContext definition/import
*/
export const mockKibanaContext = {
http: httpServiceMock.createSetupContract(),
setBreadcrumbs: jest.fn(),
enterpriseSearchUrl: 'http://localhost:3002',
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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.
*/

/**
* NOTE: This variable name MUST start with 'mock*' in order for
* Jest to accept its use within a jest.mock()
*/
import { mockKibanaContext } from './mock_kibana_context';

jest.mock('react', () => ({
...jest.requireActual('react'),
useContext: jest.fn(() => mockKibanaContext),
}));

/**
* Example usage within a component test using shallow():
*
* import '../../../test_utils/mock_shallow_usecontext'; // Must come before React's import, adjust relative path as needed
*
* import React from 'react';
* import { shallow } from 'enzyme';
*
* // ... etc.
*/

/**
* If you need to override the default mock context values, you can do so via jest.mockImplementation:
*
* import React, { useContext } from 'react';
*
* // ... etc.
*
* it('some test', () => {
* useContext.mockImplementationOnce(() => ({ enterpriseSearchUrl: 'someOverride' }));
* });
*/
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* 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 { mount } from 'enzyme';

import { KibanaContext } from '../';
import { mockKibanaContext } from './mock_kibana_context';

/**
* This helper mounts a component with a set of default KibanaContext,
* while also allowing custom context to be passed in via a second arg
*
* Example usage:
*
* const wrapper = mountWithKibanaContext(<Component />, { enterpriseSearchUrl: 'someOverride' });
*/
export const mountWithKibanaContext = (node, contextProps) => {
return mount(
<KibanaContext.Provider value={{ ...mockKibanaContext, ...contextProps }}>
{node}
</KibanaContext.Provider>
);
};

0 comments on commit 634ae14

Please sign in to comment.