Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate Button Block tests to Playwright #41494

Merged
merged 9 commits into from
Jun 20, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

95 changes: 0 additions & 95 deletions packages/e2e-tests/specs/editor/blocks/buttons.test.js

This file was deleted.

136 changes: 136 additions & 0 deletions test/e2e/specs/editor/blocks/buttons.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/**
* WordPress dependencies
*/
const { test, expect } = require( '@wordpress/e2e-test-utils-playwright' );

test.describe( 'Buttons', () => {
test.beforeEach( async ( { admin } ) => {
await admin.createNewPost();
} );

test( 'has focus on button content', async ( { editor, page } ) => {
await editor.insertBlock( { name: 'core/buttons' } );
await page.keyboard.type( 'Content' );

//Check the content
kevin940726 marked this conversation as resolved.
Show resolved Hide resolved
const content = await editor.getEditedPostContent();
expect( content ).toBe(
`<!-- wp:buttons -->
<div class="wp-block-buttons"><!-- wp:button -->
<div class="wp-block-button"><a class="wp-block-button__link wp-element-button">Content</a></div>
<!-- /wp:button --></div>
<!-- /wp:buttons -->`
);
} );

test( 'has focus on button content (slash inserter)', async ( {
editor,
page,
} ) => {
await page.click( 'role=button[name="Add default block"i]' );
await page.keyboard.type( '/buttons' );
await page.keyboard.press( 'Enter' );
await page.keyboard.type( 'Content' );

//Check the content
kevin940726 marked this conversation as resolved.
Show resolved Hide resolved
const content = await editor.getEditedPostContent();
expect( content ).toBe(
`<!-- wp:buttons -->
<div class="wp-block-buttons"><!-- wp:button -->
<div class="wp-block-button"><a class="wp-block-button__link wp-element-button">Content</a></div>
<!-- /wp:button --></div>
<!-- /wp:buttons -->`
);
} );

test( 'dismisses link editor when escape is pressed', async ( {
editor,
page,
pageUtils,
} ) => {
// Regression: https://github.com/WordPress/gutenberg/pull/19885
await editor.insertBlock( { name: 'core/buttons' } );
await pageUtils.pressKeyWithModifier( 'primary', 'k' );
await page.waitForFunction(
() => !! document.activeElement.closest( '.block-editor-url-input' )
);
kevin940726 marked this conversation as resolved.
Show resolved Hide resolved
await page.keyboard.press( 'Escape' );
await page.waitForFunction(
() =>
document.activeElement ===
document.querySelector( '.block-editor-rich-text__editable' )
);
kevin940726 marked this conversation as resolved.
Show resolved Hide resolved
await page.keyboard.type( 'WordPress' );

// Check the content
const content = await editor.getEditedPostContent();
expect( content ).toBe(
`<!-- wp:buttons -->
<div class="wp-block-buttons"><!-- wp:button -->
<div class="wp-block-button"><a class="wp-block-button__link wp-element-button">WordPress</a></div>
<!-- /wp:button --></div>
<!-- /wp:buttons -->`
);
} );

test( 'moves focus from the link editor back to the button when escape is pressed after the URL has been submitted', async ( {
editor,
page,
pageUtils,
} ) => {
// Regression: https://github.com/WordPress/gutenberg/issues/34307
await editor.insertBlock( { name: 'core/buttons' } );
await pageUtils.pressKeyWithModifier( 'primary', 'k' );
await page.waitForFunction(
() => !! document.activeElement.closest( '.block-editor-url-input' )
);
kevin940726 marked this conversation as resolved.
Show resolved Hide resolved
await page.keyboard.type( 'https://example.com' );
await page.keyboard.press( 'Enter' );
await page.waitForFunction(
() =>
document.activeElement ===
document.querySelector(
'.block-editor-link-control a[href="https://example.com"]'
)
);
kevin940726 marked this conversation as resolved.
Show resolved Hide resolved
await page.keyboard.press( 'Escape' );

// Focus should move from the link control to the button block's text.
await page.waitForFunction(
() =>
document.activeElement ===
document.querySelector( '[aria-label="Button text"]' )
);
kevin940726 marked this conversation as resolved.
Show resolved Hide resolved

// The link control should still be visible when a URL is set.
const linkControl = await page.locator( '.block-editor-link-control' );
expect( linkControl ).toBeTruthy();
kevin940726 marked this conversation as resolved.
Show resolved Hide resolved
} );

test( 'can jump to the link editor using the keyboard shortcut', async ( {
editor,
page,
pageUtils,
} ) => {
await editor.insertBlock( { name: 'core/buttons' } );
await page.keyboard.type( 'WordPress' );
await pageUtils.pressKeyWithModifier( 'primary', 'k' );
await page.keyboard.type( 'https://www.wordpress.org/' );
await page.keyboard.press( 'Enter' );
// Make sure that the dialog is still opened, and that focus is retained
// within (focusing on the link preview).
await page.waitForSelector(
':focus.block-editor-link-control__search-item-title'
);
kevin940726 marked this conversation as resolved.
Show resolved Hide resolved

// Check the content
const content = await editor.getEditedPostContent();
expect( content ).toBe(
`<!-- wp:buttons -->
<div class="wp-block-buttons"><!-- wp:button -->
<div class="wp-block-button"><a class="wp-block-button__link wp-element-button" href="https://www.wordpress.org/">WordPress</a></div>
<!-- /wp:button --></div>
<!-- /wp:buttons -->`
);
} );
} );