From 883c31465d5d6569849d488e1241356a386f06ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Zieli=C5=84ski?= Date: Mon, 6 Sep 2021 14:03:40 +0200 Subject: [PATCH] Migrate canUser --- packages/core-data/src/resolvers.js | 20 +-- packages/core-data/src/test/resolvers.js | 150 +++++++++++------------ 2 files changed, 85 insertions(+), 85 deletions(-) diff --git a/packages/core-data/src/resolvers.js b/packages/core-data/src/resolvers.js index 7026937858b47..702fb92ccb3db 100644 --- a/packages/core-data/src/resolvers.js +++ b/packages/core-data/src/resolvers.js @@ -9,6 +9,7 @@ import { find, includes, get, hasIn, compact, uniq } from 'lodash'; import { addQueryArgs } from '@wordpress/url'; import { controls } from '@wordpress/data'; import { apiFetch } from '@wordpress/data-controls'; +import triggerFetch from '@wordpress/api-fetch'; /** * Internal dependencies */ @@ -25,7 +26,6 @@ import { receiveEntityRecords, receiveThemeSupports, receiveEmbedPreview, - receiveUserPermission, receiveAutosaves, } from './actions'; import { getKindEntities, DEFAULT_ENTITY_KEY } from './entities'; @@ -294,7 +294,7 @@ export function* getEmbedPreview( url ) { * @param {string} resource REST resource to check, e.g. 'media' or 'posts'. * @param {?string} id ID of the rest resource to check. */ -export function* canUser( action, resource, id ) { +export const canUser = ( action, resource, id ) => async ( { dispatch } ) => { const methods = { create: 'POST', read: 'GET', @@ -311,7 +311,7 @@ export function* canUser( action, resource, id ) { let response; try { - response = yield apiFetch( { + response = await triggerFetch( { path, // Ideally this would always be an OPTIONS request, but unfortunately there's // a bug in the REST API which causes the Allow header to not be sent on @@ -339,8 +339,8 @@ export function* canUser( action, resource, id ) { const key = compact( [ action, resource, id ] ).join( '/' ); const isAllowed = includes( allowHeader, method ); - yield receiveUserPermission( key, isAllowed ); -} + dispatch.receiveUserPermission( key, isAllowed ); +}; /** * Checks whether the current user can perform the given action on the given @@ -350,16 +350,18 @@ export function* canUser( action, resource, id ) { * @param {string} name Entity name. * @param {string} recordId Record's id. */ -export function* canUserEditEntityRecord( kind, name, recordId ) { - const entities = yield getKindEntities( kind ); +export const canUserEditEntityRecord = ( kind, name, recordId ) => async ( { + dispatch, +} ) => { + const entities = await dispatch( getKindEntities( kind ) ); const entity = find( entities, { kind, name } ); if ( ! entity ) { return; } const resource = entity.__unstable_rest_base; - yield canUser( 'update', resource, recordId ); -} + await dispatch( canUser( 'update', resource, recordId ) ); +}; /** * Request autosave data from the REST API. diff --git a/packages/core-data/src/test/resolvers.js b/packages/core-data/src/test/resolvers.js index 2ff24b126ab8d..9eb320451aa5a 100644 --- a/packages/core-data/src/test/resolvers.js +++ b/packages/core-data/src/test/resolvers.js @@ -1,7 +1,9 @@ /** * WordPress dependencies */ -import { apiFetch } from '@wordpress/data-controls'; +import triggerFetch from '@wordpress/api-fetch'; + +jest.mock( '@wordpress/api-fetch' ); /** * Internal dependencies @@ -17,7 +19,6 @@ import { import { receiveEntityRecords, receiveEmbedPreview, - receiveUserPermission, receiveAutosaves, receiveCurrentUser, } from '../actions'; @@ -218,106 +219,103 @@ describe( 'getEmbedPreview', () => { } ); describe( 'canUser', () => { - it( 'does nothing when there is an API error', () => { - const generator = canUser( 'create', 'media' ); - - let received = generator.next(); - expect( received.done ).toBe( false ); - expect( received.value ).toEqual( - apiFetch( { - path: '/wp/v2/media', - method: 'OPTIONS', - parse: false, - } ) + beforeEach( async () => { + triggerFetch.mockReset(); + } ); + + it( 'does nothing when there is an API error', async () => { + const dispatch = Object.assign( jest.fn(), { + receiveUserPermission: jest.fn(), + } ); + + triggerFetch.mockImplementation( () => + Promise.reject( { status: 404 } ) ); - received = generator.throw( { status: 404 } ); - expect( received.done ).toBe( true ); - expect( received.value ).toBeUndefined(); + await canUser( 'create', 'media' )( { dispatch } ); + + expect( triggerFetch ).toHaveBeenCalledWith( { + path: '/wp/v2/media', + method: 'OPTIONS', + parse: false, + } ); + + expect( dispatch.receiveUserPermission ).not.toHaveBeenCalled(); } ); - it( 'receives false when the user is not allowed to perform an action', () => { - const generator = canUser( 'create', 'media' ); - - let received = generator.next(); - expect( received.done ).toBe( false ); - expect( received.value ).toEqual( - apiFetch( { - path: '/wp/v2/media', - method: 'OPTIONS', - parse: false, - } ) - ); + it( 'receives false when the user is not allowed to perform an action', async () => { + const dispatch = Object.assign( jest.fn(), { + receiveUserPermission: jest.fn(), + } ); - received = generator.next( { + triggerFetch.mockImplementation( () => ( { headers: { Allow: 'GET', }, + } ) ); + + await canUser( 'create', 'media' )( { dispatch } ); + + expect( triggerFetch ).toHaveBeenCalledWith( { + path: '/wp/v2/media', + method: 'OPTIONS', + parse: false, } ); - expect( received.done ).toBe( false ); - expect( received.value ).toEqual( - receiveUserPermission( 'create/media', false ) - ); - received = generator.next(); - expect( received.done ).toBe( true ); - expect( received.value ).toBeUndefined(); + expect( dispatch.receiveUserPermission ).toHaveBeenCalledWith( + 'create/media', + false + ); } ); - it( 'receives true when the user is allowed to perform an action', () => { - const generator = canUser( 'create', 'media' ); - - let received = generator.next(); - expect( received.done ).toBe( false ); - expect( received.value ).toEqual( - apiFetch( { - path: '/wp/v2/media', - method: 'OPTIONS', - parse: false, - } ) - ); + it( 'receives true when the user is allowed to perform an action', async () => { + const dispatch = Object.assign( jest.fn(), { + receiveUserPermission: jest.fn(), + } ); - received = generator.next( { + triggerFetch.mockImplementation( () => ( { headers: { Allow: 'POST, GET, PUT, DELETE', }, + } ) ); + + await canUser( 'create', 'media' )( { dispatch } ); + + expect( triggerFetch ).toHaveBeenCalledWith( { + path: '/wp/v2/media', + method: 'OPTIONS', + parse: false, } ); - expect( received.done ).toBe( false ); - expect( received.value ).toEqual( - receiveUserPermission( 'create/media', true ) - ); - received = generator.next(); - expect( received.done ).toBe( true ); - expect( received.value ).toBeUndefined(); + expect( dispatch.receiveUserPermission ).toHaveBeenCalledWith( + 'create/media', + true + ); } ); - it( 'receives true when the user is allowed to perform an action on a specific resource', () => { - const generator = canUser( 'update', 'blocks', 123 ); - - let received = generator.next(); - expect( received.done ).toBe( false ); - expect( received.value ).toEqual( - apiFetch( { - path: '/wp/v2/blocks/123', - method: 'GET', - parse: false, - } ) - ); + it( 'receives true when the user is allowed to perform an action on a specific resource', async () => { + const dispatch = Object.assign( jest.fn(), { + receiveUserPermission: jest.fn(), + } ); - received = generator.next( { + triggerFetch.mockImplementation( () => ( { headers: { Allow: 'POST, GET, PUT, DELETE', }, + } ) ); + + await canUser( 'create', 'blocks', 123 )( { dispatch } ); + + expect( triggerFetch ).toHaveBeenCalledWith( { + path: '/wp/v2/blocks/123', + method: 'GET', + parse: false, } ); - expect( received.done ).toBe( false ); - expect( received.value ).toEqual( - receiveUserPermission( 'update/blocks/123', true ) - ); - received = generator.next(); - expect( received.done ).toBe( true ); - expect( received.value ).toBeUndefined(); + expect( dispatch.receiveUserPermission ).toHaveBeenCalledWith( + 'create/blocks/123', + true + ); } ); } );