Skip to content

Commit

Permalink
Test: Add e2e test that ensures that filters can be added after block…
Browse files Browse the repository at this point in the history
… registration
  • Loading branch information
gziolo committed Sep 17, 2021
1 parent e10d0fc commit c954944
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 0 deletions.
26 changes: 26 additions & 0 deletions packages/e2e-tests/plugins/block-api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
/**
* Plugin Name: Gutenberg Test Block API
* Plugin URI: https://github.com/WordPress/gutenberg
* Author: Gutenberg Team
*
* @package gutenberg-test-block-api
*/

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

add_action( 'init', 'enqueue_block_api_plugin_script' );
31 changes: 31 additions & 0 deletions packages/e2e-tests/plugins/block-api/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
( function () {
const { registerBlockType } = wp.blocks;
const { addFilter } = wp.hooks;

registerBlockType( 'e2e-tests/hello-world', {
title: 'Hello World',
description: 'Hello World test block.',
category: 'widgets',
edit() {
return 'Hello Editor!';
},
save() {
return 'Hello Frontend!';
},
} );

addFilter(
'blocks.registerBlockType',
'e2e-tests/hello-world/filter-added-after-registration',
( blockType, name ) => {
if ( name === 'e2e-tests/hello-world' ) {
return {
...blockType,
title: 'Filtered Hello World',
};
}

return blockType;
}
);
} )();
33 changes: 33 additions & 0 deletions packages/e2e-tests/specs/editor/plugins/block-api.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* WordPress dependencies
*/
import {
activatePlugin,
createNewPost,
deactivatePlugin,
insertBlock,
} from '@wordpress/e2e-test-utils';

describe( 'Using Block API', () => {
beforeAll( async () => {
await activatePlugin( 'gutenberg-test-block-api' );
} );

afterAll( async () => {
await deactivatePlugin( 'gutenberg-test-block-api' );
} );

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

test( 'Inserts the filtered hello world block even when filter added after block registration', async () => {
await insertBlock( 'Filtered Hello World' );

const blockContent = await page.$eval(
'div[data-type="e2e-tests/hello-world"]',
( element ) => element.textContent
);
expect( blockContent ).toEqual( 'Hello Editor!' );
} );
} );

0 comments on commit c954944

Please sign in to comment.