fix: guard CHRNX editorial state

This commit is contained in:
chrnx committed 2026-08-11 14:44:31 -05:00
1 parent e031b753cd
commit a5b94123a8
8 files changed
+285 -19

No files matched your search

+53 -2
View File
@@ -93,12 +93,63 @@ describe( 'podcast editor helpers', () => {
expect( nonPodcast.registerPlugin.mock.calls[ 0 ][ 1 ].render() ).toBeNull();
} );
test( 'gracefully handles missing globals, post data, meta, and unresolved topic terms', () => {
test( 'fails closed when required globals or initial editor selector methods are missing', () => {
const helpers = require( modulePath );
expect( helpers.initialize() ).toBe( false );
expect( helpers.initialize( {} ) ).toBe( false );
for ( const editorSelector of [ {}, { getCurrentPostType: () => 'post' } ] ) {
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( missingEditorMethod.editPost ).not.toHaveBeenCalled();
}
for ( const key of [ 'components', 'data', 'editPost', 'element', 'plugins' ] ) {
const incomplete = createWp();
delete incomplete.wp[ key ];
expect( helpers.initialize( incomplete.wp ) ).toBe( false );
expect( incomplete.registerPlugin ).not.toHaveBeenCalled();
expect( incomplete.editPost ).not.toHaveBeenCalled();
}
} );
test( 'reactive render tolerates missing post methods, meta, and terms without dispatching', () => {
const helpers = require( modulePath );
const incomplete = createWp();
expect( helpers.initialize( incomplete.wp ) ).toBe( true );
incomplete.wp.data.useSelect = ( mapper ) =>
mapper( ( store ) => ( store === 'core/editor' ? {} : { getEntityRecord: jest.fn() } ) );
expect( () => incomplete.registerPlugin.mock.calls[ 0 ][ 1 ].render() ).not.toThrow();
expect( incomplete.registerPlugin.mock.calls[ 0 ][ 1 ].render() ).toBeNull();
expect( incomplete.editPost ).not.toHaveBeenCalled();
incomplete.wp.data.useSelect = ( mapper ) =>
mapper( ( store ) =>
store === 'core/editor'
? {
getCurrentPostType: () => 'post',
getEditedPostAttribute: jest.fn(),
}
: {},
);
expect( () => incomplete.registerPlugin.mock.calls[ 0 ][ 1 ].render() ).not.toThrow();
expect( incomplete.registerPlugin.mock.calls[ 0 ][ 1 ].render() ).toBeNull();
incomplete.wp.data.useSelect = createWp().wp.data.useSelect;
incomplete.wp.data.useDispatch = () => ( {} );
expect( () => incomplete.registerPlugin.mock.calls[ 0 ][ 1 ].render() ).not.toThrow();
expect( incomplete.registerPlugin.mock.calls[ 0 ][ 1 ].render() ).toBeNull();
expect( incomplete.editPost ).not.toHaveBeenCalled();
const unresolved = createWp( null );
expect( () => helpers.initialize( unresolved.wp ) ).not.toThrow();
unresolved.state.meta = undefined;
unresolved.state.topic = undefined;
expect( helpers.initialize( unresolved.wp ) ).toBe( true );
expect( () => unresolved.registerPlugin.mock.calls[ 0 ][ 1 ].render() ).not.toThrow();
expect( unresolved.registerPlugin.mock.calls[ 0 ][ 1 ].render() ).toBeNull();
expect( unresolved.editPost ).not.toHaveBeenCalled();
} );
} );
+53 -5
View File
@@ -87,16 +87,64 @@ describe( 'editor taxonomy helpers', () => {
} );
} );
test( 'does nothing safely outside post editors or without usable globals or terms', () => {
test( 'fails closed when required globals or initial editor selector methods are missing', () => {
const helpers = require( modulePath );
expect( helpers.initialize() ).toBe( false );
expect( helpers.initialize( {} ) ).toBe( false );
const { wp, registerPlugin, attributes, terms } = createWp();
for ( const editorSelector of [ {}, { getCurrentPostType: () => 'post' } ] ) {
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( missingEditorMethod.editPost ).not.toHaveBeenCalled();
}
for ( const key of [ 'components', 'data', 'editPost', 'element', 'plugins' ] ) {
const incomplete = createWp();
delete incomplete.wp[ key ];
expect( helpers.initialize( incomplete.wp ) ).toBe( false );
expect( incomplete.registerPlugin ).not.toHaveBeenCalled();
expect( incomplete.editPost ).not.toHaveBeenCalled();
}
} );
test( 'reactive render tolerates unresolved editor methods and terms without dispatching', () => {
const helpers = require( modulePath );
const { wp, registerPlugin, editPost } = createWp();
expect( helpers.initialize( wp ) ).toBe( true );
wp.data.useSelect = ( mapper ) =>
mapper( ( store ) => ( store === 'core/editor' ? {} : { getEntityRecords: jest.fn() } ) );
expect( () => registerPlugin.mock.calls[ 0 ][ 1 ].render() ).not.toThrow();
expect( registerPlugin.mock.calls[ 0 ][ 1 ].render() ).toBeNull();
expect( editPost ).not.toHaveBeenCalled();
wp.data.useSelect = ( mapper ) =>
mapper( ( store ) =>
store === 'core/editor'
? {
getCurrentPostType: () => 'post',
getEditedPostAttribute: jest.fn(),
}
: {},
);
expect( () => registerPlugin.mock.calls[ 0 ][ 1 ].render() ).not.toThrow();
expect( registerPlugin.mock.calls[ 0 ][ 1 ].render() ).toBeNull();
wp.data.useSelect = createWp().wp.data.useSelect;
wp.data.useDispatch = () => ( {} );
expect( () => registerPlugin.mock.calls[ 0 ][ 1 ].render() ).not.toThrow();
expect( registerPlugin.mock.calls[ 0 ][ 1 ].render() ).toBeNull();
expect( editPost ).not.toHaveBeenCalled();
} );
test( 'does not register 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();
attributes.type = 'post';
terms.chrnx_topic = undefined;
expect( () => helpers.initialize( wp ) ).not.toThrow();
} );
} );