Skip to content

Commit

Permalink
Migrate getAutosaves to thunks
Browse files Browse the repository at this point in the history
  • Loading branch information
adamziel authored and jsnajdr committed Sep 7, 2021
1 parent 5c5fd35 commit 7e6b7fa
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 43 deletions.
31 changes: 13 additions & 18 deletions packages/core-data/src/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import {
receiveThemeSupports,
receiveEmbedPreview,
receiveUserPermission,
receiveAutosaves,
} from './actions';
import { getKindEntities, DEFAULT_ENTITY_KEY } from './entities';
import { ifNotResolved, getNormalizedCommaSeparable } from './utils';
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand Down
51 changes: 26 additions & 25 deletions packages/core-data/src/test/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import {
import {
receiveEmbedPreview,
receiveUserPermission,
receiveAutosaves,
receiveCurrentUser,
} from '../actions';

Expand Down Expand Up @@ -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
);
} );

Expand All @@ -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();
} );
} );

Expand Down

0 comments on commit 7e6b7fa

Please sign in to comment.