Skip to content

Commit

Permalink
remove instance of react hooks testing library
Browse files Browse the repository at this point in the history
  • Loading branch information
eokoneyo committed Dec 31, 2024
1 parent 846717f commit 1d59a95
Show file tree
Hide file tree
Showing 23 changed files with 55 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/

import React from 'react';
import { renderHook } from '@testing-library/react-hooks';
import { renderHook, waitFor } from '@testing-library/react';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { testQueryClientConfig } from '../test_utils/test_query_client_config';
import { httpServiceMock } from '@kbn/core-http-browser-mocks';
Expand All @@ -32,7 +32,7 @@ describe('useFindAlertsQuery', () => {
});

it('calls the api correctly', async () => {
const { result, waitForValueToChange } = renderHook(
const { result } = renderHook(
() =>
useFindAlertsQuery({
...mockServices,
Expand All @@ -43,7 +43,7 @@ describe('useFindAlertsQuery', () => {
}
);

await waitForValueToChange(() => result.current.isLoading, { timeout: 5000 });
await waitFor(() => expect(result.current.isLoading).toBe(true), { timeout: 5000 });

expect(mockServices.http.post).toHaveBeenCalledTimes(1);
expect(mockServices.http.post).toBeCalledWith('/internal/rac/alerts/find', {
Expand Down
10 changes: 3 additions & 7 deletions src/plugins/share/public/components/context/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,14 @@
* 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 { useShareTabsContext } from '.';

describe('share menu context', () => {
describe('useShareTabsContext', () => {
it('throws an error if used outside of ShareMenuProvider tree', () => {
const { result } = renderHook(() => useShareTabsContext());

expect(result.error?.message).toEqual(
expect.stringContaining(
'Failed to call `useShareTabsContext` because the context from ShareMenuProvider is missing.'
)
expect(() => renderHook(() => useShareTabsContext())).toThrow(
/^Failed to call `useShareTabsContext` because the context from ShareMenuProvider is missing./
);
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { act, renderHook } from '@testing-library/react-hooks';
import { renderHook, act } from '@testing-library/react';
import {
useCreateKnowledgeBaseEntry,
UseCreateKnowledgeBaseEntryParams,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { act, renderHook } from '@testing-library/react-hooks';
import { renderHook, act } from '@testing-library/react';
import {
useDeleteKnowledgeBaseEntries,
UseDeleteKnowledgeEntriesParams,
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, waitFor } from '@testing-library/react';
import { useKnowledgeBaseEntries } from './use_knowledge_base_entries';
import { HttpSetup } from '@kbn/core/public';
import { IToasts } from '@kbn/core-notifications-browser';
Expand All @@ -27,40 +27,40 @@ describe('useKnowledgeBaseEntries', () => {
data: [{ id: '1', title: 'Entry 1' }],
});

const { result, waitForNextUpdate } = renderHook(
const { result } = renderHook(
() => useKnowledgeBaseEntries({ http: httpMock, enabled: true }),
{
wrapper: TestProviders,
}
);
expect(result.current.fetchStatus).toEqual('fetching');

await waitForNextUpdate();

expect(result.current.data).toEqual({
page: 1,
perPage: 100,
total: 1,
data: [{ id: '1', title: 'Entry 1' }],
});
await waitFor(() =>
expect(result.current.data).toEqual({
page: 1,
perPage: 100,
total: 1,
data: [{ id: '1', title: 'Entry 1' }],
})
);
});

it('handles fetch error', async () => {
const error = new Error('Fetch error');
(httpMock.fetch as jest.Mock).mockRejectedValue(error);

const { waitForNextUpdate } = renderHook(
renderHook(
() => useKnowledgeBaseEntries({ http: httpMock, toasts: toastsMock, enabled: true }),
{
wrapper: TestProviders,
}
);

await waitForNextUpdate();

expect(toastsMock.addError).toHaveBeenCalledWith(error, {
title: 'Error fetching Knowledge Base entries',
});
await waitFor(() =>
expect(toastsMock.addError).toHaveBeenCalledWith(error, {
title: 'Error fetching Knowledge Base entries',
})
);
});

it('does not fetch when disabled', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { act, renderHook } from '@testing-library/react-hooks';
import { renderHook, act } from '@testing-library/react';
import {
useUpdateKnowledgeBaseEntries,
UseUpdateKnowledgeBaseEntriesParams,
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 { CaseStatuses } from '../../../../common/types/domain';
import { useUserPermissions } from '../../user_actions/use_user_permissions';
import { useShouldDisableStatus } from './use_should_disable_status';
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 { useCaseObservables } from './use_case_observables';
import { useGetCaseConfiguration } from '../../containers/configure/use_get_case_configuration';
import { OBSERVABLE_TYPES_BUILTIN_KEYS } from '../../../common/constants';
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 { useCasesContext } from '../cases_context/use_cases_context';
import { useUserPermissions } from './use_user_permissions';
import type { UserActivityParams } from '../user_actions_activity_bar/types';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { renderHook, act } from '@testing-library/react-hooks';
import { renderHook, act, waitFor } from '@testing-library/react';
import { useDeleteObservable } from './use_delete_observables';
import { deleteObservable } from './api';
import { useCasesToast } from '../common/use_cases_toast';
Expand Down Expand Up @@ -35,7 +35,7 @@ describe('useDeleteObservable', () => {
it('should call deleteObservable and show success toast on success', async () => {
(deleteObservable as jest.Mock).mockResolvedValue({});

const { result, waitFor } = renderHook(() => useDeleteObservable(caseId, observableId), {
const { result } = renderHook(() => useDeleteObservable(caseId, observableId), {
wrapper: appMockRender.AppWrapper,
});

Expand All @@ -52,7 +52,7 @@ describe('useDeleteObservable', () => {
const error = new Error('Failed to delete observable');
(deleteObservable as jest.Mock).mockRejectedValue(error);

const { result, waitFor } = renderHook(() => useDeleteObservable(caseId, observableId), {
const { result } = renderHook(() => useDeleteObservable(caseId, observableId), {
wrapper: appMockRender.AppWrapper,
});

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 * as api from './api';
import type { AppMockRenderer } from '../common/mock';
import { createAppMockRenderer } from '../common/mock';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { renderHook, act } from '@testing-library/react-hooks';
import { renderHook, act } from '@testing-library/react';
import { usePatchObservable } from './use_patch_observables';
import { patchObservable } from './api';
import { useCasesToast } from '../common/use_cases_toast';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { renderHook, act } from '@testing-library/react-hooks';
import { renderHook, act, waitFor } from '@testing-library/react';
import * as api from './api';
import { useToasts } from '../common/lib/kibana';
import type { AppMockRenderer } from '../common/mock';
Expand Down Expand Up @@ -41,63 +41,57 @@ describe('usePostObservables', () => {

it('calls the api when invoked with the correct parameters', async () => {
const spy = jest.spyOn(api, 'postObservable');
const { waitForNextUpdate, result } = renderHook(() => usePostObservable(mockCase.id), {
const { result } = renderHook(() => usePostObservable(mockCase.id), {
wrapper: appMockRender.AppWrapper,
});

act(() => {
result.current.mutate(observableMock);
});

await waitForNextUpdate();

expect(spy).toHaveBeenCalledWith({ observable: observableMock.observable }, mockCase.id);
await waitFor(() =>
expect(spy).toHaveBeenCalledWith({ observable: observableMock.observable }, mockCase.id)
);
});

it('invalidates the queries correctly', async () => {
const queryClientSpy = jest.spyOn(appMockRender.queryClient, 'invalidateQueries');
const { waitForNextUpdate, result } = renderHook(() => usePostObservable(mockCase.id), {
const { result } = renderHook(() => usePostObservable(mockCase.id), {
wrapper: appMockRender.AppWrapper,
});

act(() => {
result.current.mutate(observableMock);
});

await waitForNextUpdate();

expect(queryClientSpy).toHaveBeenCalledWith(casesQueriesKeys.caseView());
await waitFor(() => expect(queryClientSpy).toHaveBeenCalledWith(casesQueriesKeys.caseView()));
});

it('does shows a success toaster', async () => {
const { waitForNextUpdate, result } = renderHook(() => usePostObservable(mockCase.id), {
const { result } = renderHook(() => usePostObservable(mockCase.id), {
wrapper: appMockRender.AppWrapper,
});

act(() => {
result.current.mutate(observableMock);
});

await waitForNextUpdate();

expect(addSuccess).toHaveBeenCalled();
await waitFor(() => expect(addSuccess).toHaveBeenCalled());
});

it('shows a toast error when the api return an error', async () => {
jest
.spyOn(api, 'postObservable')
.mockRejectedValue(new Error('usePostObservables: Test error'));

const { waitForNextUpdate, result } = renderHook(() => usePostObservable(mockCase.id), {
const { result } = renderHook(() => usePostObservable(mockCase.id), {
wrapper: appMockRender.AppWrapper,
});

act(() => {
result.current.mutate(observableMock);
});

await waitForNextUpdate();

expect(addError).toHaveBeenCalled();
await waitFor(() => expect(addError).toHaveBeenCalled());
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { act, renderHook } from '@testing-library/react-hooks';
import { renderHook, act, waitFor } from '@testing-library/react';
import { AppMockRenderer, createAppMockRenderer } from '../../test_utils';
import { AlertsQueryContext } from '@kbn/alerts-ui-shared/src/common/contexts/alerts_query_context';
import { useBulkUntrackAlertsByQuery } from './use_bulk_untrack_alerts_by_query';
Expand Down Expand Up @@ -35,7 +35,7 @@ describe('useBulkUntrackAlertsByQuery', () => {
it('calls the api when invoked with the correct parameters', async () => {
httpMock.mockResolvedValue(response);

const { result, waitFor } = renderHook(() => useBulkUntrackAlertsByQuery(), {
const { result } = renderHook(() => useBulkUntrackAlertsByQuery(), {
wrapper: appMockRender.AppWrapper,
});

Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/lens/public/app_plugin/app_helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { renderHook, act } from '@testing-library/react-hooks';
import { renderHook, act } from '@testing-library/react';
import { faker } from '@faker-js/faker';
import { UseNavigateBackToAppProps, useNavigateBackToApp } from './app_helpers';
import { defaultDoc, makeDefaultServices } from '../mocks/services_mock';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import React from 'react';
import { mount } from 'enzyme';
import { HttpStatusBadge } from '.';
import { renderHook } from '@testing-library/react-hooks';
import { renderHook } from '@testing-library/react';
import { useEuiTheme } from '@elastic/eui';

describe('HttpStatusBadge', () => {
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 { useTimeRange } from './use_time_range';
import * as datemath from '../utils/datemath';

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 * as spaceHook from '../../../../../hooks/use_kibana_space';
import * as paramHook from '../../../hooks/use_url_params';
import * as redux from 'react-redux';
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 { useFetchGraphData } from './use_fetch_graph_data';

const mockUseQuery = jest.fn();
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 { useExpandableFlyoutApi } from '@kbn/expandable-flyout';
import { mockFlyoutApi } from '../mocks/mock_flyout_context';
import { useWhichFlyout } from './use_which_flyout';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import { useNavigateToLeftPanel } from './use_navigate_to_left_panel';
import { useIsExperimentalFeatureEnabled } from '../../../../common/hooks/use_experimental_features';
import { useExpandableFlyoutApi } from '@kbn/expandable-flyout';
import { renderHook } from '@testing-library/react-hooks';
import { renderHook } from '@testing-library/react';
import { useDocumentDetailsContext } from '../context';
import { mockFlyoutApi } from '../mocks/mock_flyout_context';
import { DocumentDetailsRightPanelKey, DocumentDetailsLeftPanelKey } from '../constants/panel_keys';
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 { useNavigateToHostDetails } from './use_navigate_to_host_details';
import { useIsExperimentalFeatureEnabled } from '../../../../common/hooks/use_experimental_features';
import { useExpandableFlyoutApi } from '@kbn/expandable-flyout';
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 { useNavigateToUserDetails } from './use_navigate_to_user_details';
import { useIsExperimentalFeatureEnabled } from '../../../../common/hooks/use_experimental_features';
import { useExpandableFlyoutApi } from '@kbn/expandable-flyout';
Expand Down

0 comments on commit 1d59a95

Please sign in to comment.