fix: harden CHRNX editor lifecycle

This commit is contained in:
chrnx committed 2026-08-11 15:06:50 -05:00
1 parent a5b94123a8
commit 835fb74ebf
12 files changed
+512 -95

No files matched your search

+31 -3
View File
@@ -18,6 +18,7 @@ function createWp( topicSlug = 'podcasts' ) {
};
const wp = {
components: { TextControl: 'TextControl', TextareaControl: 'TextareaControl' },
i18n: { __: jest.fn( ( text ) => text ) },
data: {
select: ( store ) => {
if ( store === 'core/editor' ) {
@@ -55,6 +56,19 @@ describe( 'podcast editor helpers', () => {
);
} );
test( 'bounds participant transport to 50 names and 100 Unicode characters each', () => {
const helpers = require( modulePath );
const longUnicodeName = 'ñ'.repeat( 101 );
const lines = [ longUnicodeName ].concat(
Array.from( { length: 55 }, ( _value, index ) => `Guest ${ index }` ),
);
const participants = helpers.participantsFromText( lines.join( '\n' ) );
expect( participants ).toHaveLength( 50 );
expect( Array.from( participants[ 0 ] ) ).toHaveLength( 100 );
expect( participants[ 0 ] ).toBe( 'ñ'.repeat( 100 ) );
} );
test( 'updates one meta key while preserving unrelated metadata', () => {
const helpers = require( modulePath );
const editPost = jest.fn();
@@ -93,7 +107,7 @@ describe( 'podcast editor helpers', () => {
expect( nonPodcast.registerPlugin.mock.calls[ 0 ][ 1 ].render() ).toBeNull();
} );
test( 'fails closed when required globals or initial editor selector methods are missing', () => {
test( 'registers immediately while initial editor selectors are unresolved', () => {
const helpers = require( modulePath );
expect( helpers.initialize() ).toBe( false );
expect( helpers.initialize( {} ) ).toBe( false );
@@ -102,8 +116,8 @@ describe( 'podcast editor helpers', () => {
const missingEditorMethod = createWp();
missingEditorMethod.wp.data.select = () => editorSelector;
expect( () => helpers.initialize( missingEditorMethod.wp ) ).not.toThrow();
expect( helpers.initialize( missingEditorMethod.wp ) ).toBe( false );
expect( missingEditorMethod.registerPlugin ).not.toHaveBeenCalled();
expect( helpers.initialize( missingEditorMethod.wp ) ).toBe( true );
expect( missingEditorMethod.registerPlugin ).toHaveBeenCalledTimes( 2 );
expect( missingEditorMethod.editPost ).not.toHaveBeenCalled();
}
@@ -152,4 +166,18 @@ describe( 'podcast editor helpers', () => {
expect( unresolved.registerPlugin.mock.calls[ 0 ][ 1 ].render() ).toBeNull();
expect( unresolved.editPost ).not.toHaveBeenCalled();
} );
test( 'localizes podcast controls 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.props.title ).toBe( 'chrnx:Podcast episode' );
expect( flatten( tree ).find( ( node ) => node.type === 'TextControl' ).props.label ).toBe(
'chrnx:Episode number',
);
expect( wp.i18n.__ ).toHaveBeenCalledWith( 'Podcast episode', 'chrnx' );
} );
} );
+38 -9
View File
@@ -14,9 +14,11 @@ function createWp( overrides = {} ) {
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 ) =>
@@ -31,7 +33,7 @@ function createWp( overrides = {} ) {
getEditedPostAttribute: ( key ) => attributes[ key ],
};
}
return { getEntityRecords: ( _kind, name ) => terms[ name ] };
return { getEntityRecords };
},
dispatch: ( store ) => ( store === 'core/editor' ? { editPost } : {} ),
useDispatch,
@@ -48,7 +50,16 @@ function createWp( overrides = {} ) {
plugins: { registerPlugin },
...overrides,
};
return { wp, editPost, removeEditorPanel, registerPlugin, attributes, terms, useSelect };
return {
wp,
editPost,
getEntityRecords,
removeEditorPanel,
registerPlugin,
attributes,
terms,
useSelect,
};
}
describe( 'editor taxonomy helpers', () => {
@@ -71,7 +82,7 @@ describe( 'editor taxonomy helpers', () => {
test( 'removes native panels and registers three radio setting panels', () => {
const helpers = require( modulePath );
const { wp, removeEditorPanel, registerPlugin, useSelect } = createWp();
const { wp, getEntityRecords, removeEditorPanel, registerPlugin, useSelect } = createWp();
wp.data.dispatch = ( store ) =>
store === 'core/edit-post' ? { removeEditorPanel } : { editPost: jest.fn() };
@@ -81,13 +92,18 @@ describe( 'editor taxonomy helpers', () => {
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( 'fails closed when required globals or initial editor selector methods are missing', () => {
test( 'registers immediately while initial editor selectors are unresolved', () => {
const helpers = require( modulePath );
expect( helpers.initialize() ).toBe( false );
expect( helpers.initialize( {} ) ).toBe( false );
@@ -96,8 +112,8 @@ describe( 'editor taxonomy helpers', () => {
const missingEditorMethod = createWp();
missingEditorMethod.wp.data.select = () => editorSelector;
expect( () => helpers.initialize( missingEditorMethod.wp ) ).not.toThrow();
expect( helpers.initialize( missingEditorMethod.wp ) ).toBe( false );
expect( missingEditorMethod.registerPlugin ).not.toHaveBeenCalled();
expect( helpers.initialize( missingEditorMethod.wp ) ).toBe( true );
expect( missingEditorMethod.registerPlugin ).toHaveBeenCalledTimes( 2 );
expect( missingEditorMethod.editPost ).not.toHaveBeenCalled();
}
@@ -140,11 +156,24 @@ describe( 'editor taxonomy helpers', () => {
expect( editPost ).not.toHaveBeenCalled();
} );
test( 'does not register taxonomy panels outside post editors', () => {
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( false );
expect( registerPlugin ).not.toHaveBeenCalled();
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' );
} );
} );