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