From 2c3edede2f7c421c4627fe08c587a671ab9b1a17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Zieli=C5=84ski?= Date: Mon, 6 Sep 2021 14:41:22 +0200 Subject: [PATCH] Migrate entities.js to thunks --- packages/core-data/src/entities.js | 30 ++++------- packages/core-data/src/test/entities.js | 66 +++++++++++++++---------- 2 files changed, 50 insertions(+), 46 deletions(-) diff --git a/packages/core-data/src/entities.js b/packages/core-data/src/entities.js index c1783c85850b3..ba3275cecadcf 100644 --- a/packages/core-data/src/entities.js +++ b/packages/core-data/src/entities.js @@ -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' ), @@ -43,7 +39,6 @@ export const defaultEntities = [ key: 'slug', baseURL: '/wp/v2/types', baseURLParams: { context: 'edit' }, - rawAttributes: POST_RAW_ATTRIBUTES, }, { name: 'media', @@ -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 @@ -187,7 +182,6 @@ function* loadPostTypeEntities() { selection: true, }, mergedEdits: { meta: true }, - rawAttributes: POST_RAW_ATTRIBUTES, getTitle: ( record ) => record?.title?.rendered || record?.title || @@ -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 ) => { @@ -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; } @@ -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; -} +}; diff --git a/packages/core-data/src/test/entities.js b/packages/core-data/src/test/entities.js index ee56890246820..7c654b8d1732f 100644 --- a/packages/core-data/src/test/entities.js +++ b/packages/core-data/src/test/entities.js @@ -1,3 +1,9 @@ +/** + * WordPress dependencies + */ +import triggerFetch from '@wordpress/api-fetch'; +jest.mock( '@wordpress/api-fetch' ); + /** * Internal dependencies */ @@ -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', () => { @@ -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' + ); } ); } );