Files
chrnx-dev-wp/tests/js/editor-project.test.js
T
chrnxandClaude Opus 5 f55d9d9b45 feat: model projects as posts with a picked Selected work
Projects were hardcoded h3 elements in a pattern: no URL, no feed, no
search, uneditable without touching PHP. They follow the podcast model
now, a chrnx_topic term plus posts.

Adds a chrnx_project_featured flag with an editor toggle so the stage
lists chosen projects first and fills with the most recent. Sticky was
already spoken for by the home featured stories, so the choice needed
its own signal. journal-stream gains a pinned topic attribute, reusing
the archive filter, pagination and empty state it already had.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-13 19:41:24 -05:00

103 lines
3.4 KiB
JavaScript

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();
} );
} );