From 5ba261950c62756ad64e9f4817bde6e381b9009d Mon Sep 17 00:00:00 2001 From: Greg Date: Fri, 29 Oct 2021 15:19:14 -0600 Subject: [PATCH] Use category ID instead of category name --- packages/js/e2e-utils/CHANGELOG.md | 1 + packages/js/e2e-utils/README.md | 3 +- packages/js/e2e-utils/src/components.js | 5 ++- .../js/e2e-utils/src/flows/with-rest-api.js | 32 ++++++++++++++++++- 4 files changed, 38 insertions(+), 3 deletions(-) diff --git a/packages/js/e2e-utils/CHANGELOG.md b/packages/js/e2e-utils/CHANGELOG.md index 9d786acaf9d..714df19fe2c 100644 --- a/packages/js/e2e-utils/CHANGELOG.md +++ b/packages/js/e2e-utils/CHANGELOG.md @@ -5,6 +5,7 @@ - `utils.waitForTimeout( delay )` pause processing for `delay` milliseconds - `AdminEdit` class with utility functions for the respective edit screens - Update `shopper.addToCartFromShopPage()` and `.removeFromCart()` to accept product Id or Title +- Added `withRestApi.createProductCategory()` that creates a product category and returns the ID # 0.1.6 diff --git a/packages/js/e2e-utils/README.md b/packages/js/e2e-utils/README.md index 5a6b4fb6656..7d3ca064be4 100644 --- a/packages/js/e2e-utils/README.md +++ b/packages/js/e2e-utils/README.md @@ -151,7 +151,8 @@ This package provides support for enabling retries in tests: | `deleteAllOrders` | | Permanently delete all orders | | `updateSettingOption` | `settingsGroup`, `settingID`, `payload` | Update a settings group | | `updatePaymentGateway`| `paymentGatewayId`, `payload` | Update a payment gateway | -| `getSystemEnvironment` | | Get the current environment from the WooCommerce system status API. +| `getSystemEnvironment` | | Get the current environment from the WooCommerce system status API. | +| `createProductCategory` | `categoryName` | Create a product category with the provided name. | ### Classes diff --git a/packages/js/e2e-utils/src/components.js b/packages/js/e2e-utils/src/components.js index 86ddb9d2ad9..fc930cabff7 100644 --- a/packages/js/e2e-utils/src/components.js +++ b/packages/js/e2e-utils/src/components.js @@ -208,12 +208,15 @@ const createSimpleProduct = async ( productTitle = simpleProductName, productPri * @param categoryName Product's category which can be changed when writing a test */ const createSimpleProductWithCategory = async ( productName, productPrice, categoryName ) => { + // Get the category ID so we can add it to the product below + const categoryId = await withRestApi.createProductCategory( categoryName ); + const product = await factories.products.simple.create( { name: productName, regularPrice: productPrice, categories: [ { - name: categoryName, + id: categoryId, } ], isVirtual: true, diff --git a/packages/js/e2e-utils/src/flows/with-rest-api.js b/packages/js/e2e-utils/src/flows/with-rest-api.js index e4e4a72817d..ba6a0485cf1 100644 --- a/packages/js/e2e-utils/src/flows/with-rest-api.js +++ b/packages/js/e2e-utils/src/flows/with-rest-api.js @@ -300,5 +300,35 @@ export const withRestApi = { } else { return; } - } + }, + /** + * Create a product category and return the ID. If the category already exists, the ID of the existing category is returned. + * + * @param {String} categoryName The name of the category to create + * @returns {Promise} The ID of the category. + */ + createProductCategory: async ( categoryName ) => { + const payload = { name: categoryName }; + let categoryId; + + try { + const response = await client.post( productCategoriesEndpoint, payload ); + expect( response.status ).toEqual( 201 ); + categoryId = response.data.id; + } catch ( err ) { + if ( err.response.status === 400 && err.response.data.code === 'term_exists' ) { + // Get the list of categories and pull ID to return + const categoryList = await client.get( productCategoriesEndpoint ); + expect( categoryList.status ).toEqual( 200 ); + if ( categoryList.data && categoryList.data.length ) { + for ( let c = 0; c < categoryList.data.length; c++ ) { + if ( categoryList.data[c].name === categoryName ) { + categoryId = categoryList.data[c].id; + } + } + } + } + } + return categoryId; + }, };