184 lines
7.3 KiB
JavaScript
184 lines
7.3 KiB
JavaScript
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' },
|
|
i18n: { __: jest.fn( ( text ) => text ) },
|
|
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( '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();
|
|
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( 'registers immediately while initial editor selectors are unresolved', () => {
|
|
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( true );
|
|
expect( missingEditorMethod.registerPlugin ).toHaveBeenCalledTimes( 2 );
|
|
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 );
|
|
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();
|
|
} );
|
|
|
|
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' );
|
|
} );
|
|
} );
|