@wordpress/core-data
Version:
Access to and manipulation of core WordPress entities.
103 lines (83 loc) • 2.45 kB
JavaScript
/**
* WordPress dependencies
*/
import apiFetch from '@wordpress/api-fetch';
jest.mock( '@wordpress/api-fetch' );
/**
* Internal dependencies
*/
import {
getMethodName,
rootEntitiesConfig,
prePersistPostType,
additionalEntityConfigLoaders,
} from '../entities';
describe( 'getMethodName', () => {
it( 'should return the right method name for an entity with the root kind', () => {
const methodName = getMethodName( 'root', 'postType' );
expect( methodName ).toEqual( 'getPostType' );
} );
it( 'should use a different suffix', () => {
const methodName = getMethodName( 'root', 'postType', 'set' );
expect( methodName ).toEqual( 'setPostType' );
} );
it( 'should use the given plural form', () => {
const methodName = getMethodName( 'root', 'taxonomies', 'get' );
expect( methodName ).toEqual( 'getTaxonomies' );
} );
it( 'should include the kind in the method name', () => {
const id = rootEntitiesConfig.length;
rootEntitiesConfig[ id ] = { name: 'book', kind: 'postType' };
const methodName = getMethodName( 'postType', 'book' );
delete rootEntitiesConfig[ id ];
expect( methodName ).toEqual( 'getPostTypeBook' );
} );
} );
describe( 'prePersistPostType', () => {
it( 'set the status to draft and empty the title when saving auto-draft posts', () => {
let record = {
status: 'auto-draft',
};
const edits = {};
expect( prePersistPostType( record, edits ) ).toEqual( {
status: 'draft',
title: '',
} );
record = {
status: 'publish',
};
expect( prePersistPostType( record, edits ) ).toEqual( {} );
record = {
status: 'auto-draft',
title: 'Auto Draft',
};
expect( prePersistPostType( record, edits ) ).toEqual( {
status: 'draft',
title: '',
} );
record = {
status: 'publish',
title: 'My Title',
};
expect( prePersistPostType( record, edits ) ).toEqual( {} );
} );
} );
describe( 'loadTaxonomyEntities', () => {
beforeEach( () => {
apiFetch.mockReset();
} );
it( 'should add supportsPagination: true to taxonomy entities', async () => {
const mockTaxonomies = {
category: {
name: 'Categories',
rest_base: 'categories',
},
};
apiFetch.mockResolvedValueOnce( mockTaxonomies );
const taxonomyLoader = additionalEntityConfigLoaders.find(
( loader ) => loader.kind === 'taxonomy'
);
const entities = await taxonomyLoader.loadEntities();
expect( entities[ 0 ].supportsPagination ).toBe( true );
} );
} );