diff --git a/packages/core-data/src/resolvers.js b/packages/core-data/src/resolvers.js index 84c3a293cbec4..0e6ec93bd83dd 100644 --- a/packages/core-data/src/resolvers.js +++ b/packages/core-data/src/resolvers.js @@ -28,7 +28,6 @@ import { receiveThemeSupports, receiveEmbedPreview, receiveUserPermission, - receiveAutosaves, } from './actions'; import { getKindEntities, DEFAULT_ENTITY_KEY } from './entities'; import { ifNotResolved, getNormalizedCommaSeparable } from './utils'; @@ -365,20 +364,19 @@ export function* canUserEditEntityRecord( kind, name, recordId ) { * @param {string} postType The type of the parent post. * @param {number} postId The id of the parent post. */ -export function* getAutosaves( postType, postId ) { - const { rest_base: restBase } = yield controls.resolveSelect( - STORE_NAME, - 'getPostType', - postType - ); - const autosaves = yield apiFetch( { +export const getAutosaves = ( postType, postId ) => async ( { + dispatch, + resolveSelect, +} ) => { + const { rest_base: restBase } = await resolveSelect.getPostType( postType ); + const autosaves = await triggerFetch( { path: `/wp/v2/${ restBase }/${ postId }/autosaves?context=edit`, } ); if ( autosaves && autosaves.length ) { - yield receiveAutosaves( postId, autosaves ); + dispatch.receiveAutosaves( postId, autosaves ); } -} +}; /** * Request autosave data from the REST API. @@ -389,14 +387,11 @@ export function* getAutosaves( postType, postId ) { * @param {string} postType The type of the parent post. * @param {number} postId The id of the parent post. */ -export function* getAutosave( postType, postId ) { - yield controls.resolveSelect( - STORE_NAME, - 'getAutosaves', - postType, - postId - ); -} +export const getAutosave = ( postType, postId ) => async ( { + resolveSelect, +} ) => { + await resolveSelect.getAutosaves( postType, postId ); +}; /** * Retrieve the frontend template used for a given link. diff --git a/packages/core-data/src/test/resolvers.js b/packages/core-data/src/test/resolvers.js index 7f517291bb203..91d28f6ab5ea4 100644 --- a/packages/core-data/src/test/resolvers.js +++ b/packages/core-data/src/test/resolvers.js @@ -20,7 +20,6 @@ import { import { receiveEmbedPreview, receiveUserPermission, - receiveAutosaves, receiveCurrentUser, } from '../actions'; @@ -392,28 +391,31 @@ describe( 'getAutosaves', () => { }, ]; + beforeEach( async () => { + triggerFetch.mockReset(); + } ); + it( 'yields with fetched autosaves', async () => { const postType = 'post'; const postId = 1; const restBase = 'posts'; const postEntity = { rest_base: restBase }; - const fulfillment = getAutosaves( postType, postId ); - // Trigger generator - fulfillment.next(); + triggerFetch.mockImplementation( () => SUCCESSFUL_RESPONSE ); + const dispatch = Object.assign( jest.fn(), { + receiveAutosaves: jest.fn(), + } ); + const resolveSelect = Object.assign( jest.fn(), { + getPostType: jest.fn( () => postEntity ), + } ); + await getAutosaves( postType, postId )( { dispatch, resolveSelect } ); - // Trigger generator with the postEntity and assert that correct path is formed - // in the apiFetch request. - const { value: apiFetchAction } = fulfillment.next( postEntity ); - expect( apiFetchAction.request ).toEqual( { + expect( triggerFetch ).toHaveBeenCalledWith( { path: `/wp/v2/${ restBase }/${ postId }/autosaves?context=edit`, } ); - - // Provide apiFetch response and trigger Action - const received = ( await fulfillment.next( SUCCESSFUL_RESPONSE ) ) - .value; - expect( received ).toEqual( - receiveAutosaves( 1, SUCCESSFUL_RESPONSE ) + expect( dispatch.receiveAutosaves ).toHaveBeenCalledWith( + 1, + SUCCESSFUL_RESPONSE ); } ); @@ -422,21 +424,20 @@ describe( 'getAutosaves', () => { const postId = 1; const restBase = 'posts'; const postEntity = { rest_base: restBase }; - const fulfillment = getAutosaves( postType, postId ); - // Trigger generator - fulfillment.next(); + triggerFetch.mockImplementation( () => [] ); + const dispatch = Object.assign( jest.fn(), { + receiveAutosaves: jest.fn(), + } ); + const resolveSelect = Object.assign( jest.fn(), { + getPostType: jest.fn( () => postEntity ), + } ); + await getAutosaves( postType, postId )( { dispatch, resolveSelect } ); - // Trigger generator with the postEntity and assert that correct path is formed - // in the apiFetch request. - const { value: apiFetchAction } = fulfillment.next( postEntity ); - expect( apiFetchAction.request ).toEqual( { + expect( triggerFetch ).toHaveBeenCalledWith( { path: `/wp/v2/${ restBase }/${ postId }/autosaves?context=edit`, } ); - - // Provide apiFetch response and trigger Action - const received = ( await fulfillment.next( [] ) ).value; - expect( received ).toBeUndefined(); + expect( dispatch.receiveAutosaves ).not.toHaveBeenCalled(); } ); } );