Skip to content

Commit

Permalink
Pass the correct kind, name, and recordId to getEditedEntityRecord an…
Browse files Browse the repository at this point in the history
…d hasEditsForEntityRecord in useEntityRecord (#43517)
  • Loading branch information
adamziel authored Aug 23, 2022
1 parent 28f403f commit 567081f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
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

0 comments on commit 567081f

Please sign in to comment.