Skip to content

Commit

Permalink
Add enzyme rerender test helper
Browse files Browse the repository at this point in the history
  • Loading branch information
cee-chen committed Nov 11, 2020
1 parent e607078 commit 5ffff12
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* 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 { ShallowWrapper } from 'enzyme';

/**
* Quick and easy helper for re-rendering a React component in Enzyme
* after (e.g.) updating Kea values
*/
export const rerender = (wrapper: ShallowWrapper) => {
wrapper.setProps({}); // Re-renders
wrapper.update(); // Just in case
};
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ export { mockAllValues, mockAllActions, setMockValues, setMockActions } from './
export { mountAsync } from './mount_async.mock';
export { mountWithIntl } from './mount_with_i18n.mock';
export { shallowWithIntl } from './shallow_with_i18n.mock';
export { rerender } from './enzyme_rerender.mock';
// Note: shallow_useeffect must be imported directly as a file
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

import { setMockValues, setMockActions } from '../../../../../__mocks__/kea.mock';
import { rerender } from '../../../../../__mocks__';

import React from 'react';
import { shallow } from 'enzyme';
Expand Down Expand Up @@ -60,7 +61,7 @@ describe('FormKeyEngineAccess', () => {
...values,
fullEngineAccessChecked: false,
});
wrapper.setProps({}); // Re-render
rerender(wrapper);

expect(wrapper.find('#all_engines').prop('checked')).toEqual(false);
expect(wrapper.find('#all_engines').prop('value')).toEqual('false');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

import { setMockValues } from '../../../__mocks__/kea.mock';
import { rerender } from '../../../__mocks__';

import React from 'react';
import { shallow } from 'enzyme';
Expand Down Expand Up @@ -40,11 +41,11 @@ describe('EngineNav', () => {
expect(wrapper.find(EuiBadge)).toHaveLength(0);

setMockValues({ ...values, isSampleEngine: true });
wrapper.setProps({}); // Re-render
rerender(wrapper);
expect(wrapper.find(EuiBadge).prop('children')).toEqual('SAMPLE ENGINE');

setMockValues({ ...values, isMetaEngine: true });
wrapper.setProps({}); // Re-render
rerender(wrapper);
expect(wrapper.find(EuiBadge).prop('children')).toEqual('META ENGINE');
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import React from 'react';
import { shallow } from 'enzyme';

import { setMockValues } from '../__mocks__/kea.mock';
import { rerender } from '../__mocks__';

import { EnterpriseSearch } from './';
import { SetupGuide } from './components/setup_guide';
Expand Down Expand Up @@ -40,7 +41,7 @@ describe('EnterpriseSearch', () => {
errorConnecting: true,
config: { host: '' },
});
wrapper.setProps({}); // Re-render
rerender(wrapper);

expect(wrapper.find(ErrorConnecting)).toHaveLength(0);
expect(wrapper.find(ProductSelector)).toHaveLength(1);
Expand Down

0 comments on commit 5ffff12

Please sign in to comment.