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 mentions tests to playwright #43064

Merged
merged 13 commits into from
Nov 4, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* Internal dependencies
*/
import type { Editor } from './index';

/**
* Clicks the default block appender.
*
* @param {Editor} this
*/
export async function clickBlockAppender( this: Editor ) {
kevin940726 marked this conversation as resolved.
Show resolved Hide resolved
// The block appender is only visible when there's no selection.
await this.page.evaluate( () =>
// @ts-ignore (Reason: wp isn't typed)
window.wp.data.dispatch( 'core/block-editor' ).clearSelectedBlock()
);
const appender = await this.page.waitForSelector(
'.block-editor-default-block-appender__content'
kevin940726 marked this conversation as resolved.
Show resolved Hide resolved
);
await appender.click();
}
2 changes: 2 additions & 0 deletions packages/e2e-test-utils-playwright/src/editor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { selectBlocks } from './select-blocks';
import { showBlockToolbar } from './show-block-toolbar';
import { saveSiteEditorEntities } from './site-editor';
import { transformBlockTo } from './transform-block-to';
import { clickBlockAppender } from './click-block-appender';

type EditorConstructorProps = {
page: Page;
Expand Down Expand Up @@ -64,4 +65,5 @@ export class Editor {
selectBlocks = selectBlocks.bind( this );
showBlockToolbar = showBlockToolbar.bind( this );
transformBlockTo = transformBlockTo.bind( this );
clickBlockAppender = clickBlockAppender.bind( this );
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { createComment, deleteAllComments } from './comments';
import { createPost, deleteAllPosts } from './posts';
import { resetPreferences } from './preferences';
import { deleteAllWidgets, addWidgetBlock } from './widgets';
import { createUser, deleteUser } from './users';

interface StorageState {
cookies: Cookie[];
Expand All @@ -38,7 +39,7 @@ class RequestUtils {
baseURL?: string;

pluginsMap: Record< string, string > | null = null;

kevin940726 marked this conversation as resolved.
Show resolved Hide resolved
userID?: number;
kevin940726 marked this conversation as resolved.
Show resolved Hide resolved
static async setup( {
user,
storageStatePath,
Expand Down Expand Up @@ -133,6 +134,8 @@ class RequestUtils {
uploadMedia = uploadMedia.bind( this );
deleteMedia = deleteMedia.bind( this );
deleteAllMedia = deleteAllMedia.bind( this );
createUser = createUser.bind( this );
deleteUser = deleteUser.bind( this );
}

export type { StorageState };
Expand Down
63 changes: 63 additions & 0 deletions packages/e2e-test-utils-playwright/src/request-utils/users.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* Internal dependencies
*/
import type { RequestUtils } from './index';

/**
* Create user using REST API.
*
* @param {} this RequestUtils.
* @param {string} username User Name.
* @param {string} firstName First Name.
* @param {string} lastName Last Name.
*/
async function createUser(
this: RequestUtils,
username: string,
firstName: string,
lastName: string
) {
// Create User
// https://developer.wordpress.org/rest-api/reference/users/#create-a-user
await this.rest( {
method: 'POST',
path: '/wp/v2/users',
params: {
username,
first_name: firstName,
last_name: lastName,
email: username + '@example.com',
password: 'secret',
},
} );
}
kevin940726 marked this conversation as resolved.
Show resolved Hide resolved

/**
* Delete user using REST API.
*
* @param {} this RequestUtils.
* @param {string} username User Name.
*/
async function deleteUser( this: RequestUtils, username: string ) {
kevin940726 marked this conversation as resolved.
Show resolved Hide resolved
// List user.
// https://developer.wordpress.org/rest-api/reference/users/#list-users
const user = await this.rest( {
method: 'GET',
path: '/wp/v2/users',
params: {
search: username,
},
} );
// Delete User
// https://developer.wordpress.org/rest-api/reference/users/#delete-a-user
await this.rest( {
method: 'DELETE',
path: `/wp/v2/users/${ user[ 0 ].id }`,
params: {
force: true,
reassign: 0,
},
} );
}

export { createUser, deleteUser };
69 changes: 0 additions & 69 deletions packages/e2e-tests/specs/editor/various/mentions.test.js

This file was deleted.

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

test.describe( 'autocomplete mentions', () => {
test.beforeAll( async ( { requestUtils } ) => {
await requestUtils.createUser( 'testuser', 'Jane', 'Doe' );
} );

test.beforeEach( async ( { admin } ) => {
await admin.createNewPost();
} );

test.afterAll( async ( { requestUtils } ) => {
await requestUtils.deleteUser( 'testuser' );
} );

test( 'should insert mention', async ( { page, editor } ) => {
await editor.clickBlockAppender();
await page.keyboard.type( 'I am @a' );
await page.locator( 'button', { hasText: 'adminadmin' } ).click();
kevin940726 marked this conversation as resolved.
Show resolved Hide resolved
const content = await editor.getEditedPostContent();
expect( content ).toBe(
`<!-- wp:paragraph -->
<p>I am @admin</p>
<!-- /wp:paragraph -->`
);
} );

test( 'should insert mention between two other words', async ( {
page,
editor,
pageUtils,
} ) => {
await editor.clickBlockAppender();
await page.keyboard.type( 'Stuck in the middle with you' );
await pageUtils.pressKeyTimes( 'ArrowLeft', 'you'.length );
await page.keyboard.type( '@j' );
await page.locator( 'button', { hasText: 'Jane Doetestuser' } ).click();
kevin940726 marked this conversation as resolved.
Show resolved Hide resolved
const content = await editor.getEditedPostContent();
expect( content ).toBe(
`<!-- wp:paragraph -->
<p>Stuck in the middle with @testuseryou</p>
<!-- /wp:paragraph -->`
);
} );

test( 'should insert two subsequent mentions', async ( {
page,
editor,
} ) => {
await editor.clickBlockAppender();
await page.keyboard.type( 'I am @j' );
await page.locator( 'button', { hasText: 'Jane Doetestuser' } ).click();
await page.keyboard.type( ' ' );
await page.keyboard.type( '@a' );
await page.locator( 'button', { hasText: 'adminadmin' } ).click();
kevin940726 marked this conversation as resolved.
Show resolved Hide resolved
const content = await editor.getEditedPostContent();
expect( content ).toBe(
`<!-- wp:paragraph -->
<p>I am @testuser @admin</p>
<!-- /wp:paragraph -->`
);
} );
} );