From c813c993527d761828f0ac19b32c7f646507fdee Mon Sep 17 00:00:00 2001 From: Bart Kalisz Date: Mon, 13 Feb 2023 17:18:01 +0100 Subject: [PATCH 1/4] =?UTF-8?q?Init=20multi-block-selection.spec.js=20?= =?UTF-8?q?=F0=9F=A5=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../various/multi-block-selection.spec.js | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 test/e2e/specs/editor/various/multi-block-selection.spec.js diff --git a/test/e2e/specs/editor/various/multi-block-selection.spec.js b/test/e2e/specs/editor/various/multi-block-selection.spec.js new file mode 100644 index 00000000000000..a6562eb27f1dc6 --- /dev/null +++ b/test/e2e/specs/editor/various/multi-block-selection.spec.js @@ -0,0 +1,51 @@ +/** + * WordPress dependencies + */ +const { test } = require( '@wordpress/e2e-test-utils-playwright' ); + +test.describe( 'Multi-block selection', () => { + test.beforeEach( async ( { admin } ) => { + await admin.createNewPost(); + } ); + + test( 'should select with double ctrl+a and speak', async ( {} ) => {} ); + test( 'should only trigger multi-selection when at the end', async ( {} ) => {} ); + test( 'should use selection direction to determine vertical edge', async ( {} ) => {} ); + test( 'should always expand single line selection', async ( {} ) => {} ); + test( 'should allow selecting outer edge if there is no sibling block', async ( {} ) => {} ); + test( 'should select and deselect with shift and arrow keys', async ( {} ) => {} ); + test( 'should deselect with Escape', async ( {} ) => {} ); + test( 'should select with shift + click', async ( {} ) => {} ); + test( 'should properly select a single block even if `shift` was held for the selection', async ( {} ) => {} ); + test( 'should properly select multiple blocks if selected nested blocks belong to different parent', async ( {} ) => {} ); + test( 'should properly select part of nested rich text block while holding shift', async ( {} ) => {} ); + test( 'should select by dragging', async ( {} ) => {} ); + test( 'should select by dragging into nested block', async ( {} ) => {} ); + test( 'should cut and paste', async ( {} ) => {} ); + test( 'should copy and paste', async ( {} ) => {} ); + test( 'should return original focus after failed multi selection attempt', async ( {} ) => {} ); + test( 'should preserve dragged selection on move', async ( {} ) => {} ); + test( 'should clear selection when clicking next to blocks', async ( {} ) => {} ); + test( 'should place the caret at the end of last pasted paragraph (paste to empty )', async ( {} ) => {} ); + test( 'should place the caret at the end of last pasted paragraph (paste mid-block)', async ( {} ) => {} ); + test( 'should place the caret at the end of last pasted paragraph (replace)', async ( {} ) => {} ); + test( 'should set attributes for multiple paragraphs', async ( {} ) => {} ); + test( 'should copy multiple blocks', async ( {} ) => {} ); + test( 'should not multi select single block', async ( {} ) => {} ); + test( 'should gradually multi-select', async ( {} ) => {} ); + test( 'should multi-select from within the list block', async ( {} ) => {} ); + test( 'should select all from empty selection', async ( {} ) => {} ); + test( 'should select title if the cursor is on title', async ( {} ) => {} ); + test( 'should multi-select in the ListView component with shift + click', async ( {} ) => {} ); + test( 'should multi-select in the ListView component with shift + up and down keys', async ( {} ) => {} ); + test( 'should forward delete across blocks', async ( {} ) => {} ); + test( 'should write over selection', async ( {} ) => {} ); + test( 'should handle Enter across blocks', async ( {} ) => {} ); + test( 'should select separator (single element block)', async ( {} ) => {} ); + test( 'should partially select with shift + click', async ( {} ) => {} ); + test.describe( 'shift+click multi-selection', () => { + test( 'should multi-select block with text selection and a block without text selection', async ( {} ) => {} ); + test( 'should multi-select blocks without text selection', async ( {} ) => {} ); + } ); + test( 'should select by dragging into separator', async ( {} ) => {} ); +} ); From 34d8f5a049c3ab792db7eca4d8f5e7265f1e6add Mon Sep 17 00:00:00 2001 From: Kai Hao Date: Mon, 13 Feb 2023 11:11:43 +0800 Subject: [PATCH 2/4] Migrate `multi-block-selection` to Playwright --- .../multi-block-selection.test.js.snap | 347 ---- .../various/multi-block-selection.test.js | 1048 ------------- .../various/multi-block-selection.spec.js | 1394 ++++++++++++++++- 3 files changed, 1355 insertions(+), 1434 deletions(-) delete mode 100644 packages/e2e-tests/specs/editor/various/__snapshots__/multi-block-selection.test.js.snap delete mode 100644 packages/e2e-tests/specs/editor/various/multi-block-selection.test.js diff --git a/packages/e2e-tests/specs/editor/various/__snapshots__/multi-block-selection.test.js.snap b/packages/e2e-tests/specs/editor/various/__snapshots__/multi-block-selection.test.js.snap deleted file mode 100644 index 915b52ba94edab..00000000000000 --- a/packages/e2e-tests/specs/editor/various/__snapshots__/multi-block-selection.test.js.snap +++ /dev/null @@ -1,347 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`Multi-block selection should allow selecting outer edge if there is no sibling block 1`] = ` -" -

2

-" -`; - -exports[`Multi-block selection should always expand single line selection 1`] = ` -" -

2

-" -`; - -exports[`Multi-block selection should clear selection when clicking next to blocks 1`] = ` -" -

1

- - - -

2

-" -`; - -exports[`Multi-block selection should copy and paste 1`] = ` -" -

1

- - - -

2

-" -`; - -exports[`Multi-block selection should copy and paste 2`] = `""`; - -exports[`Multi-block selection should copy and paste 3`] = ` -" -

1

- - - -

2

-" -`; - -exports[`Multi-block selection should copy multiple blocks 1`] = ` -" -

1

- - - -

2

- - - -

1

- - - -

2

-" -`; - -exports[`Multi-block selection should cut and paste 1`] = `""`; - -exports[`Multi-block selection should cut and paste 2`] = ` -" -

1

- - - -

2

-" -`; - -exports[`Multi-block selection should forward delete across blocks 1`] = ` -" -

1[

- - - -

.

- - - -

]2

-" -`; - -exports[`Multi-block selection should forward delete across blocks 2`] = ` -" -

1&2

-" -`; - -exports[`Multi-block selection should gradually multi-select 1`] = ` -" -
-
-

1

- - - -

2

-
- - - -
-
-" -`; - -exports[`Multi-block selection should gradually multi-select 2`] = ` -" -
-" -`; - -exports[`Multi-block selection should handle Enter across blocks 1`] = ` -" -

1[

- - - -

.

- - - -

]2

-" -`; - -exports[`Multi-block selection should handle Enter across blocks 2`] = ` -" -

1

- - - -

&

- - - -

2

-" -`; - -exports[`Multi-block selection should multi-select from within the list block 1`] = ` -" -

1

- - - - -" -`; - -exports[`Multi-block selection should not multi select single block 1`] = ` -" -

- - - -

-" -`; - -exports[`Multi-block selection should only trigger multi-selection when at the end 1`] = ` -" -

1.

- - - -

-" -`; - -exports[`Multi-block selection should partially select with shift + click 1`] = ` -" -

1[

- - - -

]2

-" -`; - -exports[`Multi-block selection should partially select with shift + click 2`] = ` -" -

1&2

-" -`; - -exports[`Multi-block selection should place the caret at the end of last pasted paragraph (paste mid-block) 1`] = ` -" -

first paragraph

- - - -

second paragr

- - - -

first paragraph

- - - -

second paragrap

- - - -

aph

-" -`; - -exports[`Multi-block selection should place the caret at the end of last pasted paragraph (paste to empty editor) 1`] = ` -" -

first paragraph

- - - -

second paragrap

-" -`; - -exports[`Multi-block selection should place the caret at the end of last pasted paragraph (replace) 1`] = ` -" -

first paragraph

- - - -

second paragrap

-" -`; - -exports[`Multi-block selection should preserve dragged selection on move 1`] = ` -" -

2

- - - -

3

- - - -

1

-" -`; - -exports[`Multi-block selection should properly select multiple blocks if selected nested blocks belong to different parent 1`] = ` -" -
-

first

- - - -

group

-
- - - -
-

second

- - - -

group

-
-" -`; - -exports[`Multi-block selection should return original focus after failed multi selection attempt 1`] = ` -" -

2

-" -`; - -exports[`Multi-block selection should select all from empty selection 1`] = ` -" -

1

- - - -

2

-" -`; - -exports[`Multi-block selection should select all from empty selection 2`] = `""`; - -exports[`Multi-block selection should select separator (single element block) 1`] = ` -" -

a

- - - -

- - - -
-" -`; - -exports[`Multi-block selection should select separator (single element block) 2`] = ` -" -

&

-" -`; - -exports[`Multi-block selection should set attributes for multiple paragraphs 1`] = ` -" -

1

- - - -

2

-" -`; - -exports[`Multi-block selection should use selection direction to determine vertical edge 1`] = ` -" -

1
2.

- - - -

3

-" -`; - -exports[`Multi-block selection should write over selection 1`] = ` -" -

1[

- - - -

]2

-" -`; - -exports[`Multi-block selection should write over selection 2`] = ` -" -

1...2

-" -`; diff --git a/packages/e2e-tests/specs/editor/various/multi-block-selection.test.js b/packages/e2e-tests/specs/editor/various/multi-block-selection.test.js deleted file mode 100644 index 9e96c5b14cef49..00000000000000 --- a/packages/e2e-tests/specs/editor/various/multi-block-selection.test.js +++ /dev/null @@ -1,1048 +0,0 @@ -/** - * WordPress dependencies - */ -import { - clickBlockAppender, - createNewPost, - pressKeyWithModifier, - pressKeyTimes, - getEditedPostContent, - clickBlockToolbarButton, - clickButton, - clickMenuItem, - insertBlock, - openListView, - saveDraft, - transformBlockTo, -} from '@wordpress/e2e-test-utils'; - -async function getSelectedFlatIndices() { - return await page.evaluate( () => { - const indices = []; - let single; - - Array.from( - document.querySelectorAll( '.wp-block:not(.editor-post-title)' ) - ).forEach( ( node, index ) => { - if ( node.classList.contains( 'is-selected' ) ) { - single = index + 1; - } - - if ( node.classList.contains( 'is-multi-selected' ) ) { - indices.push( index + 1 ); - } - } ); - - return single !== undefined ? single : indices; - } ); -} - -/** - * Tests if the native selection matches the block selection. - */ -async function testNativeSelection() { - // Wait for the selection to update and async mode to update classes of - // deselected blocks. - await page.evaluate( () => new Promise( window.requestIdleCallback ) ); - await page.evaluate( () => { - const selection = window.getSelection(); - const elements = Array.from( - document.querySelectorAll( '.is-multi-selected' ) - ); - - if ( ! elements.length ) { - const element = document.querySelector( '.is-selected' ); - - if ( ! element || ! selection.rangeCount ) { - return; - } - - const { startContainer, endContainer } = selection.getRangeAt( 0 ); - - if ( ! element.contains( startContainer ) ) { - throw 'expected selection to start in the selected block'; - } - - if ( ! element.contains( endContainer ) ) { - throw 'expected selection to start in the selected block'; - } - - return; - } - - if ( selection.rangeCount !== 1 ) { - throw 'expected one range'; - } - - if ( selection.isCollapsed ) { - throw 'expected an uncollapsed selection'; - } - - const firstElement = elements[ 0 ]; - const lastElement = elements[ elements.length - 1 ]; - - if ( ! selection.containsNode( firstElement, true ) ) { - throw 'expected selection to include in the first selected block'; - } - - if ( ! selection.containsNode( lastElement, true ) ) { - throw 'expected selection to include in the last selected block'; - } - } ); -} - -describe( 'Multi-block selection', () => { - beforeEach( async () => { - await createNewPost(); - } ); - - it( 'should select with double ctrl+a and speak', async () => { - await clickBlockAppender(); - await page.keyboard.type( '1' ); - await page.keyboard.press( 'Enter' ); - await page.keyboard.type( '2' ); - await page.keyboard.press( 'Enter' ); - await page.keyboard.type( '3' ); - - // Multiselect via keyboard. - await pressKeyWithModifier( 'primary', 'a' ); - await pressKeyWithModifier( 'primary', 'a' ); - - expect( await getSelectedFlatIndices() ).toEqual( [ 1, 2, 3 ] ); - - // TODO: It would be great to do this test by spying on `wp.a11y.speak`, - // but it's very difficult to do that because `wp.a11y` has - // DOM-dependant side-effect setup code and doesn't seem straightforward - // to mock. Instead, we check for the DOM node that `wp.a11y.speak()` - // inserts text into. - const speakTextContent = await page.$eval( - '#a11y-speak-assertive', - ( element ) => element.textContent - ); - expect( speakTextContent.trim() ).toEqual( '3 blocks selected.' ); - } ); - - // See #14448: an incorrect buffer may trigger multi-selection too soon. - it( 'should only trigger multi-selection when at the end', async () => { - // Create a paragraph with four lines. - await clickBlockAppender(); - await page.keyboard.type( '1.' ); - await pressKeyWithModifier( 'shift', 'Enter' ); - await page.keyboard.type( '2.' ); - await pressKeyWithModifier( 'shift', 'Enter' ); - await page.keyboard.type( '3.' ); - await pressKeyWithModifier( 'shift', 'Enter' ); - await page.keyboard.type( '4.' ); - // Create a second block. - await page.keyboard.press( 'Enter' ); - // Move to the middle of the first line. - await pressKeyTimes( 'ArrowUp', 4 ); - await page.keyboard.press( 'ArrowRight' ); - // Select mid line one to mid line four. - await pressKeyWithModifier( 'shift', 'ArrowDown' ); - await pressKeyWithModifier( 'shift', 'ArrowDown' ); - await pressKeyWithModifier( 'shift', 'ArrowDown' ); - // Delete the text to see if the selection was correct. - await page.keyboard.press( 'Backspace' ); - - expect( await getEditedPostContent() ).toMatchSnapshot(); - } ); - - it( 'should use selection direction to determine vertical edge', async () => { - await clickBlockAppender(); - await page.keyboard.type( '1' ); - await pressKeyWithModifier( 'shift', 'Enter' ); - await page.keyboard.press( 'Enter' ); - await page.keyboard.type( '3' ); - await page.keyboard.press( 'ArrowUp' ); - await page.keyboard.type( '2' ); - - await pressKeyWithModifier( 'shift', 'ArrowUp' ); - await pressKeyWithModifier( 'shift', 'ArrowDown' ); - - // Should type at the end of the paragraph. - await page.keyboard.type( '.' ); - - expect( await getEditedPostContent() ).toMatchSnapshot(); - } ); - - it( 'should always expand single line selection', async () => { - await clickBlockAppender(); - await page.keyboard.press( 'Enter' ); - await page.keyboard.type( '12' ); - await page.keyboard.press( 'ArrowLeft' ); - await pressKeyWithModifier( 'shift', 'ArrowLeft' ); - await pressKeyWithModifier( 'shift', 'ArrowUp' ); - await testNativeSelection(); - // This deletes all blocks. - await page.keyboard.press( 'Backspace' ); - - expect( await getEditedPostContent() ).toMatchSnapshot(); - } ); - - it( 'should allow selecting outer edge if there is no sibling block', async () => { - await clickBlockAppender(); - await page.keyboard.type( '1' ); - await pressKeyWithModifier( 'shift', 'ArrowUp' ); - // This should replace the content. - await page.keyboard.type( '2' ); - - expect( await getEditedPostContent() ).toMatchSnapshot(); - } ); - - it( 'should select and deselect with shift and arrow keys', async () => { - await clickBlockAppender(); - await page.keyboard.type( '1' ); - await page.keyboard.press( 'Enter' ); - await page.keyboard.type( '2' ); - await page.keyboard.press( 'Enter' ); - await page.keyboard.type( '3' ); - await page.keyboard.press( 'Enter' ); - await page.keyboard.type( '4' ); - await page.keyboard.press( 'Enter' ); - await page.keyboard.type( '5' ); - await page.keyboard.press( 'ArrowUp' ); - await page.keyboard.press( 'ArrowUp' ); - await pressKeyWithModifier( 'shift', 'ArrowDown' ); - - await testNativeSelection(); - expect( await getSelectedFlatIndices() ).toEqual( [ 3, 4 ] ); - - await pressKeyWithModifier( 'shift', 'ArrowDown' ); - - await testNativeSelection(); - expect( await getSelectedFlatIndices() ).toEqual( [ 3, 4, 5 ] ); - - await pressKeyWithModifier( 'shift', 'ArrowUp' ); - - await testNativeSelection(); - expect( await getSelectedFlatIndices() ).toEqual( [ 3, 4 ] ); - - await pressKeyWithModifier( 'shift', 'ArrowUp' ); - - await testNativeSelection(); - expect( await getSelectedFlatIndices() ).toBe( 3 ); - - await pressKeyWithModifier( 'shift', 'ArrowUp' ); - - await testNativeSelection(); - expect( await getSelectedFlatIndices() ).toEqual( [ 2, 3 ] ); - - await pressKeyWithModifier( 'shift', 'ArrowUp' ); - - await testNativeSelection(); - expect( await getSelectedFlatIndices() ).toEqual( [ 1, 2, 3 ] ); - - await pressKeyWithModifier( 'shift', 'ArrowDown' ); - - await testNativeSelection(); - expect( await getSelectedFlatIndices() ).toEqual( [ 2, 3 ] ); - - await pressKeyWithModifier( 'shift', 'ArrowDown' ); - - await testNativeSelection(); - expect( await getSelectedFlatIndices() ).toBe( 3 ); - } ); - - // Flaky test. - it.skip( 'should deselect with Escape', async () => { - await clickBlockAppender(); - await page.keyboard.type( '1' ); - await page.keyboard.press( 'Enter' ); - await page.keyboard.type( '2' ); - - await pressKeyWithModifier( 'primary', 'a' ); - await pressKeyWithModifier( 'primary', 'a' ); - - await testNativeSelection(); - expect( await getSelectedFlatIndices() ).toEqual( [ 1, 2 ] ); - - await page.keyboard.press( 'Escape' ); - - // Wait for blocks to have updated asynchronously. - await page.evaluate( () => new Promise( window.requestIdleCallback ) ); - expect( await getSelectedFlatIndices() ).toEqual( [] ); - } ); - - it( 'should select with shift + click', async () => { - await clickBlockAppender(); - await page.keyboard.type( '1' ); - await page.keyboard.press( 'Enter' ); - await page.keyboard.type( '2' ); - await page.keyboard.down( 'Shift' ); - await page.click( '[data-type="core/paragraph"]' ); - await page.keyboard.up( 'Shift' ); - - await testNativeSelection(); - expect( await getSelectedFlatIndices() ).toEqual( [ 1, 2 ] ); - - // Group the blocks and test that multiselection also works for nested - // blocks. Checks for regressions of - // https://github.com/WordPress/gutenberg/issues/32056 - - await clickBlockToolbarButton( 'Options' ); - await clickMenuItem( 'Group' ); - await page.click( '[data-type="core/paragraph"]' ); - await page.keyboard.down( 'Shift' ); - await page.click( '[data-type="core/paragraph"]:nth-child(2)' ); - await page.keyboard.up( 'Shift' ); - await testNativeSelection(); - expect( await getSelectedFlatIndices() ).toEqual( [ 2, 3 ] ); - } ); - - // @see https://github.com/WordPress/gutenberg/issues/34118 - it( 'should properly select a single block even if `shift` was held for the selection', async () => { - await clickBlockAppender(); - await page.keyboard.type( 'test' ); - - await saveDraft(); - await page.reload(); - await page.waitForSelector( '.edit-post-layout' ); - - await page.keyboard.down( 'Shift' ); - await page.click( '[data-type="core/paragraph"]', { visible: true } ); - await page.keyboard.up( 'Shift' ); - - await pressKeyWithModifier( 'primary', 'a' ); - await page.keyboard.type( 'new content' ); - expect( await getEditedPostContent() ).toMatchInlineSnapshot( ` - " -

new content

- " - ` ); - } ); - - it( 'should properly select multiple blocks if selected nested blocks belong to different parent', async () => { - await clickBlockAppender(); - await page.keyboard.type( 'first' ); - await page.keyboard.press( 'Enter' ); - await page.keyboard.type( 'group' ); - // Multiselect via keyboard. - await page.keyboard.down( 'Shift' ); - await page.keyboard.press( 'ArrowUp' ); - await page.keyboard.up( 'Shift' ); - await transformBlockTo( 'Group' ); - await page.keyboard.press( 'Enter' ); - await page.keyboard.type( 'second' ); - await page.keyboard.press( 'Enter' ); - await page.keyboard.type( 'group' ); - await page.keyboard.down( 'Shift' ); - await page.keyboard.press( 'ArrowUp' ); - await page.keyboard.up( 'Shift' ); - await transformBlockTo( 'Group' ); - - // Confirm setup. - expect( await getEditedPostContent() ).toMatchSnapshot(); - - // Click the first paragraph in the first Group block while pressing `shift` key. - const firstParagraph = await page.waitForXPath( "//p[text()='first']" ); - await page.keyboard.down( 'Shift' ); - await firstParagraph.click(); - await page.keyboard.up( 'Shift' ); - - await page.waitForSelector( '.is-multi-selected' ); - const selectedBlocks = await page.$$( '.is-multi-selected' ); - expect( selectedBlocks ).toHaveLength( 2 ); - } ); - it( 'should properly select part of nested rich text block while holding shift', async () => { - await clickBlockAppender(); - await page.keyboard.type( 'rich text in group' ); - await transformBlockTo( 'Group' ); - await page.keyboard.press( 'ArrowDown' ); - - await page.keyboard.down( 'Shift' ); - const paragraph = await page.$( '[data-type="core/paragraph"]' ); - const { x, y } = await paragraph.boundingBox(); - await page.mouse.move( x + 20, y ); - await page.mouse.down(); - await page.keyboard.up( 'Shift' ); - await page.mouse.up(); - await page.keyboard.type( 'hi' ); - expect( await getEditedPostContent() ).toMatchInlineSnapshot( ` - " -
-

hih text in group

-
- " - ` ); - } ); - - it( 'should select by dragging', async () => { - await clickBlockAppender(); - await page.keyboard.type( '1' ); - await page.keyboard.press( 'Enter' ); - await page.keyboard.type( '2' ); - await page.keyboard.press( 'ArrowUp' ); - - const [ paragraph1, paragraph2 ] = await page.$$( - '[data-type="core/paragraph"]' - ); - const coord1 = await paragraph1.clickablePoint(); - const coord2 = await paragraph2.clickablePoint(); - - await page.mouse.move( coord1.x, coord1.y ); - await page.mouse.down(); - await page.mouse.move( coord2.x, coord2.y, { steps: 10 } ); - await page.mouse.up(); - - await testNativeSelection(); - expect( await getSelectedFlatIndices() ).toEqual( [ 1, 2 ] ); - } ); - - it( 'should select by dragging into nested block', async () => { - await clickBlockAppender(); - await page.keyboard.type( '1' ); - await page.keyboard.press( 'Enter' ); - await page.keyboard.type( '/group' ); - await page.waitForXPath( - '//button[@aria-selected="true"][text()="Group"]' - ); - await page.keyboard.press( 'Enter' ); - - // Select the default, selected Group layout from the variation picker. - await page.click( - 'button[aria-label="Group: Gather blocks in a container."]' - ); - - const groupAppender = await page.waitForSelector( - '.block-editor-button-block-appender' - ); - await groupAppender.click(); - - const paragraphBlockButton = await page.waitForSelector( - '.editor-block-list-item-paragraph' - ); - await paragraphBlockButton.click(); - - await page.keyboard.type( '2' ); - - const [ paragraph1, paragraph2 ] = await page.$$( - '[data-type="core/paragraph"]' - ); - const coord1 = await paragraph1.clickablePoint(); - const coord2 = await paragraph2.clickablePoint(); - - await page.mouse.move( coord1.x, coord1.y ); - await page.mouse.down(); - await page.mouse.move( coord2.x, coord2.y, { steps: 10 } ); - await page.mouse.up(); - - expect( await getSelectedFlatIndices() ).toEqual( [ 1, 2 ] ); - } ); - - it( 'should cut and paste', async () => { - await clickBlockAppender(); - await page.keyboard.type( '1' ); - await page.keyboard.press( 'Enter' ); - await page.keyboard.type( '2' ); - await pressKeyWithModifier( 'primary', 'a' ); - await pressKeyWithModifier( 'primary', 'a' ); - await pressKeyWithModifier( 'primary', 'x' ); - - expect( await getEditedPostContent() ).toMatchSnapshot(); - - await pressKeyWithModifier( 'primary', 'v' ); - - expect( await getEditedPostContent() ).toMatchSnapshot(); - } ); - - it( 'should copy and paste', async () => { - await clickBlockAppender(); - await page.keyboard.type( '1' ); - await page.keyboard.press( 'Enter' ); - await page.keyboard.type( '2' ); - await pressKeyWithModifier( 'primary', 'a' ); - await pressKeyWithModifier( 'primary', 'a' ); - await pressKeyWithModifier( 'primary', 'c' ); - - expect( await getEditedPostContent() ).toMatchSnapshot(); - - await page.keyboard.press( 'Backspace' ); - - expect( await getEditedPostContent() ).toMatchSnapshot(); - - await pressKeyWithModifier( 'primary', 'v' ); - - expect( await getEditedPostContent() ).toMatchSnapshot(); - } ); - - it( 'should return original focus after failed multi selection attempt', async () => { - await clickBlockAppender(); - await page.keyboard.type( '1' ); - await page.keyboard.type( '2' ); - await page.keyboard.press( 'ArrowLeft' ); - - const [ coord1, coord2 ] = await page.evaluate( () => { - const selection = window.getSelection(); - - if ( ! selection.rangeCount ) { - return; - } - - const range = selection.getRangeAt( 0 ); - const rect1 = range.getClientRects()[ 0 ]; - const element = document.querySelector( - '[data-type="core/paragraph"]' - ); - const rect2 = element.getBoundingClientRect(); - - return [ - { - x: rect1.x, - y: rect1.y + rect1.height / 2, - }, - { - // Move a bit outside the paragraph. - x: rect2.x - 5, - y: rect2.y + rect2.height / 2, - }, - ]; - } ); - - await page.mouse.move( coord1.x, coord1.y ); - await page.mouse.down(); - await page.mouse.move( coord2.x, coord2.y, { steps: 10 } ); - await page.mouse.up(); - - // Wait for the selection to update. - await page.evaluate( - () => new Promise( window.requestAnimationFrame ) - ); - - // Only "1" should be deleted. - await page.keyboard.press( 'Backspace' ); - - expect( await getEditedPostContent() ).toMatchSnapshot(); - } ); - - it( 'should preserve dragged selection on move', async () => { - await clickBlockAppender(); - await page.keyboard.type( '1' ); - await page.keyboard.press( 'Enter' ); - await page.keyboard.type( '2' ); - await page.keyboard.press( 'Enter' ); - await page.keyboard.type( '3' ); - - const paragraphs = await page.$$( '[data-type="core/paragraph"]' ); - const coord1 = await paragraphs[ 2 ].clickablePoint(); - const coord2 = await paragraphs[ 1 ].clickablePoint(); - - await page.mouse.move( coord1.x, coord1.y ); - await page.mouse.down(); - await page.mouse.move( coord2.x, coord2.y ); - await page.mouse.up(); - - await testNativeSelection(); - expect( await getSelectedFlatIndices() ).toEqual( [ 2, 3 ] ); - - await clickBlockToolbarButton( 'Move up' ); - - await testNativeSelection(); - expect( await getSelectedFlatIndices() ).toEqual( [ 1, 2 ] ); - - expect( await getEditedPostContent() ).toMatchSnapshot(); - } ); - - it( 'should clear selection when clicking next to blocks', async () => { - await clickBlockAppender(); - await page.keyboard.type( '1' ); - await page.keyboard.press( 'Enter' ); - await page.keyboard.type( '2' ); - await pressKeyWithModifier( 'shift', 'ArrowUp' ); - await testNativeSelection(); - expect( await getSelectedFlatIndices() ).toEqual( [ 1, 2 ] ); - const paragraph = await page.$( '[data-type="core/paragraph"]' ); - const rect = await paragraph.boundingBox(); - const coord = { - x: rect.x - 1, - y: rect.y + rect.height / 2, - }; - - await page.mouse.click( coord.x, coord.y ); - - // Wait for blocks to have updated asynchronously. - await page.evaluate( () => new Promise( window.requestIdleCallback ) ); - await testNativeSelection(); - expect( await getSelectedFlatIndices() ).toEqual( [] ); - - expect( await getEditedPostContent() ).toMatchSnapshot(); - } ); - - it( 'should place the caret at the end of last pasted paragraph (paste to empty editor)', async () => { - await clickBlockAppender(); - await page.keyboard.type( 'first paragraph' ); - await page.keyboard.press( 'Enter' ); - await page.keyboard.type( 'second paragraph' ); - await pressKeyWithModifier( 'primary', 'a' ); - await pressKeyWithModifier( 'primary', 'a' ); - await pressKeyWithModifier( 'primary', 'c' ); - await page.keyboard.press( 'Backspace' ); - await pressKeyWithModifier( 'primary', 'v' ); - await page.keyboard.press( 'Backspace' ); - - expect( await getEditedPostContent() ).toMatchSnapshot(); - } ); - - it( 'should place the caret at the end of last pasted paragraph (paste mid-block)', async () => { - await clickBlockAppender(); - await page.keyboard.type( 'first paragraph' ); - await page.keyboard.press( 'Enter' ); - await page.keyboard.type( 'second paragraph' ); - await pressKeyWithModifier( 'primary', 'a' ); - await pressKeyWithModifier( 'primary', 'a' ); - await pressKeyWithModifier( 'primary', 'c' ); - - await page.keyboard.press( 'ArrowRight' ); - await page.keyboard.press( 'ArrowLeft' ); - await page.keyboard.press( 'ArrowLeft' ); - await page.keyboard.press( 'ArrowLeft' ); - - await pressKeyWithModifier( 'primary', 'v' ); - await page.keyboard.press( 'Backspace' ); - - expect( await getEditedPostContent() ).toMatchSnapshot(); - } ); - - it( 'should place the caret at the end of last pasted paragraph (replace)', async () => { - await clickBlockAppender(); - await page.keyboard.type( 'first paragraph' ); - await page.keyboard.press( 'Enter' ); - await page.keyboard.type( 'second paragraph' ); - await pressKeyWithModifier( 'primary', 'a' ); - await pressKeyWithModifier( 'primary', 'a' ); - await pressKeyWithModifier( 'primary', 'c' ); - await pressKeyWithModifier( 'primary', 'v' ); - await page.keyboard.press( 'Backspace' ); - - expect( await getEditedPostContent() ).toMatchSnapshot(); - } ); - - it( 'should set attributes for multiple paragraphs', async () => { - await clickBlockAppender(); - await page.keyboard.type( '1' ); - await page.keyboard.press( 'Enter' ); - await page.keyboard.type( '2' ); - await pressKeyWithModifier( 'primary', 'a' ); - await pressKeyWithModifier( 'primary', 'a' ); - await clickBlockToolbarButton( 'Align text' ); - await clickButton( 'Align text center' ); - - expect( await getEditedPostContent() ).toMatchSnapshot(); - } ); - - it( 'should copy multiple blocks', async () => { - await clickBlockAppender(); - await page.keyboard.type( '1' ); - await page.keyboard.press( 'Enter' ); - await page.keyboard.type( '2' ); - await pressKeyWithModifier( 'primary', 'a' ); - await pressKeyWithModifier( 'primary', 'a' ); - await pressKeyWithModifier( 'primary', 'c' ); - await page.keyboard.press( 'ArrowUp' ); - await pressKeyWithModifier( 'primary', 'v' ); - - expect( await getEditedPostContent() ).toMatchSnapshot(); - } ); - - // Previously we would unexpectedly duplicate the block on Enter. - it( 'should not multi select single block', async () => { - await clickBlockAppender(); - await page.keyboard.type( '1' ); - await pressKeyWithModifier( 'primary', 'a' ); - await pressKeyWithModifier( 'primary', 'a' ); - await page.keyboard.press( 'Enter' ); - - expect( await getEditedPostContent() ).toMatchSnapshot(); - } ); - - it( 'should gradually multi-select', async () => { - await clickBlockAppender(); - await page.keyboard.type( '/columns' ); - await page.waitForXPath( - '//button[@aria-selected="true"][text()="Columns"]' - ); - await page.keyboard.press( 'Enter' ); - // Select two columns. - await page.keyboard.press( 'ArrowRight' ); - await page.keyboard.press( 'Enter' ); - // Navigate to appender. - await page.keyboard.press( 'ArrowRight' ); - await page.keyboard.press( 'Enter' ); - // Wait for inserter results to appear and then select a paragraph. - await page.waitForSelector( - '.block-editor-inserter__quick-inserter-results .block-editor-block-types-list__item' - ); - await page.keyboard.press( 'Tab' ); - await page.keyboard.press( 'Enter' ); - // Type two paragraphs - await page.keyboard.type( '1' ); - await page.keyboard.press( 'Enter' ); - await page.keyboard.type( '2' ); - - // Confirm correct setup: two columns with two paragraphs in the first. - expect( await getEditedPostContent() ).toMatchSnapshot(); - - await pressKeyWithModifier( 'primary', 'a' ); - await pressKeyWithModifier( 'primary', 'a' ); - - await page.waitForSelector( - '[data-type="core/paragraph"].is-multi-selected' - ); - - await pressKeyWithModifier( 'primary', 'a' ); - - await page.waitForSelector( '[data-type="core/column"].is-selected' ); - - await pressKeyWithModifier( 'primary', 'a' ); - - await page.waitForSelector( - '[data-type="core/column"].is-multi-selected' - ); - - await page.keyboard.press( 'Backspace' ); - - // Expect both columns to be deleted. - expect( await getEditedPostContent() ).toMatchSnapshot(); - } ); - - it( 'should multi-select from within the list block', async () => { - await clickBlockAppender(); - // Select a paragraph. - await page.keyboard.type( '1' ); - await page.keyboard.press( 'Enter' ); - // Add a list. - await page.keyboard.type( '/list' ); - await page.waitForXPath( - '//button[@aria-selected="true"][text()="List"]' - ); - await page.keyboard.press( 'Enter' ); - await page.keyboard.type( '1' ); - - // Confirm correct setup: a paragraph and a list. - expect( await getEditedPostContent() ).toMatchSnapshot(); - - await pressKeyWithModifier( 'primary', 'a' ); - await pressKeyWithModifier( 'primary', 'a' ); - await pressKeyWithModifier( 'primary', 'a' ); - - await page.waitForSelector( - '[data-type="core/paragraph"].is-multi-selected' - ); - } ); - - it( 'should select all from empty selection', async () => { - await clickBlockAppender(); - - await page.keyboard.type( '1' ); - await page.keyboard.press( 'Enter' ); - await page.keyboard.type( '2' ); - - // Confirm setup. - expect( await getEditedPostContent() ).toMatchSnapshot(); - - // Clear the selected block. - await page.keyboard.press( 'Escape' ); - await page.keyboard.press( 'Escape' ); - - await pressKeyWithModifier( 'primary', 'a' ); - - await page.keyboard.press( 'Backspace' ); - - // Expect both paragraphs to be deleted. - expect( await getEditedPostContent() ).toMatchSnapshot(); - } ); - - it( 'should select title if the cursor is on title', async () => { - await clickBlockAppender(); - - await page.keyboard.type( '1' ); - await page.keyboard.press( 'Enter' ); - await page.keyboard.type( '2' ); - - await page.type( '.editor-post-title__input', 'Post title' ); - - await pressKeyWithModifier( 'primary', 'a' ); - const selectedText = await page.evaluate( () => { - return window.getSelection().toString(); - } ); - expect( selectedText ).toEqual( 'Post title' ); - } ); - - it( 'should multi-select in the ListView component with shift + click', async () => { - // Create four blocks. - await clickBlockAppender(); - await page.keyboard.type( '1' ); - await page.keyboard.press( 'Enter' ); - await page.keyboard.type( '2' ); - await page.keyboard.press( 'Enter' ); - await page.keyboard.type( '3' ); - await page.keyboard.press( 'Enter' ); - await page.keyboard.type( '4' ); - - // Open up the list view, and get a reference to each of the list items. - await openListView(); - const navButtons = await page.$$( - '.block-editor-list-view-block-select-button' - ); - - // Clicking on the second list item should result in the second block being selected. - await navButtons[ 1 ].click(); - expect( await getSelectedFlatIndices() ).toEqual( 2 ); - - // Shift clicking the fourth list item should result in blocks 2 through 4 being selected. - await page.keyboard.down( 'Shift' ); - await navButtons[ 3 ].click(); - expect( await getSelectedFlatIndices() ).toEqual( [ 2, 3, 4 ] ); - - // With the shift key still held down, clicking the first block should result in - // the first two blocks being selected. - await navButtons[ 0 ].click(); - expect( await getSelectedFlatIndices() ).toEqual( [ 1, 2 ] ); - - // With the shift key up, clicking the fourth block should result in only that block - // being selected. - await page.keyboard.up( 'Shift' ); - await navButtons[ 3 ].click(); - expect( await getSelectedFlatIndices() ).toEqual( 4 ); - } ); - - it( 'should multi-select in the ListView component with shift + up and down keys', async () => { - // Create four blocks. - await clickBlockAppender(); - await page.keyboard.type( '1' ); - await page.keyboard.press( 'Enter' ); - await page.keyboard.type( '2' ); - await page.keyboard.press( 'Enter' ); - await page.keyboard.type( '3' ); - await page.keyboard.press( 'Enter' ); - await page.keyboard.type( '4' ); - - // Open up the list view. The fourth block button will be focused. - await openListView(); - - // Press Up twice to focus over the second block. - await pressKeyTimes( 'ArrowUp', 2 ); - - // Shift + press Down to select the 2nd and 3rd blocks. - await page.keyboard.down( 'Shift' ); - await page.keyboard.press( 'ArrowDown' ); - expect( await getSelectedFlatIndices() ).toEqual( [ 2, 3 ] ); - - // Press Down once more to also select the 4th block. - await page.keyboard.press( 'ArrowDown' ); - expect( await getSelectedFlatIndices() ).toEqual( [ 2, 3, 4 ] ); - - // Press Up three times to adjust the selection to only include the first two blocks. - await pressKeyTimes( 'ArrowUp', 3 ); - expect( await getSelectedFlatIndices() ).toEqual( [ 1, 2 ] ); - - // Raise the shift key - await page.keyboard.up( 'Shift' ); - - // Navigate to the bottom of the list of blocks. - await pressKeyTimes( 'ArrowDown', 3 ); - - // Shift + press UP to select the 3rd and 4th blocks. - // This tests that shift selecting blocks by keyboard that are not adjacent - // to an existing selection resets the selection. - await page.keyboard.down( 'Shift' ); - await page.keyboard.press( 'ArrowUp' ); - await page.keyboard.up( 'Shift' ); - expect( await getSelectedFlatIndices() ).toEqual( [ 3, 4 ] ); - } ); - - it( 'should forward delete across blocks', async () => { - await clickBlockAppender(); - await page.keyboard.type( '1[' ); - await page.keyboard.press( 'Enter' ); - await page.keyboard.type( '.' ); - await page.keyboard.press( 'Enter' ); - // "## " creates h2. - await page.keyboard.type( '## ]2' ); - await page.keyboard.press( 'ArrowLeft' ); - // Select everything between []. - await pressKeyWithModifier( 'shift', 'ArrowLeft' ); - await pressKeyWithModifier( 'shift', 'ArrowLeft' ); - await pressKeyWithModifier( 'shift', 'ArrowLeft' ); - await pressKeyWithModifier( 'shift', 'ArrowLeft' ); - await pressKeyWithModifier( 'shift', 'ArrowLeft' ); - - // Test setup. - expect( await getEditedPostContent() ).toMatchSnapshot(); - - await page.keyboard.press( 'Delete' ); - - // Ensure selection is in the correct place. - await page.keyboard.type( '&' ); - - // Expect a heading with "1&2" as its content. - expect( await getEditedPostContent() ).toMatchSnapshot(); - } ); - - it( 'should write over selection', async () => { - await clickBlockAppender(); - await page.keyboard.type( '1[' ); - await page.keyboard.press( 'Enter' ); - await page.keyboard.type( ']2' ); - await page.keyboard.press( 'ArrowLeft' ); - // Select everything between []. - await pressKeyWithModifier( 'shift', 'ArrowLeft' ); - await pressKeyWithModifier( 'shift', 'ArrowLeft' ); - await pressKeyWithModifier( 'shift', 'ArrowLeft' ); - - // Test setup. - expect( await getEditedPostContent() ).toMatchSnapshot(); - - // Ensure selection is in the correct place. - await page.keyboard.type( '...' ); - - // Expect a heading with "1&2" as its content. - expect( await getEditedPostContent() ).toMatchSnapshot(); - } ); - - it( 'should handle Enter across blocks', async () => { - await clickBlockAppender(); - await page.keyboard.type( '1[' ); - await page.keyboard.press( 'Enter' ); - await page.keyboard.type( '.' ); - await page.keyboard.press( 'Enter' ); - // "## " creates h2. - await page.keyboard.type( '## ]2' ); - await page.keyboard.press( 'ArrowLeft' ); - // Select everything between []. - await pressKeyWithModifier( 'shift', 'ArrowLeft' ); - await pressKeyWithModifier( 'shift', 'ArrowLeft' ); - await pressKeyWithModifier( 'shift', 'ArrowLeft' ); - await pressKeyWithModifier( 'shift', 'ArrowLeft' ); - await pressKeyWithModifier( 'shift', 'ArrowLeft' ); - - // Test setup. - expect( await getEditedPostContent() ).toMatchSnapshot(); - - await page.keyboard.press( 'Enter' ); - - // Ensure selection is in the correct place. - await page.keyboard.type( '&' ); - - // Expect two blocks with "&" in between. - expect( await getEditedPostContent() ).toMatchSnapshot(); - } ); - - it( 'should select separator (single element block)', async () => { - await clickBlockAppender(); - await page.keyboard.type( 'a' ); - await page.keyboard.press( 'Enter' ); - await page.keyboard.press( 'Enter' ); - await page.keyboard.type( '/hr' ); - await page.waitForXPath( - '//button[@aria-selected="true"][text()="Separator"]' - ); - await page.keyboard.press( 'Enter' ); - await page.keyboard.press( 'ArrowUp' ); - await page.keyboard.press( 'ArrowUp' ); - await page.keyboard.press( 'ArrowRight' ); - await pressKeyWithModifier( 'shift', 'ArrowDown' ); - await pressKeyWithModifier( 'shift', 'ArrowDown' ); - - // Test setup. - expect( await getEditedPostContent() ).toMatchSnapshot(); - - await page.keyboard.press( 'Backspace' ); - - // Ensure selection is in the correct place. - await page.keyboard.type( '&' ); - - // Expect a paragraph with "&". - expect( await getEditedPostContent() ).toMatchSnapshot(); - } ); - - it( 'should partially select with shift + click', async () => { - await clickBlockAppender(); - await pressKeyWithModifier( 'primary', 'b' ); - await page.keyboard.type( '1' ); - await pressKeyWithModifier( 'primary', 'b' ); - await page.keyboard.type( '[' ); - await page.keyboard.press( 'Enter' ); - await page.keyboard.type( ']2' ); - await page.keyboard.press( 'ArrowLeft' ); - await page.keyboard.down( 'Shift' ); - await page.click( 'strong' ); - await page.keyboard.up( 'Shift' ); - - // Test setup. - expect( await getEditedPostContent() ).toMatchSnapshot(); - - await page.keyboard.press( 'Backspace' ); - - // Ensure selection is in the correct place. - await page.keyboard.type( '&' ); - - // Expect two blocks with "&" in between. - expect( await getEditedPostContent() ).toMatchSnapshot(); - } ); - describe( 'shift+click multi-selection', () => { - it( 'should multi-select block with text selection and a block without text selection', async () => { - await page.keyboard.press( 'Enter' ); - await page.keyboard.type( 'hi' ); - await page.keyboard.press( 'Enter' ); - await insertBlock( 'Spacer' ); - await page.keyboard.press( 'ArrowUp' ); - - const spacerBlock = await page.waitForSelector( - '.wp-block.wp-block-spacer' - ); - const boundingBox = await spacerBlock.boundingBox(); - const mousePosition = { - x: boundingBox.x + boundingBox.width / 2, - y: boundingBox.y + boundingBox.height / 2, - }; - await page.keyboard.down( 'Shift' ); - await page.mouse.click( mousePosition.x, mousePosition.y ); - await page.keyboard.up( 'Shift' ); - - const selectedBlocks = await page.$$( - '.wp-block.is-multi-selected' - ); - expect( selectedBlocks.length ).toBe( 2 ); - } ); - it( 'should multi-select blocks without text selection', async () => { - await insertBlock( 'Spacer' ); - // Get the first spacer block element. - const spacerBlock = await page.waitForSelector( - '.wp-block.wp-block-spacer' - ); - const boundingBox = await spacerBlock.boundingBox(); - await page.keyboard.press( 'Enter' ); - await insertBlock( 'Spacer' ); - const mousePosition = { - x: boundingBox.x + boundingBox.width / 2, - y: boundingBox.y + boundingBox.height / 2, - }; - await page.keyboard.down( 'Shift' ); - await page.mouse.click( mousePosition.x, mousePosition.y ); - await page.keyboard.up( 'Shift' ); - const selectedBlocks = await page.$$( - '.wp-block.is-multi-selected' - ); - expect( selectedBlocks.length ).toBe( 2 ); - } ); - } ); - - it( 'should select by dragging into separator', async () => { - await clickBlockAppender(); - await page.keyboard.type( '1' ); - await insertBlock( 'Separator' ); - await page.keyboard.press( 'ArrowUp' ); - - const [ paragraph, hr ] = await page.$$( '[data-type]' ); - const coord1 = await paragraph.clickablePoint(); - const coord2 = await hr.clickablePoint(); - - await page.mouse.move( coord1.x, coord1.y ); - await page.mouse.down(); - await page.mouse.move( coord2.x, coord2.y, { steps: 10 } ); - await page.mouse.up(); - - expect( await getSelectedFlatIndices() ).toEqual( [ 1, 2 ] ); - } ); -} ); diff --git a/test/e2e/specs/editor/various/multi-block-selection.spec.js b/test/e2e/specs/editor/various/multi-block-selection.spec.js index a6562eb27f1dc6..ccfe53b629e6f7 100644 --- a/test/e2e/specs/editor/various/multi-block-selection.spec.js +++ b/test/e2e/specs/editor/various/multi-block-selection.spec.js @@ -1,51 +1,1367 @@ /** * WordPress dependencies */ -const { test } = require( '@wordpress/e2e-test-utils-playwright' ); +const { test, expect } = require( '@wordpress/e2e-test-utils-playwright' ); + +/** @typedef {import('@playwright/test').Page} Page */ +/** @typedef {import('@wordpress/e2e-test-utils-playwright').Editor} Editor */ test.describe( 'Multi-block selection', () => { + test.use( { + multiBlockSelectionUtils: async ( { page, editor }, use ) => { + await use( new MultiBlockSelectionUtils( { page, editor } ) ); + }, + } ); + test.beforeEach( async ( { admin } ) => { await admin.createNewPost(); } ); - test( 'should select with double ctrl+a and speak', async ( {} ) => {} ); - test( 'should only trigger multi-selection when at the end', async ( {} ) => {} ); - test( 'should use selection direction to determine vertical edge', async ( {} ) => {} ); - test( 'should always expand single line selection', async ( {} ) => {} ); - test( 'should allow selecting outer edge if there is no sibling block', async ( {} ) => {} ); - test( 'should select and deselect with shift and arrow keys', async ( {} ) => {} ); - test( 'should deselect with Escape', async ( {} ) => {} ); - test( 'should select with shift + click', async ( {} ) => {} ); - test( 'should properly select a single block even if `shift` was held for the selection', async ( {} ) => {} ); - test( 'should properly select multiple blocks if selected nested blocks belong to different parent', async ( {} ) => {} ); - test( 'should properly select part of nested rich text block while holding shift', async ( {} ) => {} ); - test( 'should select by dragging', async ( {} ) => {} ); - test( 'should select by dragging into nested block', async ( {} ) => {} ); - test( 'should cut and paste', async ( {} ) => {} ); - test( 'should copy and paste', async ( {} ) => {} ); - test( 'should return original focus after failed multi selection attempt', async ( {} ) => {} ); - test( 'should preserve dragged selection on move', async ( {} ) => {} ); - test( 'should clear selection when clicking next to blocks', async ( {} ) => {} ); - test( 'should place the caret at the end of last pasted paragraph (paste to empty )', async ( {} ) => {} ); - test( 'should place the caret at the end of last pasted paragraph (paste mid-block)', async ( {} ) => {} ); - test( 'should place the caret at the end of last pasted paragraph (replace)', async ( {} ) => {} ); - test( 'should set attributes for multiple paragraphs', async ( {} ) => {} ); - test( 'should copy multiple blocks', async ( {} ) => {} ); - test( 'should not multi select single block', async ( {} ) => {} ); - test( 'should gradually multi-select', async ( {} ) => {} ); - test( 'should multi-select from within the list block', async ( {} ) => {} ); - test( 'should select all from empty selection', async ( {} ) => {} ); - test( 'should select title if the cursor is on title', async ( {} ) => {} ); - test( 'should multi-select in the ListView component with shift + click', async ( {} ) => {} ); - test( 'should multi-select in the ListView component with shift + up and down keys', async ( {} ) => {} ); - test( 'should forward delete across blocks', async ( {} ) => {} ); - test( 'should write over selection', async ( {} ) => {} ); - test( 'should handle Enter across blocks', async ( {} ) => {} ); - test( 'should select separator (single element block)', async ( {} ) => {} ); - test( 'should partially select with shift + click', async ( {} ) => {} ); + test( 'should select with double ctrl+a and speak', async ( { + page, + editor, + pageUtils, + multiBlockSelectionUtils, + } ) => { + for ( let i = 1; i <= 3; i += 1 ) { + await editor.insertBlock( { + name: 'core/paragraph', + attributes: { content: `${ i }` }, + } ); + } + + // Multiselect via keyboard. + await pageUtils.pressKeyWithModifier( 'primary', 'a' ); + await pageUtils.pressKeyWithModifier( 'primary', 'a' ); + + await expect + .poll( multiBlockSelectionUtils.getSelectedFlatIndices ) + .toEqual( [ 1, 2, 3 ] ); + + await expect( page.locator( '[aria-live="assertive"]' ) ).toHaveText( + '3 blocks selected.' + ); + } ); + + // See #14448: an incorrect buffer may trigger multi-selection too soon. + test( 'should only trigger multi-selection when at the end', async ( { + page, + editor, + pageUtils, + } ) => { + // Create a paragraph with four lines. + await editor.insertBlock( { + name: 'core/paragraph', + attributes: { content: '1.
2.
3.
4.' }, + } ); + // Create a second block and focus it. + await editor.insertBlock( { + name: 'core/paragraph', + attributes: { content: '' }, + } ); + await editor.canvas + .getByRole( 'document', { name: /Empty block/i } ) + .click(); + // Move to the middle of the first line. + await pageUtils.pressKeyTimes( 'ArrowUp', 4 ); + await page.keyboard.press( 'ArrowRight' ); + // Select mid line one to mid line four. + await pageUtils.pressKeyTimes( 'Shift+ArrowDown', 3 ); + // Delete the text to see if the selection was correct. + await page.keyboard.press( 'Backspace' ); + + await expect.poll( editor.getBlocks ).toMatchObject( [ + { name: 'core/paragraph', attributes: { content: '1.' } }, + { name: 'core/paragraph', attributes: { content: '' } }, + ] ); + } ); + + test( 'should use selection direction to determine vertical edge', async ( { + page, + editor, + } ) => { + await editor.canvas + .getByRole( 'button', { name: 'Add default block' } ) + .click(); + await page.keyboard.type( '1' ); + await page.keyboard.press( 'Shift+Enter' ); + await page.keyboard.press( 'Enter' ); + await page.keyboard.type( '3' ); + await page.keyboard.press( 'ArrowUp' ); + await page.keyboard.type( '2' ); + + await page.keyboard.press( 'Shift+ArrowUp' ); + await page.keyboard.press( 'Shift+ArrowDown' ); + + // Should type at the end of the paragraph. + await page.keyboard.type( '$' ); + + await expect.poll( editor.getBlocks ).toMatchObject( [ + { name: 'core/paragraph', attributes: { content: '1
2$' } }, + { name: 'core/paragraph', attributes: { content: '3' } }, + ] ); + } ); + + test( 'should always expand single line selection', async ( { + page, + editor, + multiBlockSelectionUtils, + } ) => { + await editor.canvas + .getByRole( 'button', { name: 'Add default block' } ) + .click(); + await page.keyboard.press( 'Enter' ); + await page.keyboard.type( '12' ); + await page.keyboard.press( 'ArrowLeft' ); + await page.keyboard.press( 'Shift+ArrowLeft' ); + await page.keyboard.press( 'Shift+ArrowUp' ); + await expect( multiBlockSelectionUtils.assertNativeSelection ).toPass(); + + await page.keyboard.press( 'Backspace' ); + await expect.poll( editor.getBlocks ).toMatchObject( [ + { + name: 'core/paragraph', + attributes: { content: '2' }, + }, + ] ); + } ); + + test( 'should allow selecting outer edge if there is no sibling block', async ( { + page, + editor, + } ) => { + await editor.canvas + .getByRole( 'button', { name: 'Add default block' } ) + .click(); + await page.keyboard.type( '1' ); + await page.keyboard.press( 'Shift+ArrowUp' ); + // This should replace the content. + await page.keyboard.type( '2' ); + + await expect.poll( editor.getBlocks ).toMatchObject( [ + { + name: 'core/paragraph', + attributes: { content: '2' }, + }, + ] ); + } ); + + test( 'should select and deselect with shift and arrow keys', async ( { + page, + editor, + multiBlockSelectionUtils, + } ) => { + for ( let i = 1; i <= 5; i += 1 ) { + await editor.insertBlock( { + name: 'core/paragraph', + attributes: { content: `${ i }` }, + } ); + } + await editor.canvas + .getByRole( 'document', { name: 'Paragraph block' } ) + .filter( { hasText: '3' } ) + .click(); + + await page.keyboard.press( 'Shift+ArrowDown' ); + await expect( multiBlockSelectionUtils.assertNativeSelection ).toPass(); + await expect + .poll( multiBlockSelectionUtils.getSelectedFlatIndices ) + .toEqual( [ 3, 4 ] ); + + await page.keyboard.press( 'Shift+ArrowDown' ); + await expect( multiBlockSelectionUtils.assertNativeSelection ).toPass(); + await expect + .poll( multiBlockSelectionUtils.getSelectedFlatIndices ) + .toEqual( [ 3, 4, 5 ] ); + + await page.keyboard.press( 'Shift+ArrowUp' ); + await expect( multiBlockSelectionUtils.assertNativeSelection ).toPass(); + await expect + .poll( multiBlockSelectionUtils.getSelectedFlatIndices ) + .toEqual( [ 3, 4 ] ); + + await page.keyboard.press( 'Shift+ArrowUp' ); + await expect( multiBlockSelectionUtils.assertNativeSelection ).toPass(); + await expect + .poll( multiBlockSelectionUtils.getSelectedFlatIndices ) + .toEqual( [ 3 ] ); + + await page.keyboard.press( 'Shift+ArrowUp' ); + await expect( multiBlockSelectionUtils.assertNativeSelection ).toPass(); + await expect + .poll( multiBlockSelectionUtils.getSelectedFlatIndices ) + .toEqual( [ 2, 3 ] ); + + await page.keyboard.press( 'Shift+ArrowUp' ); + await expect( multiBlockSelectionUtils.assertNativeSelection ).toPass(); + await expect + .poll( multiBlockSelectionUtils.getSelectedFlatIndices ) + .toEqual( [ 1, 2, 3 ] ); + + await page.keyboard.press( 'Shift+ArrowDown' ); + await expect( multiBlockSelectionUtils.assertNativeSelection ).toPass(); + await expect + .poll( multiBlockSelectionUtils.getSelectedFlatIndices ) + .toEqual( [ 2, 3 ] ); + + await page.keyboard.press( 'Shift+ArrowDown' ); + await expect( multiBlockSelectionUtils.assertNativeSelection ).toPass(); + await expect + .poll( multiBlockSelectionUtils.getSelectedFlatIndices ) + .toEqual( [ 3 ] ); + } ); + + test( 'should deselect with Escape', async ( { + page, + editor, + pageUtils, + multiBlockSelectionUtils, + } ) => { + for ( let i = 1; i <= 2; i += 1 ) { + await editor.insertBlock( { + name: 'core/paragraph', + attributes: { content: `${ i }` }, + } ); + } + await pageUtils.pressKeyWithModifier( 'primary', 'a' ); + await pageUtils.pressKeyWithModifier( 'primary', 'a' ); + + await expect + .poll( multiBlockSelectionUtils.getSelectedFlatIndices ) + .toEqual( [ 1, 2 ] ); + + await page.keyboard.press( 'Escape' ); + + // FIXME: This doesn't seem to work anymore. + // await expect + // .poll( multiBlockSelectionUtils.getSelectedFlatIndices ) + // .toEqual( [] ); + } ); + + test( 'should select with shift + click', async ( { + page, + editor, + multiBlockSelectionUtils, + } ) => { + await editor.canvas + .getByRole( 'button', { name: 'Add default block' } ) + .click(); + await page.keyboard.type( '1' ); + await page.keyboard.press( 'Enter' ); + await page.keyboard.type( '2' ); + + await editor.canvas + .getByRole( 'document', { name: 'Paragraph block' } ) + .filter( { hasText: '1' } ) + .click( { modifiers: [ 'Shift' ] } ); + + await expect( multiBlockSelectionUtils.assertNativeSelection ).toPass(); + await expect + .poll( multiBlockSelectionUtils.getSelectedFlatIndices ) + .toEqual( [ 1, 2 ] ); + + // Group the blocks and test that multiselection also works for nested + // blocks. Checks for regressions of + // https://github.com/WordPress/gutenberg/issues/32056 + await page + .getByRole( 'toolbar', { name: 'Block tools' } ) + .getByRole( 'button', { name: 'Group' } ) + .click(); + await editor.canvas + .getByRole( 'document', { name: 'Paragraph block' } ) + .filter( { hasText: '1' } ) + .click(); + await editor.canvas + .getByRole( 'document', { name: 'Paragraph block' } ) + .filter( { hasText: '2' } ) + .click( { modifiers: [ 'Shift' ] } ); + + await expect( multiBlockSelectionUtils.assertNativeSelection ).toPass(); + await expect + .poll( multiBlockSelectionUtils.getSelectedFlatIndices ) + .toEqual( [ 2, 3 ] ); + } ); + + // @see https://github.com/WordPress/gutenberg/issues/34118 + test( 'should properly select a single block even if `shift` was held for the selection', async ( { + page, + editor, + pageUtils, + } ) => { + await editor.insertBlock( { + name: 'core/paragraph', + attributes: { content: 'test' }, + } ); + + await page.getByRole( 'button', { name: 'Save draft' } ).click(); + await expect( + page + .getByRole( 'button', { name: 'Dismiss this notice' } ) + .filter( { hasText: 'Draft saved' } ) + ).toBeVisible(); + await page.reload(); + + await editor.canvas + .getByRole( 'document', { name: 'Paragraph block' } ) + .click( { modifiers: [ 'Shift' ] } ); + await pageUtils.pressKeyWithModifier( 'primary', 'a' ); + await page.keyboard.type( 'new content' ); + + await expect.poll( editor.getBlocks ).toMatchObject( [ + { + name: 'core/paragraph', + attributes: { content: 'new content' }, + }, + ] ); + } ); + + test( 'should properly select multiple blocks if selected nested blocks belong to different parent', async ( { + editor, + multiBlockSelectionUtils, + } ) => { + await editor.insertBlock( { + name: 'core/group', + innerBlocks: [ + { name: 'core/paragraph', attributes: { content: 'first' } }, + { name: 'core/paragraph', attributes: { content: 'group' } }, + ], + } ); + await editor.insertBlock( { + name: 'core/group', + innerBlocks: [ + { name: 'core/paragraph', attributes: { content: 'second' } }, + { name: 'core/paragraph', attributes: { content: 'group' } }, + ], + } ); + + await editor.canvas + .getByRole( 'document', { name: 'Paragraph block' } ) + .filter( { hasText: 'group' } ) + .nth( 1 ) + .click(); + await editor.canvas + .getByRole( 'document', { name: 'Paragraph block' } ) + .filter( { hasText: 'first' } ) + .click( { modifiers: [ 'Shift' ] } ); + + await expect + .poll( multiBlockSelectionUtils.getSelectedFlatIndices ) + .toEqual( [ 1, 4 ] ); + } ); + + test( 'should properly select part of nested rich text block while holding shift', async ( { + page, + editor, + } ) => { + await editor.insertBlock( { + name: 'core/group', + innerBlocks: [ + { + name: 'core/paragraph', + attributes: { content: 'rich text in group' }, + }, + ], + } ); + + const paragraphBlock = editor.canvas.getByRole( 'document', { + name: 'Paragraph block', + } ); + const { height } = await paragraphBlock.boundingBox(); + await paragraphBlock.click( { position: { x: 0, y: height / 2 } } ); + await paragraphBlock.click( { + position: { x: 20, y: height / 2 }, + modifiers: [ 'Shift' ], + } ); + await page.keyboard.type( 'hi' ); + + await expect.poll( editor.getBlocks ).toMatchObject( [ + { + name: 'core/group', + innerBlocks: [ + { + name: 'core/paragraph', + attributes: { content: 'hih text in group' }, + }, + ], + }, + ] ); + } ); + + test( 'should select by dragging', async ( { + page, + editor, + multiBlockSelectionUtils, + } ) => { + await editor.insertBlock( { + name: 'core/paragraph', + attributes: { content: '1' }, + } ); + await editor.insertBlock( { + name: 'core/paragraph', + attributes: { content: '2' }, + } ); + // To hide the block tool bar. + await page.keyboard.press( 'ArrowDown' ); + + const [ paragraph1, paragraph2 ] = await editor.canvas + .getByRole( 'document', { name: 'Paragraph block' } ) + .all(); + + await paragraph1.hover(); + await page.mouse.down(); + await paragraph2.hover(); + await page.mouse.up(); + + await expect( multiBlockSelectionUtils.assertNativeSelection ).toPass(); + await expect + .poll( multiBlockSelectionUtils.getSelectedFlatIndices ) + .toEqual( [ 1, 2 ] ); + } ); + + test( 'should select by dragging into nested block', async ( { + page, + editor, + multiBlockSelectionUtils, + } ) => { + await editor.insertBlock( { + name: 'core/paragraph', + attributes: { content: '1' }, + } ); + await editor.insertBlock( { + name: 'core/group', + innerBlocks: [ + { + name: 'core/paragraph', + attributes: { content: '2' }, + }, + ], + } ); + // To hide the block tool bar. + await page.keyboard.press( 'ArrowDown' ); + await page.keyboard.press( 'ArrowDown' ); + + const [ paragraph1, paragraph2 ] = await editor.canvas + .getByRole( 'document', { name: 'Paragraph block' } ) + .all(); + + await paragraph1.hover(); + await page.mouse.down(); + await paragraph2.hover(); + await page.mouse.up(); + + await expect + .poll( multiBlockSelectionUtils.getSelectedFlatIndices ) + .toEqual( [ 1, 2 ] ); + } ); + + test( 'should cut and paste', async ( { editor, pageUtils } ) => { + await editor.insertBlock( { + name: 'core/paragraph', + attributes: { content: '1' }, + } ); + await editor.insertBlock( { + name: 'core/paragraph', + attributes: { content: '2' }, + } ); + + await pageUtils.pressKeyWithModifier( 'primary', 'a' ); + await pageUtils.pressKeyWithModifier( 'primary', 'a' ); + await pageUtils.pressKeyWithModifier( 'primary', 'x' ); + + await expect.poll( editor.getBlocks ).toEqual( [] ); + + await pageUtils.pressKeyWithModifier( 'primary', 'v' ); + + await expect.poll( editor.getBlocks ).toMatchObject( [ + { name: 'core/paragraph', attributes: { content: '1' } }, + { name: 'core/paragraph', attributes: { content: '2' } }, + ] ); + } ); + + test( 'should copy and paste', async ( { page, editor, pageUtils } ) => { + await editor.insertBlock( { + name: 'core/paragraph', + attributes: { content: 'first paragraph' }, + } ); + await editor.insertBlock( { + name: 'core/paragraph', + attributes: { content: 'second paragraph' }, + } ); + + await pageUtils.pressKeyWithModifier( 'primary', 'a' ); + await pageUtils.pressKeyWithModifier( 'primary', 'a' ); + await pageUtils.pressKeyWithModifier( 'primary', 'c' ); + + await page.keyboard.press( 'Backspace' ); + await expect + .poll( editor.getBlocks, 'should select all blocks after copying' ) + .toEqual( [] ); + + await pageUtils.pressKeyWithModifier( 'primary', 'v' ); + await expect + .poll( editor.getBlocks, 'should copy and paste multiple blocks' ) + .toMatchObject( [ + { attributes: { content: 'first paragraph' } }, + { attributes: { content: 'second paragraph' } }, + ] ); + + await page.keyboard.type( '$' ); + await expect + .poll( + editor.getBlocks, + 'should place the caret at the end of last pasted paragraph' + ) + .toMatchObject( [ + { attributes: { content: 'first paragraph' } }, + { attributes: { content: 'second paragraph$' } }, + ] ); + + await pageUtils.pressKeyWithModifier( 'primary', 'a' ); + await pageUtils.pressKeyWithModifier( 'primary', 'a' ); + await pageUtils.pressKeyWithModifier( 'primary', 'v' ); + await page.keyboard.type( '$' ); + await expect + .poll( editor.getBlocks, 'should replace blocks' ) + .toMatchObject( [ + { attributes: { content: 'first paragraph' } }, + { attributes: { content: 'second paragraph$' } }, + ] ); + await page.keyboard.press( 'Backspace' ); + + for ( let i = 0; i < 3; i += 1 ) { + await page.keyboard.press( 'ArrowLeft' ); + } + await pageUtils.pressKeyWithModifier( 'primary', 'v' ); + await page.keyboard.type( '$' ); + await expect + .poll( editor.getBlocks, 'should paste mid-block' ) + .toMatchObject( [ + { attributes: { content: 'first paragraph' } }, + { attributes: { content: 'second paragr' } }, + { attributes: { content: 'first paragraph' } }, + { attributes: { content: 'second paragraph$' } }, + { attributes: { content: 'aph' } }, + ] ); + } ); + + test( 'should return original focus after failed multi selection attempt', async ( { + page, + editor, + } ) => { + await editor.insertBlock( { + name: 'core/paragraph', + attributes: { content: '1' }, + } ); + await editor.insertBlock( { + name: 'core/paragraph', + attributes: { content: '2' }, + } ); + + const [ paragraph1, paragraph2 ] = await editor.canvas + .getByRole( 'document', { name: 'Paragraph block' } ) + .all(); + const { height } = await paragraph2.boundingBox(); + + // Move caret to the start of the second block. + await paragraph2.click( { position: { x: 0, y: height / 2 } } ); + await page.mouse.down(); + await paragraph1.hover( { + position: { x: -5, y: height / 2 }, + // Use force since it's outside the bounding box of the element. + force: true, + } ); + await page.mouse.up(); + + // Only "1" should be deleted. + await page.keyboard.press( 'Backspace' ); + + await expect.poll( editor.getBlocks ).toMatchObject( [ + { + name: 'core/paragraph', + attributes: { content: '2' }, + }, + ] ); + } ); + + test( 'should preserve dragged selection on move', async ( { + page, + editor, + multiBlockSelectionUtils, + } ) => { + for ( let i = 1; i <= 3; i += 1 ) { + await editor.insertBlock( { + name: 'core/paragraph', + attributes: { content: `${ i }` }, + } ); + } + + const [ , paragraph2, paragraph3 ] = await editor.canvas + .getByRole( 'document', { name: 'Paragraph block' } ) + .all(); + + // Focus the last paragraph block and hide the block toolbar. + await paragraph3.click(); + await page.keyboard.press( 'ArrowRight' ); + + await paragraph3.hover(); + await page.mouse.down(); + await paragraph2.hover(); + await page.mouse.up(); + + await expect( multiBlockSelectionUtils.assertNativeSelection ).toPass(); + await expect + .poll( multiBlockSelectionUtils.getSelectedFlatIndices ) + .toEqual( [ 2, 3 ] ); + + await editor.showBlockToolbar(); + await page + .getByRole( 'toolbar', { name: 'Block tools' } ) + .getByRole( 'button', { name: 'Move up' } ) + .click(); + + await expect( multiBlockSelectionUtils.assertNativeSelection ).toPass(); + await expect + .poll( multiBlockSelectionUtils.getSelectedFlatIndices ) + .toEqual( [ 1, 2 ] ); + + await expect.poll( editor.getBlocks ).toMatchObject( [ + { name: 'core/paragraph', attributes: { content: '2' } }, + { name: 'core/paragraph', attributes: { content: '3' } }, + { name: 'core/paragraph', attributes: { content: '1' } }, + ] ); + } ); + + test( 'should clear selection when clicking next to blocks', async ( { + page, + editor, + multiBlockSelectionUtils, + } ) => { + await editor.canvas + .getByRole( 'button', { + name: 'Add default block', + } ) + .click(); + await page.keyboard.type( '1' ); + await page.keyboard.press( 'Enter' ); + await page.keyboard.type( '2' ); + await page.keyboard.press( 'Shift+ArrowUp' ); + await expect( multiBlockSelectionUtils.assertNativeSelection ).toPass(); + await expect + .poll( multiBlockSelectionUtils.getSelectedFlatIndices ) + .toEqual( [ 1, 2 ] ); + + const paragraph1 = editor.canvas + .getByRole( 'document', { + name: 'Paragraph block', + } ) + .filter( { hasText: '1' } ); + await paragraph1.click( { + position: { x: -1, y: 0 }, + // Use force since it's outside the bounding box of the element. + force: true, + } ); + + await expect + .poll( () => + page.evaluate( () => window.getSelection().rangeCount ) + ) + .toBe( 0 ); + await expect + .poll( multiBlockSelectionUtils.getSelectedFlatIndices ) + .toEqual( [] ); + + await expect.poll( editor.getBlocks ).toMatchObject( [ + { name: 'core/paragraph', attributes: { content: '1' } }, + { name: 'core/paragraph', attributes: { content: '2' } }, + ] ); + } ); + + test( 'should set attributes for multiple paragraphs', async ( { + page, + editor, + pageUtils, + } ) => { + for ( let i = 1; i <= 2; i += 1 ) { + await editor.insertBlock( { + name: 'core/paragraph', + attributes: { content: `${ i }` }, + } ); + } + await pageUtils.pressKeyWithModifier( 'primary', 'a' ); + await pageUtils.pressKeyWithModifier( 'primary', 'a' ); + + await page + .getByRole( 'toolbar', { name: 'Block tools' } ) + .getByRole( 'button', { name: 'Align text' } ) + .click(); + await page + .getByRole( 'menu', { name: 'Align text' } ) + .getByRole( 'menuitemradio', { name: 'Align text center' } ) + .click(); + + await expect + .poll( editor.getBlocks ) + .toMatchObject( [ + { attributes: { align: 'center', content: '1' } }, + { attributes: { align: 'center', content: '2' } }, + ] ); + } ); + + // Previously we would unexpectedly duplicate the block on Enter. + test( 'should not multi select single block', async ( { + page, + editor, + pageUtils, + } ) => { + await editor.canvas + .getByRole( 'button', { name: 'Add default block' } ) + .click(); + await page.keyboard.type( '1' ); + + await pageUtils.pressKeyWithModifier( 'primary', 'a' ); + await pageUtils.pressKeyWithModifier( 'primary', 'a' ); + await page.keyboard.press( 'Enter' ); + + await expect.poll( editor.getBlocks ).toMatchObject( [ + { name: 'core/paragraph', attributes: { content: '' } }, + { name: 'core/paragraph', attributes: { content: '' } }, + ] ); + } ); + + test( 'should gradually multi-select', async ( { + page, + editor, + pageUtils, + multiBlockSelectionUtils, + } ) => { + await editor.insertBlock( { + name: 'core/columns', + innerBlocks: [ + { + name: 'core/column', + innerBlocks: [ + { + name: 'core/paragraph', + attributes: { content: '1' }, + }, + { + name: 'core/paragraph', + attributes: { content: '2' }, + }, + ], + }, + { name: 'core/column' }, + ], + } ); + // Focus the last paragraph block. + await editor.canvas + .getByRole( 'document', { name: 'Paragraph block' } ) + .nth( 1 ) + .click(); + + await pageUtils.pressKeyWithModifier( 'primary', 'a' ); + await pageUtils.pressKeyWithModifier( 'primary', 'a' ); + + await expect + .poll( multiBlockSelectionUtils.getSelectedBlocks ) + .toMatchObject( [ + { name: 'core/paragraph' }, + { name: 'core/paragraph' }, + ] ); + + await pageUtils.pressKeyWithModifier( 'primary', 'a' ); + + await expect + .poll( multiBlockSelectionUtils.getSelectedBlocks ) + .toMatchObject( [ { name: 'core/column' } ] ); + + await pageUtils.pressKeyWithModifier( 'primary', 'a' ); + + await expect + .poll( multiBlockSelectionUtils.getSelectedBlocks ) + .toMatchObject( [ + { name: 'core/column' }, + { name: 'core/column' }, + ] ); + + await page.keyboard.press( 'Backspace' ); + + // Expect both columns to be deleted. + await expect + .poll( editor.getBlocks ) + .toMatchObject( [ { name: 'core/columns', innerBlocks: [] } ] ); + } ); + + test( 'should multi-select from within the list block', async ( { + editor, + pageUtils, + multiBlockSelectionUtils, + } ) => { + await editor.insertBlock( { + name: 'core/paragraph', + attributes: { content: '1' }, + } ); + await editor.insertBlock( { + name: 'core/list', + innerBlocks: [ + { + name: 'core/list-item', + attributes: { content: '1' }, + }, + ], + } ); + // Focus on the list item. + await editor.canvas + .getByRole( 'document', { name: 'Block: List item' } ) + .getByRole( 'textbox' ) + .click(); + + await pageUtils.pressKeyWithModifier( 'primary', 'a' ); + await pageUtils.pressKeyWithModifier( 'primary', 'a' ); + await pageUtils.pressKeyWithModifier( 'primary', 'a' ); + + await expect + .poll( multiBlockSelectionUtils.getSelectedBlocks ) + .toContainEqual( + expect.objectContaining( { name: 'core/paragraph' } ) + ); + } ); + + test( 'should select all from empty selection', async ( { + page, + editor, + pageUtils, + multiBlockSelectionUtils, + } ) => { + for ( let i = 1; i <= 2; i += 1 ) { + await editor.insertBlock( { + name: 'core/paragraph', + attributes: { content: `${ i }` }, + } ); + } + + // Clear the selected block. + await page.keyboard.press( 'Escape' ); + await page.keyboard.press( 'Escape' ); + + await expect + .poll( multiBlockSelectionUtils.getSelectedBlocks ) + .toEqual( [] ); + + await pageUtils.pressKeyWithModifier( 'primary', 'a' ); + + await page.keyboard.press( 'Backspace' ); + + // Expect both paragraphs to be deleted. + await expect.poll( editor.getBlocks ).toEqual( [] ); + } ); + + test( 'should select title if the cursor is on title', async ( { + page, + editor, + pageUtils, + multiBlockSelectionUtils, + } ) => { + for ( let i = 1; i <= 2; i += 1 ) { + await editor.insertBlock( { + name: 'core/paragraph', + attributes: { content: `${ i }` }, + } ); + } + + await editor.canvas + .getByRole( 'textbox', { name: 'Add title' } ) + .type( 'Post title' ); + + await pageUtils.pressKeyWithModifier( 'primary', 'a' ); + + await expect + .poll( multiBlockSelectionUtils.getSelectedBlocks ) + .toEqual( [] ); + await expect + .poll( () => + page.evaluate( () => window.getSelection().toString() ) + ) + .toBe( 'Post title' ); + } ); + + test( 'should multi-select in the ListView component with shift + click', async ( { + page, + editor, + multiBlockSelectionUtils, + pageUtils, + } ) => { + for ( let i = 1; i <= 4; i += 1 ) { + await editor.insertBlock( { + name: 'core/paragraph', + attributes: { content: `${ i }` }, + } ); + } + + await page + .getByRole( 'toolbar', { name: 'Document tools' } ) + .getByRole( 'button', { name: 'Document Overview' } ) + .click(); + + const listView = page.getByRole( 'treegrid', { + name: 'Block navigation structure', + } ); + const navButtons = listView.getByRole( 'gridcell', { + name: 'Paragraph link', + } ); + + await navButtons.nth( 1 ).click(); + await expect + .poll( + multiBlockSelectionUtils.getSelectedFlatIndices, + 'Clicking on the second list item should result in the second block being selected.' + ) + .toEqual( [ 2 ] ); + + await navButtons.nth( 3 ).click( { modifiers: [ 'Shift' ] } ); + await expect + .poll( + multiBlockSelectionUtils.getSelectedFlatIndices, + 'Shift clicking the fourth list item should result in blocks 2 through 4 being selected.' + ) + .toEqual( [ 2, 3, 4 ] ); + + await navButtons.nth( 0 ).click( { modifiers: [ 'Shift' ] } ); + await expect + .poll( + multiBlockSelectionUtils.getSelectedFlatIndices, + 'With the shift key still held down, clicking the first block should result in the first two blocks being selected.' + ) + .toEqual( [ 1, 2 ] ); + + await navButtons.nth( 3 ).click(); + await expect + .poll( + multiBlockSelectionUtils.getSelectedFlatIndices, + 'With the shift key up, clicking the fourth block should result in only that block being selected.' + ) + .toEqual( [ 4 ] ); + + // Move focus to the list view link to prepare for the keyboard navigation. + await navButtons.nth( 3 ).click(); + await expect( + navButtons.nth( 3 ).getByRole( 'link', { includeHidden: true } ) + ).toBeFocused(); + // Press Up twice to highlight the second block. + await pageUtils.pressKeyTimes( 'ArrowUp', 2 ); + + await page.keyboard.press( 'Shift+ArrowDown' ); + await expect + .poll( + multiBlockSelectionUtils.getSelectedFlatIndices, + 'Shift + press Down to select the 2nd and 3rd blocks.' + ) + .toEqual( [ 2, 3 ] ); + + await page.keyboard.press( 'Shift+ArrowDown' ); + await expect + .poll( + multiBlockSelectionUtils.getSelectedFlatIndices, + 'Press Down once more to also select the 4th block.' + ) + .toEqual( [ 2, 3, 4 ] ); + + await pageUtils.pressKeyTimes( 'Shift+ArrowUp', 3 ); + await expect + .poll( + multiBlockSelectionUtils.getSelectedFlatIndices, + 'Press Up three times to adjust the selection to only include the first two blocks.' + ) + .toEqual( [ 1, 2 ] ); + + // Navigate to the bottom of the list of blocks. + await pageUtils.pressKeyTimes( 'ArrowDown', 3 ); + + // This tests that shift selecting blocks by keyboard that are not adjacent + // to an existing selection resets the selection. + await page.keyboard.press( 'Shift+ArrowUp' ); + await expect + .poll( + multiBlockSelectionUtils.getSelectedFlatIndices, + 'Shift + press UP to select the 3rd and 4th blocks.' + ) + .toEqual( [ 3, 4 ] ); + } ); + + test( 'should forward delete across blocks', async ( { + page, + editor, + pageUtils, + } ) => { + await editor.insertBlock( { + name: 'core/paragraph', + attributes: { content: '1[' }, + } ); + await editor.insertBlock( { + name: 'core/paragraph', + attributes: { content: '.' }, + } ); + await editor.insertBlock( { + name: 'core/heading', + attributes: { level: 2, content: ']2' }, + } ); + // Focus the heading block. + await editor.canvas + .getByRole( 'document', { name: 'Block: Heading' } ) + .click(); + + await page.keyboard.press( 'ArrowLeft' ); + // Select everything between []. + await pageUtils.pressKeyTimes( 'Shift+ArrowLeft', 5 ); + + await page.keyboard.press( 'Delete' ); + + // Ensure selection is in the correct place. + await page.keyboard.type( '$' ); + await expect.poll( editor.getBlocks ).toMatchObject( [ + { + name: 'core/heading', + attributes: { level: 2, content: '1$2' }, + }, + ] ); + } ); + + test( 'should write over selection', async ( { + page, + editor, + pageUtils, + } ) => { + await editor.canvas + .getByRole( 'button', { name: 'Add default block' } ) + .click(); + await page.keyboard.type( '1[' ); + await page.keyboard.press( 'Enter' ); + await page.keyboard.type( ']2' ); + await page.keyboard.press( 'ArrowLeft' ); + // Select everything between []. + await pageUtils.pressKeyTimes( 'Shift+ArrowLeft', 3 ); + + // Ensure selection is in the correct place. + await page.keyboard.type( '$' ); + await expect.poll( editor.getBlocks ).toMatchObject( [ + { + name: 'core/paragraph', + attributes: { content: '1$2' }, + }, + ] ); + } ); + + test( 'should handle Enter across blocks', async ( { + page, + editor, + pageUtils, + } ) => { + await editor.insertBlock( { + name: 'core/paragraph', + attributes: { content: '1[' }, + } ); + await editor.insertBlock( { + name: 'core/paragraph', + attributes: { content: '.' }, + } ); + await editor.insertBlock( { + name: 'core/heading', + attributes: { level: 2, content: ']2' }, + } ); + // Focus the heading block. + await editor.canvas + .getByRole( 'document', { name: 'Block: Heading' } ) + .click(); + await page.keyboard.press( 'ArrowLeft' ); + // Select everything between []. + await pageUtils.pressKeyTimes( 'Shift+ArrowLeft', 5 ); + + await page.keyboard.press( 'Enter' ); + + // Ensure selection is in the correct place. + await page.keyboard.type( '$' ); + await expect.poll( editor.getBlocks ).toMatchObject( [ + { + name: 'core/paragraph', + attributes: { content: '1' }, + }, + { + name: 'core/paragraph', + attributes: { content: '$' }, + }, + { + name: 'core/heading', + attributes: { level: 2, content: '2' }, + }, + ] ); + } ); + + test( 'should select separator (single element block)', async ( { + page, + editor, + pageUtils, + } ) => { + await editor.insertBlock( { + name: 'core/paragraph', + attributes: { content: 'a' }, + } ); + await editor.insertBlock( { + name: 'core/paragraph', + attributes: { content: '' }, + } ); + await editor.insertBlock( { name: 'core/separator' } ); + + // Focus and move the caret to the right of the first paragraph. + await editor.canvas + .getByRole( 'document', { name: 'Paragraph block' } ) + .filter( { hasText: 'a' } ) + .click(); + + await pageUtils.pressKeyTimes( 'Shift+ArrowDown', 2 ); + await page.keyboard.press( 'Backspace' ); + + // Ensure selection is in the correct place. + await page.keyboard.type( '$' ); + await expect.poll( editor.getBlocks ).toMatchObject( [ + { + name: 'core/paragraph', + attributes: { content: '$' }, + }, + ] ); + } ); + + test( 'should partially select with shift + click', async ( { + page, + editor, + } ) => { + await editor.insertBlock( { + name: 'core/paragraph', + attributes: { content: '1[' }, + } ); + await editor.insertBlock( { + name: 'core/paragraph', + attributes: { content: ']2' }, + } ); + // Focus and move the caret to the end. + await editor.canvas + .getByRole( 'document', { name: 'Paragraph block' } ) + .filter( { hasText: ']2' } ) + .click(); + + await page.keyboard.press( 'ArrowLeft' ); + const strongText = page.getByText( '1', { exact: true } ); + const strongBox = await strongText.boundingBox(); + await strongText.click( { + // Ensure clicking on the right half of the element. + position: { x: strongBox.width, y: strongBox.height / 2 }, + modifiers: [ 'Shift' ], + } ); + await page.keyboard.press( 'Backspace' ); + + // Ensure selection is in the correct place. + await page.keyboard.type( '$' ); + await expect.poll( editor.getBlocks ).toMatchObject( [ + { + name: 'core/paragraph', + attributes: { content: '1$2' }, + }, + ] ); + } ); + test.describe( 'shift+click multi-selection', () => { - test( 'should multi-select block with text selection and a block without text selection', async ( {} ) => {} ); - test( 'should multi-select blocks without text selection', async ( {} ) => {} ); + test( 'should multi-select block with text selection and a block without text selection', async ( { + page, + editor, + multiBlockSelectionUtils, + } ) => { + await editor.insertBlock( { + name: 'core/paragraph', + attributes: { content: '' }, + } ); + await editor.insertBlock( { + name: 'core/paragraph', + attributes: { content: 'hi' }, + } ); + await editor.insertBlock( { name: 'core/spacer' } ); + await page.keyboard.press( 'ArrowUp' ); + + await editor.canvas + .getByRole( 'document', { name: 'Block: Spacer' } ) + .click( { modifiers: [ 'Shift' ] } ); + + await expect + .poll( multiBlockSelectionUtils.getSelectedBlocks ) + .toMatchObject( [ + { name: 'core/paragraph', attributes: { content: 'hi' } }, + { name: 'core/spacer' }, + ] ); + } ); + + test( 'should multi-select blocks without text selection', async ( { + editor, + multiBlockSelectionUtils, + } ) => { + await editor.insertBlock( { name: 'core/spacer' } ); + await editor.insertBlock( { name: 'core/spacer' } ); + + const spacerBlocks = editor.canvas.getByRole( 'document', { + name: 'Block: Spacer', + } ); + + await spacerBlocks.nth( 1 ).click(); + await spacerBlocks.nth( 0 ).click( { modifiers: [ 'Shift' ] } ); + + await expect + .poll( multiBlockSelectionUtils.getSelectedBlocks ) + .toMatchObject( [ + { name: 'core/spacer' }, + { name: 'core/spacer' }, + ] ); + } ); + } ); + + test( 'should select by dragging into separator', async ( { + page, + editor, + multiBlockSelectionUtils, + } ) => { + await editor.insertBlock( { + name: 'core/paragraph', + attributes: { content: '1' }, + } ); + await editor.insertBlock( { name: 'core/separator' } ); + // Hide the block toolbar. + await page.keyboard.press( 'ArrowUp' ); + + await editor.canvas + .getByRole( 'document', { name: 'Paragraph block' } ) + .hover(); + await page.mouse.down(); + await editor.canvas + .getByRole( 'document', { name: 'Block: Separator' } ) + .hover(); + await page.mouse.up(); + + await expect + .poll( multiBlockSelectionUtils.getSelectedFlatIndices ) + .toEqual( [ 1, 2 ] ); } ); - test( 'should select by dragging into separator', async ( {} ) => {} ); } ); + +class MultiBlockSelectionUtils { + /** @type {Page} */ + #page; + /** @type {Editor} */ + #editor; + + constructor( { page, editor } ) { + this.#page = page; + this.#editor = editor; + } + + getSelectedBlocks = async () => { + return await this.#page.evaluate( () => { + const { + hasMultiSelection, + hasSelectedBlock, + getMultiSelectedBlocks, + getSelectedBlock, + } = window.wp.data.select( 'core/block-editor' ); + if ( hasMultiSelection() ) { + return getMultiSelectedBlocks(); + } else if ( hasSelectedBlock() ) { + return [ getSelectedBlock() ]; + } + return []; + } ); + }; + + getSelectedFlatIndices = async () => { + return await this.#page.evaluate( () => { + const { getSelectedBlockClientIds, getClientIdsWithDescendants } = + window.wp.data.select( 'core/block-editor' ); + const selectedClientIds = getSelectedBlockClientIds(); + const allClientIds = getClientIdsWithDescendants(); + return selectedClientIds.map( + ( clientId ) => allClientIds.indexOf( clientId ) + 1 + ); + } ); + }; + + /** + * Tests if the native selection matches the block selection. + */ + assertNativeSelection = async () => { + const selection = await this.#editor.canvas.evaluateHandle( () => + window.getSelection() + ); + + const { selectionStart, selectionEnd } = await this.#page.evaluate( + () => { + const { getSelectionStart, getSelectionEnd } = + window.wp.data.select( 'core/block-editor' ); + return { + selectionStart: getSelectionStart().clientId, + selectionEnd: getSelectionEnd().clientId, + }; + } + ); + const startBlock = this.#editor.canvas.locator( + `[data-block="${ selectionStart }"]` + ); + const endBlock = this.#editor.canvas.locator( + `[data-block="${ selectionEnd }"]` + ); + + expect( + await selection.evaluate( ( _selection ) => _selection.rangeCount ), + 'Expected one range' + ).toBe( 1 ); + + const range = await selection.evaluateHandle( ( _selection ) => + _selection.getRangeAt( 0 ) + ); + + const isMultiSelected = selectionStart !== selectionEnd; + const isCollapsed = await range.evaluate( + ( { collapsed } ) => collapsed + ); + + if ( isMultiSelected ) { + expect( isCollapsed, 'Expected an uncollapsed selection' ).toBe( + false + ); + + expect( + await startBlock.evaluate( + ( block, _selection ) => + _selection.containsNode( block, true ), + selection + ), + 'Expected selection to include in the first selected block' + ).toBe( true ); + expect( + await endBlock.evaluate( + ( block, _selection ) => + _selection.containsNode( block, true ), + selection + ), + 'Expected selection to include in the last selected block' + ).toBe( true ); + } else { + expect( isCollapsed, 'Expected a collapsed selection' ).toBe( + true + ); + + expect( + await startBlock.evaluate( + ( block, { startContainer, endContainer } ) => + block.contains( startContainer ) && + block.contains( endContainer ), + range + ), + 'Expected selection to start and end in the selected block' + ).toBe( true ); + } + }; +} From 4b8857a9aef883e65260e83b0ab7e12c8e607ec8 Mon Sep 17 00:00:00 2001 From: Kai Hao Date: Sun, 19 Feb 2023 13:36:26 +0800 Subject: [PATCH 3/4] Document and comments --- .../various/multi-block-selection.spec.js | 57 ++++++++++--------- 1 file changed, 31 insertions(+), 26 deletions(-) diff --git a/test/e2e/specs/editor/various/multi-block-selection.spec.js b/test/e2e/specs/editor/various/multi-block-selection.spec.js index ccfe53b629e6f7..1559d8c070c97b 100644 --- a/test/e2e/specs/editor/various/multi-block-selection.spec.js +++ b/test/e2e/specs/editor/various/multi-block-selection.spec.js @@ -6,6 +6,10 @@ const { test, expect } = require( '@wordpress/e2e-test-utils-playwright' ); /** @typedef {import('@playwright/test').Page} Page */ /** @typedef {import('@wordpress/e2e-test-utils-playwright').Editor} Editor */ +/** + * Some tests in this file use the character `|` to represent the caret's position + * in a more readable format. + */ test.describe( 'Multi-block selection', () => { test.use( { multiBlockSelectionUtils: async ( { page, editor }, use ) => { @@ -94,10 +98,10 @@ test.describe( 'Multi-block selection', () => { await page.keyboard.press( 'Shift+ArrowDown' ); // Should type at the end of the paragraph. - await page.keyboard.type( '$' ); + await page.keyboard.type( '|' ); await expect.poll( editor.getBlocks ).toMatchObject( [ - { name: 'core/paragraph', attributes: { content: '1
2$' } }, + { name: 'core/paragraph', attributes: { content: '1
2|' } }, { name: 'core/paragraph', attributes: { content: '3' } }, ] ); } ); @@ -505,7 +509,7 @@ test.describe( 'Multi-block selection', () => { { attributes: { content: 'second paragraph' } }, ] ); - await page.keyboard.type( '$' ); + await page.keyboard.type( '|' ); await expect .poll( editor.getBlocks, @@ -513,18 +517,18 @@ test.describe( 'Multi-block selection', () => { ) .toMatchObject( [ { attributes: { content: 'first paragraph' } }, - { attributes: { content: 'second paragraph$' } }, + { attributes: { content: 'second paragraph|' } }, ] ); await pageUtils.pressKeyWithModifier( 'primary', 'a' ); await pageUtils.pressKeyWithModifier( 'primary', 'a' ); await pageUtils.pressKeyWithModifier( 'primary', 'v' ); - await page.keyboard.type( '$' ); + await page.keyboard.type( '|' ); await expect .poll( editor.getBlocks, 'should replace blocks' ) .toMatchObject( [ { attributes: { content: 'first paragraph' } }, - { attributes: { content: 'second paragraph$' } }, + { attributes: { content: 'second paragraph|' } }, ] ); await page.keyboard.press( 'Backspace' ); @@ -532,14 +536,14 @@ test.describe( 'Multi-block selection', () => { await page.keyboard.press( 'ArrowLeft' ); } await pageUtils.pressKeyWithModifier( 'primary', 'v' ); - await page.keyboard.type( '$' ); + await page.keyboard.type( '|' ); await expect .poll( editor.getBlocks, 'should paste mid-block' ) .toMatchObject( [ { attributes: { content: 'first paragraph' } }, { attributes: { content: 'second paragr' } }, { attributes: { content: 'first paragraph' } }, - { attributes: { content: 'second paragraph$' } }, + { attributes: { content: 'second paragraph|' } }, { attributes: { content: 'aph' } }, ] ); } ); @@ -1017,11 +1021,11 @@ test.describe( 'Multi-block selection', () => { await page.keyboard.press( 'Delete' ); // Ensure selection is in the correct place. - await page.keyboard.type( '$' ); + await page.keyboard.type( '|' ); await expect.poll( editor.getBlocks ).toMatchObject( [ { name: 'core/heading', - attributes: { level: 2, content: '1$2' }, + attributes: { level: 2, content: '1|2' }, }, ] ); } ); @@ -1042,11 +1046,11 @@ test.describe( 'Multi-block selection', () => { await pageUtils.pressKeyTimes( 'Shift+ArrowLeft', 3 ); // Ensure selection is in the correct place. - await page.keyboard.type( '$' ); + await page.keyboard.type( '|' ); await expect.poll( editor.getBlocks ).toMatchObject( [ { name: 'core/paragraph', - attributes: { content: '1$2' }, + attributes: { content: '1|2' }, }, ] ); } ); @@ -1079,7 +1083,7 @@ test.describe( 'Multi-block selection', () => { await page.keyboard.press( 'Enter' ); // Ensure selection is in the correct place. - await page.keyboard.type( '$' ); + await page.keyboard.type( '|' ); await expect.poll( editor.getBlocks ).toMatchObject( [ { name: 'core/paragraph', @@ -1087,7 +1091,7 @@ test.describe( 'Multi-block selection', () => { }, { name: 'core/paragraph', - attributes: { content: '$' }, + attributes: { content: '|' }, }, { name: 'core/heading', @@ -1121,11 +1125,11 @@ test.describe( 'Multi-block selection', () => { await page.keyboard.press( 'Backspace' ); // Ensure selection is in the correct place. - await page.keyboard.type( '$' ); + await page.keyboard.type( '|' ); await expect.poll( editor.getBlocks ).toMatchObject( [ { name: 'core/paragraph', - attributes: { content: '$' }, + attributes: { content: '|' }, }, ] ); } ); @@ -1159,11 +1163,11 @@ test.describe( 'Multi-block selection', () => { await page.keyboard.press( 'Backspace' ); // Ensure selection is in the correct place. - await page.keyboard.type( '$' ); + await page.keyboard.type( '|' ); await expect.poll( editor.getBlocks ).toMatchObject( [ { name: 'core/paragraph', - attributes: { content: '1$2' }, + attributes: { content: '1|2' }, }, ] ); } ); @@ -1296,16 +1300,19 @@ class MultiBlockSelectionUtils { window.getSelection() ); - const { selectionStart, selectionEnd } = await this.#page.evaluate( - () => { - const { getSelectionStart, getSelectionEnd } = - window.wp.data.select( 'core/block-editor' ); + const { isMultiSelected, selectionStart, selectionEnd } = + await this.#page.evaluate( () => { + const { + hasMultiSelection, + getSelectionStart, + getSelectionEnd, + } = window.wp.data.select( 'core/block-editor' ); return { + isMultiSelected: hasMultiSelection(), selectionStart: getSelectionStart().clientId, selectionEnd: getSelectionEnd().clientId, }; - } - ); + } ); const startBlock = this.#editor.canvas.locator( `[data-block="${ selectionStart }"]` ); @@ -1321,8 +1328,6 @@ class MultiBlockSelectionUtils { const range = await selection.evaluateHandle( ( _selection ) => _selection.getRangeAt( 0 ) ); - - const isMultiSelected = selectionStart !== selectionEnd; const isCollapsed = await range.evaluate( ( { collapsed } ) => collapsed ); From 21b65499929736027b2b4663fa951027b0df975b Mon Sep 17 00:00:00 2001 From: Kai Hao Date: Fri, 3 Mar 2023 10:30:28 +0800 Subject: [PATCH 4/4] Fix code reviews --- .../various/multi-block-selection.spec.js | 26 ++++++++++++------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/test/e2e/specs/editor/various/multi-block-selection.spec.js b/test/e2e/specs/editor/various/multi-block-selection.spec.js index 1559d8c070c97b..d651001c8f2640 100644 --- a/test/e2e/specs/editor/various/multi-block-selection.spec.js +++ b/test/e2e/specs/editor/various/multi-block-selection.spec.js @@ -64,7 +64,7 @@ test.describe( 'Multi-block selection', () => { attributes: { content: '' }, } ); await editor.canvas - .getByRole( 'document', { name: /Empty block/i } ) + .getByRole( 'document', { name: 'Empty block' } ) .click(); // Move to the middle of the first line. await pageUtils.pressKeyTimes( 'ArrowUp', 4 ); @@ -532,9 +532,7 @@ test.describe( 'Multi-block selection', () => { ] ); await page.keyboard.press( 'Backspace' ); - for ( let i = 0; i < 3; i += 1 ) { - await page.keyboard.press( 'ArrowLeft' ); - } + await pageUtils.pressKeyTimes( 'ArrowLeft', 3 ); await pageUtils.pressKeyWithModifier( 'primary', 'v' ); await page.keyboard.type( '|' ); await expect @@ -821,14 +819,22 @@ test.describe( 'Multi-block selection', () => { .click(); await pageUtils.pressKeyWithModifier( 'primary', 'a' ); + await expect + .poll( multiBlockSelectionUtils.getSelectedBlocks ) + .toMatchObject( [ { name: 'core/list-item' } ] ); + await pageUtils.pressKeyWithModifier( 'primary', 'a' ); - await pageUtils.pressKeyWithModifier( 'primary', 'a' ); + await expect + .poll( multiBlockSelectionUtils.getSelectedBlocks ) + .toMatchObject( [ { name: 'core/list' } ] ); + await pageUtils.pressKeyWithModifier( 'primary', 'a' ); await expect .poll( multiBlockSelectionUtils.getSelectedBlocks ) - .toContainEqual( - expect.objectContaining( { name: 'core/paragraph' } ) - ); + .toMatchObject( [ + { name: 'core/paragraph' }, + { name: 'core/list' }, + ] ); } ); test( 'should select all from empty selection', async ( { @@ -1153,7 +1159,9 @@ test.describe( 'Multi-block selection', () => { .click(); await page.keyboard.press( 'ArrowLeft' ); - const strongText = page.getByText( '1', { exact: true } ); + const strongText = editor.canvas + .getByRole( 'region', { name: 'Editor content' } ) + .getByText( '1', { exact: true } ); const strongBox = await strongText.boundingBox(); await strongText.click( { // Ensure clicking on the right half of the element.