Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@
"@svgr/core": "8.0.0",
"@svgr/plugin-jsx": "^8.0.1",
"@svgr/plugin-svgo": "^8.0.1",
"@testing-library/dom": "^8.12.0",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@testing-library/dom is unused at this point as this PR switches to using fireEvent imported from @testing-library/react (and automatically wrapped in act() so we don't need to do that manually)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice - was that a recent change in @testing-library/react? Or did I just add a library I didn't need to when I was originally setting up RTL? 😅

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe it existed for quite some time now, but it was never well documented and people get confused all the time!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doh! Thanks a million for the cleanup in that case! 💯

"@testing-library/jest-dom": "^5.16.3",
"@testing-library/react": "^14.0.0",
"@testing-library/react-16-17": "npm:@testing-library/react@^12.1.5",
Expand Down
2 changes: 1 addition & 1 deletion src/components/accessibility/skip_link/skip_link.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import React from 'react';
import { mount } from 'enzyme';
import { fireEvent } from '@testing-library/dom';
import { fireEvent } from '@testing-library/react';
import { render } from '../../../test/rtl';
import { requiredProps } from '../../../test';

Expand Down
2 changes: 1 addition & 1 deletion src/components/button/button_empty/button_empty.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import React from 'react';
import { fireEvent } from '@testing-library/dom';
import { fireEvent } from '@testing-library/react';
import { render } from '../../../test/rtl';
import { requiredProps } from '../../../test/required_props';
import { shouldRenderCustomStyles } from '../../../test/internal';
Expand Down
2 changes: 1 addition & 1 deletion src/components/button/button_icon/button_icon.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import React from 'react';
import { fireEvent } from '@testing-library/dom';
import { fireEvent } from '@testing-library/react';
import { render } from '../../../test/rtl';
import { requiredProps } from '../../../test/required_props';
import { shouldRenderCustomStyles } from '../../../test/internal';
Expand Down
2 changes: 1 addition & 1 deletion src/components/code/code_block.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import React, { useState, useEffect } from 'react';
import ReactDOM from 'react-dom';
import { mount, render, shallow } from 'enzyme';
import { act } from 'react-dom/test-utils';
import { act } from '@testing-library/react';
import { requiredProps } from '../../test/required_props';

import { EuiCodeBlock, FONT_SIZES, PADDING_SIZES } from './code_block';
Expand Down
2 changes: 1 addition & 1 deletion src/components/code/code_block_annotations.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import React from 'react';

import { fireEvent } from '@testing-library/dom';
import { fireEvent } from '@testing-library/react';
import { render, waitForEuiPopoverOpen } from '../../test/rtl';
import { shouldRenderCustomStyles } from '../../test/internal';
import { requiredProps } from '../../test';
Expand Down
29 changes: 22 additions & 7 deletions src/components/color_picker/color_picker.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import React from 'react';
import { render, mount } from 'enzyme';
import { act } from '@testing-library/react';

import { EuiColorPicker } from './color_picker';
import { VISUALIZATION_COLORS, keys } from '../../services';
Expand Down Expand Up @@ -190,15 +191,29 @@ test('popover color selector is hidden when the ESC key pressed', async () => {
/>
);

findTestSubject(colorPicker, 'euiColorPickerAnchor').simulate('click');
await sleep();
findTestSubject(colorPicker, 'euiColorPickerAnchor').simulate('keydown', {
key: keys.ENTER,
await act(
async () =>
await findTestSubject(colorPicker, 'euiColorPickerAnchor').simulate(
'click'
)
);

await act(() => sleep());

act(() => {
findTestSubject(colorPicker, 'euiColorPickerAnchor').simulate('keydown', {
key: keys.ENTER,
});
});
await sleep();
findTestSubject(colorPicker, 'euiColorPickerPopover').simulate('keydown', {
key: keys.ESCAPE,

await act(() => sleep());

act(() => {
findTestSubject(colorPicker, 'euiColorPickerPopover').simulate('keydown', {
key: keys.ESCAPE,
});
});

// Portal removal not working with Jest. The blur handler is called just before the portal would be removed.
expect(onBlurHandler).toBeCalled();
});
Expand Down
38 changes: 26 additions & 12 deletions src/components/combo_box/combo_box.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import React, { ReactNode } from 'react';
import { shallow, render, mount } from 'enzyme';
import { act } from '@testing-library/react';
import {
requiredProps,
findTestSubject,
Expand Down Expand Up @@ -89,7 +90,9 @@ describe('props', () => {
/>
);

component.setState({ isListOpen: true });
act(() => {
component.setState({ isListOpen: true });
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

React 17 act() can only return void or Promise so all one-liners like this one need to be inserted in a block

});
expect(takeMountedSnapshot(component)).toMatchSnapshot();
});

Expand Down Expand Up @@ -282,10 +285,16 @@ describe('behavior', () => {
/>
);

component.setState({ searchValue: 'foo' });
const searchInput = findTestSubject(component, 'comboBoxSearchInput');
searchInput.simulate('focus');
searchInput.simulate('keyDown', { key: comboBoxKeys.ENTER });
act(() => {
component.setState({ searchValue: 'foo' });
});

act(() => {
const searchInput = findTestSubject(component, 'comboBoxSearchInput');
searchInput.simulate('focus');
searchInput.simulate('keyDown', { key: comboBoxKeys.ENTER });
});

expect(onCreateOptionHandler).toHaveBeenCalledTimes(1);
expect(onCreateOptionHandler).toHaveBeenNthCalledWith(1, 'foo', options);
});
Expand Down Expand Up @@ -344,15 +353,20 @@ describe('behavior', () => {
/>
);

component.setState({ searchValue: 'foo' });
const searchInput = findTestSubject(component, 'comboBoxSearchInput');
searchInput.simulate('focus');

const searchInputNode = searchInput.getDOMNode();
// React doesn't support `focusout` so we have to manually trigger it
searchInputNode.dispatchEvent(
new FocusEvent('focusout', { bubbles: true })
);
act(() => {
component.setState({ searchValue: 'foo' });
searchInput.simulate('focus');
});

act(() => {
const searchInputNode = searchInput.getDOMNode();
// React doesn't support `focusout` so we have to manually trigger it
searchInputNode.dispatchEvent(
new FocusEvent('focusout', { bubbles: true })
);
});

expect(onCreateOptionHandler).toHaveBeenCalledTimes(1);
expect(onCreateOptionHandler).toHaveBeenNthCalledWith(1, 'foo', options);
Expand Down
6 changes: 2 additions & 4 deletions src/components/context_menu/context_menu.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
*/

import React from 'react';
import { act } from '@testing-library/react';
import { render, mount } from 'enzyme';
import { requiredProps, takeMountedSnapshot } from '../../test';

import { EuiContextMenu, SIZES } from './context_menu';
import { setTimeout } from 'timers';

const panel3 = {
id: 3,
Expand Down Expand Up @@ -64,9 +64,7 @@ const panel0 = {
const panels = [panel0, panel1, panel2, panel3];

export const tick = (ms = 0) =>
new Promise((resolve) => {
setTimeout(resolve, ms);
});
act(() => new Promise((resolve) => setTimeout(resolve, ms)));

describe('EuiContextMenu', () => {
test('is rendered', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import React, { useEffect } from 'react';
import { fireEvent } from '@testing-library/dom';
import { fireEvent } from '@testing-library/react';
import { render } from '../../../test/rtl';

import { EuiDataGridProps } from '../data_grid_types';
Expand Down
25 changes: 19 additions & 6 deletions src/components/datagrid/body/data_grid_cell.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

import React, { useEffect } from 'react';
import { act } from '@testing-library/react';
import { mount, render, ReactWrapper } from 'enzyme';
import { keys } from '../../../services';
import { RowHeightUtils } from '../utils/__mocks__/row_heights';
Expand Down Expand Up @@ -79,7 +80,9 @@ describe('EuiDataGridCell', () => {
}}
/>
);
component.setState({ enableInteractions: true });
act(() => {
component.setState({ enableInteractions: true });
});

const getCellActions = () => component.find('EuiDataGridCellActions');
expect(getCellActions()).toHaveLength(1);
Expand Down Expand Up @@ -174,19 +177,29 @@ describe('EuiDataGridCell', () => {

describe('when state changes:', () => {
it('cellProps', () => {
component.setState({ cellProps: {} });
act(() => {
component.setState({ cellProps: {} });
});
});
it('isEntered', () => {
component.setState({ isEntered: true });
act(() => {
component.setState({ isEntered: true });
});
});
it('isFocused', () => {
component.setState({ isFocused: true });
act(() => {
component.setState({ isFocused: true });
});
});
it('enableInteractions', () => {
component.setState({ enableInteractions: true });
act(() => {
component.setState({ enableInteractions: true });
});
});
it('disableCellTabIndex', () => {
component.setState({ disableCellTabIndex: true });
act(() => {
component.setState({ disableCellTabIndex: true });
});
});
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import React from 'react';
import { act } from 'react-dom/test-utils';
import { act } from '@testing-library/react';
import { mount } from 'enzyme';

import { keys } from '../../../../services';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Side Public License, v 1.
*/

import { act } from 'react-dom/test-utils';
import { act } from '@testing-library/react';
import { testCustomHook } from '../../../../test/internal';
import { useHeaderIsInteractive } from './header_is_interactive';

Expand Down
2 changes: 1 addition & 1 deletion src/components/datagrid/controls/column_selector.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import React from 'react';
import { act } from 'react-dom/test-utils';
import { act } from '@testing-library/react';
import { shallow, mount, ReactWrapper } from 'enzyme';
import { findTestSubject, testByReactVersion } from '../../../test';

Expand Down
2 changes: 1 addition & 1 deletion src/components/datagrid/controls/column_sorting.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import React from 'react';
import { act } from 'react-dom/test-utils';
import { act } from '@testing-library/react';
import { mount, ReactWrapper } from 'enzyme';
import { findTestSubject, testByReactVersion } from '../../../test';

Expand Down
2 changes: 1 addition & 1 deletion src/components/datagrid/controls/display_selector.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import React from 'react';
import { act } from 'react-dom/test-utils';
import { act } from '@testing-library/react';
import { shallow, mount, ShallowWrapper, ReactWrapper } from 'enzyme';
import { testCustomHook } from '../../../test/internal';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import React from 'react';
import { act } from 'react-dom/test-utils';
import { act } from '@testing-library/react';
import { shallow } from 'enzyme';
import { keys } from '../../../services';
import { testCustomHook } from '../../../test/internal';
Expand Down
Loading