From 0be7e3deb0123b7bee0ac3ed44f4b14434be7eb6 Mon Sep 17 00:00:00 2001 From: Adrian Moldovan <3854374+adimoldovan@users.noreply.github.com> Date: Tue, 3 Sep 2024 05:53:32 +0100 Subject: [PATCH] [e2e tests] Fix linked products flaky test (#51099) --- .../e2e-fix-create-linked-product-tests | 4 + ...d-product-tab-product-block-editor.spec.js | 75 +++++++++++++++---- 2 files changed, 64 insertions(+), 15 deletions(-) create mode 100644 plugins/woocommerce/changelog/e2e-fix-create-linked-product-tests diff --git a/plugins/woocommerce/changelog/e2e-fix-create-linked-product-tests b/plugins/woocommerce/changelog/e2e-fix-create-linked-product-tests new file mode 100644 index 00000000000..4f73f5cc6c2 --- /dev/null +++ b/plugins/woocommerce/changelog/e2e-fix-create-linked-product-tests @@ -0,0 +1,4 @@ +Significance: patch +Type: dev + + diff --git a/plugins/woocommerce/tests/e2e-pw/tests/merchant/products/block-editor/linked-product-tab-product-block-editor.spec.js b/plugins/woocommerce/tests/e2e-pw/tests/merchant/products/block-editor/linked-product-tab-product-block-editor.spec.js index e5b4cb7ea39..777c933b3fb 100644 --- a/plugins/woocommerce/tests/e2e-pw/tests/merchant/products/block-editor/linked-product-tab-product-block-editor.spec.js +++ b/plugins/woocommerce/tests/e2e-pw/tests/merchant/products/block-editor/linked-product-tab-product-block-editor.spec.js @@ -2,15 +2,19 @@ const { test } = require( '../../../../fixtures/block-editor-fixtures' ); const { expect } = require( '@playwright/test' ); const { clickOnTab } = require( '../../../../utils/simple-products' ); -const { api } = require( '../../../../utils' ); +const { helpers } = require( '../../../../utils' ); const NEW_EDITOR_ADD_PRODUCT_URL = 'wp-admin/admin.php?page=wc-admin&path=%2Fadd-product'; const isTrackingSupposedToBeEnabled = !! process.env.ENABLE_TRACKING; +const uniqueId = helpers.random(); +let categoryId = 0; +const categoryName = `cat_${ uniqueId }`; +const productName = `Product ${ uniqueId }`; const productData = { - name: `Linked product Name ${ new Date().getTime().toString() }`, + name: `Linked ${ productName }`, summary: 'This is a product summary', }; @@ -20,27 +24,45 @@ let productId = 0; test.describe( 'General tab', { tag: '@gutenberg' }, () => { test.describe( 'Linked product', () => { - test.beforeAll( async () => { + test.beforeAll( async ( { api } ) => { + await api + .post( 'products/categories', { + name: categoryName, + } ) + .then( ( response ) => { + categoryId = response.data.id; + } ); + for ( let i = 1; i <= 5; i++ ) { const product = { - name: `Product name ${ i } ${ new Date() - .getTime() - .toString() }`, - productPrice: `${ i }00`, + name: `Product ${ uniqueId } ${ i }`, + regular_price: `${ i }0000`, + sale_price: `${ i }000`, type: 'simple', + categories: [ { id: categoryId } ], }; - linkedProductsData.push( product ); - const id = await api.create.product( product ); - productIds.push( id ); + await api.post( 'products', product ).then( ( response ) => { + productIds.push( response.data.id ); + linkedProductsData.push( product ); + } ); } } ); - test.afterAll( async () => { + test.afterAll( async ( { api } ) => { for ( const aProductId of productIds ) { - await api.deletePost.product( aProductId ); + await api.delete( `products/${ aProductId }`, { + force: true, + } ); } - await api.deletePost.product( productId ); + await api.delete( `products/${ productId }`, { + force: true, + } ); + + await api.delete( `products/categories/${ categoryId }`, { + force: true, + } ); } ); + test.skip( isTrackingSupposedToBeEnabled, 'The block product editor is not being tested' @@ -61,7 +83,30 @@ test.describe( 'General tab', { tag: '@gutenberg' }, () => { .last() .fill( productData.summary ); + // Include in category + await clickOnTab( 'Organization', page ); + const waitForCategoriesResponse = page.waitForResponse( + ( response ) => + response.url().includes( '/wp-json/wp/v2/product_cat' ) && + response.status() === 200 + ); + await page.getByLabel( 'Categories' ).click(); + await waitForCategoriesResponse; + await page.getByLabel( categoryName ).check(); + await page.getByLabel( `Remove Uncategorized` ).click(); + await expect( + page.getByLabel( `Remove ${ categoryName }` ) + ).toBeVisible(); + + const waitForProductsSearchResponse = page.waitForResponse( + ( response ) => + response + .url() + .includes( '/wp-json/wc/v3/products?search' ) && + response.status() === 200 + ); await clickOnTab( 'Linked products', page ); + await waitForProductsSearchResponse; await expect( page.getByRole( 'heading', { @@ -75,7 +120,7 @@ test.describe( 'General tab', { tag: '@gutenberg' }, () => { ) .first() .getByRole( 'combobox' ) - .fill( linkedProductsData[ 0 ].name ); + .fill( productName ); await page.getByText( linkedProductsData[ 0 ].name ).click(); @@ -92,7 +137,7 @@ test.describe( 'General tab', { tag: '@gutenberg' }, () => { await chooseProductsResponsePromise; await expect( - page.getByRole( 'row', { name: 'Product name' } ) + page.getByRole( 'row', { name: productName } ) ).toHaveCount( 4 ); const upsellsRows = page.locator(