Skip to content

Commit

Permalink
Document and comments
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin940726 committed Feb 19, 2023
1 parent 245ad39 commit e1e9623
Showing 1 changed file with 31 additions and 26 deletions.
57 changes: 31 additions & 26 deletions test/e2e/specs/editor/various/multi-block-selection.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) => {
Expand Down Expand Up @@ -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<br>2$' } },
{ name: 'core/paragraph', attributes: { content: '1<br>2|' } },
{ name: 'core/paragraph', attributes: { content: '3' } },
] );
} );
Expand Down Expand Up @@ -505,41 +509,41 @@ test.describe( 'Multi-block selection', () => {
{ attributes: { content: 'second paragraph' } },
] );

await page.keyboard.type( '$' );
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$' } },
{ 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' );

for ( let i = 0; i < 3; i += 1 ) {
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' } },
] );
} );
Expand Down Expand Up @@ -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' },
},
] );
} );
Expand All @@ -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' },
},
] );
} );
Expand Down Expand Up @@ -1079,15 +1083,15 @@ 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',
attributes: { content: '1' },
},
{
name: 'core/paragraph',
attributes: { content: '$' },
attributes: { content: '|' },
},
{
name: 'core/heading',
Expand Down Expand Up @@ -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: '|' },
},
] );
} );
Expand Down Expand Up @@ -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: '<strong>1</strong>$2' },
attributes: { content: '<strong>1</strong>|2' },
},
] );
} );
Expand Down Expand Up @@ -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 }"]`
);
Expand All @@ -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
);
Expand Down

0 comments on commit e1e9623

Please sign in to comment.