Skip to content

Commit

Permalink
🔥 Delete/repurpose mount with context helpers
Browse files Browse the repository at this point in the history
- Remove mountWithKibanaContext completely
- Change mountWithContext to mountWithIntl, since that's the only context remaining, and move it to its own file
- Change mountWithAsyncContext to just mountAsync and move it to its own file
  + add an option to pair it w/ mountWithIntl for formatted date/number support
  • Loading branch information
cee-chen committed Sep 24, 2020
1 parent 167bf07 commit 90bb26b
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 124 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ export { mockHttpValues } from './http_logic.mock';
export { mockFlashMessagesValues, mockFlashMessagesActions } from './flash_messages_logic.mock';
export { mockAllValues, mockAllActions, setMockValues } from './kea.mock';

export {
mountWithContext,
mountWithKibanaContext,
mountWithAsyncContext,
} from './mount_with_context.mock';
export { mountAsync } from './mount_async.mock';
export { mountWithIntl } from './mount_with_i18n.mock';
export { shallowWithIntl } from './shallow_with_i18n.mock';
// Note: shallow_useeffect must be imported directly as a file
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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 { act } from 'react-dom/test-utils';
import { mount, ReactWrapper } from 'enzyme';

import { mountWithIntl } from './';

/**
* This helper is intended for components that have async effects
* (e.g. http fetches) on mount. It mostly adds act/update boilerplate
* that's needed for the wrapper to play nice with Enzyme/Jest
*
* Example usage:
*
* const wrapper = mountAsync(<Component />);
*/

interface IOptions {
i18n?: boolean;
}

export const mountAsync = async (
children: React.ReactElement,
options: IOptions
): Promise<ReactWrapper> => {
let wrapper: ReactWrapper | undefined;

// We get a lot of act() warning/errors in the terminal without this.
// TBH, I don't fully understand why since Enzyme's mount is supposed to
// have act() baked in - could be because of the wrapping context provider?
await act(async () => {
wrapper = options.i18n ? mountWithIntl(children) : mount(children);
});
if (wrapper) {
wrapper.update(); // This seems to be required for the DOM to actually update

return wrapper;
} else {
throw new Error('Could not mount wrapper');
}
};

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* 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 { I18nProvider } from '@kbn/i18n/react';

/**
* This helper wraps a component with react-intl's <I18nProvider> which
* fixes "Could not find required `intl` object" console errors when running tests
*
* Example usage (should be the same as mount()):
*
* const wrapper = mountWithI18n(<Component />);
*/
export const mountWithIntl = (children: React.ReactElement) => {
return mount(<I18nProvider>{children}</I18nProvider>);
};
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import React from 'react';
import { act } from 'react-dom/test-utils';
import { shallow, ReactWrapper } from 'enzyme';

import { mountWithAsyncContext, mockHttpValues, setMockValues } from '../../../__mocks__';
import { mountAsync, mockHttpValues, setMockValues } from '../../../__mocks__';

import { LoadingState, EmptyState } from './components';
import { EngineTable } from './engine_table';
Expand All @@ -36,7 +36,7 @@ describe('EngineOverview', () => {
}),
},
});
const wrapper = await mountWithAsyncContext(<EngineOverview />);
const wrapper = await mountAsync(<EngineOverview />, { i18n: true });

expect(wrapper.find(EmptyState)).toHaveLength(1);
});
Expand Down Expand Up @@ -69,7 +69,7 @@ describe('EngineOverview', () => {
});

