Skip to content

Commit

Permalink
[React18] Migrate test suites to account for testing library upgrades…
Browse files Browse the repository at this point in the history
… security-threat-hunting-explore (#201142)

This PR migrates test suites that use `renderHook` from the library
`@testing-library/react-hooks` to adopt the equivalent and replacement
of `renderHook` from the export that is now available from
`@testing-library/react`. This work is required for the planned
migration to react18.

##  Context

In this PR, usages of `waitForNextUpdate` that previously could have
been destructured from `renderHook` are now been replaced with `waitFor`
exported from `@testing-library/react`, furthermore `waitFor`
that would also have been destructured from the same renderHook result
is now been replaced with `waitFor` from the export of
`@testing-library/react`.

***Why is `waitFor` a sufficient enough replacement for
`waitForNextUpdate`, and better for testing values subject to async
computations?***

WaitFor will retry the provided callback if an error is returned, till
the configured timeout elapses. By default the retry interval is `50ms`
with a timeout value of `1000ms` that
effectively translates to at least 20 retries for assertions placed
within waitFor. See
https://testing-library.com/docs/dom-testing-library/api-async/#waitfor
for more information.
This however means that for person's writing tests, said person has to
be explicit about expectations that describe the internal state of the
hook being tested.
This implies checking for instance when a react query hook is being
rendered, there's an assertion that said hook isn't loading anymore.

In this PR you'd notice that this pattern has been adopted, with most
existing assertions following an invocation of `waitForNextUpdate` being
placed within a `waitFor`
invocation. In some cases the replacement is simply a `waitFor(() => new
Promise((resolve) => resolve(null)))` (many thanks to @kapral18, for
point out exactly why this works),
where this suffices the assertions that follow aren't placed within a
waitFor so this PR doesn't get larger than it needs to be.

It's also worth pointing out this PR might also contain changes to test
and application code to improve said existing test.

### What to do next?
1. Review the changes in this PR.
2. If you think the changes are correct, approve the PR.

## Any questions?
If you have any questions or need help with this PR, please leave
comments in this PR.

---------

Co-authored-by: Elastic Machine <[email protected]>
Co-authored-by: Karen Grigoryan <[email protected]>
Co-authored-by: kibanamachine <[email protected]>
  • Loading branch information
4 people authored Dec 4, 2024
1 parent 4d8f711 commit da2ede4
Show file tree
Hide file tree
Showing 55 changed files with 1,001 additions and 996 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import { renderHook } from '@testing-library/react-hooks';
import { renderHook } from '@testing-library/react';
import React, { type PropsWithChildren } from 'react';
import { makeAction, makeActionContext } from '../mocks/helpers';
import { CellActionsProvider, useCellActionsContext } from './cell_actions_context';
Expand All @@ -29,9 +29,8 @@ describe('CellActionContext', () => {
});

it('should throw error when context not found', () => {
const { result } = renderHook(useCellActionsContext);
expect(result.error).toEqual(
new Error('No CellActionsContext found. Please wrap the application with CellActionsProvider')
expect(() => renderHook(useCellActionsContext)).toThrow(
/No CellActionsContext found. Please wrap the application with CellActionsProvider/
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import type { JSXElementConstructor, MutableRefObject } from 'react';
import React from 'react';
import type { EuiDataGridColumnCellActionProps, EuiDataGridRefProps } from '@elastic/eui';
import { EuiButtonEmpty, type EuiDataGridColumnCellAction } from '@elastic/eui';
import { render, waitFor } from '@testing-library/react';
import { renderHook } from '@testing-library/react-hooks';
import { render, waitFor, renderHook } from '@testing-library/react';
import { makeAction } from '../mocks/helpers';
import type { UseDataGridColumnsCellActionsProps } from './use_data_grid_column_cell_actions';
import { useDataGridColumnsCellActions } from './use_data_grid_column_cell_actions';
Expand Down Expand Up @@ -72,75 +71,88 @@ describe('useDataGridColumnsCellActions', () => {
});

it('should return array with actions for each columns', async () => {
const { result, waitForNextUpdate } = renderHook(useDataGridColumnsCellActions, {
const { result } = renderHook(useDataGridColumnsCellActions, {
initialProps: useDataGridColumnsCellActionsProps,
});

expect(result.current).toHaveLength(0);

await waitForNextUpdate();

expect(result.current).toHaveLength(columns.length);
expect(result.current[0]).toHaveLength(actions.length);
await waitFor(() => {
expect(result.current).toHaveLength(columns.length);
expect(result.current[0]).toHaveLength(actions.length);
});
});

it('should call getCellValue with the proper params', async () => {
const { result, waitForNextUpdate } = renderHook(useDataGridColumnsCellActions, {
const { result } = renderHook(useDataGridColumnsCellActions, {
initialProps: useDataGridColumnsCellActionsProps,
});

await waitForNextUpdate();
await waitFor(() => {
expect(result.current).toHaveLength(columns.length);
});

renderCellAction(result.current[0][0], { rowIndex: 0 });
renderCellAction(result.current[0][1], { rowIndex: 1 });
renderCellAction(result.current[1][0], { rowIndex: 0 });
renderCellAction(result.current[1][1], { rowIndex: 1 });

expect(mockGetCellValue).toHaveBeenCalledTimes(4);
expect(mockGetCellValue).toHaveBeenCalledWith(field1.name, 0);
expect(mockGetCellValue).toHaveBeenCalledWith(field1.name, 1);
expect(mockGetCellValue).toHaveBeenCalledWith(field2.name, 0);
expect(mockGetCellValue).toHaveBeenCalledWith(field2.name, 1);
await waitFor(() => {
expect(mockGetCellValue).toHaveBeenCalledTimes(4);
expect(mockGetCellValue).toHaveBeenCalledWith(field1.name, 0);
expect(mockGetCellValue).toHaveBeenCalledWith(field1.name, 1);
expect(mockGetCellValue).toHaveBeenCalledWith(field2.name, 0);
expect(mockGetCellValue).toHaveBeenCalledWith(field2.name, 1);
});
});

it('should render the cell actions', async () => {
const { result, waitForNextUpdate } = renderHook(useDataGridColumnsCellActions, {
const { result } = renderHook(useDataGridColumnsCellActions, {
initialProps: useDataGridColumnsCellActionsProps,
});

await waitForNextUpdate();
await waitFor(() => {
expect(result.current).toHaveLength(columns.length);
});

const cellAction1 = renderCellAction(result.current[0][0]);

expect(cellAction1.getByTestId(`dataGridColumnCellAction-${action1.id}`)).toBeInTheDocument();
expect(cellAction1.getByText(action1.getDisplayName())).toBeInTheDocument();
await waitFor(() => {
expect(cellAction1.getByTestId(`dataGridColumnCellAction-${action1.id}`)).toBeInTheDocument();
expect(cellAction1.getByText(action1.getDisplayName())).toBeInTheDocument();
});

const cellAction2 = renderCellAction(result.current[0][1]);

expect(cellAction2.getByTestId(`dataGridColumnCellAction-${action2.id}`)).toBeInTheDocument();
expect(cellAction2.getByText(action2.getDisplayName())).toBeInTheDocument();
await waitFor(() => {
expect(cellAction2.getByTestId(`dataGridColumnCellAction-${action2.id}`)).toBeInTheDocument();
expect(cellAction2.getByText(action2.getDisplayName())).toBeInTheDocument();
});
});

it('should execute the action on click', async () => {
const { result, waitForNextUpdate } = renderHook(useDataGridColumnsCellActions, {
const { result } = renderHook(useDataGridColumnsCellActions, {
initialProps: useDataGridColumnsCellActionsProps,
});
await waitForNextUpdate();

const cellAction = renderCellAction(result.current[0][0]);

cellAction.getByTestId(`dataGridColumnCellAction-${action1.id}`).click();
await waitFor(() => {
const cellAction = renderCellAction(result.current[0][0]);
cellAction.getByTestId(`dataGridColumnCellAction-${action1.id}`).click();
});

waitFor(() => {
await waitFor(() => {
expect(action1.execute).toHaveBeenCalled();
});
});

it('should execute the action with correct context', async () => {
const { result, waitForNextUpdate } = renderHook(useDataGridColumnsCellActions, {
const { result } = renderHook(useDataGridColumnsCellActions, {
initialProps: useDataGridColumnsCellActionsProps,
});
await waitForNextUpdate();

await waitFor(() => {
expect(result.current).toHaveLength(columns.length);
});

const cellAction1 = renderCellAction(result.current[0][0], { rowIndex: 1 });

Expand Down Expand Up @@ -194,18 +206,19 @@ describe('useDataGridColumnsCellActions', () => {
});

it('should execute the action with correct page value', async () => {
const { result, waitForNextUpdate } = renderHook(useDataGridColumnsCellActions, {
const { result } = renderHook(useDataGridColumnsCellActions, {
initialProps: useDataGridColumnsCellActionsProps,
});
await waitForNextUpdate();

const cellAction = renderCellAction(result.current[0][0], { rowIndex: 25 });
await waitFor(() => {
expect(result.current).toHaveLength(columns.length);
});

const cellAction = renderCellAction(result.current[0][0], { rowIndex: 25 });
cellAction.getByTestId(`dataGridColumnCellAction-${action1.id}`).click();

expect(mockGetCellValue).toHaveBeenCalledWith(field1.name, 25);

await waitFor(() => {
expect(mockGetCellValue).toHaveBeenCalledWith(field1.name, 25);
expect(action1.execute).toHaveBeenCalledWith(
expect.objectContaining({
data: [
Expand All @@ -225,13 +238,15 @@ describe('useDataGridColumnsCellActions', () => {
});

it('should close popover then action executed', async () => {
const { result, waitForNextUpdate } = renderHook(useDataGridColumnsCellActions, {
const { result } = renderHook(useDataGridColumnsCellActions, {
initialProps: useDataGridColumnsCellActionsProps,
});
await waitForNextUpdate();

const cellAction = renderCellAction(result.current[0][0], { rowIndex: 25 });
await waitFor(() => {
expect(result.current).toHaveLength(columns.length);
});

const cellAction = renderCellAction(result.current[0][0], { rowIndex: 25 });
cellAction.getByTestId(`dataGridColumnCellAction-${action1.id}`).click();

await waitFor(() => {
Expand All @@ -240,30 +255,30 @@ describe('useDataGridColumnsCellActions', () => {
});

it('should return empty array of actions when list of fields is empty', async () => {
const { result, waitForNextUpdate } = renderHook(useDataGridColumnsCellActions, {
const { result } = renderHook(useDataGridColumnsCellActions, {
initialProps: {
...useDataGridColumnsCellActionsProps,
fields: [],
},
});

await waitForNextUpdate();

expect(result.current).toBeInstanceOf(Array);
expect(result.current.length).toBe(0);
await waitFor(() => {
expect(result.current).toBeInstanceOf(Array);
expect(result.current.length).toBe(0);
});
});

it('should return empty array of actions when list of fields is undefined', async () => {
const { result, waitForNextUpdate } = renderHook(useDataGridColumnsCellActions, {
const { result } = renderHook(useDataGridColumnsCellActions, {
initialProps: {
...useDataGridColumnsCellActionsProps,
fields: undefined,
},
});

await waitForNextUpdate();

expect(result.current).toBeInstanceOf(Array);
expect(result.current.length).toBe(0);
await waitFor(() => {
expect(result.current).toBeInstanceOf(Array);
expect(result.current.length).toBe(0);
});
});
});
Loading

0 comments on commit da2ede4

Please sign in to comment.