Skip to content

Commit 65a89c9

Browse files
authored
Migrate list block test to Playwright (#41555)
* convert e2e test for list block to playwright. * add snapshots * add snapshots * fix strict mode error * fix transformBlockTo * remove last line form snapshot * use locator * replace snapshot to toBe * remove all snapshot * replage $eval * use role selector. * use locator * remove list.test.js form e2e-tests * use page.click * bind this for Editor methods * use expect.poll * Use wordpress api in transformBlockTo. * use getEditedPostContent instead of innerHTML * use nth selector * Wait with selector instead of requestIdleCallback. * add bind(this) * add bind(this) for Admin methods * replace waitForSelector to locator * use clickBlockToolbarButton * replace NBSP to unicode escape sequence * fix cast rest and batchRest * fix type error
1 parent 59fb859 commit 65a89c9

File tree

11 files changed

+939
-911
lines changed

11 files changed

+939
-911
lines changed

packages/e2e-test-utils-playwright/src/admin/index.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ export class Admin {
3333
this.pageUtils = pageUtils;
3434
}
3535

36-
createNewPost = createNewPost;
37-
getPageError = getPageError;
38-
visitAdminPage = visitAdminPage;
39-
visitSiteEditor = visitSiteEditor;
36+
createNewPost = createNewPost.bind( this );
37+
getPageError = getPageError.bind( this );
38+
visitAdminPage = visitAdminPage.bind( this );
39+
visitSiteEditor = visitSiteEditor.bind( this );
4040
}

packages/e2e-test-utils-playwright/src/admin/visit-site-editor.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { addQueryArgs } from '@wordpress/url';
88
*/
99
import type { Admin } from './';
1010

