Skip to content

Commit

Permalink
__experimentalGetBlockAttributesNamesByRole tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ntsekouras committed Apr 2, 2021
1 parent 002bedf commit 74e9cd0
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 4 deletions.
90 changes: 90 additions & 0 deletions packages/blocks/src/api/test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
getAccessibleBlockLabel,
getBlockLabel,
__experimentalSanitizeBlockAttributes,
__experimentalGetBlockAttributesNamesByRole,
} from '../utils';

describe( 'block helpers', () => {
Expand Down Expand Up @@ -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( [] );
} );
} );
6 changes: 2 additions & 4 deletions packages/blocks/src/api/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 74e9cd0

Please sign in to comment.