Skip to content

Commit

Permalink
Testing: Remove @testing-library/react-hooks in favor of @testing-lib…
Browse files Browse the repository at this point in the history
…rary/react (#80914)
  • Loading branch information
tyxla authored and pull[bot] committed Dec 1, 2023
1 parent 528542f commit 7960428
Show file tree
Hide file tree
Showing 36 changed files with 116 additions and 158 deletions.
3 changes: 1 addition & 2 deletions client/components/backup-storage-space/test/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ jest.mock( 'calypso/state/ui/selectors/get-selected-site-slug', () =>
jest.mock( 'calypso/lib/jetpack/is-jetpack-cloud' );
jest.mock( 'calypso/state/analytics/actions/record' );

import { render } from '@testing-library/react';
import { renderHook } from '@testing-library/react-hooks';
import { render, renderHook } from '@testing-library/react';
import {
useDaysOfBackupsSavedText,
useStorageUsageText,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
/**
* @jest-environment jsdom
*/
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { renderHook, act } from '@testing-library/react-hooks';
import { act, renderHook, waitFor } from '@testing-library/react';
import React from 'react';
import request from 'wpcom-proxy-request';
import { NewsletterCategories } from '../types';
Expand Down Expand Up @@ -38,7 +41,7 @@ describe( 'useMarkAsNewsletterCategoryMutation', () => {
success: true,
} );

const { result, waitFor } = renderHook( () => useMarkAsNewsletterCategoryMutation( siteId ), {
const { result } = renderHook( () => useMarkAsNewsletterCategoryMutation( siteId ), {
wrapper,
} );

Expand Down Expand Up @@ -74,7 +77,7 @@ describe( 'useMarkAsNewsletterCategoryMutation', () => {
} );

it( 'should throw an error when ID is missing', async () => {
const { result, waitFor } = renderHook( () => useMarkAsNewsletterCategoryMutation( siteId ), {
const { result } = renderHook( () => useMarkAsNewsletterCategoryMutation( siteId ), {
wrapper,
} );

Expand All @@ -94,7 +97,7 @@ describe( 'useMarkAsNewsletterCategoryMutation', () => {
it( 'should throw an error when API response is unsuccessful', async () => {
( request as jest.Mock ).mockResolvedValue( { success: false } );

const { result, waitFor } = renderHook( () => useMarkAsNewsletterCategoryMutation( siteId ), {
const { result } = renderHook( () => useMarkAsNewsletterCategoryMutation( siteId ), {
wrapper,
} );

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
/**
* @jest-environment jsdom
*/
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { renderHook, act } from '@testing-library/react-hooks';
import { act, renderHook, waitFor } from '@testing-library/react';
import React from 'react';
import request from 'wpcom-proxy-request';
import { NewsletterCategories } from '../types';
Expand Down Expand Up @@ -38,7 +41,7 @@ describe( 'useUnmarkAsNewsletterCategoryMutation', () => {
success: true,
} );

const { result, waitFor } = renderHook( () => useUnmarkAsNewsletterCategoryMutation( siteId ), {
const { result } = renderHook( () => useUnmarkAsNewsletterCategoryMutation( siteId ), {
wrapper,
} );

Expand Down Expand Up @@ -74,7 +77,7 @@ describe( 'useUnmarkAsNewsletterCategoryMutation', () => {
} );

it( 'should throw an error when ID is missing', async () => {
const { result, waitFor } = renderHook( () => useUnmarkAsNewsletterCategoryMutation( siteId ), {
const { result } = renderHook( () => useUnmarkAsNewsletterCategoryMutation( siteId ), {
wrapper,
} );

Expand All @@ -94,7 +97,7 @@ describe( 'useUnmarkAsNewsletterCategoryMutation', () => {
it( 'should throw an error when API response is unsuccessful', async () => {
( request as jest.Mock ).mockResolvedValue( { success: false } );

const { result, waitFor } = renderHook( () => useUnmarkAsNewsletterCategoryMutation( siteId ), {
const { result } = renderHook( () => useUnmarkAsNewsletterCategoryMutation( siteId ), {
wrapper,
} );

Expand Down
6 changes: 3 additions & 3 deletions client/landing/stepper/hooks/test/use-countries.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @jest-environment jsdom
*/
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { renderHook } from '@testing-library/react-hooks';
import { renderHook, waitFor } from '@testing-library/react';
import nock from 'nock';
import { useCountries } from '../use-countries';

Expand All @@ -24,11 +24,11 @@ describe( 'use-countries hook', () => {
.get( '/wpcom/v2/woocommerce/countries/regions/' )
.reply( 200, expected );

const { result, waitFor } = renderHook( () => useCountries(), {
const { result } = renderHook( () => useCountries(), {
wrapper,
} );

await waitFor( () => result.current.isSuccess );
await waitFor( () => expect( result.current.isSuccess ).toBe( true ) );

expect( result.current.data ).toEqual( expected );
} );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @jest-environment jsdom
*/
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { renderHook } from '@testing-library/react-hooks';
import { renderHook } from '@testing-library/react';
import nock from 'nock';
import { useSendEmailVerification } from '../use-send-email-verification';

Expand Down
2 changes: 1 addition & 1 deletion client/landing/stepper/hooks/test/use-site-id-param.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @jest-environment jsdom
*/
import { renderHook } from '@testing-library/react-hooks';
import { renderHook } from '@testing-library/react';
import { useSiteIdParam } from '../use-site-id-param';

jest.mock( 'react-router-dom', () => ( {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @jest-environment jsdom
*/

import { renderHook } from '@testing-library/react-hooks';
import { renderHook } from '@testing-library/react';
import { getFileExtension } from 'calypso/lib/media/utils/get-file-extension';
import { useTruncatedFileName } from '../hooks';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/**
* @jest-environment jsdom
*/
import { fireEvent, render, screen } from '@testing-library/react';
import { renderHook } from '@testing-library/react-hooks';
import { fireEvent, render, renderHook, screen } from '@testing-library/react';
import { MailboxForm } from 'calypso/my-sites/email/form/mailboxes';
import { MailboxField } from 'calypso/my-sites/email/form/mailboxes/components/mailbox-field';
import { useGetDefaultFieldLabelText } from 'calypso/my-sites/email/form/mailboxes/components/use-get-default-field-label-text';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/**
* @jest-environment jsdom
*/
import { fireEvent, render, screen } from '@testing-library/react';
import { renderHook } from '@testing-library/react-hooks';
import { fireEvent, render, renderHook, screen } from '@testing-library/react';
import { MailboxForm } from 'calypso/my-sites/email/form/mailboxes';
import { MailboxFormWrapper } from 'calypso/my-sites/email/form/mailboxes/components/mailbox-form-wrapper';
import { useGetDefaultFieldLabelText } from 'calypso/my-sites/email/form/mailboxes/components/use-get-default-field-label-text';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
* @jest-environment jsdom
*/
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { fireEvent, render, screen } from '@testing-library/react';
import { renderHook } from '@testing-library/react-hooks';
import { fireEvent, render, renderHook, screen } from '@testing-library/react';
import nock from 'nock';
import { ReactElement } from 'react';
import { Provider } from 'react-redux';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/**
* @jest-environment jsdom
*/
import {
PLAN_ENTERPRISE_GRID_WPCOM,
PLAN_BUSINESS,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/**
* @jest-environment jsdom
*/
import {
PLAN_BUSINESS,
PLAN_PREMIUM,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/**
* @jest-environment jsdom
*/
jest.mock( 'react-redux', () => ( {
...jest.requireActual( 'react-redux' ),
useDispatch: jest.fn(),
Expand All @@ -16,7 +19,7 @@ jest.mock( 'calypso/state/ui/selectors', () => ( {
} ) );

import { PLAN_JETPACK_FREE } from '@automattic/calypso-products';
import { renderHook } from '@testing-library/react-hooks';
import { renderHook } from '@testing-library/react';
import { JPC_PATH_BASE } from 'calypso/jetpack-connect/constants';
import { storePlan } from 'calypso/jetpack-connect/persistence-utils';
import isJetpackCloud from 'calypso/lib/jetpack/is-jetpack-cloud';
Expand Down
5 changes: 4 additions & 1 deletion client/my-sites/plugins/hooks/test/use-get-dialog-text.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { renderHook } from '@testing-library/react-hooks';
/**
* @jest-environment jsdom
*/
import { renderHook } from '@testing-library/react';
import { PluginActions } from '../types';
import useGetDialogText from '../use-get-dialog-text';
import type { Site, Plugin } from '../types';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
/**
* @jest-environment jsdom
*/

import { render } from '@testing-library/react';
import { renderHook } from '@testing-library/react-hooks';
import { render, renderHook } from '@testing-library/react';
import { PluginActions } from '../types';
import useShowPluginActionDialog from '../use-show-plugin-action-dialog';

Expand Down
2 changes: 1 addition & 1 deletion client/my-sites/site-monitoring/test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/

import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { renderHook } from '@testing-library/react-hooks';
import { renderHook } from '@testing-library/react';
import React from 'react';
import { Provider } from 'react-redux';
import configureStore from 'redux-mock-store';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { renderHook } from '@testing-library/react-hooks';
/**
* @jest-environment jsdom
*/
import { renderHook } from '@testing-library/react';
import { useGroupByTime } from '../hooks/use-group-by-time';
import { PeriodData } from '../use-metrics-query';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { renderHook } from '@testing-library/react-hooks';
/**
* @jest-environment jsdom
*/
import { renderHook } from '@testing-library/react';
import defaultCalypsoI18n, { I18NContext } from 'i18n-calypso';
import { Provider } from 'react-redux';
import { createStore } from 'redux';
Expand Down
5 changes: 4 additions & 1 deletion client/my-sites/themes/test/use-theme-showcase-title.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { renderHook } from '@testing-library/react-hooks';
/**
* @jest-environment jsdom
*/
import { renderHook } from '@testing-library/react';
import defaultCalypsoI18n, { I18NContext } from 'i18n-calypso';
import { Provider } from 'react-redux';
import { createStore } from 'redux';
Expand Down
1 change: 0 additions & 1 deletion client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,6 @@
"@testing-library/dom": "^8.20.1",
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^14.0.0",
"@testing-library/react-hooks": "7.0.2",
"@testing-library/user-event": "^14.4.3",
"@types/react": "^18.2.14",
"@types/redux-mock-store": "1.0.3",
Expand Down
23 changes: 9 additions & 14 deletions client/state/partner-portal/invoices/test/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/

import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { act, renderHook } from '@testing-library/react-hooks';
import { act, renderHook, waitFor } from '@testing-library/react';
import nock from 'nock';
import { useDispatch } from 'react-redux';
import usePayInvoiceMutation from 'calypso/state/partner-portal/invoices/hooks/pay-invoice-mutation';
Expand Down Expand Up @@ -55,7 +55,7 @@ describe( 'useInvoicesQuery', () => {
.get( '/wpcom/v2/jetpack-licensing/partner/invoices?starting_after=&ending_before=' )
.reply( 200, stub );

const { result, waitFor } = renderHook(
const { result } = renderHook(
() =>
useInvoicesQuery( {
starting_after: '',
Expand All @@ -66,7 +66,7 @@ describe( 'useInvoicesQuery', () => {
}
);

await waitFor( () => result.current.isSuccess );
await waitFor( () => expect( result.current.isSuccess ).toBe( true ) );

expect( result.current.data ).toEqual( formattedStub );
} );
Expand All @@ -84,7 +84,7 @@ describe( 'useInvoicesQuery', () => {
const dispatch = jest.fn();
useDispatch.mockReturnValue( dispatch );

const { result, waitFor } = renderHook(
const { result } = renderHook(
() =>
useInvoicesQuery(
{
Expand All @@ -98,9 +98,8 @@ describe( 'useInvoicesQuery', () => {
}
);

await waitFor( () => result.current.isError );
await waitFor( () => expect( result.current.isError ).toBe( true ) );

expect( result.current.isError ).toBe( true );
expect( dispatch.mock.calls[ 0 ][ 0 ].type ).toBe( 'NOTICE_CREATE' );
expect( dispatch.mock.calls[ 0 ][ 0 ].notice.noticeId ).toBe(
'partner-portal-invoices-failure'
Expand Down Expand Up @@ -132,7 +131,7 @@ describe( 'usePayInvoiceMutation', () => {
const dispatch = jest.fn();
useDispatch.mockReturnValue( dispatch );

const { result, waitFor } = renderHook( () => usePayInvoiceMutation(), {
const { result } = renderHook( () => usePayInvoiceMutation(), {
wrapper,
} );

Expand Down Expand Up @@ -167,12 +166,9 @@ describe( 'usePayInvoiceMutation', () => {
useDispatch.mockReturnValue( dispatch );

// Prevent console.error from being loud during testing because of the test 500 error.
const { result, waitFor } = renderHook(
() => usePayInvoiceMutation( { useErrorBoundary: false } ),
{
wrapper,
}
);
const { result } = renderHook( () => usePayInvoiceMutation( { useErrorBoundary: false } ), {
wrapper,
} );

try {
await act( async () => result.current.mutateAsync( { invoiceId: invoiceStub.id } ) );
Expand All @@ -182,7 +178,6 @@ describe( 'usePayInvoiceMutation', () => {
}

await waitFor( () => {
expect( result.current.isError ).toBe( true );
expect( dispatch.mock.calls[ 0 ][ 0 ].type ).toBe( 'NOTICE_CREATE' );
expect( dispatch.mock.calls[ 0 ][ 0 ].notice.text ).toBe( stub.message );
expect( dispatch.mock.calls[ 0 ][ 0 ].notice.noticeId ).toBe(
Expand Down
Loading

0 comments on commit 7960428

Please sign in to comment.