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

E2E Tests: Migrate Comments block test to Playwright #39826

Merged
merged 23 commits into from
Jul 8, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions packages/e2e-test-utils-playwright/src/admin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type { Browser, Page, BrowserContext } from '@playwright/test';
*/
import { createNewPost } from './create-new-post';
import { getPageError } from './get-page-error';
import { setOption } from './set-option';
import { visitAdminPage } from './visit-admin-page';
import { visitSiteEditor } from './visit-site-editor';
import type { PageUtils } from '../page-utils';
Expand All @@ -35,6 +36,7 @@ export class Admin {

createNewPost = createNewPost.bind( this );
getPageError = getPageError.bind( this );
setOption = setOption.bind( this );
visitAdminPage = visitAdminPage.bind( this );
visitSiteEditor = visitSiteEditor.bind( this );
}
14 changes: 14 additions & 0 deletions packages/e2e-test-utils-playwright/src/admin/set-option.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Internal dependencies
*/
import type { Admin } from './';

export async function setOption( this: Admin, setting: string, value: string ) {
Copy link
Member

Choose a reason for hiding this comment

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

Personally, I wouldn't put this inside e2e-test-utils-playwright since it's just a temporary solution for one test suite. Breaking changes will be made once the REST options API is implemented.

Instead, I would put this inside the comments test file to make it clear that it's an internal API.

await this.visitAdminPage( 'options.php', '' );
const previousValue = await this.page.inputValue( `#${ setting }` );

await this.page.fill( `#${ setting }`, value );

await this.page.click( '#Update' );
return previousValue;
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,9 @@ export async function publishPost( this: Editor ) {
await this.page.click(
'role=region[name="Editor publish"i] >> role=button[name="Publish"i]'
);

const urlString = await this.page.inputValue( 'text="Post address"' );
Copy link
Member

Choose a reason for hiding this comment

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

Normally, we would prefer role selectors over text selectors if they're available.

const url = new URL( urlString );
const postId = url.searchParams.get( 'p' );
return postId;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* Internal dependencies
*/
import type { RequestUtils } from './index';

export interface Comment {
id: number;
author: number;
content: string;
}

/**
* Create new comment using the REST API.
*
* @param {} this RequestUtils.
* @param {} comment Comment.
*/
export async function createComment( this: RequestUtils, comment: Comment ) {
this.rest( {
Copy link
Member

Choose a reason for hiding this comment

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

Missing await here?

We should probably return the response of the API too to capture the created comment id.

method: 'POST',
path: '/wp/v2/comments',
data: comment,
Copy link
Member

Choose a reason for hiding this comment

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

I don't think we have the id field of the comment data yet before creating it?

Copy link
Member

Choose a reason for hiding this comment

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

Also, why couldn't we call /wp/v2/users/me here to get the current user id to pass it into the data? So that we don't have to create another util getCurrentUser for this.

} );
}

/**
* Delete all comments using the REST API.
*
* @param {} this RequestUtils.
*/
export async function deleteAllComments( this: RequestUtils ) {
// List all comments.
// https://developer.wordpress.org/rest-api/reference/comments/#list-comments
const comments = await this.rest( {
path: '/wp/v2/comments',
params: {
per_page: 100,
// All possible statuses.
status: 'unapproved,approved,spam,trash',
},
} );

// Delete all comments one by one.
// https://developer.wordpress.org/rest-api/reference/comments/#delete-a-comment
// "/wp/v2/comments" doesn't support batch requests yet.
await Promise.all(
comments.map( ( comment: Comment ) =>
this.rest( {
method: 'DELETE',
path: `/wp/v2/comments/${ comment.id }`,
params: {
force: true,
},
} )
)
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ import { getPluginsMap, activatePlugin, deactivatePlugin } from './plugins';
import { deleteAllTemplates } from './templates';
import { activateTheme } from './themes';
import { deleteAllBlocks } from './blocks';
import { createComment, deleteAllComments } from './comments';
import { deleteAllPosts } from './posts';
import { resetPreferences } from './preferences';
import { getCurrentUser } from './user';
import { deleteAllWidgets, addWidgetBlock } from './widgets';

interface StorageState {
Expand Down Expand Up @@ -121,6 +123,8 @@ class RequestUtils {
activateTheme = activateTheme.bind( this );
deleteAllBlocks = deleteAllBlocks;
deleteAllPosts = deleteAllPosts.bind( this );
createComment = createComment.bind( this );
deleteAllComments = deleteAllComments.bind( this );
deleteAllWidgets = deleteAllWidgets.bind( this );
addWidgetBlock = addWidgetBlock.bind( this );
deleteAllTemplates = deleteAllTemplates.bind( this );
Expand All @@ -129,6 +133,7 @@ class RequestUtils {
uploadMedia = uploadMedia.bind( this );
deleteMedia = deleteMedia.bind( this );
deleteAllMedia = deleteAllMedia.bind( this );
getCurrentUser = getCurrentUser.bind( this );
}

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

/**
* Get current user
*
* @param {this} this Request utils.
*/
export async function getCurrentUser( this: RequestUtils ) {
const response = await this.rest( {
path: '/wp/v2/users/me',
method: 'GET',
} );

return response;
}
131 changes: 0 additions & 131 deletions packages/e2e-tests/specs/editor/blocks/comments.test.js

This file was deleted.

Loading