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-management (#201146)

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]>
  • Loading branch information
eokoneyo and elasticmachine authored Nov 25, 2024
1 parent 1c3bcea commit 27224f1
Show file tree
Hide file tree
Showing 10 changed files with 14 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ describe('NumberInput', () => {
expect(input).toBeDisabled();
});

it('recovers if value is null', () => {
it('recovers if value is null', async () => {
const { getByTestId } = render(
wrap(<NumberInput {...defaultProps} field={{ ...defaultProps.field, defaultValue: null }} />)
);
const input = getByTestId(`${TEST_SUBJ_PREFIX_FIELD}-${id}`);
waitFor(() => expect(input).toHaveValue(undefined));

await waitFor(() => expect(getByTestId(`${TEST_SUBJ_PREFIX_FIELD}-${id}`)).toHaveValue(null));
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
*/

import { createMemoryHistory } from 'history';
import { renderHook, act, cleanup } from '@testing-library/react-hooks';

import { renderHook, act, cleanup, waitFor } from '@testing-library/react';

import { coreMock } from '@kbn/core/public/mocks';
import { CoreScopedHistory } from '@kbn/core/public';
Expand Down Expand Up @@ -73,7 +74,7 @@ describe('useUnsavedChangesPrompt', () => {
act(() => history.push('/test'));

// needed because we have an async useEffect
await act(() => new Promise((resolve) => resolve()));
await waitFor(() => new Promise((resolve) => resolve(null)));

expect(navigateToUrl).toBeCalledWith('/mock/test', expect.anything());
expect(coreStart.overlays.openConfirm).toBeCalled();
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, act } from '@testing-library/react-hooks';
import { renderHook, act } from '@testing-library/react';
import { useSetInitialValue } from './use_set_initial_value';
import { IToasts } from '@kbn/core-notifications-browser';
import { decompressFromEncodedURIComponent } from 'lz-string';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { renderHook } from '@testing-library/react-hooks';
import { renderHook } from '@testing-library/react';

import { useComponentTemplatesContext } from '../../component_templates_context';

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

import { renderHook } from '@testing-library/react-hooks';
import { renderHook } from '@testing-library/react';
import { createMemoryHistory } from 'history';
import { useStepFromQueryString } from './use_step_from_query_string';

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

import { renderHook } from '@testing-library/react-hooks';
import { renderHook } from '@testing-library/react';
import { CustomInferenceEndpointConfig, SemanticTextField } from '../../../../../types';
import { useSemanticText } from './use_semantic_text';
import { act } from 'react-dom/test-utils';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { renderHook } from '@testing-library/react-hooks';
import { renderHook } from '@testing-library/react';
import { coreMock } from '@kbn/core/public/mocks';
import { createMemoryHistory } from 'history';
import { useRedirectPath } from './redirect_path';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { renderHook } from '@testing-library/react-hooks';
import { renderHook } from '@testing-library/react';
import { NormalizedFields } from '../application/components/mappings_editor/types';
import { useDetailsPageMappingsModelManagement } from './use_details_page_mappings_model_management';

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

import { renderHook } from '@testing-library/react-hooks';
import { renderHook } from '@testing-library/react';
import { createMemoryHistory } from 'history';
import { useRedirectPath } from './redirect_path';
import { useKibana } from '../../shared_imports';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { renderHook } from '@testing-library/react-hooks';
import { renderHook } from '@testing-library/react';

import { useCollapsibleList } from './use_collapsible_list';

Expand Down

0 comments on commit 27224f1

Please sign in to comment.