diff --git a/packages/blocks/src/api/test/utils.js b/packages/blocks/src/api/test/utils.js index ea2aa6c2141859..6425e70055d246 100644 --- a/packages/blocks/src/api/test/utils.js +++ b/packages/blocks/src/api/test/utils.js @@ -18,6 +18,7 @@ import { getAccessibleBlockLabel, getBlockLabel, __experimentalSanitizeBlockAttributes, + __experimentalGetBlockAttributesNamesByRole, } from '../utils'; describe( 'block helpers', () => { @@ -310,3 +311,92 @@ describe( 'sanitizeBlockAttributes', () => { } ); } ); } ); + +describe( '__experimentalGetBlockAttributesNamesByRole', () => { + beforeAll( () => { + registerBlockType( 'core/test-block-1', { + attributes: { + align: { + type: 'string', + }, + content: { + type: 'boolean', + role: 'content', + }, + level: { + type: 'number', + role: 'content', + }, + color: { + type: 'string', + role: 'other', + }, + }, + save: noop, + category: 'text', + title: 'test block 1', + } ); + registerBlockType( 'core/test-block-2', { + attributes: { + align: { type: 'string' }, + content: { type: 'boolean' }, + color: { type: 'string' }, + }, + save: noop, + category: 'text', + title: 'test block 2', + } ); + registerBlockType( 'core/test-block-3', { + save: noop, + category: 'text', + title: 'test block 3', + } ); + } ); + afterAll( () => { + [ + 'core/test-block-1', + 'core/test-block-2', + 'core/test-block-3', + ].forEach( unregisterBlockType ); + } ); + it( 'should return empty array if block has no attributes', () => { + expect( + __experimentalGetBlockAttributesNamesByRole( 'core/test-block-3' ) + ).toEqual( [] ); + } ); + it( 'should return all attribute names if no role is provided', () => { + const res = __experimentalGetBlockAttributesNamesByRole( + 'core/test-block-1' + ); + expect( res ).toEqual( + expect.arrayContaining( [ 'align', 'content', 'level', 'color' ] ) + ); + } ); + it( 'should return proper results with existing attributes and provided role', () => { + expect( + __experimentalGetBlockAttributesNamesByRole( + 'core/test-block-1', + 'content' + ) + ).toEqual( expect.arrayContaining( [ 'content', 'level' ] ) ); + expect( + __experimentalGetBlockAttributesNamesByRole( + 'core/test-block-1', + 'other' + ) + ).toEqual( [ 'color' ] ); + expect( + __experimentalGetBlockAttributesNamesByRole( + 'core/test-block-1', + 'not-exists' + ) + ).toEqual( [] ); + // A block with no `role` in any attributes. + expect( + __experimentalGetBlockAttributesNamesByRole( + 'core/test-block-2', + 'content' + ) + ).toEqual( [] ); + } ); +} ); diff --git a/packages/blocks/src/api/utils.js b/packages/blocks/src/api/utils.js index 5048dc9e276e94..f2bd2a76a45830 100644 --- a/packages/blocks/src/api/utils.js +++ b/packages/blocks/src/api/utils.js @@ -277,18 +277,16 @@ export function __experimentalSanitizeBlockAttributes( name, attributes ) { } /** - * I created this wrapper to hide the complexity for the consumer.. + * Filter block attributes by `role` and return their names. * * @param {string} name Block attribute's name. * @param {string} role The role of a block attribute. * * @return {string[]} The attribute names that have the provided role. */ -// TODO jsdoc -// TODO tests export function __experimentalGetBlockAttributesNamesByRole( name, role ) { const attributes = getBlockType( name )?.attributes; - if ( ! attributes ) return; + if ( ! attributes ) return []; const attributesNames = Object.keys( attributes ); if ( ! role ) return attributesNames; return attributesNames.filter(