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 code block test case to playwright #40844

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Clicks the default block appender.
*
* @this {import('./').PageUtils}
*/

export async function clickBlockAppender() {
// The block appender is only visible when there's no selection.
await this.page.evaluate( () =>
window.wp.data.dispatch( 'core/block-editor' ).clearSelectedBlock()
);
const appender = await this.page.waitForSelector(
'.block-editor-default-block-appender__content'
);
await appender.click();
await this.page.evaluate( () => new Promise( window.requestIdleCallback ) );
}
3 changes: 3 additions & 0 deletions packages/e2e-test-utils-playwright/src/page/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import { setBrowserViewport } from './set-browser-viewport';
import { showBlockToolbar } from './show-block-toolbar';
import { visitAdminPage } from './visit-admin-page';
import { visitSiteEditor } from './site-editor';
import {clickBlockAppender} from './click-block-appender';


class PageUtils {
browser: Browser;
Expand Down Expand Up @@ -50,6 +52,7 @@ class PageUtils {
openPreviewPage = openPreviewPage;
setBrowserViewport = setBrowserViewport;
pressKeyTimes = pressKeyTimes;
clickBlockAppender = clickBlockAppender;
}

export { PageUtils };
48 changes: 0 additions & 48 deletions packages/e2e-tests/specs/editor/blocks/code.test.js

This file was deleted.

58 changes: 58 additions & 0 deletions test/e2e/specs/editor/blocks/code.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* WordPress dependencies
*/

const { test, expect } = require( '@wordpress/e2e-test-utils-playwright' );


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

test( 'can be created by three backticks and enter', async ({page,pageUtils}) => {
await pageUtils.clickBlockAppender();
Copy link
Member

Choose a reason for hiding this comment

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

We usually want to limit the usage of e2e utils if we can inline them directly. If we're just creating a page, I believe we can just click on the appender button directly:

Suggested change
await pageUtils.clickBlockAppender();
await page.click( 'role=button[name="Add default block"i]' );

The same code has also been used in copy-cut-paste tests. This way we don't have to create the clickBlockAppender util (unless we really need it in other situations).

await page.keyboard.type( '```' );
await page.keyboard.press( 'Enter' );
await page.keyboard.type( '<?php' );

// Check the content
const content = await pageUtils.getEditedPostContent();
expect( content ).toBe(
`<!-- wp:code -->
<pre class="wp-block-code"><code>&lt;?php</code></pre>
<!-- /wp:code -->`
);

} );

test( 'should delete block when backspace in an empty code', async ({page,pageUtils}) => {
await pageUtils.insertBlock({ name: 'core/code' });

await page.keyboard.type( 'a' );

await page.keyboard.press( 'Backspace' );
await page.keyboard.press( 'Backspace' );

// Expect code block to be deleted.
expect( await pageUtils.getEditedPostContent() ).toBe( '' );
} );

test( 'should paste plain text', async ({page,pageUtils}) => {
await pageUtils.insertBlock( { name: 'core/code' });

// Test to see if HTML and white space is kept.
await pageUtils.setClipboardData( { plainText: '<img />\n\t<br>' } );

await pageUtils.pressKeyWithModifier( 'primary', 'v' );

// Check the content
const content = await pageUtils.getEditedPostContent();
expect( content ).toBe(
`<!-- wp:code -->
<pre class="wp-block-code"><code>&lt;img />
&lt;br></code></pre>
<!-- /wp:code -->`
);
} );
} );