add common utilities for cart, checkout coupon tests

This commit is contained in:
Ron Rennick 2021-11-17 15:21:22 -04:00
parent 66ad8774d9
commit 202469fd5b
3 changed files with 67 additions and 61 deletions

View File

@ -9,6 +9,7 @@ const {
applyCoupon,
removeCoupon,
} = require( '@woocommerce/e2e-utils' );
const { getCouponId, getCouponsTable } = require( '../utils/coupons' );
/**
* External dependencies
@ -19,36 +20,12 @@ const {
beforeAll,
} = require( '@jest/globals' );
const couponsTable = [
['fixed cart', { text: '$5.00' }, { text: '$4.99' }],
['percentage', { text: '$4.99' }, { text: '$5.00' }],
['fixed product', { text: '$5.00' }, { text: '$4.99' }]
];
let couponFixedCart;
let couponPercentage;
let couponFixedProduct;
const getCoupon = (couponType) => {
switch (couponType) {
case 'fixed cart':
return couponFixedCart;
case 'percentage':
return couponPercentage;
case 'fixed product':
return couponFixedProduct;
}
};
const runCartApplyCouponsTest = () => {
describe('Cart applying coupons', () => {
let productId;
beforeAll(async () => {
productId = await createSimpleProduct();
couponFixedCart = await createCoupon();
couponPercentage = await createCoupon('50', 'Percentage discount');
couponFixedProduct = await createCoupon('5', 'Fixed product discount');
await shopper.emptyCart();
await shopper.goToShop();
await shopper.addToCartFromShopPage( productId );
@ -56,8 +33,8 @@ const runCartApplyCouponsTest = () => {
await shopper.goToCart();
});
it.each(couponsTable)('allows cart to apply %s coupon', async (couponType, cartDiscount, orderTotal) => {
const coupon = getCoupon(couponType);
it.each( getCouponsTable() )( 'allows cart to apply %s coupon', async ( couponType, cartDiscount, orderTotal ) => {
const coupon = await getCouponId( couponType );
await applyCoupon(coupon);
await expect(page).toMatchElement('.woocommerce-message', { text: 'Coupon code applied successfully.' });
@ -69,9 +46,10 @@ const runCartApplyCouponsTest = () => {
});
it('prevents cart applying same coupon twice', async () => {
await applyCoupon(couponFixedCart);
const couponId = await getCouponId( 'fixed cart' );
await applyCoupon( couponId );
await expect(page).toMatchElement('.woocommerce-message', {text: 'Coupon code applied successfully.'});
await applyCoupon(couponFixedCart);
await applyCoupon( couponId );
// Verify only one discount applied
// This is a work around for Puppeteer inconsistently finding 'Coupon code already applied'
await expect(page).toMatchElement('.cart-discount .amount', {text: '$5.00'});
@ -79,7 +57,7 @@ const runCartApplyCouponsTest = () => {
});
it('allows cart to apply multiple coupons', async () => {
await applyCoupon(couponFixedProduct);
await applyCoupon( await getCouponId( 'fixed product' ) );
await expect(page).toMatchElement('.woocommerce-message', {text: 'Coupon code applied successfully.'});
// Verify discount applied and order total
@ -88,8 +66,8 @@ const runCartApplyCouponsTest = () => {
});
it('restores cart total when coupons are removed', async () => {
await removeCoupon(couponFixedCart);
await removeCoupon(couponFixedProduct);
await removeCoupon( await getCouponId( 'fixed cart' ) );
await removeCoupon( await getCouponId( 'fixed product' ) );
await expect(page).toMatchElement('.order-total .amount', {text: '$9.99'});
});
});

View File

@ -10,6 +10,7 @@ const {
removeCoupon,
waitForSelectorWithoutThrow,
} = require( '@woocommerce/e2e-utils' );
const { getCouponId, getCouponsTable } = require( '../utils/coupons' );
/**
* External dependencies
@ -20,26 +21,6 @@ const {
beforeAll,
} = require( '@jest/globals' );
const couponsTable = [
['fixed cart', { text: '$5.00' }, { text: '$4.99' }],
['percentage', { text: '$4.99' }, { text: '$5.00' }],
['fixed product', { text: '$5.00' }, { text: '$4.99' }]
];
let couponFixedCart;
let couponPercentage;
let couponFixedProduct;
const getCoupon = (couponType) => {
switch (couponType) {
case 'fixed cart':
return couponFixedCart;
case 'percentage':
return couponPercentage;
case 'fixed product':
return couponFixedProduct;
}
};
const runCheckoutApplyCouponsTest = () => {
describe('Checkout coupons', () => {
@ -47,9 +28,6 @@ const runCheckoutApplyCouponsTest = () => {
beforeAll(async () => {
productId = await createSimpleProduct();
couponFixedCart = await createCoupon();
couponPercentage = await createCoupon('50', 'Percentage discount');
couponFixedProduct = await createCoupon('5', 'Fixed product discount');
await shopper.emptyCart();
await shopper.goToShop();
await waitForSelectorWithoutThrow( '.add_to_cart_button' );
@ -58,8 +36,8 @@ const runCheckoutApplyCouponsTest = () => {
await shopper.goToCheckout();
});
it.each(couponsTable)('allows checkout to apply %s coupon', async (couponType, cartDiscount, orderTotal) => {
const coupon = getCoupon(couponType);
it.each( getCouponsTable() )( 'allows checkout to apply %s coupon', async ( couponType, cartDiscount, orderTotal ) => {
const coupon = await getCouponId( couponType );
await applyCoupon(coupon);
await expect(page).toMatchElement('.woocommerce-message', { text: 'Coupon code applied successfully.' });
@ -73,9 +51,10 @@ const runCheckoutApplyCouponsTest = () => {
});
it('prevents checkout applying same coupon twice', async () => {
await applyCoupon(couponFixedCart);
const couponId = await getCouponId( 'fixed cart' );
await applyCoupon( couponId );
await expect(page).toMatchElement('.woocommerce-message', {text: 'Coupon code applied successfully.'});
await applyCoupon(couponFixedCart);
await applyCoupon( couponId );
// Verify only one discount applied
// This is a work around for Puppeteer inconsistently finding 'Coupon code already applied'
await expect(page).toMatchElement('.cart-discount .amount', {text: '$5.00'});
@ -83,7 +62,7 @@ const runCheckoutApplyCouponsTest = () => {
});
it('allows checkout to apply multiple coupons', async () => {
await applyCoupon(couponFixedProduct);
await applyCoupon( await getCouponId( 'fixed product' ) );
await expect(page).toMatchElement('.woocommerce-message', {text: 'Coupon code applied successfully.'});
// Verify discount applied and order total
@ -92,8 +71,8 @@ const runCheckoutApplyCouponsTest = () => {
});
it('restores checkout total when coupons are removed', async () => {
await removeCoupon(couponFixedCart);
await removeCoupon(couponFixedProduct);
await removeCoupon( await getCouponId( 'fixed cart' ) );
await removeCoupon( await getCouponId( 'fixed product' ) );
await expect(page).toMatchElement('.order-total .amount', {text: '$9.99'});
});
});

View File

@ -0,0 +1,49 @@
/**
* Internal dependencies
*/
const { createCoupon } = require( '@woocommerce/e2e-utils' );
const couponsTable = [
[ 'fixed cart', { text: '$5.00' }, { text: '$4.99' } ],
[ 'percentage', { text: '$4.99' }, { text: '$5.00' } ],
[ 'fixed product', { text: '$5.00' }, { text: '$4.99' } ]
];
let couponFixedCart;
let couponPercentage;
let couponFixedProduct;
/**
* Get a test coupon Id. Create the coupon if it does not exist.
*
* @param {string} couponType Coupon type.
* @return {string} Coupon code.
*/
const getCouponId = async ( couponType ) => {
switch ( couponType ) {
case 'fixed cart':
if ( ! couponFixedCart ) {
couponFixedCart = await createCoupon();
}
return couponFixedCart;
case 'percentage':
if ( ! couponPercentage ) {
couponPercentage = await createCoupon( '50', 'Percentage discount' );
}
return couponPercentage;
case 'fixed product':
if ( ! couponFixedProduct ) {
couponFixedProduct = await createCoupon( '5', 'Fixed product discount' );
}
return couponFixedProduct;
}
};
const getCouponsTable = () => {
return couponsTable;
};
module.exports = {
getCouponsTable,
getCouponId,
};