-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add block patterns e2e tests (#22414)
- Loading branch information
1 parent
7fa12a3
commit 692ec8c
Showing
8 changed files
with
163 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { pressKeyWithModifier } from './press-key-with-modifier'; | ||
|
||
/** | ||
* Opens the global block inserter. | ||
*/ | ||
export async function openGlobalBlockInserter() { | ||
if ( ! ( await isGlobalInserterOpen() ) ) { | ||
await toggleGlobalBlockInserter(); | ||
|
||
// Waiting here is necessary because sometimes the inserter takes more time to | ||
// render than Puppeteer takes to complete the 'click' action | ||
await page.waitForSelector( '.block-editor-inserter__menu' ); | ||
} | ||
|
||
// Select the block tab by default if the tabs are visible | ||
const hasTabs = !! ( await page.$( '.block-editor-inserter__tabs' ) ); | ||
if ( hasTabs ) { | ||
await page.click( | ||
'.block-editor-inserter__tabs [aria-controls="0-blocks-view"]' | ||
); | ||
} | ||
} | ||
|
||
export async function closeGlobalBlockInserter() { | ||
if ( await isGlobalInserterOpen() ) { | ||
await toggleGlobalBlockInserter(); | ||
} | ||
} | ||
|
||
async function isGlobalInserterOpen() { | ||
return await page.evaluate( () => { | ||
return !! document.querySelector( | ||
'.edit-post-header [aria-label="Add block"].is-pressed, .edit-site-header [aria-label="Add block"].is-pressed' | ||
); | ||
} ); | ||
} | ||
|
||
async function toggleGlobalBlockInserter() { | ||
await page.click( | ||
'.edit-post-header [aria-label="Add block"], .edit-site-header [aria-label="Add block"]' | ||
); | ||
} | ||
|
||
/** | ||
* Search for block in the global inserter | ||
* | ||
* @param {string} searchTerm The text to search the inserter for. | ||
*/ | ||
export async function searchForBlock( searchTerm ) { | ||
await openGlobalBlockInserter(); | ||
await page.focus( '.block-editor-inserter__search-input' ); | ||
await pressKeyWithModifier( 'primary', 'a' ); | ||
await page.keyboard.type( searchTerm ); | ||
} | ||
|
||
/** | ||
* Search for pattern in the global inserter | ||
* | ||
* @param {string} searchTerm The text to search the inserter for. | ||
*/ | ||
export async function searchForPattern( searchTerm ) { | ||
await openGlobalBlockInserter(); | ||
// Select the patterns tab | ||
await page.click( | ||
'.block-editor-inserter__tabs [aria-controls="0-patterns-view"]' | ||
); | ||
await page.focus( '.block-editor-inserter__search-input' ); | ||
await pressKeyWithModifier( 'primary', 'a' ); | ||
await page.keyboard.type( searchTerm ); | ||
} | ||
|
||
/** | ||
* Opens the inserter, searches for the given term, then selects the first | ||
* result that appears. It then waits briefly for the block list to update. | ||
* | ||
* @param {string} searchTerm The text to search the inserter for. | ||
*/ | ||
export async function insertBlock( searchTerm ) { | ||
await searchForBlock( searchTerm ); | ||
const insertButton = ( | ||
await page.$x( `//button//span[contains(text(), '${ searchTerm }')]` ) | ||
)[ 0 ]; | ||
await insertButton.click(); | ||
} | ||
|
||
/** | ||
* Opens the inserter, searches for the given pattern, then selects the first | ||
* result that appears. It then waits briefly for the block list to update. | ||
* | ||
* @param {string} searchTerm The text to search the inserter for. | ||
*/ | ||
export async function insertPattern( searchTerm ) { | ||
await searchForPattern( searchTerm ); | ||
const insertButton = ( | ||
await page.$x( | ||
`//div[@role = 'button']//div[contains(text(), '${ searchTerm }')]` | ||
) | ||
)[ 0 ]; | ||
await insertButton.click(); | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
13 changes: 13 additions & 0 deletions
13
packages/e2e-tests/specs/editor/various/__snapshots__/adding-patterns.test.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`adding blocks should insert a block pattern 1`] = ` | ||
"<!-- wp:buttons {\\"align\\":\\"center\\"} --> | ||
<div class=\\"wp-block-buttons aligncenter\\"><!-- wp:button {\\"borderRadius\\":0,\\"backgroundColor\\":\\"very-dark-gray\\"} --> | ||
<div class=\\"wp-block-button\\"><a class=\\"wp-block-button__link has-very-dark-gray-background-color has-background no-border-radius\\">Button One</a></div> | ||
<!-- /wp:button --> | ||
<!-- wp:button {\\"borderRadius\\":0,\\"textColor\\":\\"very-dark-gray\\",\\"className\\":\\"is-style-outline\\"} --> | ||
<div class=\\"wp-block-button is-style-outline\\"><a class=\\"wp-block-button__link has-very-dark-gray-color has-text-color no-border-radius\\">Button Two</a></div> | ||
<!-- /wp:button --></div> | ||
<!-- /wp:buttons -->" | ||
`; |
22 changes: 22 additions & 0 deletions
22
packages/e2e-tests/specs/editor/various/adding-patterns.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { | ||
createNewPost, | ||
insertPattern, | ||
getEditedPostContent, | ||
} from '@wordpress/e2e-test-utils'; | ||
|
||
/** @typedef {import('puppeteer').ElementHandle} ElementHandle */ | ||
|
||
describe( 'adding blocks', () => { | ||
beforeEach( async () => { | ||
await createNewPost(); | ||
} ); | ||
|
||
it( 'should insert a block pattern', async () => { | ||
await insertPattern( 'Two Buttons' ); | ||
|
||
expect( await getEditedPostContent() ).toMatchSnapshot(); | ||
} ); | ||
} ); |