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

178 lines
6.5 KiB
JavaScript

const fs = require( 'node:fs' );
const path = require( 'node:path' );
const modulePath = '../../assets/js/editor-related-posts.js';
function flatten( node ) {
if ( ! node || typeof node !== 'object' ) {
return [];
}
return [ node ].concat( ( node.children || [] ).flatMap( flatten ) );
}
function createWp() {
const registerBlockType = jest.fn();
const wp = {
blocks: { registerBlockType },
blockEditor: { InspectorControls: 'InspectorControls' },
components: { PanelBody: 'PanelBody', SelectControl: 'SelectControl' },
element: {
createElement: ( type, props, ...children ) => ( {
type,
props: props || {},
children,
} ),
Fragment: 'Fragment',
},
i18n: { __: jest.fn( ( text ) => text ) },
serverSideRender: 'ServerSideRender',
};
return { wp, registerBlockType };
}
describe( 'Related Stories editor block', () => {
test( 'registers the dynamic block once with exact metadata attributes and null save output', () => {
const helpers = require( modulePath );
const { wp, registerBlockType } = createWp();
const metadata = JSON.parse(
fs.readFileSync( path.join( process.cwd(), 'blocks/related-posts/block.json' ), 'utf8' ),
);
expect( helpers.initialize( wp ) ).toBe( true );
expect( registerBlockType ).toHaveBeenCalledTimes( 1 );
expect( registerBlockType.mock.calls[ 0 ][ 0 ] ).toBe( 'chrnx/related-posts' );
expect( registerBlockType.mock.calls[ 0 ][ 1 ].attributes ).toEqual( metadata.attributes );
expect( registerBlockType.mock.calls[ 0 ][ 1 ].save() ).toBeNull();
} );
test( 'renders inspector controls beside one server preview', () => {
const helpers = require( modulePath );
const { wp, registerBlockType } = createWp();
helpers.initialize( wp );
const settings = registerBlockType.mock.calls[ 0 ][ 1 ];
const tree = settings.edit( {
attributes: { limit: 3, layout: 'adaptive', headingLevel: 2 },
setAttributes: jest.fn(),
} );
const nodes = flatten( tree );
expect( nodes.filter( ( node ) => node.type === 'ServerSideRender' ) ).toHaveLength( 1 );
expect( nodes.filter( ( node ) => node.type === 'InspectorControls' ) ).toHaveLength( 1 );
expect( nodes.filter( ( node ) => node.type === 'SelectControl' ) ).toHaveLength( 2 );
expect( nodes.find( ( node ) => node.type === 'ServerSideRender' ).props ).toEqual( {
block: 'chrnx/related-posts',
attributes: { limit: 3, layout: 'adaptive', headingLevel: 2 },
} );
} );
test( 'coerces limit selections to the approved numeric values', () => {
const helpers = require( modulePath );
const { wp, registerBlockType } = createWp();
helpers.initialize( wp );
const setAttributes = jest.fn();
const tree = registerBlockType.mock.calls[ 0 ][ 1 ].edit( {
attributes: { limit: 3, layout: 'adaptive' },
setAttributes,
} );
const limit = flatten( tree ).find(
( node ) => node.type === 'SelectControl' && node.props.label === 'Stories to show',
);
limit.props.onChange( '5' );
expect( setAttributes ).toHaveBeenLastCalledWith( { limit: 5 } );
limit.props.onChange( 5 );
expect( setAttributes ).toHaveBeenLastCalledWith( { limit: 5 } );
for ( const malformed of [ '3junk', '12', '', null, {}, 5.5 ] ) {
limit.props.onChange( malformed );
expect( setAttributes ).toHaveBeenLastCalledWith( { limit: 3 } );
}
} );
test( 'persists only allowlisted layouts and defaults malformed values to adaptive', () => {
const helpers = require( modulePath );
const { wp, registerBlockType } = createWp();
helpers.initialize( wp );
const setAttributes = jest.fn();
const tree = registerBlockType.mock.calls[ 0 ][ 1 ].edit( {
attributes: { limit: 3, layout: 'adaptive' },
setAttributes,
} );
const layout = flatten( tree ).find(
( node ) => node.type === 'SelectControl' && node.props.label === 'Layout',
);
for ( const approved of [ 'adaptive', 'grid', 'carousel' ] ) {
layout.props.onChange( approved );
expect( setAttributes ).toHaveBeenLastCalledWith( {
layout: approved,
} );
}
for ( const malformed of [ 'masonry', 'carousel evil', '', null, 1, [] ] ) {
layout.props.onChange( malformed );
expect( setAttributes ).toHaveBeenLastCalledWith( {
layout: 'adaptive',
} );
}
} );
test( 'fails closed when WordPress globals or any required dependency are missing', () => {
const helpers = require( modulePath );
expect( () => helpers.initialize() ).not.toThrow();
expect( helpers.initialize() ).toBe( false );
expect( helpers.initialize( {} ) ).toBe( false );
for ( const key of [
'blocks',
'blockEditor',
'components',
'element',
'i18n',
'serverSideRender',
] ) {
const { wp, registerBlockType } = createWp();
delete wp[ key ];
expect( () => helpers.initialize( wp ) ).not.toThrow();
expect( helpers.initialize( wp ) ).toBe( false );
expect( registerBlockType ).not.toHaveBeenCalled();
}
} );
test( 'passes every editor UI string through i18n with the chrnx domain', () => {
const helpers = require( modulePath );
const { wp, registerBlockType } = createWp();
wp.i18n.__ = jest.fn( ( text, domain ) => `${ domain }:${ text }` );
helpers.initialize( wp );
const tree = registerBlockType.mock.calls[ 0 ][ 1 ].edit( {
attributes: { limit: 3, layout: 'adaptive' },
setAttributes: jest.fn(),
} );
const nodes = flatten( tree );
const controls = nodes.filter( ( node ) => node.type === 'SelectControl' );
expect( nodes.find( ( node ) => node.type === 'PanelBody' ).props.title ).toBe(
'chrnx:Related stories settings',
);
expect( controls.map( ( control ) => control.props.label ) ).toEqual( [
'chrnx:Stories to show',
'chrnx:Layout',
] );
expect( controls[ 1 ].props.help ).toBe(
'chrnx:Adaptive uses a grid on larger screens and horizontal scrolling on smaller screens.',
);
const translated = wp.i18n.__.mock.calls.map( ( call ) => call[ 0 ] );
expect( translated ).toEqual(
expect.arrayContaining( [
'Related stories settings',
'Stories to show',
'Layout',
'Adaptive',
'Grid',
'Carousel',
'Adaptive uses a grid on larger screens and horizontal scrolling on smaller screens.',
] ),
);
expect( wp.i18n.__.mock.calls.every( ( call ) => call[ 1 ] === 'chrnx' ) ).toBe( true );
} );
} );