it('renders and calls the engines API', async () => {
const wrapper = await mountWithAsyncContext(<EngineOverview />);
const wrapper = await mountAsync(<EngineOverview />, { i18n: true });

expect(wrapper.find(EngineTable)).toHaveLength(1);
expect(mockApi).toHaveBeenNthCalledWith(1, '/api/app_search/engines', {
Expand All @@ -86,7 +86,7 @@ describe('EngineOverview', () => {
hasPlatinumLicense: true,
http: { ...mockHttpValues.http, get: mockApi },
});
const wrapper = await mountWithAsyncContext(<EngineOverview />);
const wrapper = await mountAsync(<EngineOverview />, { i18n: true });

expect(wrapper.find(EngineTable)).toHaveLength(2);
expect(mockApi).toHaveBeenNthCalledWith(2, '/api/app_search/engines', {
Expand All @@ -103,15 +103,15 @@ describe('EngineOverview', () => {
wrapper.find(EngineTable).prop('pagination');

it('passes down page data from the API', async () => {
const wrapper = await mountWithAsyncContext(<EngineOverview />);
const wrapper = await mountAsync(<EngineOverview />, { i18n: true });
const pagination = getTablePagination(wrapper);

expect(pagination.totalEngines).toEqual(100);
expect(pagination.pageIndex).toEqual(0);
});

it('re-polls the API on page change', async () => {
const wrapper = await mountWithAsyncContext(<EngineOverview />);
const wrapper = await mountAsync(<EngineOverview />, { i18n: true });
await act(async () => getTablePagination(wrapper).onPaginate(5));
wrapper.update();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@

import '../../../__mocks__/kea.mock';
import '../../../__mocks__/enterprise_search_url.mock';
import { mockHttpValues } from '../../../__mocks__/';
import { mockHttpValues, mountWithIntl } from '../../../__mocks__/';

import React from 'react';
import { mount } from 'enzyme';
import { I18nProvider } from '@kbn/i18n/react';
import { EuiBasicTable, EuiPagination, EuiButtonEmpty, EuiLink } from '@elastic/eui';

jest.mock('../../../shared/telemetry', () => ({ sendTelemetry: jest.fn() }));
Expand All @@ -21,24 +19,22 @@ import { EngineTable } from './engine_table';
describe('EngineTable', () => {
const onPaginate = jest.fn(); // onPaginate updates the engines API call upstream

const wrapper = mount(
<I18nProvider>
<EngineTable
data={[
{
name: 'test-engine',
created_at: 'Fri, 1 Jan 1970 12:00:00 +0000',
document_count: 99999,
field_count: 10,
},
]}
pagination={{
totalEngines: 50,
pageIndex: 0,
onPaginate,
}}
/>
</I18nProvider>
const wrapper = mountWithIntl(
<EngineTable
data={[
{
name: 'test-engine',
created_at: 'Fri, 1 Jan 1970 12:00:00 +0000',
document_count: 99999,
field_count: 10,
},
]}
pagination={{
totalEngines: 50,
pageIndex: 0,
onPaginate,
}}
/>
);
const table = wrapper.find(EuiBasicTable);

Expand Down Expand Up @@ -78,13 +74,8 @@ describe('EngineTable', () => {
});

it('handles empty data', () => {
const emptyWrapper = mount(
<I18nProvider>
<EngineTable
data={[]}
pagination={{ totalEngines: 0, pageIndex: 0, onPaginate: () => {} }}
/>
</I18nProvider>
const emptyWrapper = mountWithIntl(
<EngineTable data={[]} pagination={{ totalEngines: 0, pageIndex: 0, onPaginate: () => {} }} />
);
const emptyTable = emptyWrapper.find(EuiBasicTable);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import React from 'react';
import { shallow } from 'enzyme';
import { EuiSteps, EuiIcon, EuiLink } from '@elastic/eui';

import { mountWithContext } from '../../__mocks__';
import { mountWithIntl } from '../../__mocks__';

import { SetupGuide } from './';

Expand All @@ -27,7 +27,7 @@ describe('SetupGuide', () => {
});

it('renders with optional auth links', () => {
const wrapper = mountWithContext(
const wrapper = mountWithIntl(
<SetupGuide
productName="Foo"
productEuiIcon="logoAppSearch"
Expand Down

0 comments on commit 90bb26b

Please sign in to comment.