Skip to content

Commit

Permalink
Add JS tests for useCreateAccounts hook.
Browse files Browse the repository at this point in the history
  • Loading branch information
ankitrox committed Sep 25, 2024
1 parent 767a906 commit 2de300a
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 0 deletions.
7 changes: 7 additions & 0 deletions js/src/hooks/useCreateAccounts.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ import useGoogleAdsAccount from '.~/hooks/useGoogleAdsAccount';
* Custom hook to handle the creation of MC and Ads accounts.
*/
const useCreateAccounts = () => {
/**
* Refs are used to avoid the re-render of the parent component.
*
* accountCreationChecksResolvedRef - Indicates if the account creation checks have been resolved.
* isCreatingAccountsRef - Indicates if the accounts are being created.
* accountsCreatedRef - Indicates if the accounts have been created.
*/
const accountCreationChecksResolvedRef = useRef( false );
const isCreatingAccountsRef = useRef( false );
const accountsCreatedRef = useRef( false );
Expand Down
105 changes: 105 additions & 0 deletions js/src/hooks/useCreateAccounts.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/* eslint-disable testing-library/no-unnecessary-act */
/**
* External dependencies
*/
import { renderHook, act } from '@testing-library/react';

/**
* Internal dependencies
*/
import useCreateAccounts from './useCreateAccounts';
import useCreateMCAccount from '../components/google-mc-account-card/useCreateMCAccount';
import useUpsertAdsAccount from './useUpsertAdsAccount';
import useExistingGoogleAdsAccounts from './useExistingGoogleAdsAccounts';
import useExistingGoogleMCAccounts from './useExistingGoogleMCAccounts';
import useGoogleAdsAccount from './useGoogleAdsAccount';

jest.mock( '../components/google-mc-account-card/useCreateMCAccount' );
jest.mock( './useUpsertAdsAccount' );
jest.mock( './useExistingGoogleAdsAccounts' );
jest.mock( './useExistingGoogleMCAccounts' );
jest.mock( './useGoogleAdsAccount' );

describe( 'useCreateAccounts hook', () => {
let handleCreateAccount;
let upsertAdsAccount;

beforeEach( () => {
handleCreateAccount = jest.fn( () => Promise.resolve() );
upsertAdsAccount = jest.fn( () => Promise.resolve() );

useCreateMCAccount.mockReturnValue( [
handleCreateAccount,
{ data: undefined, response: { status: 200 } },
] );
useUpsertAdsAccount.mockReturnValue( [
upsertAdsAccount,
{ loading: false },
] );
useGoogleAdsAccount.mockReturnValue( {
googleAdsAccount: { id: 0 },
hasFinishedResolution: true,
} );
useExistingGoogleMCAccounts.mockReturnValue( {
data: [],
hasFinishedResolution: true,
} );
useExistingGoogleAdsAccounts.mockReturnValue( {
existingAccounts: [],
isResolving: false,
} );
} );

it( 'should not call "handleCreateAccount" and "upsertAdsAccount" when there are existing accounts', () => {
// Simulate existing accounts
useExistingGoogleMCAccounts.mockReturnValue( {
data: [ { id: 1 } ],
hasFinishedResolution: true,
} );
useExistingGoogleAdsAccounts.mockReturnValue( {
existingAccounts: [ { id: 1 } ],
isResolving: false,
} );

const { result } = renderHook( () => useCreateAccounts() );

expect( result.current.isCreatingAccounts ).toBe( false );
expect( result.current.accountsCreated ).toBe( false );
expect( handleCreateAccount ).not.toHaveBeenCalled();
expect( upsertAdsAccount ).not.toHaveBeenCalled();
} );

it( 'should call "handleCreateAccount" and "upsertAdsAccount" when there are no existing accounts', async () => {
// Simulate the initial state and mock behavior for account creation
const { result, rerender } = renderHook( () => useCreateAccounts() );

expect( result.current.isCreatingAccounts ).toBe( false );

useCreateMCAccount.mockReturnValueOnce( [
handleCreateAccount,
{ data: {}, response: { status: 200 } },
] );
useUpsertAdsAccount.mockReturnValueOnce( [
upsertAdsAccount,
{ loading: false },
] );

await act( async () => {
rerender(); // Trigger the effect that begins account creation
} );

// At this point, isCreatingAccounts should be true
expect( result.current.isCreatingAccounts ).toBe( true );

await act( async () => {
rerender(); // Trigger the effect that begins account creation
} );

expect( result.current.isCreatingAccounts ).toBe( false );
expect( result.current.accountsCreated ).toBe( true );

// Finally, check that the functions were called correctly
expect( handleCreateAccount ).toHaveBeenCalledTimes( 1 );
expect( upsertAdsAccount ).toHaveBeenCalledTimes( 1 );
} );
} );

0 comments on commit 2de300a

Please sign in to comment.