Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import triggerFetch from '@wordpress/api-fetch';
import { createRegistry, RegistryProvider } from '@wordpress/data';
jest.mock( '@wordpress/api-fetch' );
import { act, render, waitFor } from '@testing-library/react';
import { act, renderHook, waitFor } from '@testing-library/react';
import { createElement } from '@wordpress/element';
import { store as coreDataStore } from '../../index';
import useEntityRecord from '../use-entity-record';

Expand All @@ -14,25 +15,24 @@ describe( 'useEntityRecord', () => {
triggerFetch.mockReset();
} );

function renderHookWithRegistry( hook, options = {} ) {
const Wrapper = ( { children } ) =>
createElement( RegistryProvider, { value: registry }, children );
return renderHook( hook, { wrapper: Wrapper, ...options } );
}

const TEST_RECORD = { id: 1, hello: 'world' };
const TEST_RECORD_RESPONSE = { json: () => Promise.resolve( TEST_RECORD ) };

it( 'resolves the entity record when missing from the state', async () => {
// Provide response
triggerFetch.mockImplementation( () => TEST_RECORD_RESPONSE );

let data;
const TestComponent = () => {
data = useEntityRecord( 'root', 'widget', 1 );
return <div />;
};
render(
<RegistryProvider value={ registry }>
<TestComponent />
</RegistryProvider>
const { result } = renderHookWithRegistry( () =>
useEntityRecord( 'root', 'widget', 1 )
);

expect( data ).toEqual( {
expect( result.current ).toEqual( {
edit: expect.any( Function ),
editedRecord: false,
hasEdits: false,
Expand All @@ -53,7 +53,7 @@ describe( 'useEntityRecord', () => {
} )
);

expect( data ).toEqual( {
expect( result.current ).toEqual( {
edit: expect.any( Function ),
editedRecord: { hello: 'world', id: 1 },
hasEdits: false,
Expand All @@ -71,19 +71,12 @@ describe( 'useEntityRecord', () => {
// Provide response
triggerFetch.mockImplementation( () => TEST_RECORD_RESPONSE );

let widget;
const TestComponent = () => {
widget = useEntityRecord( 'root', 'widget', 1 );
return <div />;
};
render(
<RegistryProvider value={ registry }>
<TestComponent />
</RegistryProvider>
const { result } = renderHookWithRegistry( () =>
useEntityRecord( 'root', 'widget', 1 )
);

await waitFor( () =>
expect( widget ).toEqual( {
expect( result.current ).toEqual( {
edit: expect.any( Function ),
editedRecord: { hello: 'world', id: 1 },
hasEdits: false,
Expand All @@ -98,32 +91,30 @@ describe( 'useEntityRecord', () => {
);

await act( async () => {
widget.edit( { hello: 'foo' } );
result.current.edit( { hello: 'foo' } );
} );

await waitFor( () => expect( widget.hasEdits ).toEqual( true ) );
await waitFor( () =>
expect( result.current.hasEdits ).toEqual( true )
);

expect( widget.record ).toEqual( { hello: 'world', id: 1 } );
expect( widget.editedRecord ).toEqual( { hello: 'foo', id: 1 } );
expect( widget.edits ).toEqual( { hello: 'foo' } );
expect( result.current.record ).toEqual( { hello: 'world', id: 1 } );
expect( result.current.editedRecord ).toEqual( {
hello: 'foo',
id: 1,
} );
expect( result.current.edits ).toEqual( { hello: 'foo' } );
} );

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

let data;
const TestComponent = ( { enabled } ) => {
data = useEntityRecord( 'root', 'widget', 1, { enabled } );
return <div />;
};
const UI = ( { enabled } ) => (
<RegistryProvider value={ registry }>
<TestComponent enabled={ enabled } />
</RegistryProvider>
const { result, rerender } = renderHookWithRegistry(
( { enabled } ) =>
useEntityRecord( 'root', 'widget', 1, { enabled } ),
{ initialProps: { enabled: true } }
);

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

// A minimum delay for a fetch request. The same delay is used again as a control.
await act(
() => new Promise( ( resolve ) => setTimeout( resolve, 0 ) )
Expand All @@ -137,9 +128,9 @@ describe( 'useEntityRecord', () => {
// Clear the fetch call history.
triggerFetch.mockReset();

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

expect( data ).toEqual( {
expect( result.current ).toEqual( {
edit: expect.any( Function ),
editedRecord: {},
hasEdits: false,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import triggerFetch from '@wordpress/api-fetch';
import { createRegistry, RegistryProvider } from '@wordpress/data';
jest.mock( '@wordpress/api-fetch' );
import { render, waitFor } from '@testing-library/react';
import { renderHook, waitFor } from '@testing-library/react';
import { createElement } from '@wordpress/element';
import { store as coreDataStore } from '../../index';
import useEntityRecords, {
useEntityRecordsWithPermissions,
Expand All @@ -15,6 +16,12 @@ describe( 'useEntityRecords', () => {
registry.register( coreDataStore );
} );

function renderHookWithRegistry( hook, options = {} ) {
const Wrapper = ( { children } ) =>
createElement( RegistryProvider, { value: registry }, children );
return renderHook( hook, { wrapper: Wrapper, ...options } );
}

const TEST_RECORDS = [
{ id: 1, hello: 'world1' },
{ id: 2, hello: 'world2' },
Expand All @@ -25,18 +32,11 @@ describe( 'useEntityRecords', () => {
// Provide response
triggerFetch.mockImplementation( () => TEST_RECORDS );

let data;
const TestComponent = () => {
data = useEntityRecords( 'root', 'widget', { status: 'draft' } );
return <div />;
};
render(
<RegistryProvider value={ registry }>
<TestComponent />
</RegistryProvider>
const { result } = renderHookWithRegistry( () =>
useEntityRecords( 'root', 'widget', { status: 'draft' } )
);

expect( data ).toEqual( {
expect( result.current ).toEqual( {
records: null,
hasResolved: false,
hasStarted: false,
Expand All @@ -53,7 +53,7 @@ describe( 'useEntityRecords', () => {
} )
);

expect( data ).toEqual( {
expect( result.current ).toEqual( {
records: TEST_RECORDS,
hasResolved: true,
hasStarted: true,
Expand Down Expand Up @@ -83,6 +83,12 @@ describe( 'useEntityRecordsWithPermissions', () => {
] );
} );

function renderHookWithRegistry( hook, options = {} ) {
const Wrapper = ( { children } ) =>
createElement( RegistryProvider, { value: registry }, children );
return renderHook( hook, { wrapper: Wrapper, ...options } );
}

const TEST_RECORDS = [
{ id: 1, title: 'Post 1', slug: 'post-1' },
{ id: 2, title: 'Post 2', slug: 'post-2' },
Expand All @@ -94,16 +100,10 @@ describe( 'useEntityRecordsWithPermissions', () => {
// Mock the API response
triggerFetch.mockImplementation( () => TEST_RECORDS );

const TestComponent = () => {
renderHookWithRegistry( () =>
useEntityRecordsWithPermissions( 'postType', 'post', {
_fields: fieldsFromMock,
} );
return <div />;
};
render(
<RegistryProvider value={ registry }>
<TestComponent />
</RegistryProvider>
} )
);

// Should inject _links into the _fields parameter
Expand All @@ -120,14 +120,8 @@ describe( 'useEntityRecordsWithPermissions', () => {
// Mock the API response
triggerFetch.mockImplementation( () => TEST_RECORDS );

const TestComponent = () => {
useEntityRecordsWithPermissions( 'postType', 'post', {} );
return <div />;
};
render(
<RegistryProvider value={ registry }>
<TestComponent />
</RegistryProvider>
renderHookWithRegistry( () =>
useEntityRecordsWithPermissions( 'postType', 'post', {} )
);

// Should not add _fields when not originally provided
Expand All @@ -143,16 +137,10 @@ describe( 'useEntityRecordsWithPermissions', () => {
triggerFetch.mockImplementation( () => TEST_RECORDS );

const fieldsWithLinks = fieldsFromMock + ',_links';
const TestComponent = () => {
renderHookWithRegistry( () =>
useEntityRecordsWithPermissions( 'postType', 'post', {
_fields: fieldsWithLinks,
} );
return <div />;
};
render(
<RegistryProvider value={ registry }>
<TestComponent />
</RegistryProvider>
} )
);

// Should not duplicate _links (deduplication working correctly)
Expand Down
Loading
Loading