feat: add CHRNX editorial metadata

This commit is contained in:
chrnx committed 2026-08-11 14:35:51 -05:00
1 parent ab305c98d1
commit e031b753cd
10 files changed
+1127 -1

No files matched your search

+104
View File
@@ -0,0 +1,104 @@
const modulePath = '../../assets/js/editor-podcast.js';
function flatten( node ) {
if ( ! node || typeof node !== 'object' ) {
return [];
}
return [ node ].concat( ( node.children || [] ).flatMap( flatten ) );
}
function createWp( topicSlug = 'podcasts' ) {
const editPost = 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 state = {
meta: { unrelated: 'keep', chrnx_episode_number: 4 },
topic: [ 12 ],
};
const wp = {
components: { TextControl: 'TextControl', TextareaControl: 'TextareaControl' },
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,
useSelect,
},
editPost: { PluginDocumentSettingPanel: 'PluginDocumentSettingPanel' },
element: {
createElement: ( type, props, ...children ) => ( { type, props: props || {}, children } ),
},
plugins: { registerPlugin },
};
return { wp, editPost, registerPlugin, state, useSelect };
}
describe( 'podcast editor helpers', () => {
test( 'converts participant lines to an ordered trimmed transport array', () => {
const helpers = require( modulePath );
expect( helpers.participantsFromText( ' Ada Example\n\n Bob Example \n' ) ).toEqual( [
'Ada Example',
'Bob Example',
] );
expect( helpers.participantsToText( [ 'Ada Example', 'Bob Example' ] ) ).toBe(
'Ada Example\nBob Example',
);
} );
test( 'updates one meta key while preserving unrelated metadata', () => {
const helpers = require( modulePath );
const editPost = jest.fn();
helpers.updateMeta(
editPost,
{ unrelated: 'keep', chrnx_episode_number: 4 },
'chrnx_episode_duration',
'1:18:23',
);
expect( editPost ).toHaveBeenCalledWith( {
meta: { unrelated: 'keep', chrnx_episode_number: 4, chrnx_episode_duration: '1:18:23' },
} );
} );
test( 'renders four controls only for the exact podcasts topic', () => {
const helpers = require( modulePath );
const { wp, registerPlugin, useSelect } = createWp( 'podcasts' );
expect( helpers.initialize( wp ) ).toBe( true );
const tree = registerPlugin.mock.calls[ 0 ][ 1 ].render();
expect( useSelect ).toHaveBeenCalled();
expect( tree.type ).toBe( 'PluginDocumentSettingPanel' );
const controls = flatten( tree ).filter(
( node ) => node.type === 'TextControl' || node.type === 'TextareaControl',
);
expect( controls ).toHaveLength( 4 );
expect( controls.map( ( control ) => control.props.label ) ).toEqual( [
'Episode number',
'Duration',
'Participants (one per line)',
'Canonical episode URL',
] );
expect( JSON.stringify( tree ) ).not.toContain( 'embed' );
const nonPodcast = createWp( 'research' );
helpers.initialize( nonPodcast.wp );
expect( nonPodcast.registerPlugin.mock.calls[ 0 ][ 1 ].render() ).toBeNull();
} );
test( 'gracefully handles missing globals, post data, meta, and unresolved topic terms', () => {
const helpers = require( modulePath );
expect( helpers.initialize() ).toBe( false );
expect( helpers.initialize( {} ) ).toBe( false );
const unresolved = createWp( null );
expect( () => helpers.initialize( unresolved.wp ) ).not.toThrow();
expect( unresolved.registerPlugin.mock.calls[ 0 ][ 1 ].render() ).toBeNull();
} );
} );
+102
View File
@@ -0,0 +1,102 @@
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();
} );
} );