Use category ID instead of category name

This commit is contained in:
Greg 2021-10-29 15:19:14 -06:00
parent ec5ab7cdb5
commit 5ba261950c
4 changed files with 38 additions and 3 deletions

View File

@ -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

View File

@ -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

View File

@ -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,

View File

@ -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<Number>} 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;
},
};