-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add __experimentalUseResourcePermissions (#38785)
Add useResourcePermissions hook that makes it easy to fetch user permissions: const [ hasResolved, { canCreate, canUpdate, canDelete } ] = __experimentalUseResourcePermissions( 'navigation', ref ); It returns a tuple with hasResolved as the first item to make it intentionally hard to overlook. Co-authored-by: Kai Hao <[email protected]>
- Loading branch information
1 parent
6acad24
commit d3157c7
Showing
6 changed files
with
276 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
packages/core-data/src/hooks/test/use-resource-permissions.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import triggerFetch from '@wordpress/api-fetch'; | ||
import { createRegistry, RegistryProvider } from '@wordpress/data'; | ||
|
||
jest.mock( '@wordpress/api-fetch' ); | ||
|
||
/** | ||
* External dependencies | ||
*/ | ||
import { act, render } from '@testing-library/react'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { store as coreDataStore } from '../../index'; | ||
import useResourcePermissions from '../use-resource-permissions'; | ||
|
||
describe( 'useResourcePermissions', () => { | ||
let registry; | ||
beforeEach( () => { | ||
jest.useFakeTimers(); | ||
|
||
registry = createRegistry(); | ||
registry.register( coreDataStore ); | ||
|
||
triggerFetch.mockImplementation( () => ( { | ||
headers: { | ||
get: () => ( { | ||
allow: 'POST', | ||
} ), | ||
}, | ||
} ) ); | ||
} ); | ||
|
||
afterEach( () => { | ||
jest.runOnlyPendingTimers(); | ||
jest.useRealTimers(); | ||
} ); | ||
|
||
it( 'retrieves the relevant permissions for a key-less resource', async () => { | ||
let data; | ||
const TestComponent = () => { | ||
data = useResourcePermissions( 'widgets' ); | ||
return <div />; | ||
}; | ||
render( | ||
<RegistryProvider value={ registry }> | ||
<TestComponent /> | ||
</RegistryProvider> | ||
); | ||
expect( data ).toEqual( [ | ||
false, | ||
{ | ||
status: 'IDLE', | ||
isResolving: false, | ||
canCreate: false, | ||
}, | ||
] ); | ||
|
||
// Required to make sure no updates happen outside of act() | ||
await act( async () => { | ||
jest.advanceTimersByTime( 1 ); | ||
} ); | ||
|
||
expect( data ).toEqual( [ | ||
true, | ||
{ | ||
status: 'SUCCESS', | ||
isResolving: false, | ||
canCreate: true, | ||
}, | ||
] ); | ||
} ); | ||
|
||
it( 'retrieves the relevant permissions for a resource with a key', async () => { | ||
let data; | ||
const TestComponent = () => { | ||
data = useResourcePermissions( 'widgets', 1 ); | ||
return <div />; | ||
}; | ||
render( | ||
<RegistryProvider value={ registry }> | ||
<TestComponent /> | ||
</RegistryProvider> | ||
); | ||
expect( data ).toEqual( [ | ||
false, | ||
{ | ||
status: 'IDLE', | ||
isResolving: false, | ||
canCreate: false, | ||
canUpdate: false, | ||
canDelete: false, | ||
}, | ||
] ); | ||
|
||
// Required to make sure no updates happen outside of act() | ||
await act( async () => { | ||
jest.advanceTimersByTime( 1 ); | ||
} ); | ||
|
||
expect( data ).toEqual( [ | ||
true, | ||
{ | ||
status: 'SUCCESS', | ||
isResolving: false, | ||
canCreate: true, | ||
canUpdate: false, | ||
canDelete: false, | ||
}, | ||
] ); | ||
} ); | ||
} ); |
Oops, something went wrong.