Skip to content

Commit

Permalink
Throw an error when an entity resource object is malformed
Browse files Browse the repository at this point in the history
  • Loading branch information
Mamaduka committed Jul 10, 2024
1 parent 3246e87 commit b1e5dfc
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 18 deletions.
2 changes: 1 addition & 1 deletion packages/core-data/src/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ export const canUser =
let resourcePath = null;
if ( typeof resource === 'object' ) {
if ( ! resource.kind || ! resource.name ) {
return;
throw new Error( 'The entity resource object is not valid.' );
}

const configs = await dispatch(
Expand Down
24 changes: 7 additions & 17 deletions packages/core-data/src/test/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,23 +333,13 @@ describe( 'canUser', () => {
expect( dispatch.receiveUserPermission ).not.toHaveBeenCalled();
} );

it( 'does nothing when entity kind or name is missing', async () => {
triggerFetch.mockImplementation( () =>
Promise.reject( { status: 404 } )
);

await canUser( 'create', { kind: 'root', name: 'media' } )( {
dispatch,
registry,
} );
await canUser( 'create', { name: 'wp_block' } )( {
dispatch,
registry,
} );

expect( triggerFetch ).not.toHaveBeenCalledWith();

expect( dispatch.receiveUserPermission ).not.toHaveBeenCalled();
it( 'throws an error when an entity resource object is malformed', async () => {
await expect(
canUser( 'create', { name: 'wp_block' } )( {
dispatch,
registry,
} )
).rejects.toThrow( 'The entity resource object is not valid.' );
} );

it( 'receives false when the user is not allowed to perform an action', async () => {
Expand Down

0 comments on commit b1e5dfc

Please sign in to comment.