From 5ff940732e97b7ebc4feeadb3e0c85ad43c3ec1d Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Wed, 10 Jul 2024 18:52:30 +0400 Subject: [PATCH] Throw an error when an entity resource object is malformed --- packages/core-data/src/resolvers.js | 2 +- packages/core-data/src/test/resolvers.js | 24 +++++++----------------- 2 files changed, 8 insertions(+), 18 deletions(-) diff --git a/packages/core-data/src/resolvers.js b/packages/core-data/src/resolvers.js index d9b90e593620b7..d3ca23221eadd0 100644 --- a/packages/core-data/src/resolvers.js +++ b/packages/core-data/src/resolvers.js @@ -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( diff --git a/packages/core-data/src/test/resolvers.js b/packages/core-data/src/test/resolvers.js index c751a67f73d46e..95a70b5e5c45f9 100644 --- a/packages/core-data/src/test/resolvers.js +++ b/packages/core-data/src/test/resolvers.js @@ -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 () => {