Fix instantiation of grouped product variables

This commit is contained in:
Rodel Calasagsag 2021-06-09 17:08:45 +08:00
parent 78ef1e4411
commit f2305a2486
2 changed files with 68 additions and 34 deletions

View File

@ -21,6 +21,7 @@ const defaultVariableProduct = config.get( 'products.variable' );
let variableProductId;
// Variables for grouped product
const simpleProductPrice = config.has('products.simple.price') ? config.get('products.simple.price') : '9.99';
let groupedPostIdValue;
const runSingleProductPageTest = () => {
@ -86,7 +87,22 @@ const runSingleProductPageTest = () => {
describe('Grouped Product Page', () => {
beforeAll(async () => {
groupedPostIdValue = await createGroupedProduct();
// Instantiate simple products to be grouped
const simple1 = {
name: simpleProductName + ' 1',
regularPrice: simpleProductPrice
};
const simple2 = {
name: simpleProductName + ' 2',
regularPrice: simpleProductPrice
};
// Create grouped product
const groupedProduct = {
name: 'Grouped Product',
groupedProducts: [simple1, simple2]
};
groupedPostIdValue = await createGroupedProduct(groupedProduct);
});
it('should be able to add grouped products to the cart', async () => {
@ -94,37 +110,51 @@ const runSingleProductPageTest = () => {
await shopper.goToProduct(groupedPostIdValue);
await page.waitForSelector('form.grouped_form');
await shopper.addToCart();
await expect(page).toMatchElement('.woocommerce-error',
{text: 'Please choose the quantity of items you wish to add to your cart…'});
await expect(page).toMatchElement('.woocommerce-error', {
text:
'Please choose the quantity of items you wish to add to your cart…'
});
const quantityFields = await page.$$('div.quantity input.qty');
await quantityFields[0].click({clickCount: 3});
await quantityFields[0].click({ clickCount: 3 });
await quantityFields[0].type('5');
await quantityFields[1].click({clickCount: 3});
await quantityFields[1].click({ clickCount: 3 });
await quantityFields[1].type('5');
await shopper.addToCart();
await expect(page).toMatchElement('.woocommerce-message',
{text: '“'+simpleProductName+' 1” and “'+simpleProductName+' 2” have been added to your cart.'});
await expect(page).toMatchElement('.woocommerce-message', {
text:
'“' +
simpleProductName +
' 1” and “' +
simpleProductName +
' 2” have been added to your cart.'
});
// Verify cart contents
await shopper.goToCart();
await shopper.productIsInCart(simpleProductName+' 1');
await shopper.productIsInCart(simpleProductName+' 2');
await shopper.productIsInCart(simpleProductName + ' 1');
await shopper.productIsInCart(simpleProductName + ' 2');
});
it('should be able to remove grouped products from the cart', async () => {
// Remove items from cart
await shopper.removeFromCart(simpleProductName+' 1');
await shopper.removeFromCart(simpleProductName + ' 1');
await uiUnblocked();
await expect(page).toMatchElement('.woocommerce-message', {text: '“'+simpleProductName+' 1” removed.'});
await Promise.all( [
await expect(page).toMatchElement('.woocommerce-message', {
text: '“' + simpleProductName + ' 1” removed.'
});
await Promise.all([
// Reload page and perform item removal, since removeFromCart won't remove it when placed in a row
page.reload(),
page.waitForNavigation( { waitUntil: 'networkidle0' } ),
] );
await shopper.removeFromCart(simpleProductName+' 2');
page.waitForNavigation({ waitUntil: 'networkidle0' })
]);
await shopper.removeFromCart(simpleProductName + ' 2');
await uiUnblocked();
await expect(page).toMatchElement('.woocommerce-message', {text: '“'+simpleProductName+' 2” removed.'});
await expect(page).toMatchElement('.cart-empty', {text: 'Your cart is currently empty.'});
await expect(page).toMatchElement('.woocommerce-message', {
text: '“' + simpleProductName + ' 2” removed.'
});
await expect(page).toMatchElement('.cart-empty', {
text: 'Your cart is currently empty.'
});
});
});
};

View File

@ -23,6 +23,7 @@ const config = require( 'config' );
const simpleProductName = config.get( 'products.simple.name' );
const simpleProductPrice = config.has('products.simple.price') ? config.get('products.simple.price') : '9.99';
const defaultVariableProduct = config.get('products.variable');
const defaultGroupedProduct = config.get('products.grouped');
/**
* Verify and publish
@ -226,7 +227,7 @@ const createSimpleProductWithCategory = async ( productName, productPrice, categ
*/
const createVariableProduct = async (varProduct = defaultVariableProduct) => {
const { attributes } = varProduct;
const { id } = await factories.products.variable.create(varProduct);
const { id } = await factories.products.variable.create(varProduct); // create the variable product
const variations = [];
const buffer = []; // accumulated attributes while looping
const aIdx = 0; // attributes[] index
@ -292,25 +293,28 @@ const createVariableProduct = async (varProduct = defaultVariableProduct) => {
/**
* Create grouped product.
*
* @param groupedProduct Defaults to the grouped product object in `default.json`
* @returns ID of the grouped product
*/
const createGroupedProduct = async () => {
// Create two products to be linked in a grouped product after
const simple1 = await factories.products.simple.create({
name: simpleProductName + ' 1',
regularPrice: simpleProductPrice
});
const simple2 = await factories.products.simple.create({
name: simpleProductName + ' 2',
regularPrice: simpleProductPrice
});
const groupedProduct = {
name: 'Grouped Product',
type: 'grouped',
groupedProducts: [simple1.id, simple2.id]
};
const createGroupedProduct = async (groupedProduct = defaultGroupedProduct) => {
const { name, groupedProducts } = groupedProduct;
const simpleProductIds = [];
let groupedProductRequest;
const { id } = await factories.products.grouped.create(groupedProduct);
// Using the api, create simple products to be grouped
for (const simpleProduct of groupedProducts) {
const { id } = await factories.products.simple.create(simpleProduct);
simpleProductIds.push(id);
}
// Using the api, create the grouped product
groupedProductRequest = {
name: name,
groupedProducts: simpleProductIds
};
const { id } = await factories.products.grouped.create(
groupedProductRequest
);
return id;
};