Skip to content
This repository was archived by the owner on Feb 23, 2024. It is now read-only.

Commit 6872626

Browse files
gigituximanish003
andauthored
Fix Products block tests (#9951)
* fix Products block test * test now * fix test * improve E2E test * restore pw configuration * change order * remove timeout --------- Co-authored-by: Manish Menaria <[email protected]>
1 parent e9a9bda commit 6872626

File tree

4 files changed

+96
-21
lines changed

4 files changed

+96
-21
lines changed

tests/e2e-pw/playwright-utils/test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,8 @@ const test = base.extend<
134134
},
135135
templateApiUtils: async ( {}, use ) =>
136136
await use( new TemplateApiUtils( request ) ),
137-
editorUtils: async ( { editor }, use ) => {
138-
await use( new EditorUtils( editor ) );
137+
editorUtils: async ( { editor, page }, use ) => {
138+
await use( new EditorUtils( editor, page ) );
139139
},
140140
requestUtils: [
141141
async ( {}, use, workerInfo ) => {

tests/e2e-pw/tests/products/products.block_theme.side_effects.spec.ts

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@ const templates = {
2626
templateTitle: 'Product Attribute',
2727
slug: 'taxonomy-product_attribute',
2828
frontendPage: '/product-attribute/color/',
29+
legacyBlockName: 'woocommerce/legacy-template',
2930
},
3031
'taxonomy-product_cat': {
3132
templateTitle: 'Product Category',
3233
slug: 'taxonomy-product_cat',
3334
frontendPage: '/product-category/music/',
35+
legacyBlockName: 'woocommerce/legacy-template',
3436
},
3537
// We don't have products with tags in the test site. Uncomment this when we have products with tags.
3638
// 'taxonomy-product_tag': {
@@ -42,11 +44,13 @@ const templates = {
4244
templateTitle: 'Product Catalog',
4345
slug: 'archive-product',
4446
frontendPage: '/shop/',
47+
legacyBlockName: 'woocommerce/legacy-template',
4548
},
4649
'product-search-results': {
4750
templateTitle: 'Product Search Results',
4851
slug: 'product-search-results',
4952
frontendPage: '/?s=shirt&post_type=product',
53+
legacyBlockName: 'woocommerce/legacy-template',
5054
},
5155
};
5256

@@ -110,14 +114,18 @@ test.describe( `${ blockData.name } Block `, () => {
110114
await expect( inheritQueryFromTemplateOption ).toBeVisible();
111115
} );
112116
} );
113-
for ( const { templateTitle, slug, frontendPage } of Object.values(
114-
templates
115-
) ) {
116-
test.afterAll( async ( { requestUtils } ) => {
117-
await requestUtils.deleteAllTemplates( 'wp_template' );
118-
await requestUtils.deleteAllTemplates( 'wp_template_part' );
119-
} );
120-
test.describe( `${ templateTitle } template`, () =>
117+
118+
for ( const {
119+
templateTitle,
120+
slug,
121+
frontendPage,
122+
legacyBlockName,
123+
} of Object.values( templates ) ) {
124+
test.describe( `${ templateTitle } template`, () => {
125+
test.afterAll( async ( { requestUtils } ) => {
126+
await requestUtils.deleteAllTemplates( 'wp_template' );
127+
await requestUtils.deleteAllTemplates( 'wp_template_part' );
128+
} );
121129
test( 'Products block matches with classic template block', async ( {
122130
admin,
123131
editor,
@@ -132,17 +140,34 @@ for ( const { templateTitle, slug, frontendPage } of Object.values(
132140
await editor.canvas.click( 'body' );
133141
await editor.canvas.waitForLoadState( 'networkidle' );
134142
const block = await editorUtils.getBlockByName( blockData.name );
143+
const clientId = ( await block.getAttribute( 'data-block' ) ) ?? '';
144+
const parentClientId =
145+
( await editorUtils.getBlockRootClientId( clientId ) ) ?? '';
135146
await editor.selectBlocks( block );
136-
await editor.insertBlock( {
137-
name: 'woocommerce/legacy-template',
138-
attributes: {
139-
template: slug,
140-
},
141-
} );
147+
await editorUtils.insertBlock(
148+
{ name: legacyBlockName },
149+
undefined,
150+
parentClientId
151+
);
142152

143153
await editor.saveSiteEditorEntities();
144154

145-
await page.goto( frontendPage );
155+
await page.waitForResponse( ( response ) =>
156+
response.url().includes( 'wp-json/wp/v2/templates/' )
157+
);
158+
159+
// @todo This is a workaround to wait for the save button to be enabled. It works only without Gutenberg enabled. We have to refactor this.
160+
await page
161+
.locator(
162+
"button.edit-site-save-button__button[aria-label='Save'][aria-disabled='true']"
163+
)
164+
.waitFor( {
165+
state: 'visible',
166+
} );
167+
168+
await page.goto( frontendPage, {
169+
waitUntil: 'load',
170+
} );
146171

147172
const classicProducts = await getProductsNameFromClassicTemplate(
148173
page
@@ -152,6 +177,6 @@ for ( const { templateTitle, slug, frontendPage } of Object.values(
152177
);
153178

154179
expect( classicProducts ).toEqual( productQueryProducts );
155-
} )
156-
);
180+
} );
181+
} );
157182
}

tests/e2e-pw/utils/constants.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import path from 'path';
55

66
export const BLOCK_THEME_SLUG = 'twentytwentythree';
77
export const CLASSIC_THEME_SLUG = 'twentytwentyone';
8-
export const MIN_TIMEOUT = 10000;
98
export const BASE_URL = 'http://localhost:8889';
109
export const STORAGE_STATE_PATH = path.join(
1110
process.cwd(),
Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,69 @@
11
/**
22
* External dependencies
33
*/
4+
import { Page } from '@playwright/test';
45
import { Editor } from '@wordpress/e2e-test-utils-playwright';
6+
import { BlockRepresentation } from '@wordpress/e2e-test-utils-playwright/build-types/editor/insert-block';
57
/**
68
* Internal dependencies
79
*/
810

911
export class EditorUtils {
1012
editor: Editor;
11-
constructor( editor: Editor ) {
13+
page: Page;
14+
constructor( editor: Editor, page: Page ) {
1215
this.editor = editor;
16+
this.page = page;
1317
}
1418

1519
async getBlockByName( name: string ) {
1620
return this.editor.canvas.locator( `[data-type="${ name }"]` );
1721
}
22+
23+
// todo: Make a PR to @wordpress/e2e-test-utils-playwright to add this method.
24+
/**
25+
* Inserts a block after a given client ID.
26+
*
27+
*/
28+
async insertBlock(
29+
blockRepresentation: BlockRepresentation,
30+
index?: string,
31+
rootClientId?: string
32+
) {
33+
await this.page.evaluate(
34+
( {
35+
blockRepresentation: _blockRepresentation,
36+
index: _index,
37+
rootClientId: _rootClientId,
38+
} ) => {
39+
function recursiveCreateBlock( {
40+
name,
41+
attributes = {},
42+
innerBlocks = [],
43+
}: BlockRepresentation ): BlockRepresentation {
44+
return window.wp.blocks.createBlock(
45+
name,
46+
attributes,
47+
innerBlocks.map( ( innerBlock ) =>
48+
recursiveCreateBlock( innerBlock )
49+
)
50+
);
51+
}
52+
const block = recursiveCreateBlock( _blockRepresentation );
53+
54+
window.wp.data
55+
.dispatch( 'core/block-editor' )
56+
.insertBlock( block, _index, _rootClientId );
57+
},
58+
{ blockRepresentation, index, rootClientId }
59+
);
60+
}
61+
62+
async getBlockRootClientId( clientId: string ) {
63+
return this.page.evaluate< string | null, string >( ( id ) => {
64+
return window.wp.data
65+
.select( 'core/block-editor' )
66+
.getBlockRootClientId( id );
67+
}, clientId );
68+
}
1869
}

0 commit comments

Comments
 (0)