Skip to content

Commit

Permalink
E2E Tests: Add Child Blocks tests and test plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
noisysocks committed Jun 18, 2020
1 parent 436d7c0 commit 3bb29ad
Show file tree
Hide file tree
Showing 3 changed files with 172 additions and 0 deletions.
28 changes: 28 additions & 0 deletions packages/e2e-tests/plugins/child-blocks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/**
* Plugin Name: Gutenberg Test Child Blocks
* Plugin URI: https://github.com/WordPress/gutenberg
* Author: Gutenberg Team
*
* @package gutenberg-test-child-blocks
*/

/**
* Registers a custom script for the plugin.
*/
function enqueue_child_blocks_script() {
wp_enqueue_script(
'gutenberg-test-child-blocks',
plugins_url( 'child-blocks/index.js', __FILE__ ),
array(
'wp-blocks',
'wp-block-editor',
'wp-element',
'wp-i18n',
),
filemtime( plugin_dir_path( __FILE__ ) . 'child-blocks/index.js' ),
true
);
}

add_action( 'init', 'enqueue_child_blocks_script' );
79 changes: 79 additions & 0 deletions packages/e2e-tests/plugins/child-blocks/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
( function() {
const { InnerBlocks } = wp.blockEditor;
const { createElement: el } = wp.element;
const { registerBlockType } = wp.blocks;

registerBlockType( 'test/child-blocks-unrestricted-parent', {
title: 'Child Blocks Unrestricted Parent',
icon: 'carrot',
category: 'text',

edit() {
return el(
'div',
{},
el( InnerBlocks )
);
},

save() {
return el(
'div',
{},
el( InnerBlocks.Content )
);
},
} );

registerBlockType( 'test/child-blocks-restricted-parent', {
title: 'Child Blocks Restricted Parent',
icon: 'carrot',
category: 'text',

edit() {
return el(
'div',
{},
el(
InnerBlocks,
{ allowedBlocks: [ 'core/paragraph', 'core/image' ] }
)
);
},

save() {
return el(
'div',
{},
el( InnerBlocks.Content )
);
},
} );

registerBlockType( 'test/child-blocks-child', {
title: 'Child Blocks Child',
icon: 'carrot',
category: 'text',

parent: [
'test/child-blocks-unrestricted-parent',
'test/child-blocks-restricted-parent',
],

edit() {
return el(
'div',
{},
'Child'
);
},

save() {
return el(
'div',
{},
'Child'
);
},
} );
} )();
65 changes: 65 additions & 0 deletions packages/e2e-tests/specs/editor/plugins/child-blocks.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* WordPress dependencies
*/
import {
activatePlugin,
closeGlobalBlockInserter,
createNewPost,
deactivatePlugin,
getAllBlockInserterItemTitles,
insertBlock,
openGlobalBlockInserter,
} from '@wordpress/e2e-test-utils';

describe( 'Child Blocks', () => {
beforeAll( async () => {
await activatePlugin( 'gutenberg-test-child-blocks' );
} );

beforeEach( async () => {
await createNewPost();
} );

afterAll( async () => {
await deactivatePlugin( 'gutenberg-test-child-blocks' );
} );

it( 'are hidden from the global block inserter', async () => {
await openGlobalBlockInserter();
await expect( await getAllBlockInserterItemTitles() ).not.toContain(
'Child Blocks Child'
);
} );

it( 'shows up in a parent block', async () => {
await insertBlock( 'Child Blocks Unrestricted Parent' );
await closeGlobalBlockInserter();
await page.waitForSelector(
'[data-type="test/child-blocks-unrestricted-parent"] .block-editor-default-block-appender'
);
await page.click(
'[data-type="test/child-blocks-unrestricted-parent"] .block-editor-default-block-appender'
);
await openGlobalBlockInserter();
const inserterItemTitles = await getAllBlockInserterItemTitles();
expect( inserterItemTitles ).toContain( 'Child Blocks Child' );
expect( inserterItemTitles.length ).toBeGreaterThan( 20 );
} );

it( 'display in a parent block with allowedItems', async () => {
await insertBlock( 'Child Blocks Restricted Parent' );
await closeGlobalBlockInserter();
await page.waitForSelector(
'[data-type="test/child-blocks-restricted-parent"] .block-editor-default-block-appender'
);
await page.click(
'[data-type="test/child-blocks-restricted-parent"] .block-editor-default-block-appender'
);
await openGlobalBlockInserter();
expect( await getAllBlockInserterItemTitles() ).toEqual( [
'Child Blocks Child',
'Image',
'Paragraph',
] );
} );
} );

0 comments on commit 3bb29ad

Please sign in to comment.