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
… kibana-data-discovery,security-threat-hunting-investigations (elastic#201150)

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.
  • Loading branch information
eokoneyo authored Nov 22, 2024
1 parent 06de5f3 commit 11d8f0c
Show file tree
Hide file tree
Showing 10 changed files with 11 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import { EuiDataGridCellValueElementProps, EuiDataGridSetCellProps } from '@elas
import { buildDataTableRecord } from '@kbn/discover-utils';
import { generateEsHits, additionalFieldGroups } from '@kbn/discover-utils/src/__mocks__';
import { fieldFormatsMock } from '@kbn/field-formats-plugin/common/mocks';
import { render, screen } from '@testing-library/react';
import { renderHook } from '@testing-library/react-hooks';
import { render, screen, renderHook } from '@testing-library/react';
import React from 'react';
import { ReactNode, useState } from 'react';
import { dataViewWithTimefieldMock } from '../../../../__mocks__/data_view_with_timefield';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ import {
FIELD_COLUMN_WIDTH,
useComparisonColumns,
} from './use_comparison_columns';
import { renderHook } from '@testing-library/react-hooks';
import type { EuiDataGridColumn, EuiDataGridColumnActions } from '@elastic/eui';
import { render, screen } from '@testing-library/react';
import { render, screen, renderHook } from '@testing-library/react';
import React from 'react';
import userEvent from '@testing-library/user-event';
import { generateEsHits } from '@kbn/discover-utils/src/__mocks__';
Expand Down
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 { useComparisonCss } from './use_comparison_css';

describe('useComparisonCss', () => {
Expand Down
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 { buildDataTableRecord } from '@kbn/discover-utils';
import {
MAX_COMPARISON_FIELDS,
Expand Down
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 { useColumns } from './use_data_grid_columns';
import { dataViewMock } from '@kbn/discover-utils/src/__mocks__';
import { configMock } from '../../__mocks__/config';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/

import type { Storage } from '@kbn/kibana-utils-plugin/public';
import { renderHook } from '@testing-library/react-hooks';
import { renderHook } from '@testing-library/react';
import { useDataGridDensity } from './use_data_grid_density';
import { DATA_GRID_STYLE_EXPANDED, DataGridDensity } from '../constants';

Expand Down
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 { act, renderHook } from '@testing-library/react-hooks';
import { renderHook, act } from '@testing-library/react';
import {
EUI_DATA_GRID_FULL_SCREEN_CLASS,
UNIFIED_DATA_TABLE_FULL_SCREEN_CLASS,
Expand Down Expand Up @@ -40,7 +40,7 @@ const nextTick = () => {
return act(() => {
return new Promise((resolve) =>
requestAnimationFrame(() => {
resolve();
resolve(null);
})
);
});
Expand Down
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 { createLocalStorageMock } from '../../__mocks__/local_storage_mock';
import { useRowHeight } from './use_row_height';
import { RowHeightMode } from '../components/row_height_settings';
Expand Down
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 { useRowHeightsOptions } from './use_row_heights_options';

describe('useRowHeightsOptions', () => {
Expand Down
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 { act, renderHook } from '@testing-library/react-hooks';
import { renderHook, act } from '@testing-library/react';
import { buildDataTableRecord } from '@kbn/discover-utils';
import { useSelectedDocs } from './use_selected_docs';
import { generateEsHits } from '@kbn/discover-utils/src/__mocks__';
Expand Down

0 comments on commit 11d8f0c

Please sign in to comment.