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] use waitFor with assertion callbacks in place of waitForNextUpdate #195087

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
7bda43b
swap waitForNextUpdate for waitFor
eokoneyo Sep 12, 2024
383ee75
fix missed waitFor usages
eokoneyo Sep 23, 2024
f437266
use waitFor and act appropriately to improve test stability
eokoneyo Sep 24, 2024
e1d8026
adjust tests to match testing-library changes
eokoneyo Sep 24, 2024
54ac6d4
more test fixes
eokoneyo Oct 1, 2024
6514f0f
fix sourcerer
eokoneyo Oct 10, 2024
92c4848
fix issue with ftr test
eokoneyo Oct 10, 2024
1e60c4c
fix endpoint list related tests
eokoneyo Sep 30, 2024
e3ccdfe
use waitFor with assertion callbacks
eokoneyo Oct 10, 2024
cc652d3
fix types
eokoneyo Oct 10, 2024
3b7eecc
more adoption of waitFor assertion callbacks
eokoneyo Oct 10, 2024
6e5f43a
use act from renderHook for special instances
eokoneyo Oct 11, 2024
b0d94cc
fix types
eokoneyo Oct 11, 2024
5849f01
add lint rule to prevent any further usage of waitForNextUpdate
eokoneyo Oct 14, 2024
885cd01
fix missed waitForNextUpdate caught by lint rule
eokoneyo Oct 14, 2024
0ad46a3
improve test for loading state
eokoneyo Oct 15, 2024
7389e60
resolve feedback from teams
eokoneyo Oct 21, 2024
30b6b71
patch testing-library/react-hooks so renderer in use matches testing-…
eokoneyo Oct 24, 2024
84b68de
opt in to correct usage for waitFor
eokoneyo Oct 25, 2024
e254d87
[CI] Auto-commit changed files from 'node scripts/lint_ts_projects --…
kibanamachine Oct 25, 2024
65a2f0e
mock only canvas element creation that needs to be tested
eokoneyo Nov 5, 2024
e8363dc
fix lint issue
eokoneyo Nov 6, 2024
2af3af7
await returned promise from waitFor
eokoneyo Nov 7, 2024
2ae9d0c
fix console manager test
eokoneyo Nov 6, 2024
f537b89
combine waitFor with assertion where possible
eokoneyo Nov 11, 2024
03243d9
fix persist exception tests
eokoneyo Nov 11, 2024
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
11 changes: 11 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,17 @@ module.exports = {
message:
'lodash.template is unsafe, and not compatible with our content security policy.',
},
{
object: 'renderHook',
property: 'waitFor',
message:
"use waitFor exported from @testing-library/react instead. waitFor is not available on the returned object from renderHook in it's next version",
},
{
property: 'waitForNextUpdate',
message:
"use waitFor exported from @testing-library/react instead. waitForNextUpdate is not available on the returned object from renderHook in it's next version.",
},
],
},
},
Expand Down
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"dev-docs": "scripts/dev_docs.sh",
"es": "node scripts/es",
"preinstall": "node ./preinstall_check",
"postinstall": "patch-package",
"kbn": "node scripts/kbn",
"lint": "yarn run lint:es && yarn run lint:style",
"lint:es": "node scripts/eslint",
Expand Down Expand Up @@ -1778,6 +1779,8 @@
"oboe": "^2.1.4",
"openapi-types": "^10.0.0",
"p-reflect": "2.1.0",
"patch-package": "^8.0.0",
"pbf": "3.2.1",
"peggy": "^1.2.0",
"picomatch": "^2.3.1",
"pidusage": "^3.0.2",
Expand All @@ -1791,6 +1794,7 @@
"postcss-loader": "^4.2.0",
"postcss-prefix-selector": "^1.16.0",
"postcss-scss": "^4.0.4",
"postinstall-postinstall": "^2.1.0",
"prettier": "^2.8.8",
"proxy": "^2.1.1",
"raw-loader": "^3.1.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import type { ControlGroupRuntimeState } from '@kbn/controls-plugin/public';
import { renderHook } from '@testing-library/react-hooks';
import { waitFor, act } from '@testing-library/react';
import { useControlGroupSyncToLocalStorage } from './use_control_group_sync_to_local_storage';
import { Storage } from '@kbn/kibana-utils-plugin/public';

