Skip to content

Commit

Permalink
Migrate entities.js to thunks
Browse files Browse the repository at this point in the history
  • Loading branch information
adamziel committed Sep 7, 2021
1 parent 7791d58 commit 75441d3
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 46 deletions.
30 changes: 10 additions & 20 deletions packages/core-data/src/entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,16 @@ 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';

const POST_RAW_ATTRIBUTES = [ 'title', 'excerpt', 'content' ];

export const defaultEntities = [
{
label: __( 'Base' ),
Expand All @@ -43,7 +39,6 @@ export const defaultEntities = [
key: 'slug',
baseURL: '/wp/v2/types',
baseURLParams: { context: 'edit' },
rawAttributes: POST_RAW_ATTRIBUTES,
},
{
name: 'media',
Expand Down Expand Up @@ -170,8 +165,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 All @@ -187,7 +182,6 @@ function* loadPostTypeEntities() {
selection: true,
},
mergedEdits: { meta: true },
rawAttributes: POST_RAW_ATTRIBUTES,
getTitle: ( record ) =>
record?.title?.rendered ||
record?.title ||
Expand All @@ -203,8 +197,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 +246,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 +257,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

0 comments on commit 75441d3

Please sign in to comment.