11-
interface SiteEditorQueryParams {
11+
export interface SiteEditorQueryParams {
1212
postId: string | number;
1313
postType: string;
1414
}

packages/e2e-test-utils-playwright/src/editor/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { publishPost } from './publish-post';
1616
import { selectBlocks } from './select-blocks';
1717
import { showBlockToolbar } from './show-block-toolbar';
1818
import { saveSiteEditorEntities } from './site-editor';
19+
import { transformBlockTo } from './transform-block-to';
1920

2021
type EditorConstructorProps = {
2122
page: Page;
@@ -52,7 +53,6 @@ export class Editor {
5253

5354
return frame;
5455
}
55-
5656
clickBlockOptionsMenuItem = clickBlockOptionsMenuItem.bind( this );
5757
clickBlockToolbarButton = clickBlockToolbarButton.bind( this );
5858
getEditedPostContent = getEditedPostContent.bind( this );
@@ -63,4 +63,5 @@ export class Editor {
6363
saveSiteEditorEntities = saveSiteEditorEntities.bind( this );
6464
selectBlocks = selectBlocks.bind( this );
6565
showBlockToolbar = showBlockToolbar.bind( this );
66+
transformBlockTo = transformBlockTo.bind( this );
6667
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Internal dependencies
3+
*/
4+
import type { Editor } from './index';
5+
6+
/**
7+
* Clicks the default block appender.
8+
*
9+
* @param {Editor} this
10+
* @param {string} name Block name.
11+
*/
12+
export async function transformBlockTo( this: Editor, name: string ) {
13+
await this.page.evaluate(
14+
( [ blockName ] ) => {
15+
// @ts-ignore (Reason: wp isn't typed)
16+
const clientIds = window.wp.data
17+
.select( 'core/block-editor' )
18+
.getSelectedBlockClientIds();
19+
// @ts-ignore (Reason: wp isn't typed)
20+
const blocks = window.wp.data
21+
.select( 'core/block-editor' )
22+
.getBlocksByClientId( clientIds );
23+
// @ts-ignore (Reason: wp isn't typed)
24+
window.wp.data.dispatch( 'core/block-editor' ).replaceBlocks(
25+
clientIds,
26+
// @ts-ignore (Reason: wp isn't typed)
27+
window.wp.blocks.switchToBlockType( blocks, blockName )
28+
);
29+
},
30+
[ name ]
31+
);
32+
}

packages/e2e-test-utils-playwright/src/page-utils/index.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ class PageUtils {
2929
this.browser = this.context.browser()!;
3030
}
3131

32-
isCurrentURL = isCurrentURL;
33-
pressKeyTimes = pressKeyTimes;
34-
pressKeyWithModifier = pressKeyWithModifier;
35-
setBrowserViewport = setBrowserViewport;
36-
setClipboardData = setClipboardData;
32+
isCurrentURL = isCurrentURL.bind( this );
33+
pressKeyTimes = pressKeyTimes.bind( this );
34+
pressKeyWithModifier = pressKeyWithModifier.bind( this );
35+
setBrowserViewport = setBrowserViewport.bind( this );
36+
setClipboardData = setClipboardData.bind( this );
3737
}
3838

3939
export { PageUtils };

packages/e2e-test-utils-playwright/src/request-utils/index.ts

+20-18
Original file line numberDiff line numberDiff line change
@@ -108,25 +108,27 @@ class RequestUtils {
108108
this.baseURL = baseURL;
109109
}
110110

111-
login = login;
112-
setupRest = setupRest;
113-
rest = rest;
114-
getMaxBatchSize = getMaxBatchSize;
115-
batchRest = batchRest;
116-
getPluginsMap = getPluginsMap;
117-
activatePlugin = activatePlugin;
118-
deactivatePlugin = deactivatePlugin;
119-
activateTheme = activateTheme;
111+
login = login.bind( this );
112+
setupRest = setupRest.bind( this );
113+
// .bind() drops the generic types. Re-casting it to keep the type signature.
114+
rest = rest.bind( this ) as typeof rest;
115+
getMaxBatchSize = getMaxBatchSize.bind( this );
116+
// .bind() drops the generic types. Re-casting it to keep the type signature.
117+
batchRest = batchRest.bind( this ) as typeof batchRest;
118+
getPluginsMap = getPluginsMap.bind( this );
119+
activatePlugin = activatePlugin.bind( this );
120+
deactivatePlugin = deactivatePlugin.bind( this );
121+
activateTheme = activateTheme.bind( this );
120122
deleteAllBlocks = deleteAllBlocks;
121-
deleteAllPosts = deleteAllPosts;
122-
deleteAllWidgets = deleteAllWidgets;
123-
addWidgetBlock = addWidgetBlock;
124-
deleteAllTemplates = deleteAllTemplates;
125-
resetPreferences = resetPreferences;
126-
listMedia = listMedia;
127-
uploadMedia = uploadMedia;
128-
deleteMedia = deleteMedia;
129-
deleteAllMedia = deleteAllMedia;
123+
deleteAllPosts = deleteAllPosts.bind( this );
124+
deleteAllWidgets = deleteAllWidgets.bind( this );
125+
addWidgetBlock = addWidgetBlock.bind( this );
126+
deleteAllTemplates = deleteAllTemplates.bind( this );
127+
resetPreferences = resetPreferences.bind( this );
128+
listMedia = listMedia.bind( this );
129+
uploadMedia = uploadMedia.bind( this );
130+
deleteMedia = deleteMedia.bind( this );
131+
deleteAllMedia = deleteAllMedia.bind( this );
130132
}
131133

132134
export type { StorageState };

packages/e2e-test-utils-playwright/src/request-utils/media.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import * as fs from 'fs';
88
*/
99
import type { RequestUtils } from './index';
1010

11-
interface Media {
11+
export interface Media {
1212
id: number;
1313
title: {
1414
raw: string;

packages/e2e-test-utils-playwright/src/request-utils/rest.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ type RequestFetchOptions = Exclude<
7070
Parameters< APIRequestContext[ 'fetch' ] >[ 1 ],
7171
undefined
7272
>;
73-
interface RestOptions extends RequestFetchOptions {
73+
export interface RestOptions extends RequestFetchOptions {
7474
path: string;
7575
}
7676

@@ -152,7 +152,7 @@ async function getMaxBatchSize( this: RequestUtils, forceRefetch = false ) {
152152
return this.maxBatchSize;
153153
}
154154

155-
interface BatchRequest {
155+
export interface BatchRequest {
156156
method?: string;
157157
path: string;
158158
headers?: Record< string, string | string[] >;

0 commit comments

Comments
 (0)