Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate getAutosaves resolver to thunks #34581

Merged
merged 1 commit into from
Sep 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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