Skip to content

Commit

Permalink
Warn consumers about incorrect usage
Browse files Browse the repository at this point in the history
  • Loading branch information
Mamaduka committed Jul 23, 2024
1 parent 90a0d22 commit 140d3de
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
2 changes: 2 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/core-data/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"@wordpress/sync": "file:../sync",
"@wordpress/undo-manager": "file:../undo-manager",
"@wordpress/url": "file:../url",
"@wordpress/warning": "file:../warning",
"change-case": "^4.1.2",
"equivalent-key-map": "^0.2.2",
"fast-deep-equal": "^3.1.3",
Expand Down
22 changes: 22 additions & 0 deletions packages/core-data/src/hooks/test/use-resource-permissions.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,26 @@ describe( 'useResourcePermissions', () => {
} )
);
} );

it( 'should warn when called with incorrect arguments signature', () => {
const TestComponent = () => {
useResourcePermissions(
{
kind: 'root',
name: 'media',
},
1
);
return null;
};
render(
<RegistryProvider value={ registry }>
<TestComponent />
</RegistryProvider>
);

expect( console ).toHaveWarnedWith(
`When 'resource' is an entity object, passing 'id' as a separate argument isn't supported.`
);
} );
} );
9 changes: 8 additions & 1 deletion packages/core-data/src/hooks/use-resource-permissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* WordPress dependencies
*/
import deprecated from '@wordpress/deprecated';
import warning from '@wordpress/warning';

/**
* Internal dependencies
Expand Down Expand Up @@ -134,9 +135,15 @@ function useResourcePermissions< IdType = void >(
const resourceAsString =
typeof resource === 'object' ? JSON.stringify( resource ) : resource;

const isEntity = typeof resource === 'object';
if ( isEntity && typeof id !== 'undefined' ) {
warning(
`When 'resource' is an entity object, passing 'id' as a separate argument isn't supported.`
);
}

return useQuerySelect(
( resolve ) => {
const isEntity = typeof resource === 'object';
const hasId = isEntity ? !! resource.id : !! id;
const { canUser } = resolve( coreStore );
const create = canUser(
Expand Down

0 comments on commit 140d3de

Please sign in to comment.