mirror of
https://github.com/elastic/kibana.git
synced 2025-04-24 09:48:58 -04:00
[8.x] [React18] Migrate test suites to account for testing library upgrades kibana-presentation (#201156) (#201321)
# Backport This will backport the following commits from `main` to `8.x`: - [[React18] Migrate test suites to account for testing library upgrades kibana-presentation (#201156)](https://github.com/elastic/kibana/pull/201156) <!--- Backport version: 9.4.3 --> ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sqren/backport) <!--BACKPORT [{"author":{"name":"Eyo O. Eyo","email":"7893459+eokoneyo@users.noreply.github.com"},"sourceCommit":{"committedDate":"2024-11-22T08:49:11Z","message":"[React18] Migrate test suites to account for testing library upgrades kibana-presentation (#201156)\n\nThis PR migrates test suites that use `renderHook` from the library\r\n`@testing-library/react-hooks` to adopt the equivalent and replacement\r\nof `renderHook` from the export that is now available from\r\n`@testing-library/react`. This work is required for the planned\r\nmigration to react18.\r\n\r\n## Context\r\n\r\nIn this PR, usages of `waitForNextUpdate` that previously could have\r\nbeen destructured from `renderHook` are now been replaced with `waitFor`\r\nexported from `@testing-library/react`, furthermore `waitFor`\r\nthat would also have been destructured from the same renderHook result\r\nis now been replaced with `waitFor` from the export of\r\n`@testing-library/react`.\r\n\r\n***Why is `waitFor` a sufficient enough replacement for\r\n`waitForNextUpdate`, and better for testing values subject to async\r\ncomputations?***\r\n\r\nWaitFor will retry the provided callback if an error is returned, till\r\nthe configured timeout elapses. By default the retry interval is `50ms`\r\nwith a timeout value of `1000ms` that\r\neffectively translates to at least 20 retries for assertions placed\r\nwithin waitFor. See\r\nhttps://testing-library.com/docs/dom-testing-library/api-async/#waitfor\r\nfor more information.\r\nThis however means that for person's writing tests, said person has to\r\nbe explicit about expectations that describe the internal state of the\r\nhook being tested.\r\nThis implies checking for instance when a react query hook is being\r\nrendered, there's an assertion that said hook isn't loading anymore.\r\n\r\nIn this PR you'd notice that this pattern has been adopted, with most\r\nexisting assertions following an invocation of `waitForNextUpdate` being\r\nplaced within a `waitFor`\r\ninvocation. In some cases the replacement is simply a `waitFor(() => new\r\nPromise((resolve) => resolve(null)))` (many thanks to @kapral18, for\r\npoint out exactly why this works),\r\nwhere this suffices the assertions that follow aren't placed within a\r\nwaitFor so this PR doesn't get larger than it needs to be.\r\n\r\nIt's also worth pointing out this PR might also contain changes to test\r\nand application code to improve said existing test.\r\n\r\n### What to do next?\r\n1. Review the changes in this PR.\r\n2. If you think the changes are correct, approve the PR.\r\n\r\n## Any questions?\r\nIf you have any questions or need help with this PR, please leave\r\ncomments in this PR.","sha":"cdfcd592449e0cea25becd206705e8aaa0c8e34a","branchLabelMapping":{"^v9.0.0$":"main","^v8.18.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["Feature:Embedding","Team:Presentation","release_note:skip","v9.0.0","backport:prev-minor","React@18"],"title":"[React18] Migrate test suites to account for testing library upgrades kibana-presentation","number":201156,"url":"https://github.com/elastic/kibana/pull/201156","mergeCommit":{"message":"[React18] Migrate test suites to account for testing library upgrades kibana-presentation (#201156)\n\nThis PR migrates test suites that use `renderHook` from the library\r\n`@testing-library/react-hooks` to adopt the equivalent and replacement\r\nof `renderHook` from the export that is now available from\r\n`@testing-library/react`. This work is required for the planned\r\nmigration to react18.\r\n\r\n## Context\r\n\r\nIn this PR, usages of `waitForNextUpdate` that previously could have\r\nbeen destructured from `renderHook` are now been replaced with `waitFor`\r\nexported from `@testing-library/react`, furthermore `waitFor`\r\nthat would also have been destructured from the same renderHook result\r\nis now been replaced with `waitFor` from the export of\r\n`@testing-library/react`.\r\n\r\n***Why is `waitFor` a sufficient enough replacement for\r\n`waitForNextUpdate`, and better for testing values subject to async\r\ncomputations?***\r\n\r\nWaitFor will retry the provided callback if an error is returned, till\r\nthe configured timeout elapses. By default the retry interval is `50ms`\r\nwith a timeout value of `1000ms` that\r\neffectively translates to at least 20 retries for assertions placed\r\nwithin waitFor. See\r\nhttps://testing-library.com/docs/dom-testing-library/api-async/#waitfor\r\nfor more information.\r\nThis however means that for person's writing tests, said person has to\r\nbe explicit about expectations that describe the internal state of the\r\nhook being tested.\r\nThis implies checking for instance when a react query hook is being\r\nrendered, there's an assertion that said hook isn't loading anymore.\r\n\r\nIn this PR you'd notice that this pattern has been adopted, with most\r\nexisting assertions following an invocation of `waitForNextUpdate` being\r\nplaced within a `waitFor`\r\ninvocation. In some cases the replacement is simply a `waitFor(() => new\r\nPromise((resolve) => resolve(null)))` (many thanks to @kapral18, for\r\npoint out exactly why this works),\r\nwhere this suffices the assertions that follow aren't placed within a\r\nwaitFor so this PR doesn't get larger than it needs to be.\r\n\r\nIt's also worth pointing out this PR might also contain changes to test\r\nand application code to improve said existing test.\r\n\r\n### What to do next?\r\n1. Review the changes in this PR.\r\n2. If you think the changes are correct, approve the PR.\r\n\r\n## Any questions?\r\nIf you have any questions or need help with this PR, please leave\r\ncomments in this PR.","sha":"cdfcd592449e0cea25becd206705e8aaa0c8e34a"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/201156","number":201156,"mergeCommit":{"message":"[React18] Migrate test suites to account for testing library upgrades kibana-presentation (#201156)\n\nThis PR migrates test suites that use `renderHook` from the library\r\n`@testing-library/react-hooks` to adopt the equivalent and replacement\r\nof `renderHook` from the export that is now available from\r\n`@testing-library/react`. This work is required for the planned\r\nmigration to react18.\r\n\r\n## Context\r\n\r\nIn this PR, usages of `waitForNextUpdate` that previously could have\r\nbeen destructured from `renderHook` are now been replaced with `waitFor`\r\nexported from `@testing-library/react`, furthermore `waitFor`\r\nthat would also have been destructured from the same renderHook result\r\nis now been replaced with `waitFor` from the export of\r\n`@testing-library/react`.\r\n\r\n***Why is `waitFor` a sufficient enough replacement for\r\n`waitForNextUpdate`, and better for testing values subject to async\r\ncomputations?***\r\n\r\nWaitFor will retry the provided callback if an error is returned, till\r\nthe configured timeout elapses. By default the retry interval is `50ms`\r\nwith a timeout value of `1000ms` that\r\neffectively translates to at least 20 retries for assertions placed\r\nwithin waitFor. See\r\nhttps://testing-library.com/docs/dom-testing-library/api-async/#waitfor\r\nfor more information.\r\nThis however means that for person's writing tests, said person has to\r\nbe explicit about expectations that describe the internal state of the\r\nhook being tested.\r\nThis implies checking for instance when a react query hook is being\r\nrendered, there's an assertion that said hook isn't loading anymore.\r\n\r\nIn this PR you'd notice that this pattern has been adopted, with most\r\nexisting assertions following an invocation of `waitForNextUpdate` being\r\nplaced within a `waitFor`\r\ninvocation. In some cases the replacement is simply a `waitFor(() => new\r\nPromise((resolve) => resolve(null)))` (many thanks to @kapral18, for\r\npoint out exactly why this works),\r\nwhere this suffices the assertions that follow aren't placed within a\r\nwaitFor so this PR doesn't get larger than it needs to be.\r\n\r\nIt's also worth pointing out this PR might also contain changes to test\r\nand application code to improve said existing test.\r\n\r\n### What to do next?\r\n1. Review the changes in this PR.\r\n2. If you think the changes are correct, approve the PR.\r\n\r\n## Any questions?\r\nIf you have any questions or need help with this PR, please leave\r\ncomments in this PR.","sha":"cdfcd592449e0cea25becd206705e8aaa0c8e34a"}}]}] BACKPORT--> Co-authored-by: Eyo O. Eyo <7893459+eokoneyo@users.noreply.github.com>
This commit is contained in:
parent
ad17f7aafc
commit
03f474b157
11 changed files with 42 additions and 70 deletions
|
@ -17,7 +17,7 @@ import {
|
|||
VisualizationsStart,
|
||||
type BaseVisType,
|
||||
} from '@kbn/visualizations-plugin/public';
|
||||
import { renderHook } from '@testing-library/react-hooks';
|
||||
import { renderHook } from '@testing-library/react';
|
||||
|
||||
import { uiActionsService, visualizationsService } from '../../../services/kibana_services';
|
||||
import { useGetDashboardPanels } from './use_get_dashboard_panels';
|
||||
|
|
|
@ -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 { getDashboardBackupService } from '../../services/dashboard_backup_service';
|
||||
import { getDashboardContentManagementService } from '../../services/dashboard_content_management_service';
|
||||
|
|
|
@ -9,8 +9,7 @@
|
|||
|
||||
import React from 'react';
|
||||
import { waitFor } from '@testing-library/react';
|
||||
import { render } from '@testing-library/react';
|
||||
import { renderHook } from '@testing-library/react-hooks';
|
||||
import { render, renderHook } from '@testing-library/react';
|
||||
import {
|
||||
HelloWorldEmbeddable,
|
||||
HelloWorldEmbeddableFactoryDefinition,
|
||||
|
@ -28,7 +27,7 @@ describe('useEmbeddableFactory', () => {
|
|||
);
|
||||
doStart();
|
||||
|
||||
const { result, waitForNextUpdate } = renderHook(() =>
|
||||
const { result } = renderHook(() =>
|
||||
useEmbeddableFactory({ factory: getFactory(), input: { id: 'hello' } })
|
||||
);
|
||||
|
||||
|
@ -36,10 +35,10 @@ describe('useEmbeddableFactory', () => {
|
|||
|
||||
expect(loading).toBe(true);
|
||||
|
||||
await waitForNextUpdate();
|
||||
|
||||
const [embeddable] = result.current;
|
||||
expect(embeddable).toBeDefined();
|
||||
await waitFor(() => {
|
||||
const [embeddable] = result.current;
|
||||
expect(embeddable).toBeDefined();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
|
|
@ -8,8 +8,7 @@
|
|||
*/
|
||||
|
||||
import React, { useEffect, useState } from 'react';
|
||||
import { render, screen, fireEvent } from '@testing-library/react';
|
||||
import { renderHook } from '@testing-library/react-hooks';
|
||||
import { render, screen, fireEvent, renderHook } from '@testing-library/react';
|
||||
import { usePresentationPanelTitleClickHandler } from './presentation_panel_title';
|
||||
|
||||
describe('usePresentationPanelTitleClickHandler', () => {
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
*/
|
||||
|
||||
import React, { FC, PropsWithChildren } from 'react';
|
||||
import { renderHook } from '@testing-library/react-hooks';
|
||||
import { renderHook } from '@testing-library/react';
|
||||
import { useAutoplayHelper } from './use_autoplay_helper';
|
||||
import { WorkpadRoutingContext, WorkpadRoutingContextType } from '../workpad_routing_context';
|
||||
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
* 2.0.
|
||||
*/
|
||||
|
||||
import { renderHook } from '@testing-library/react-hooks';
|
||||
import { renderHook } from '@testing-library/react';
|
||||
import { usePageSync } from './use_page_sync';
|
||||
|
||||
const mockDispatch = jest.fn();
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
*/
|
||||
|
||||
import React, { PropsWithChildren } from 'react';
|
||||
import { renderHook } from '@testing-library/react-hooks';
|
||||
import { renderHook } from '@testing-library/react';
|
||||
import { useRefreshHelper } from './use_refresh_helper';
|
||||
import { WorkpadRoutingContext, WorkpadRoutingContextType } from '../workpad_routing_context';
|
||||
|
||||
|
@ -31,7 +31,7 @@ const getMockedContext = (context: any) =>
|
|||
|
||||
const getContextWrapper =
|
||||
(context: WorkpadRoutingContextType) =>
|
||||
({ children }: PropsWithChildren<unknown>) =>
|
||||
({ children }: PropsWithChildren) =>
|
||||
<WorkpadRoutingContext.Provider value={context}>{children}</WorkpadRoutingContext.Provider>;
|
||||
|
||||
describe('useRefreshHelper', () => {
|
||||
|
@ -77,7 +77,7 @@ describe('useRefreshHelper', () => {
|
|||
expect(mockDispatch).not.toHaveBeenCalledWith(refreshAction);
|
||||
|
||||
state.transient.inFlight = true;
|
||||
// @ts-expect-error @types/react@18 - Type '() => void' has no properties in common with type '{ children?: ReactNode; }'.
|
||||
|
||||
rerender(useRefreshHelper);
|
||||
|
||||
jest.runAllTimers();
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
* 2.0.
|
||||
*/
|
||||
|
||||
import { renderHook } from '@testing-library/react-hooks';
|
||||
import { renderHook } from '@testing-library/react';
|
||||
import { useRestoreHistory } from './use_restore_history';
|
||||
import { encode } from '../route_state';
|
||||
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
* 2.0.
|
||||
*/
|
||||
|
||||
import { renderHook } from '@testing-library/react-hooks';
|
||||
import { waitFor, renderHook } from '@testing-library/react';
|
||||
import { useWorkpad } from './use_workpad';
|
||||
import { spacesService } from '../../../services/kibana_services';
|
||||
|
||||
|
@ -62,7 +62,7 @@ describe('useWorkpad', () => {
|
|||
workpad: workpadResponse,
|
||||
});
|
||||
|
||||
const { waitFor, unmount } = renderHook(() => useWorkpad(workpadId, true, getRedirectPath));
|
||||
const { unmount } = renderHook(() => useWorkpad(workpadId, true, getRedirectPath));
|
||||
|
||||
try {
|
||||
await waitFor(() => expect(mockDispatch).toHaveBeenCalledTimes(3));
|
||||
|
@ -88,7 +88,7 @@ describe('useWorkpad', () => {
|
|||
aliasId,
|
||||
});
|
||||
|
||||
const { waitFor, unmount } = renderHook(() => useWorkpad(workpadId, true, getRedirectPath));
|
||||
const { unmount } = renderHook(() => useWorkpad(workpadId, true, getRedirectPath));
|
||||
|
||||
try {
|
||||
await waitFor(() => expect(mockDispatch).toHaveBeenCalledTimes(3));
|
||||
|
@ -118,7 +118,7 @@ describe('useWorkpad', () => {
|
|||
aliasPurpose: 'savedObjectConversion',
|
||||
});
|
||||
|
||||
const { waitFor, unmount } = renderHook(() => useWorkpad(workpadId, true, getRedirectPath));
|
||||
const { unmount } = renderHook(() => useWorkpad(workpadId, true, getRedirectPath));
|
||||
try {
|
||||
await waitFor(() => expect(mockRedirectLegacyUrl).toHaveBeenCalled());
|
||||
expect(mockRedirectLegacyUrl).toBeCalledWith({
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
* 2.0.
|
||||
*/
|
||||
|
||||
import { renderHook } from '@testing-library/react-hooks';
|
||||
import { renderHook } from '@testing-library/react';
|
||||
import { useWorkpadHistory } from './use_workpad_history';
|
||||
import { encode } from '../route_state';
|
||||
|
||||
|
|
|
@ -4,11 +4,13 @@
|
|||
* 2.0; you may not use this file except in compliance with the Elastic License
|
||||
* 2.0.
|
||||
*/
|
||||
import { renderHook } from '@testing-library/react-hooks';
|
||||
|
||||
import crypto from 'crypto';
|
||||
import { renderHook } from '@testing-library/react';
|
||||
import { useWorkpadPersist } from './use_workpad_persist';
|
||||
|
||||
const mockGetState = jest.fn();
|
||||
const mockUpdateWorkpad = jest.fn();
|
||||
const mockUpdateWorkpad = jest.fn(() => Promise.resolve(null));
|
||||
const mockUpdateAssets = jest.fn();
|
||||
const mockUpdate = jest.fn();
|
||||
|
||||
|
@ -36,53 +38,41 @@ jest.mock('../../../services', () => ({
|
|||
}));
|
||||
|
||||
describe('useWorkpadPersist', () => {
|
||||
const initialState = {
|
||||
persistent: {
|
||||
workpad: { id: crypto.randomUUID(), some: 'workpad' },
|
||||
},
|
||||
assets: {
|
||||
asset1: 'some asset',
|
||||
asset2: 'other asset',
|
||||
},
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
jest.resetAllMocks();
|
||||
// create a default state for each test
|
||||
mockGetState.mockReturnValue(initialState);
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
afterEach(() => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
test('initial render does not persist state', () => {
|
||||
const state = {
|
||||
persistent: {
|
||||
workpad: { some: 'workpad' },
|
||||
},
|
||||
assets: {
|
||||
asset1: 'some asset',
|
||||
asset2: 'other asset',
|
||||
},
|
||||
};
|
||||
|
||||
mockGetState.mockReturnValue(state);
|
||||
|
||||
renderHook(useWorkpadPersist);
|
||||
|
||||
expect(mockUpdateWorkpad).not.toBeCalled();
|
||||
});
|
||||
|
||||
test('changes to workpad cause a workpad update', () => {
|
||||
const state = {
|
||||
persistent: {
|
||||
workpad: { some: 'workpad' },
|
||||
},
|
||||
assets: {
|
||||
asset1: 'some asset',
|
||||
asset2: 'other asset',
|
||||
},
|
||||
};
|
||||
|
||||
mockGetState.mockReturnValue(state);
|
||||
|
||||
const { rerender } = renderHook(useWorkpadPersist);
|
||||
|
||||
const newState = {
|
||||
...state,
|
||||
...initialState,
|
||||
persistent: {
|
||||
workpad: { new: 'workpad' },
|
||||
workpad: { id: crypto.randomUUID(), new: 'workpad' },
|
||||
},
|
||||
};
|
||||
|
||||
mockGetState.mockReturnValue(newState);
|
||||
|
||||
rerender();
|
||||
|
@ -91,13 +81,6 @@ describe('useWorkpadPersist', () => {
|
|||
});
|
||||
|
||||
test('non changes causes no updated', () => {
|
||||
const state = {
|
||||
persistent: {
|
||||
workpad: { some: 'workpad' },
|
||||
},
|
||||
};
|
||||
mockGetState.mockReturnValue(state);
|
||||
|
||||
const { rerender } = renderHook(useWorkpadPersist);
|
||||
|
||||
rerender();
|
||||
|
@ -106,26 +89,17 @@ describe('useWorkpadPersist', () => {
|
|||
});
|
||||
|
||||
test('non write permissions causes no updates', () => {
|
||||
const state = {
|
||||
persistent: {
|
||||
workpad: { some: 'workpad' },
|
||||
},
|
||||
transient: {
|
||||
canUserWrite: false,
|
||||
},
|
||||
};
|
||||
mockGetState.mockReturnValue(state);
|
||||
|
||||
const { rerender } = renderHook(useWorkpadPersist);
|
||||
|
||||
const newState = {
|
||||
persistent: {
|
||||
workpad: { new: 'workpad value' },
|
||||
workpad: { id: crypto.randomUUID(), new: 'workpad value' },
|
||||
},
|
||||
transient: {
|
||||
canUserWrite: false,
|
||||
},
|
||||
};
|
||||
|
||||
mockGetState.mockReturnValue(newState);
|
||||
|
||||
rerender();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue