const modulePath = '../../assets/js/editor-project.js'; function flatten( node ) { if ( ! node || typeof node !== 'object' ) { return []; } return [ node ].concat( ( node.children || [] ).flatMap( flatten ) ); } function createWp( topicSlug = 'projects', meta = { unrelated: 'keep' } ) { const editPost = jest.fn(); const registerPlugin = jest.fn(); const state = { meta, topic: [ 12 ] }; const wp = { components: { ToggleControl: 'ToggleControl' }, i18n: { __: jest.fn( ( text ) => text ) }, data: { select: ( store ) => { if ( store === 'core/editor' ) { return { getCurrentPostType: () => 'post', getEditedPostAttribute: ( key ) => state[ key ], }; } return { getEntityRecord: () => ( topicSlug ? { id: 12, slug: topicSlug } : undefined ), }; }, dispatch: () => ( { editPost } ), useDispatch: ( store ) => wp.data.dispatch( store ), useSelect: ( mapper ) => mapper( wp.data.select ), }, editPost: { PluginDocumentSettingPanel: 'PluginDocumentSettingPanel' }, element: { createElement: ( type, props, ...children ) => ( { type, props: props || {}, children } ), }, plugins: { registerPlugin }, }; return { wp, editPost, registerPlugin }; } function renderPanel( context ) { const { initialize } = require( modulePath ); expect( initialize( context.wp ) ).toBe( true ); const [ , plugin ] = context.registerPlugin.mock.calls[ 0 ]; return plugin.render(); } describe( 'project editor panel', () => { beforeEach( () => { jest.resetModules(); } ); test( 'refuses to initialize without the required editor globals', () => { const { initialize } = require( modulePath ); expect( initialize( undefined ) ).toBe( false ); expect( initialize( {} ) ).toBe( false ); } ); test( 'renders the Selected work toggle only for the projects topic', () => { const context = createWp( 'projects' ); const toggles = flatten( renderPanel( context ) ).filter( ( node ) => node.type === 'ToggleControl', ); expect( toggles ).toHaveLength( 1 ); expect( toggles[ 0 ].props.checked ).toBe( false ); } ); test.each( [ 'podcasts', 'code', null ] )( 'renders nothing for the %s topic', ( slug ) => { expect( renderPanel( createWp( slug ) ) ).toBeNull(); } ); test( 'reflects an existing flag and preserves unrelated meta when toggling', () => { const context = createWp( 'projects', { unrelated: 'keep', chrnx_project_featured: true } ); const toggle = flatten( renderPanel( context ) ).find( ( node ) => node.type === 'ToggleControl', ); expect( toggle.props.checked ).toBe( true ); toggle.props.onChange( false ); expect( context.editPost ).toHaveBeenCalledWith( { meta: { unrelated: 'keep', chrnx_project_featured: false }, } ); } ); test( 'coerces the toggle value to a boolean', () => { const context = createWp( 'projects' ); const toggle = flatten( renderPanel( context ) ).find( ( node ) => node.type === 'ToggleControl', ); toggle.props.onChange( 'yes' ); expect( context.editPost ).toHaveBeenCalledWith( { meta: { unrelated: 'keep', chrnx_project_featured: true }, } ); } ); test( 'updateMeta ignores a missing dispatcher', () => { const { updateMeta } = require( modulePath ); expect( () => updateMeta( undefined, {}, 'chrnx_project_featured', true ) ).not.toThrow(); } ); } );