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 entities.js to thunks #34582

Merged
merged 3 commits 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
26 changes: 10 additions & 16 deletions packages/core-data/src/entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@ import { upperFirst, camelCase, map, find, get, startCase } from 'lodash';
/**
* WordPress dependencies
*/
import { controls } from '@wordpress/data';
import { apiFetch } from '@wordpress/data-controls';
import apiFetch from '@wordpress/api-fetch';
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import { addEntities } from './actions';
import { STORE_NAME } from './name';

export const DEFAULT_ENTITY_KEY = 'id';

Expand Down Expand Up @@ -170,8 +168,8 @@ export const prePersistPostType = ( persistedRecord, edits ) => {
*
* @return {Promise} Entities promise
*/
function* loadPostTypeEntities() {
const postTypes = yield apiFetch( { path: '/wp/v2/types?context=edit' } );
async function loadPostTypeEntities() {
const postTypes = await apiFetch( { path: '/wp/v2/types?context=edit' } );
return map( postTypes, ( postType, name ) => {
const isTemplate = [ 'wp_template', 'wp_template_part' ].includes(
name
Expand Down Expand Up @@ -203,8 +201,8 @@ function* loadPostTypeEntities() {
*
* @return {Promise} Entities promise
*/
function* loadTaxonomyEntities() {
const taxonomies = yield apiFetch( {
async function loadTaxonomyEntities() {
const taxonomies = await apiFetch( {
path: '/wp/v2/taxonomies?context=edit',
} );
return map( taxonomies, ( taxonomy, name ) => {
Expand Down Expand Up @@ -252,12 +250,8 @@ export const getMethodName = (
*
* @return {Array} Entities
*/
export function* getKindEntities( kind ) {
let entities = yield controls.select(
STORE_NAME,
'getEntitiesByKind',
kind
);
export const getKindEntities = ( kind ) => async ( { select, dispatch } ) => {
let entities = select.getEntitiesByKind( kind );
if ( entities && entities.length !== 0 ) {
return entities;
}
Expand All @@ -267,8 +261,8 @@ export function* getKindEntities( kind ) {
return [];
}

entities = yield kindConfig.loadEntities();
yield addEntities( entities );
entities = await kindConfig.loadEntities();
dispatch( addEntities( entities ) );

return entities;
}
};
66 changes: 40 additions & 26 deletions packages/core-data/src/test/entities.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/**
* WordPress dependencies
*/
import triggerFetch from '@wordpress/api-fetch';
jest.mock( '@wordpress/api-fetch' );

/**
* Internal dependencies
*/
Expand All @@ -7,7 +13,6 @@ import {
getKindEntities,
prePersistPostType,
} from '../entities';
import { addEntities } from '../actions';

describe( 'getMethodName', () => {
it( 'should return the right method name for an entity with the root kind', () => {
Expand Down Expand Up @@ -45,43 +50,52 @@ describe( 'getMethodName', () => {
} );

describe( 'getKindEntities', () => {
beforeEach( async () => {
triggerFetch.mockReset();
jest.useFakeTimers();
} );

it( 'shouldn’t do anything if the entities have already been resolved', async () => {
const dispatch = jest.fn();
const select = {
getEntitiesByKind: jest.fn( () => entities ),
};
const entities = [ { kind: 'postType' } ];
const fulfillment = getKindEntities( 'postType' );
// Start the generator
fulfillment.next();
// Provide the entities
const end = fulfillment.next( entities );
expect( end.done ).toBe( true );
await getKindEntities( 'postType' )( { dispatch, select } );
expect( dispatch ).not.toHaveBeenCalled();
} );

it( 'shouldn’t do anything if there no defined kind config', async () => {
const fulfillment = getKindEntities( 'unknownKind' );
// Start the generator
fulfillment.next();
// Provide no entities to continue
const end = fulfillment.next( [] );
expect( end.done ).toBe( true );
const dispatch = jest.fn();
const select = {
getEntitiesByKind: jest.fn( () => [] ),
};
await getKindEntities( 'unknownKind' )( { dispatch, select } );
expect( dispatch ).not.toHaveBeenCalled();
} );

it( 'should fetch and add the entities', async () => {
const fetchedEntities = [
{
baseURL: '/wp/v2/posts',
kind: 'postType',
name: 'post',
rest_base: 'posts',
labels: {
singular_name: 'post',
},
},
];
const fulfillment = getKindEntities( 'postType' );
// Start the generator
fulfillment.next();
// Provide no entities to continue
fulfillment.next( [] );
// Fetch entities and trigger action
const { value: action } = fulfillment.next( fetchedEntities );
expect( action ).toEqual( addEntities( fetchedEntities ) );
const end = fulfillment.next();
expect( end ).toEqual( { done: true, value: fetchedEntities } );
const dispatch = jest.fn();
const select = {
getEntitiesByKind: jest.fn( () => [] ),
};
triggerFetch.mockImplementation( () => fetchedEntities );

await getKindEntities( 'postType' )( { dispatch, select } );
expect( dispatch ).toHaveBeenCalledTimes( 1 );
expect( dispatch.mock.calls[ 0 ][ 0 ].type ).toBe( 'ADD_ENTITIES' );
expect( dispatch.mock.calls[ 0 ][ 0 ].entities.length ).toBe( 1 );
expect( dispatch.mock.calls[ 0 ][ 0 ].entities[ 0 ].baseURL ).toBe(
'/wp/v2/posts'
);
} );
} );

Expand Down
Loading