From ca8f61f21ee012fe62fd71097ddf493dd4dd6253 Mon Sep 17 00:00:00 2001 From: Jorge Date: Fri, 1 Mar 2019 15:17:59 +0000 Subject: [PATCH] Add: End to end test to make sure when all blocks get removed the default block appender gets inserter and selected --- packages/e2e-test-utils/src/index.js | 1 + .../e2e-test-utils/src/is-in-default-block.js | 15 +++++++++++++ .../e2e-tests/specs/block-deletion.test.js | 22 +++++++++++++++++++ .../e2e-tests/specs/splitting-merging.test.js | 12 ++-------- 4 files changed, 40 insertions(+), 10 deletions(-) create mode 100644 packages/e2e-test-utils/src/is-in-default-block.js diff --git a/packages/e2e-test-utils/src/index.js b/packages/e2e-test-utils/src/index.js index 6f48ab320a206..4df7c7f64d14b 100644 --- a/packages/e2e-test-utils/src/index.js +++ b/packages/e2e-test-utils/src/index.js @@ -21,6 +21,7 @@ export { hasBlockSwitcher } from './has-block-switcher'; export { insertBlock } from './insert-block'; export { installPlugin } from './install-plugin'; export { isCurrentURL } from './is-current-url'; +export { isInDefaultBlock } from './is-in-default-block'; export { loginUser } from './login-user'; export { observeFocusLoss } from './observe-focus-loss'; export { openDocumentSettingsSidebar } from './open-document-settings-sidebar'; diff --git a/packages/e2e-test-utils/src/is-in-default-block.js b/packages/e2e-test-utils/src/is-in-default-block.js new file mode 100644 index 0000000000000..194ca34f6b960 --- /dev/null +++ b/packages/e2e-test-utils/src/is-in-default-block.js @@ -0,0 +1,15 @@ +/** + * Checks if the block that is focused is the default block. + * + * @return {Promise} Promise resolving with a boolean indicating if the focused block is the default block. + */ +export function isInDefaultBlock() { + return page.evaluate( () => { + const activeBlockName = document.activeElement + .closest( '[data-type]' ) + .getAttribute( 'data-type' ); + const defaultBlockName = window.wp.blocks.getDefaultBlockName(); + + return activeBlockName === defaultBlockName; + } ); +} diff --git a/packages/e2e-tests/specs/block-deletion.test.js b/packages/e2e-tests/specs/block-deletion.test.js index b08eacd1f6d62..d36ff3fd791d8 100644 --- a/packages/e2e-tests/specs/block-deletion.test.js +++ b/packages/e2e-tests/specs/block-deletion.test.js @@ -5,6 +5,7 @@ import { clickBlockAppender, getEditedPostContent, createNewPost, + isInDefaultBlock, pressKeyWithModifier, } from '@wordpress/e2e-test-utils'; @@ -109,3 +110,24 @@ describe( 'block deletion -', () => { } ); } ); } ); + +describe( 'deleting all blocks', () => { + it( 'results in the default block getting selected', async () => { + await createNewPost(); + await clickBlockAppender(); + await page.keyboard.type( 'Paragraph' ); + + await page.keyboard.press( 'Escape' ); + + await clickOnBlockSettingsMenuItem( 'Remove Block' ); + + // There is a default block: + expect( await page.$$( '.editor-block-list__block' ) ).toHaveLength( 1 ); + + // But the effective saved content is still empty: + expect( await getEditedPostContent() ).toBe( '' ); + + // And focus is retained: + expect( await isInDefaultBlock() ).toBe( true ); + } ); +} ); diff --git a/packages/e2e-tests/specs/splitting-merging.test.js b/packages/e2e-tests/specs/splitting-merging.test.js index 1120f04a2b767..c5ea8bf94ce77 100644 --- a/packages/e2e-tests/specs/splitting-merging.test.js +++ b/packages/e2e-tests/specs/splitting-merging.test.js @@ -4,6 +4,7 @@ import { createNewPost, insertBlock, + isInDefaultBlock, getEditedPostContent, pressKeyTimes, pressKeyWithModifier, @@ -199,15 +200,6 @@ describe( 'splitting and merging blocks', () => { expect( await getEditedPostContent() ).toBe( '' ); // And focus is retained: - const isInDefaultBlock = await page.evaluate( () => { - const activeBlockName = document.activeElement - .closest( '[data-type]' ) - .getAttribute( 'data-type' ); - const defaultBlockName = window.wp.blocks.getDefaultBlockName(); - - return activeBlockName === defaultBlockName; - } ); - - expect( isInDefaultBlock ).toBe( true ); + expect( await isInDefaultBlock() ).toBe( true ); } ); } );