Expand Down Expand Up @@ -37,52 +38,60 @@ describe('Filters Sync to Local Storage', () => {
afterEach(() => {
mockLocalStorage = {};
});
it('should not be undefined if localStorage has initial value', () => {
it('should not be undefined if localStorage has initial value', async () => {
global.localStorage.setItem(TEST_STORAGE_KEY, JSON.stringify(DEFAULT_STORED_VALUE));
const { result, waitForNextUpdate } = renderHook(() =>
const { result } = renderHook(() =>
useControlGroupSyncToLocalStorage({
Storage,
storageKey: TEST_STORAGE_KEY,
shouldSync: true,
})
);
waitForNextUpdate();
expect(result.current.controlGroupState).toMatchObject(DEFAULT_STORED_VALUE);
await waitFor(() =>
expect(result.current.controlGroupState).toMatchObject(DEFAULT_STORED_VALUE)
);
});
it('should be undefined if localstorage as NO initial value', () => {
const { result, waitForNextUpdate } = renderHook(() =>
it('should be undefined if localstorage as NO initial value', async () => {
const { result } = renderHook(() =>
useControlGroupSyncToLocalStorage({
Storage,
storageKey: TEST_STORAGE_KEY,
shouldSync: true,
})
);
waitForNextUpdate();
expect(result.current.controlGroupState).toBeUndefined();
expect(result.current.setControlGroupState).toBeTruthy();
await waitFor(() =>
expect(result.current).toEqual(
expect.objectContaining({
controlGroupState: undefined,
setControlGroupState: expect.any(Function),
})
)
);
});
it('should be update values to local storage when sync is ON', () => {
const { result, waitFor } = renderHook(() =>
it('should be update values to local storage when sync is ON', async () => {
const { result } = renderHook(() =>
useControlGroupSyncToLocalStorage({
Storage,
storageKey: TEST_STORAGE_KEY,
shouldSync: true,
})
);
waitFor(() => {
await waitFor(() => {
expect(result.current.controlGroupState).toBeUndefined();
expect(result.current.setControlGroupState).toBeTruthy();
});
result.current.setControlGroupState(DEFAULT_STORED_VALUE);
waitFor(() => {
act(() => {
result.current.setControlGroupState(DEFAULT_STORED_VALUE);
});
await waitFor(() => {
expect(result.current.controlGroupState).toMatchObject(DEFAULT_STORED_VALUE);
expect(global.localStorage.getItem(TEST_STORAGE_KEY)).toBe(
JSON.stringify(DEFAULT_STORED_VALUE)
);
});
});
it('should not update values to local storage when sync is OFF', () => {
const { waitFor, result, rerender } = renderHook(() =>
it('should not update values to local storage when sync is OFF', async () => {
const { result, rerender } = renderHook(() =>
useControlGroupSyncToLocalStorage({
Storage,
storageKey: TEST_STORAGE_KEY,
Expand All @@ -91,20 +100,20 @@ describe('Filters Sync to Local Storage', () => {
);

// Sync is ON
waitFor(() => {
await waitFor(() => {
expect(result.current.controlGroupState).toBeUndefined();
expect(result.current.setControlGroupState).toBeTruthy();
});

result.current.setControlGroupState(DEFAULT_STORED_VALUE);
waitFor(() => {
await waitFor(() => {
expect(result.current.controlGroupState).toMatchObject(DEFAULT_STORED_VALUE);
});

// Sync is OFF
rerender({ storageKey: TEST_STORAGE_KEY, shouldSync: false });
result.current.setControlGroupState(ANOTHER_SAMPLE_VALUE);
waitFor(() => {
await waitFor(() => {
expect(result.current.controlGroupState).toMatchObject(ANOTHER_SAMPLE_VALUE);
// old value
expect(global.localStorage.getItem(TEST_STORAGE_KEY)).toBe(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import React, { FC } from 'react';
import { AlertConsumers } from '@kbn/rule-data-utils';
import * as ReactQuery from '@tanstack/react-query';
import { renderHook } from '@testing-library/react-hooks';
import { waitFor } from '@testing-library/react';
import { testQueryClientConfig } from '../test_utils/test_query_client_config';
import { useFetchAlertsFieldsQuery } from './use_fetch_alerts_fields_query';
import { httpServiceMock } from '@kbn/core-http-browser-mocks';
Expand Down Expand Up @@ -88,35 +89,37 @@ describe('useFetchAlertsFieldsQuery', () => {
});

it('should call the api only once', async () => {
const { result, rerender, waitForValueToChange } = renderHook(
const { result, rerender } = renderHook(
() => useFetchAlertsFieldsQuery({ http: mockHttpClient, featureIds: ['apm'] }),
{
wrapper,
}
);

await waitForValueToChange(() => result.current.data);

expect(mockHttpGet).toHaveBeenCalledTimes(1);
expect(result.current.data).toEqual({
browserFields: { fakeCategory: {} },
fields: [
{
name: 'fakeCategory',
},
],
await waitFor(() => {
expect(mockHttpGet).toHaveBeenCalledTimes(1);
expect(result.current.data).toEqual({
browserFields: { fakeCategory: {} },
fields: [
{
name: 'fakeCategory',
},
],
});
});

rerender();

expect(mockHttpGet).toHaveBeenCalledTimes(1);
expect(result.current.data).toEqual({
browserFields: { fakeCategory: {} },
fields: [
{
name: 'fakeCategory',
},
],
await waitFor(() => {
expect(mockHttpGet).toHaveBeenCalledTimes(1);
expect(result.current.data).toEqual({
browserFields: { fakeCategory: {} },
fields: [
{
name: 'fakeCategory',
},
],
});
});
});

Expand All @@ -132,8 +135,10 @@ describe('useFetchAlertsFieldsQuery', () => {
}
);

expect(mockHttpGet).toHaveBeenCalledTimes(0);
expect(result.current.data).toEqual(emptyData);
await waitFor(() => {
expect(mockHttpGet).toHaveBeenCalledTimes(0);
expect(result.current.data).toEqual(emptyData);
});
});

it('should not fetch if all featureId are not valid', async () => {
Expand All @@ -148,8 +153,10 @@ describe('useFetchAlertsFieldsQuery', () => {
}
);

expect(mockHttpGet).toHaveBeenCalledTimes(0);
expect(result.current.data).toEqual(emptyData);
await waitFor(() => {
expect(mockHttpGet).toHaveBeenCalledTimes(0);
expect(result.current.data).toEqual(emptyData);
});
});

it('should filter out the non valid feature id', async () => {
Expand All @@ -164,9 +171,11 @@ describe('useFetchAlertsFieldsQuery', () => {
}
);

expect(mockHttpGet).toHaveBeenCalledTimes(1);
expect(mockHttpGet).toHaveBeenCalledWith('/internal/rac/alerts/browser_fields', {
query: { featureIds: ['apm', 'logs'] },
await waitFor(() => {
expect(mockHttpGet).toHaveBeenCalledTimes(1);
expect(mockHttpGet).toHaveBeenCalledWith('/internal/rac/alerts/browser_fields', {
query: { featureIds: ['apm', 'logs'] },
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import React, { FunctionComponent } from 'react';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { renderHook } from '@testing-library/react-hooks';
import { waitFor } from '@testing-library/react';
import { testQueryClientConfig } from '../test_utils/test_query_client_config';
import { useFetchAlertsIndexNamesQuery } from './use_fetch_alerts_index_names_query';
import { fetchAlertsIndexNames } from '../apis/fetch_alerts_index_names';
Expand Down Expand Up @@ -56,14 +57,14 @@ describe('useFetchAlertsIndexNamesQuery', () => {
});

it('correctly caches the index names', async () => {
const { result, rerender, waitForValueToChange } = renderHook(
const { result, rerender } = renderHook(
() => useFetchAlertsIndexNamesQuery({ http: mockHttpClient, featureIds: ['apm'] }),
{
wrapper,
}
);

await waitForValueToChange(() => result.current.data);
await waitFor(() => result.current.data);

expect(mockFetchAlertsIndexNames).toHaveBeenCalledTimes(1);

Expand Down
Loading