Skip to content

Commit

Permalink
Fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Mamaduka committed Jul 17, 2024
1 parent ec2abb7 commit 5c25763
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 17 deletions.
69 changes: 54 additions & 15 deletions packages/block-library/src/navigation/test/use-navigation-menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,37 +63,76 @@ function resolveRecords( registry, menus ) {

function resolveReadPermission( registry, allowed ) {
const dispatch = registry.dispatch( coreStore );
dispatch.receiveUserPermission( 'create/navigation', allowed );
dispatch.startResolution( 'canUser', [ 'read', 'navigation' ] );
dispatch.finishResolution( 'canUser', [ 'read', 'navigation' ] );
dispatch.receiveUserPermission( 'read/postType/wp_navigation', allowed );
dispatch.startResolution( 'canUser', [
'read',
{ kind: 'postType', name: 'wp_navigation', id: undefined },
] );
dispatch.finishResolution( 'canUser', [
'read',
{ kind: 'postType', name: 'wp_navigation', id: undefined },
] );
}

function resolveReadRecordPermission( registry, ref, allowed ) {
const dispatch = registry.dispatch( coreStore );
dispatch.receiveUserPermission( 'create/navigation', allowed );
dispatch.startResolution( 'canUser', [ 'read', 'navigation', ref ] );
dispatch.finishResolution( 'canUser', [ 'read', 'navigation', ref ] );
dispatch.receiveUserPermission(
`read/postType/wp_navigation/${ ref }`,
allowed
);
dispatch.startResolution( 'canUser', [
'read',
{ kind: 'postType', name: 'wp_navigation', id: ref },
] );
dispatch.finishResolution( 'canUser', [
'read',
{ kind: 'postType', name: 'wp_navigation', id: ref },
] );
}

function resolveCreatePermission( registry, allowed ) {
const dispatch = registry.dispatch( coreStore );
dispatch.receiveUserPermission( 'create/navigation', allowed );
dispatch.startResolution( 'canUser', [ 'create', 'navigation' ] );
dispatch.finishResolution( 'canUser', [ 'create', 'navigation' ] );
dispatch.receiveUserPermission( 'create/postType/wp_navigation', allowed );
dispatch.startResolution( 'canUser', [
'create',
{ kind: 'postType', name: 'wp_navigation' },
] );
dispatch.finishResolution( 'canUser', [
'create',
{ kind: 'postType', name: 'wp_navigation' },
] );
}

function resolveUpdatePermission( registry, ref, allowed ) {
const dispatch = registry.dispatch( coreStore );
dispatch.receiveUserPermission( `update/navigation/${ ref }`, allowed );
dispatch.startResolution( 'canUser', [ 'update', 'navigation', ref ] );
dispatch.finishResolution( 'canUser', [ 'update', 'navigation', ref ] );
dispatch.receiveUserPermission(
`update/postType/wp_navigation/${ ref }`,
allowed
);
dispatch.startResolution( 'canUser', [
'update',
{ kind: 'postType', name: 'wp_navigation', id: ref },
] );
dispatch.finishResolution( 'canUser', [
'update',
{ kind: 'postType', name: 'wp_navigation', id: ref },
] );
}

function resolveDeletePermission( registry, ref, allowed ) {
const dispatch = registry.dispatch( coreStore );
dispatch.receiveUserPermission( `delete/navigation/${ ref }`, allowed );
dispatch.startResolution( 'canUser', [ 'delete', 'navigation', ref ] );
dispatch.finishResolution( 'canUser', [ 'delete', 'navigation', ref ] );
dispatch.receiveUserPermission(
`delete/postType/wp_navigation/${ ref }`,
allowed
);
dispatch.startResolution( 'canUser', [
'delete',
{ kind: 'postType', name: 'wp_navigation', id: ref },
] );
dispatch.finishResolution( 'canUser', [
'delete',
{ kind: 'postType', name: 'wp_navigation', id: ref },
] );
}

describe( 'useNavigationMenus', () => {
Expand Down
10 changes: 8 additions & 2 deletions packages/core-data/src/hooks/use-resource-permissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,16 @@ export default function useResourcePermissions< IdType = void >(

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

const hasId = typeof resource === 'object' ? !! resource.id : !! id;
if ( ! hasId ) {
const read = canUser( 'read', resource );

Expand Down

0 comments on commit 5c25763

Please sign in to comment.