Files
chrnx-dev-wp/tests/js/editor-podcast.test.js
T

105 lines
3.7 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' },
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( '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( 'gracefully handles missing globals, post data, meta, and unresolved topic terms', () => {
const helpers = require( modulePath );
expect( helpers.initialize() ).toBe( false );
expect( helpers.initialize( {} ) ).toBe( false );
const unresolved = createWp( null );
expect( () => helpers.initialize( unresolved.wp ) ).not.toThrow();
expect( unresolved.registerPlugin.mock.calls[ 0 ][ 1 ].render() ).toBeNull();
} );
} );