Skip to content

Commit

Permalink
Add end 2 end test dynamic allowed blocks (#14992)
Browse files Browse the repository at this point in the history
## Description
This PR adds a simple end 2 end test that makes sure allowed blocks inside a parent block can be changed dynamically depending on some condition e.g: the number of child's the parent contains.

## How has this been tested?
We just need to make sure the test cases pass.

Related: #14515
  • Loading branch information
jorgefilipecosta authored Apr 16, 2019
1 parent 626778e commit b080acc
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
29 changes: 29 additions & 0 deletions packages/e2e-tests/plugins/inner-blocks-allowed-blocks/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
( function() {
const { withSelect } = wp.data;
const { registerBlockType } = wp.blocks;
const { createElement: el } = wp.element;
const { InnerBlocks } = wp.editor;
Expand All @@ -9,6 +10,8 @@
[ 'core/paragraph', { placeholder: __( 'Add a description' ) } ],
[ 'core/quote' ]
];
const allowedBlocksWhenSingleEmptyChild = [ 'core/image', 'core/list' ];
const allowedBlocksWhenMultipleChildren = [ 'core/gallery', 'core/video' ];

const save = function() {
return el( 'div', divProps,
Expand Down Expand Up @@ -55,4 +58,30 @@
save,
} );

registerBlockType( 'test/allowed-blocks-dynamic', {
title: 'Allowed Blocks Dynamic',
icon: 'carrot',
category: 'common',

edit: withSelect( function( select, ownProps ) {
var getBlockOrder = select( 'core/editor' ).getBlockOrder;
return {
numberOfChildren: getBlockOrder( ownProps.clientId ).length,
};
} )( function( props ) {
return el( 'div', divProps,
el(
InnerBlocks,
{
allowedBlocks: props.numberOfChildren < 2 ?
allowedBlocksWhenSingleEmptyChild :
allowedBlocksWhenMultipleChildren,
}
)
);
} ),

save,
} );

} )();
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,30 @@ describe( 'Allowed Blocks Setting on InnerBlocks ', () => {
'Quote',
] );
} );

it( 'correctly applies dynamic allowed blocks restrictions', async () => {
await insertBlock( 'Allowed Blocks Dynamic' );
const parentBlockSelector = '[data-type="test/allowed-blocks-dynamic"]';
const blockAppender = '.block-list-appender button';
const appenderSelector = `${ parentBlockSelector } ${ blockAppender }`;
await page.waitForSelector( appenderSelector );
await page.click( appenderSelector );
await openAllBlockInserterCategories();
expect(
await getAllBlockInserterItemTitles()
).toEqual( [
'Image',
'List',
] );
await page.click( `.block-editor-block-types-list__item[aria-label="List"]` );
await insertBlock( 'Image' );
await page.click( appenderSelector );
await openAllBlockInserterCategories();
expect(
await getAllBlockInserterItemTitles()
).toEqual( [
'Gallery',
'Video',
] );
} );
} );

0 comments on commit b080acc

Please sign in to comment.