fix: harden CHRNX editor lifecycle
This commit is contained in:
12 files changed
+512
-95
No files matched your search
@@ -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' );
|
||||
} );
|
||||
} );
|
||||
Reference in new issue
Block a user