Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[React18] Migrate test suites to account for testing library upgrades security-threat-hunting-explore #201142

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,24 +71,24 @@ 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(() => new Promise((resolve) => resolve(null)));

renderCellAction(result.current[0][0], { rowIndex: 0 });
renderCellAction(result.current[0][1], { rowIndex: 1 });
Expand All @@ -104,11 +103,11 @@ describe('useDataGridColumnsCellActions', () => {
});

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

await waitForNextUpdate();
await waitFor(() => new Promise((resolve) => resolve(null)));

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

Expand All @@ -122,25 +121,25 @@ describe('useDataGridColumnsCellActions', () => {
});

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

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(() => new Promise((resolve) => resolve(null)));

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

Expand Down Expand Up @@ -194,10 +193,10 @@ 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();
await waitFor(() => new Promise((resolve) => resolve(null)));

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

Expand Down Expand Up @@ -225,10 +224,10 @@ describe('useDataGridColumnsCellActions', () => {
});

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

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

Expand All @@ -240,28 +239,28 @@ 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();
await waitFor(() => new Promise((resolve) => resolve(null)));

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();
await waitFor(() => new Promise((resolve) => resolve(null)));

expect(result.current).toBeInstanceOf(Array);
expect(result.current.length).toBe(0);
Expand Down
Loading