Skip to content

Commit

Permalink
Add renderHook for useUser test.
Browse files Browse the repository at this point in the history
Hook should be called by a component. In test, renderHook would do that for us.
  • Loading branch information
renintw committed Jun 9, 2023
1 parent 01a7114 commit fe2689f
Showing 1 changed file with 26 additions and 9 deletions.
35 changes: 26 additions & 9 deletions settings/src/tests/hooks/useUser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
import { useSelect } from '@wordpress/data';
import { useEntityRecord } from '@wordpress/core-data';
import { renderHook } from '@testing-library/react';

/**
* Local dependencies
Expand All @@ -23,7 +24,7 @@ describe( 'useUser', () => {
it( 'should call useEntityRecord with correct arguments', () => {
useEntityRecord.mockReturnValue( { record: {} } );

useUser( 1 );
renderHook( () => useUser( 1 ) );

expect( useEntityRecord ).toHaveBeenCalledWith( 'root', 'user', 1 );
} );
Expand All @@ -32,7 +33,9 @@ describe( 'useUser', () => {
useEntityRecord.mockReturnValue( { record: {} } );
useSelect.mockReturnValue( true );

const user = useUser( 1 );
const {
result: { current: user },
} = renderHook( () => useUser( 1 ) );

expect( useSelect ).toHaveBeenCalled();
expect( user.isSaving ).toBeTruthy();
Expand All @@ -43,31 +46,39 @@ describe( 'useUser', () => {
record: { password: 'test-password' },
} );

const user = useUser( 1 );
const {
result: { current: user },
} = renderHook( () => useUser( 1 ) );

expect( user.userRecord.record.password ).toBe( 'test-password' );
} );

it( 'should set hasPrimaryProvider to false if user.userRecord.record is undefined', () => {
useEntityRecord.mockReturnValue( {} );

const user = useUser( 1 );
const {
result: { current: user },
} = renderHook( () => useUser( 1 ) );

expect( user.hasPrimaryProvider ).toBe( false );
} );

it( 'should set hasPrimaryProvider to false if 2fa_available_providers is undefined', () => {
useEntityRecord.mockReturnValue( { record: {} } );

const user = useUser( 1 );
const {
result: { current: user },
} = renderHook( () => useUser( 1 ) );

expect( user.hasPrimaryProvider ).toBe( false );
} );

it( 'should set hasPrimaryProvider to false if 2fa_available_providers is empty', () => {
useEntityRecord.mockReturnValue( { record: { '2fa_available_providers': [] } } );

const user = useUser( 1 );
const {
result: { current: user },
} = renderHook( () => useUser( 1 ) );

expect( user.hasPrimaryProvider ).toBe( false );
} );
Expand All @@ -77,7 +88,9 @@ describe( 'useUser', () => {
record: { '2fa_available_providers': [ 'Two_Factor_Backup_Codes' ] },
} );

const user = useUser( 1 );
const {
result: { current: user },
} = renderHook( () => useUser( 1 ) );

expect( user.hasPrimaryProvider ).toBe( false );
} );
Expand All @@ -87,7 +100,9 @@ describe( 'useUser', () => {
record: { '2fa_available_providers': [ 'Two_Factor_Totp', 'Two_Factor_Backup_Codes' ] },
} );

const user = useUser( 1 );
const {
result: { current: user },
} = renderHook( () => useUser( 1 ) );

expect( user.hasPrimaryProvider ).toBe( true );
} );
Expand All @@ -102,7 +117,9 @@ describe( 'useUser', () => {
},
} );

const user = useUser( 1 );
const {
result: { current: user },
} = renderHook( () => useUser( 1 ) );

expect( user.hasPrimaryProvider ).toBe( true );
} );
Expand Down

0 comments on commit fe2689f

Please sign in to comment.