From 7ea50218a1de9cd00aceb0a58b4c879719f26d9b Mon Sep 17 00:00:00 2001 From: Riad Benguella Date: Tue, 25 Dec 2018 10:02:51 +0100 Subject: [PATCH] Add an e2e test for blocks with meta attributes (#13095) --- .../meta-attribute-block.test.js.snap | 3 ++ test/e2e/specs/meta-attribute-block.test.js | 47 +++++++++++++++++++ .../e2e/test-plugins/meta-attribute-block.php | 36 ++++++++++++++ .../meta-attribute-block/index.js | 35 ++++++++++++++ 4 files changed, 121 insertions(+) create mode 100644 test/e2e/specs/__snapshots__/meta-attribute-block.test.js.snap create mode 100644 test/e2e/specs/meta-attribute-block.test.js create mode 100644 test/e2e/test-plugins/meta-attribute-block.php create mode 100644 test/e2e/test-plugins/meta-attribute-block/index.js diff --git a/test/e2e/specs/__snapshots__/meta-attribute-block.test.js.snap b/test/e2e/specs/__snapshots__/meta-attribute-block.test.js.snap new file mode 100644 index 00000000000000..076a8ebbaac6eb --- /dev/null +++ b/test/e2e/specs/__snapshots__/meta-attribute-block.test.js.snap @@ -0,0 +1,3 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Block with a meta attribute Should persist the meta attribute properly 1`] = `""`; diff --git a/test/e2e/specs/meta-attribute-block.test.js b/test/e2e/specs/meta-attribute-block.test.js new file mode 100644 index 00000000000000..b8d7fc4c5188e9 --- /dev/null +++ b/test/e2e/specs/meta-attribute-block.test.js @@ -0,0 +1,47 @@ +/** + * Internal dependencies + */ +import { + newPost, + getEditedPostContent, + saveDraft, + insertBlock, +} from '../support/utils'; +import { activatePlugin, deactivatePlugin } from '../support/plugins'; + +describe( 'Block with a meta attribute', () => { + beforeAll( async () => { + await activatePlugin( 'gutenberg-test-meta-attribute-block' ); + } ); + + beforeEach( async () => { + await newPost(); + } ); + + afterAll( async () => { + await deactivatePlugin( 'gutenberg-test-meta-attribute-block' ); + } ); + + it( 'Should persist the meta attribute properly', async () => { + await insertBlock( 'Test Meta Attribute Block' ); + await page.keyboard.type( 'Meta Value' ); + await saveDraft(); + await page.reload(); + + expect( await getEditedPostContent() ).toMatchSnapshot(); + const persistedValue = await page.evaluate( () => document.querySelector( '.my-meta-input' ).value ); + expect( persistedValue ).toBe( 'Meta Value' ); + } ); + + it( 'Should use the same value in all the blocks', async () => { + await insertBlock( 'Test Meta Attribute Block' ); + await insertBlock( 'Test Meta Attribute Block' ); + await insertBlock( 'Test Meta Attribute Block' ); + await page.keyboard.type( 'Meta Value' ); + + const persistedValues = await page.evaluate( () => Array.from( document.querySelectorAll( '.my-meta-input' ) ).map( ( input ) => input.value ) ); + persistedValues.forEach( ( val ) => { + expect( val ).toBe( 'Meta Value' ); + } ); + } ); +} ); diff --git a/test/e2e/test-plugins/meta-attribute-block.php b/test/e2e/test-plugins/meta-attribute-block.php new file mode 100644 index 00000000000000..45fbcc661e1083 --- /dev/null +++ b/test/e2e/test-plugins/meta-attribute-block.php @@ -0,0 +1,36 @@ + true, + 'single' => true, + 'type' => 'string', + ) + ); +} + +add_action( 'init', 'init_test_meta_attribute_block_plugin' ); diff --git a/test/e2e/test-plugins/meta-attribute-block/index.js b/test/e2e/test-plugins/meta-attribute-block/index.js new file mode 100644 index 00000000000000..0910e648299aef --- /dev/null +++ b/test/e2e/test-plugins/meta-attribute-block/index.js @@ -0,0 +1,35 @@ +( function() { + var registerBlockType = wp.blocks.registerBlockType; + var el = wp.element.createElement; + + registerBlockType( 'test/test-meta-attribute-block', { + title: 'Test Meta Attribute Block', + icon: 'star', + category: 'common', + + attributes: { + content: { + type: "string", + source: "meta", + meta: "my_meta", + }, + }, + + edit: function( props ) { + return el( + 'input', + { + className: 'my-meta-input', + value: props.attributes.content, + onChange: function( event ) { + props.setAttributes( { content: event.target.value } ); + }, + } + ); + }, + + save: function() { + return null; + }, + } ); +} )();