180 lines
6.9 KiB
JavaScript
180 lines
6.9 KiB
JavaScript
const modulePath = '../../assets/js/editor-taxonomies.js';
|
|
|
|
function createWp( overrides = {} ) {
|
|
const editPost = jest.fn();
|
|
const removeEditorPanel = jest.fn();
|
|
const registerPlugin = jest.fn();
|
|
const useSelect = jest.fn( ( mapper ) => mapper( wp.data.select ) );
|
|
const useDispatch = jest.fn( ( store ) => wp.data.dispatch( store ) );
|
|
const terms = {
|
|
chrnx_topic: [
|
|
{ id: 9, name: 'Zulu' },
|
|
{ id: 3, name: 'Alpha' },
|
|
],
|
|
chrnx_language: [ { id: 4, name: 'English' } ],
|
|
chrnx_show: [ { id: 7, name: 'Silicon Ranch' } ],
|
|
};
|
|
const getEntityRecords = jest.fn( ( _kind, name ) => terms[ name ] );
|
|
const attributes = { type: 'post', topic: [ 3 ], language: [], show: [] };
|
|
const wp = {
|
|
components: { RadioControl: 'RadioControl' },
|
|
i18n: { __: jest.fn( ( text ) => text ) },
|
|
compose: {
|
|
compose:
|
|
( ...funcs ) =>
|
|
( component ) =>
|
|
funcs.reduceRight( ( value, func ) => func( value ), component ),
|
|
},
|
|
data: {
|
|
select: ( store ) => {
|
|
if ( store === 'core/editor' ) {
|
|
return {
|
|
getCurrentPostType: () => attributes.type,
|
|
getEditedPostAttribute: ( key ) => attributes[ key ],
|
|
};
|
|
}
|
|
return { getEntityRecords };
|
|
},
|
|
dispatch: ( store ) => ( store === 'core/editor' ? { editPost } : {} ),
|
|
useDispatch,
|
|
useSelect,
|
|
withDispatch: ( mapper ) => ( Component ) => ( props ) =>
|
|
Component( { ...props, ...mapper( () => ( { editPost } ), props ) } ),
|
|
withSelect: ( mapper ) => ( Component ) => ( props ) =>
|
|
Component( { ...props, ...mapper( wp.data.select, props ) } ),
|
|
},
|
|
editPost: { PluginDocumentSettingPanel: 'PluginDocumentSettingPanel' },
|
|
element: {
|
|
createElement: ( type, props, ...children ) => ( { type, props: props || {}, children } ),
|
|
},
|
|
plugins: { registerPlugin },
|
|
...overrides,
|
|
};
|
|
return {
|
|
wp,
|
|
editPost,
|
|
getEntityRecords,
|
|
removeEditorPanel,
|
|
registerPlugin,
|
|
attributes,
|
|
terms,
|
|
useSelect,
|
|
};
|
|
}
|
|
|
|
describe( 'editor taxonomy helpers', () => {
|
|
test( 'orders terms stably and writes one selected value or Not set', () => {
|
|
const helpers = require( modulePath );
|
|
const editPost = jest.fn();
|
|
expect(
|
|
helpers
|
|
.orderTerms( [
|
|
{ id: 9, name: 'Zulu' },
|
|
{ id: 3, name: 'Alpha' },
|
|
] )
|
|
.map( ( term ) => term.id ),
|
|
).toEqual( [ 3, 9 ] );
|
|
helpers.writeSelection( editPost, 'topic', '9' );
|
|
expect( editPost ).toHaveBeenLastCalledWith( { topic: [ 9 ] } );
|
|
helpers.writeSelection( editPost, 'topic', '' );
|
|
expect( editPost ).toHaveBeenLastCalledWith( { topic: [] } );
|
|
} );
|
|
|
|
test( 'removes native panels and registers three radio setting panels', () => {
|
|
const helpers = require( modulePath );
|
|
const { wp, getEntityRecords, removeEditorPanel, registerPlugin, useSelect } = createWp();
|
|
wp.data.dispatch = ( store ) =>
|
|
store === 'core/edit-post' ? { removeEditorPanel } : { editPost: jest.fn() };
|
|
|
|
expect( helpers.initialize( wp ) ).toBe( true );
|
|
expect( removeEditorPanel ).toHaveBeenCalledTimes( 3 );
|
|
expect( registerPlugin ).toHaveBeenCalledTimes( 1 );
|
|
const tree = registerPlugin.mock.calls[ 0 ][ 1 ].render();
|
|
expect( useSelect ).toHaveBeenCalled();
|
|
expect( tree.children ).toHaveLength( 3 );
|
|
expect( getEntityRecords ).toHaveBeenCalledWith( 'taxonomy', 'chrnx_topic', {
|
|
per_page: 100,
|
|
orderby: 'name',
|
|
order: 'asc',
|
|
} );
|
|
tree.children.forEach( ( panel ) => {
|
|
expect( panel.type ).toBe( 'PluginDocumentSettingPanel' );
|
|
expect( panel.children[ 0 ].type ).toBe( 'RadioControl' );
|
|
} );
|
|
} );
|
|
|
|
test( 'registers immediately while initial editor selectors are unresolved', () => {
|
|
const helpers = require( modulePath );
|
|
expect( helpers.initialize() ).toBe( false );
|
|
expect( helpers.initialize( {} ) ).toBe( false );
|
|
|
|
for ( const editorSelector of [ {}, { getCurrentPostType: () => 'post' } ] ) {
|
|
const missingEditorMethod = createWp();
|
|
missingEditorMethod.wp.data.select = () => editorSelector;
|
|
expect( () => helpers.initialize( missingEditorMethod.wp ) ).not.toThrow();
|
|
expect( helpers.initialize( missingEditorMethod.wp ) ).toBe( true );
|
|
expect( missingEditorMethod.registerPlugin ).toHaveBeenCalledTimes( 2 );
|
|
expect( missingEditorMethod.editPost ).not.toHaveBeenCalled();
|
|
}
|
|
|
|
for ( const key of [ 'components', 'data', 'editPost', 'element', 'plugins' ] ) {
|
|
const incomplete = createWp();
|
|
delete incomplete.wp[ key ];
|
|
expect( helpers.initialize( incomplete.wp ) ).toBe( false );
|
|
expect( incomplete.registerPlugin ).not.toHaveBeenCalled();
|
|
expect( incomplete.editPost ).not.toHaveBeenCalled();
|
|
}
|
|
} );
|
|
|
|
test( 'reactive render tolerates unresolved editor methods and terms without dispatching', () => {
|
|
const helpers = require( modulePath );
|
|
const { wp, registerPlugin, editPost } = createWp();
|
|
expect( helpers.initialize( wp ) ).toBe( true );
|
|
|
|
wp.data.useSelect = ( mapper ) =>
|
|
mapper( ( store ) => ( store === 'core/editor' ? {} : { getEntityRecords: jest.fn() } ) );
|
|
expect( () => registerPlugin.mock.calls[ 0 ][ 1 ].render() ).not.toThrow();
|
|
expect( registerPlugin.mock.calls[ 0 ][ 1 ].render() ).toBeNull();
|
|
expect( editPost ).not.toHaveBeenCalled();
|
|
|
|
wp.data.useSelect = ( mapper ) =>
|
|
mapper( ( store ) =>
|
|
store === 'core/editor'
|
|
? {
|
|
getCurrentPostType: () => 'post',
|
|
getEditedPostAttribute: jest.fn(),
|
|
}
|
|
: {},
|
|
);
|
|
expect( () => registerPlugin.mock.calls[ 0 ][ 1 ].render() ).not.toThrow();
|
|
expect( registerPlugin.mock.calls[ 0 ][ 1 ].render() ).toBeNull();
|
|
|
|
wp.data.useSelect = createWp().wp.data.useSelect;
|
|
wp.data.useDispatch = () => ( {} );
|
|
expect( () => registerPlugin.mock.calls[ 0 ][ 1 ].render() ).not.toThrow();
|
|
expect( registerPlugin.mock.calls[ 0 ][ 1 ].render() ).toBeNull();
|
|
expect( editPost ).not.toHaveBeenCalled();
|
|
} );
|
|
|
|
test( 'registers safely but renders no taxonomy panels outside post editors', () => {
|
|
const helpers = require( modulePath );
|
|
const { wp, registerPlugin, attributes } = createWp();
|
|
attributes.type = 'page';
|
|
expect( helpers.initialize( wp ) ).toBe( true );
|
|
expect( registerPlugin ).toHaveBeenCalledTimes( 1 );
|
|
expect( registerPlugin.mock.calls[ 0 ][ 1 ].render() ).toBeNull();
|
|
} );
|
|
|
|
test( 'localizes taxonomy editor labels with the CHRNX text domain', () => {
|
|
const helpers = require( modulePath );
|
|
const { wp, registerPlugin } = createWp();
|
|
wp.i18n.__ = jest.fn( ( text, domain ) => `${ domain }:${ text }` );
|
|
|
|
helpers.initialize( wp );
|
|
const tree = registerPlugin.mock.calls[ 0 ][ 1 ].render();
|
|
expect( tree.children[ 0 ].props.title ).toBe( 'chrnx:Topic' );
|
|
expect( tree.children[ 0 ].children[ 0 ].props.options[ 0 ].label ).toBe( 'chrnx:Not set' );
|
|
expect( wp.i18n.__ ).toHaveBeenCalledWith( 'Topic', 'chrnx' );
|
|
} );
|
|
} );
|