@wordpress/core-data
Version:
Access to and manipulation of core WordPress entities.
153 lines (127 loc) • 3.79 kB
JavaScript
/**
* WordPress dependencies
*/
import apiFetch from '@wordpress/api-fetch';
jest.mock( '@wordpress/api-fetch' );
jest.mock( '../sync', () => ( {
...jest.requireActual( '../sync' ),
getSyncManager: jest.fn(),
} ) );
/**
* Internal dependencies
*/
import {
getMethodName,
rootEntitiesConfig,
prePersistPostType,
additionalEntityConfigLoaders,
} from '../entities';
import { getSyncManager } from '../sync';
import { POST_META_KEY_FOR_CRDT_DOC_PERSISTENCE } from '../utils/crdt';
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', async () => {
let record = {
status: 'auto-draft',
};
const edits = {};
expect(
await prePersistPostType( record, edits, 'post', false )
).toEqual( {
status: 'draft',
title: '',
} );
record = {
status: 'publish',
};
expect(
await prePersistPostType( record, edits, 'post', false )
).toEqual( {} );
record = {
status: 'auto-draft',
title: 'Auto Draft',
};
expect(
await prePersistPostType( record, edits, 'post', false )
).toEqual( {
status: 'draft',
title: '',
} );
record = {
status: 'publish',
title: 'My Title',
};
expect(
await prePersistPostType( record, edits, 'post', false )
).toEqual( {} );
} );
it( 'does not set the status to draft and empty the title when saving templates', async () => {
const record = {
status: 'auto-draft',
title: 'Auto Draft',
};
const edits = {};
expect(
await prePersistPostType( record, edits, 'post', true )
).toEqual( {} );
} );
it( 'adds meta with serialized CRDT doc when createPersistedCRDTDoc returns a value', async () => {
const mockSerializedDoc = 'serialized-crdt-doc-data';
getSyncManager.mockReturnValue( {
createPersistedCRDTDoc: jest
.fn()
.mockReturnValue( mockSerializedDoc ),
} );
const record = { id: 123, status: 'publish' };
const edits = {};
const result = await prePersistPostType( record, edits, 'post', false );
expect( result.meta ).toEqual( {
[ POST_META_KEY_FOR_CRDT_DOC_PERSISTENCE ]: mockSerializedDoc,
} );
expect( getSyncManager ).toHaveBeenCalled();
expect( getSyncManager().createPersistedCRDTDoc ).toHaveBeenCalledWith(
'postType/post',
123
);
getSyncManager.mockReset();
} );
} );
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 );
} );
} );