Skip to content

Commit

Permalink
Link control: migrate tests to Playwright. Can be created by selectin…
Browse files Browse the repository at this point in the history
…g text and using keyboard shortcuts (#50996)

* initial setup

* fixed utils

* update snapshots, commented waitForURLFieldAutoFocus

* removed old test

* removed unneeded code

* removed old snapshots

* use locator instead of component class

* remove appender function

* use locators instead of tabbing around

* rename button

* avoid snapshots and fix locator for Save button

* removed snapshots

* added comment
  • Loading branch information
MaggieCabrera authored May 29, 2023
1 parent 2746bda commit 069d541
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,6 @@ exports[`Links can be created by selecting text and clicking Link 1`] = `
<!-- /wp:paragraph -->"
`;

exports[`Links can be created by selecting text and using keyboard shortcuts 1`] = `
"<!-- wp:paragraph -->
<p>This is Gutenberg</p>
<!-- /wp:paragraph -->"
`;

exports[`Links can be created by selecting text and using keyboard shortcuts 2`] = `
"<!-- wp:paragraph -->
<p>This is <a href="https://wordpress.org/gutenberg" target="_blank" rel="noreferrer noopener">Gutenberg</a></p>
<!-- /wp:paragraph -->"
`;

exports[`Links can be created instantly when a URL is selected 1`] = `
"<!-- wp:paragraph -->
<p>This is Gutenberg: <a href="https://wordpress.org/gutenberg">https://wordpress.org/gutenberg</a></p>
Expand Down
42 changes: 0 additions & 42 deletions packages/e2e-tests/specs/editor/various/links.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,48 +103,6 @@ describe( 'Links', () => {
expect( urlInputValue ).toBe( '' );
} );

it( 'can be created by selecting text and using keyboard shortcuts', async () => {
// Create a block with some text.
await clickBlockAppender();
await page.keyboard.type( 'This is Gutenberg' );

// Select some text.
await pressKeyWithModifier( 'shiftAlt', 'ArrowLeft' );

// Press Cmd+K to insert a link.
await pressKeyWithModifier( 'primary', 'K' );

// Wait for the URL field to auto-focus.
await waitForURLFieldAutoFocus();

// Type a URL.
await page.keyboard.type( 'https://wordpress.org/gutenberg' );

// Open settings.
await page.keyboard.press( 'Tab' );
await page.keyboard.press( 'Space' );

// Navigate to and toggle the "Open in new tab" checkbox.
await page.keyboard.press( 'Tab' );
await page.keyboard.press( 'Space' );

// Toggle should still have focus and be checked.
await page.waitForSelector(
':focus:checked.components-checkbox-control__input'
);

// Ensure that the contents of the post have not been changed, since at
// this point the link is still not inserted.
expect( await getEditedPostContent() ).toMatchSnapshot();

// Tab back to the Submit and apply the link.
await page.keyboard.press( 'Tab' );
await page.keyboard.press( 'Enter' );

// The link should have been inserted.
expect( await getEditedPostContent() ).toMatchSnapshot();
} );

it( 'can be created without any text selected', async () => {
// Create a block with some text.
await clickBlockAppender();
Expand Down
69 changes: 69 additions & 0 deletions test/e2e/specs/editor/blocks/links.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* WordPress dependencies
*/
const { test, expect } = require( '@wordpress/e2e-test-utils-playwright' );

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

test( `can be created by selecting text and using keyboard shortcuts`, async ( {
page,
editor,
pageUtils,
} ) => {
// Create a block with some text.
await editor.insertBlock( {
name: 'core/paragraph',
} );
await page.keyboard.type( 'This is Gutenberg' );

// Select some text.
await pageUtils.pressKeys( 'shiftAlt+ArrowLeft' );

// Press Cmd+K to insert a link.
await pageUtils.pressKeys( 'primary+K' );

// Type a URL.
await page.keyboard.type( 'https://wordpress.org/gutenberg' );

// Open settings.
await page.getByRole( 'button', { name: 'Link Settings' } ).click();

// Navigate to and toggle the "Open in new tab" checkbox.
const checkbox = page.getByLabel( 'Open in new tab' );
await checkbox.click();

// Toggle should still have focus and be checked.
await expect( checkbox ).toBeChecked();
await expect( checkbox ).toBeFocused();

// Ensure that the contents of the post have not been changed, since at
// this point the link is still not inserted.
await expect.poll( editor.getBlocks ).toMatchObject( [
{
name: 'core/paragraph',
attributes: { content: 'This is Gutenberg' },
},
] );

// Tab back to the Submit and apply the link.
await page
//TODO: change to a better selector when https://github.com/WordPress/gutenberg/issues/51060 is resolved.
.locator( '.block-editor-link-control' )
.getByRole( 'button', { name: 'Save' } )
.click();

// The link should have been inserted.
await expect.poll( editor.getBlocks ).toMatchObject( [
{
name: 'core/paragraph',
attributes: {
content:
'This is <a href="https://wordpress.org/gutenberg" target="_blank" rel="noreferrer noopener">Gutenberg</a>',
},
},
] );
} );
} );

1 comment on commit 069d541

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in 069d541.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/5113693395
📝 Reported issues:

Please sign in to comment.