Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

useEntityRecord: Improve unit tests #56415

Merged
merged 2 commits into from
Nov 23, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 16 additions & 15 deletions packages/core-data/src/hooks/test/use-entity-record.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,22 +116,27 @@ describe( 'useEntityRecord', () => {
} );

it( 'does not resolve entity record when disabled via options', async () => {
// Provide response
triggerFetch.mockImplementation( () => TEST_RECORD );

let data;
const TestComponent = () => {
data = useEntityRecord( 'root', 'widget', 2, {
options: { enabled: false },
} );
const TestComponent = ( { enabled } ) => {
data = useEntityRecord( 'root', 'widget', 1, { enabled } );
return <div />;
};
render(
const UI = ( { enabled } ) => (
<RegistryProvider value={ registry }>
<TestComponent />
<TestComponent enabled={ enabled } />
</RegistryProvider>
);

const { rerender } = render( <UI enabled={ true } /> );
await act(
() => new Promise( ( resolve ) => setTimeout( resolve, 0 ) )
kevin940726 marked this conversation as resolved.
Show resolved Hide resolved
);
expect( triggerFetch ).toHaveBeenCalledTimes( 1 );

rerender( <UI enabled={ false } /> );

expect( data ).toEqual( {
edit: expect.any( Function ),
editedRecord: {},
Expand All @@ -141,14 +146,10 @@ describe( 'useEntityRecord', () => {
save: expect.any( Function ),
} );

// Fetch request should have been issued.
await waitFor( () => {
expect( triggerFetch ).not.toHaveBeenCalled();
} );
await waitFor( () =>
expect( triggerFetch ).not.toHaveBeenCalledWith( {
path: '/wp/v2/widgets/2?context=edit',
} )
// The same delay.
await act(
() => new Promise( ( resolve ) => setTimeout( resolve, 0 ) )
);
expect( triggerFetch ).toHaveBeenCalledTimes( 1 );
} );
} );
Loading