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] Pass the correct kind, name, and recordId to getEditedEntityRecord #43517

Merged
merged 1 commit into from
Aug 23, 2022
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
42 changes: 41 additions & 1 deletion packages/core-data/src/hooks/test/use-entity-record.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,53 @@ describe( 'useEntityRecord', () => {

expect( data ).toEqual( {
edit: expect.any( Function ),
editedRecord: {},
editedRecord: { hello: 'world', id: 1 },
hasEdits: false,
record: { hello: 'world', id: 1 },
save: expect.any( Function ),
hasResolved: true,
isResolving: false,
status: 'SUCCESS',
} );
} );

it( 'applies edits to the entity record', async () => {
// Provide response
triggerFetch.mockImplementation( () => TEST_RECORD );

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

await act( async () => {
jest.advanceTimersByTime( 1 );
} );

expect( widget ).toEqual( {
edit: expect.any( Function ),
editedRecord: { hello: 'world', id: 1 },
hasEdits: false,
record: { hello: 'world', id: 1 },
save: expect.any( Function ),
hasResolved: true,
isResolving: false,
status: 'SUCCESS',
} );

await act( async () => {
widget.edit( { hello: 'foo' } );
jest.advanceTimersByTime( 1 );
} );

expect( widget.hasEdits ).toEqual( true );
expect( widget.record ).toEqual( { hello: 'world', id: 1 } );
expect( widget.editedRecord ).toEqual( { hello: 'foo', id: 1 } );
} );
} );
12 changes: 10 additions & 2 deletions packages/core-data/src/hooks/use-entity-record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,16 @@ export default function useEntityRecord< RecordType >(

const { editedRecord, hasEdits } = useSelect(
( select ) => ( {
editedRecord: select( coreStore ).getEditedEntityRecord(),
hasEdits: select( coreStore ).hasEditsForEntityRecord(),
editedRecord: select( coreStore ).getEditedEntityRecord(
kind,
name,
recordId
),
hasEdits: select( coreStore ).hasEditsForEntityRecord(
kind,
name,
recordId
),
} ),
[ kind, name, recordId ]
);
Expand Down