Skip to content

Commit

Permalink
Clean up getOrLoadEntitiesConfig leftovers
Browse files Browse the repository at this point in the history
  • Loading branch information
Mamaduka committed Oct 30, 2024
1 parent 4bd2045 commit 384aac1
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 144 deletions.
64 changes: 0 additions & 64 deletions packages/core-data/src/entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,7 @@ import apiFetch from '@wordpress/api-fetch';
import { __ } from '@wordpress/i18n';
import { RichTextData } from '@wordpress/rich-text';

/**
* Internal dependencies
*/
import { addEntities } from './actions';
import { getSyncProvider } from './sync';

export const DEFAULT_ENTITY_KEY = 'id';

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

export const rootEntitiesConfig = [
Expand Down Expand Up @@ -458,60 +451,3 @@ export const getMethodName = ( kind, name, prefix = 'get' ) => {
const suffix = pascalCase( name );
return `${ prefix }${ kindPrefix }${ suffix }`;
};

function registerSyncConfigs( configs ) {
configs.forEach( ( { syncObjectType, syncConfig } ) => {
getSyncProvider().register( syncObjectType, syncConfig );
const editSyncConfig = { ...syncConfig };
delete editSyncConfig.fetch;
getSyncProvider().register( syncObjectType + '--edit', editSyncConfig );
} );
}

/**
* Loads the entities into the store.
*
* Note: The `name` argument is used for `root` entities requiring additional server data.
*
* @param {string} kind Kind
* @param {string} name Name
* @return {(thunkArgs: object) => Promise<Array>} Entities
*/
export const getOrLoadEntitiesConfig =
( kind, name ) =>
async ( { select, dispatch } ) => {
let configs = select.getEntitiesConfig( kind );
const hasConfig = !! select.getEntityConfig( kind, name );

if ( configs?.length > 0 && hasConfig ) {
if ( window.__experimentalEnableSync ) {
if ( globalThis.IS_GUTENBERG_PLUGIN ) {
registerSyncConfigs( configs );
}
}

return configs;
}

const loader = additionalEntityConfigLoaders.find( ( l ) => {
if ( ! name || ! l.name ) {
return l.kind === kind;
}

return l.kind === kind && l.name === name;
} );
if ( ! loader ) {
return [];
}

configs = await loader.loadEntities();
if ( window.__experimentalEnableSync ) {
if ( globalThis.IS_GUTENBERG_PLUGIN ) {
registerSyncConfigs( configs );
}
}

dispatch( addEntities( configs ) );

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

/**
* Internal dependencies
*/
import {
getMethodName,
rootEntitiesConfig,
getOrLoadEntitiesConfig,
prePersistPostType,
} from '../entities';

Expand Down Expand Up @@ -43,79 +36,6 @@ describe( 'getMethodName', () => {
} );
} );

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

it( 'shouldn’t do anything if the entities have already been resolved', async () => {
const dispatch = jest.fn();
const select = {
getEntitiesConfig: jest.fn( () => entities ),
getEntityConfig: jest.fn( () => ( {
kind: 'postType',
name: 'post',
} ) ),
};
const entities = [ { kind: 'postType' } ];
await getOrLoadEntitiesConfig(
'postType',
'post'
)( { dispatch, select } );
expect( dispatch ).not.toHaveBeenCalled();
} );

it( 'shouldn’t do anything if there no defined kind config', async () => {
const dispatch = jest.fn();
const select = {
getEntitiesConfig: jest.fn( () => [] ),
getEntityConfig: jest.fn( () => undefined ),
};
await getOrLoadEntitiesConfig(
'unknownKind',
undefined
)( { dispatch, select } );
expect( dispatch ).not.toHaveBeenCalled();
} );

it( 'should fetch and add the entities', async () => {
const fetchedEntities = [
{
rest_base: 'posts',
labels: {
singular_name: 'post',
},
supports: {
revisions: true,
},
},
];
const dispatch = jest.fn();
const select = {
getEntitiesConfig: jest.fn( () => [] ),
getEntityConfig: jest.fn( () => undefined ),
};
triggerFetch.mockImplementation( () => fetchedEntities );

await getOrLoadEntitiesConfig(
'postType',
'post'
)( { 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'
);
expect(
dispatch.mock.calls[ 0 ][ 0 ].entities[ 0 ].getRevisionsUrl( 1 )
).toBe( '/wp/v2/posts/1/revisions' );
expect(
dispatch.mock.calls[ 0 ][ 0 ].entities[ 0 ].getRevisionsUrl( 1, 2 )
).toBe( '/wp/v2/posts/1/revisions/2' );
} );
} );

describe( 'prePersistPostType', () => {
it( 'set the status to draft and empty the title when saving auto-draft posts', () => {
let record = {
Expand Down

0 comments on commit 384aac1

Please sign in to comment.