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 attributes = { type: 'post', topic: [ 3 ], language: [], show: [] }; const wp = { components: { RadioControl: 'RadioControl' }, 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: ( _kind, name ) => terms[ name ] }; }, 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, 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, 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 ); tree.children.forEach( ( panel ) => { expect( panel.type ).toBe( 'PluginDocumentSettingPanel' ); expect( panel.children[ 0 ].type ).toBe( 'RadioControl' ); } ); } ); test( 'does nothing safely outside post editors or without usable globals or terms', () => { const helpers = require( modulePath ); expect( helpers.initialize() ).toBe( false ); expect( helpers.initialize( {} ) ).toBe( false ); const { wp, registerPlugin, attributes, terms } = createWp(); attributes.type = 'page'; expect( helpers.initialize( wp ) ).toBe( false ); expect( registerPlugin ).not.toHaveBeenCalled(); attributes.type = 'post'; terms.chrnx_topic = undefined; expect( () => helpers.initialize( wp ) ).not.toThrow(); } ); } );