update order coupon test to use api, evalAndClick

This commit is contained in:
Ron Rennick 2021-04-21 14:45:45 -03:00
parent 6ece4bc82d
commit c4074e7bf2
2 changed files with 31 additions and 31 deletions

View File

@ -28,29 +28,27 @@ const runOrderApplyCouponTest = () => {
describe('WooCommerce Orders > Apply coupon', () => {
beforeAll(async () => {
await merchant.login();
await Promise.all([
await createSimpleProduct(),
couponCode = await createCoupon(),
orderId = await createSimpleOrder('Pending payment', simpleProductName),
await addProductToOrder(orderId, simpleProductName),
await createSimpleProduct();
couponCode = await createCoupon();
orderId = await createSimpleOrder('Pending payment', simpleProductName);
await addProductToOrder(orderId, simpleProductName);
// We need to remove any listeners on the `dialog` event otherwise we can't catch the dialog below
page.removeAllListeners('dialog'),
]);
// We need to remove any listeners on the `dialog` event otherwise we can't catch the dialog below
await page.removeAllListeners('dialog');
// Make sure the simple product price is greater than the coupon amount
await expect(Number(simpleProductPrice)).toBeGreaterThan(5.00);
} );
it('can apply a coupon', async () => {
await page.waitForSelector('button.add-coupon');
const couponDialog = await expect(page).toDisplayDialog(async () => {
await expect(page).toClick('button.add-coupon');
await evalAndClick('button.add-coupon');
});
expect(couponDialog.message()).toMatch(couponDialogMessage);
// Accept the dialog with the coupon code
await couponDialog.accept(couponCode);
await uiUnblocked();
// Verify the coupon list is showing
@ -64,12 +62,6 @@ const runOrderApplyCouponTest = () => {
});
it('can remove a coupon', async () => {
// Temporarily add screenshot
const screenshot = await takeScreenshotFor( 'before removing a coupon' );
if ( screenshot.filePath ) {
await sendFailedTestScreenshotToSlack( screenshot.filePath );
}
// Make sure we have a coupon on the page to use
await page.waitForSelector('.wc-used-coupons');
await expect(page).toMatchElement('.wc_coupon_list li.code.editable', { text: couponCode.toLowerCase() });

View File

@ -17,7 +17,9 @@ import {
clearAndFillInput,
} from './page-utils';
import factories from './factories';
import { Coupon } from '@woocommerce/api';
const client = factories.api.withDefaultPermalinks;
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';
@ -452,23 +454,29 @@ const addProductToOrder = async ( orderId, productName ) => {
* @param discountType Type of a coupon. Defaults to Fixed cart discount.
*/
const createCoupon = async ( couponAmount = '5', discountType = 'Fixed cart discount' ) => {
await merchant.openNewCoupon();
let couponType;
switch ( discountType ) {
case "Fixed cart discount":
couponType = 'fixed_cart';
break;
case "Fixed product discount":
couponType = 'fixed_product';
break;
case "Percentage discount":
couponType = 'percent';
break;
default:
return '';
}
// Fill in coupon code
let couponCode = 'Code-' + discountType + new Date().getTime().toString();
await expect(page).toFill( '#title', couponCode );
// Set general coupon data
await clickTab( 'General' );
await expect(page).toSelect( '#discount_type', discountType );
await expect(page).toFill( '#coupon_amount', couponAmount );
// Publish coupon
await expect( page ).toClick( '#publish' );
await page.waitForSelector( '.updated.notice' );
// Verify
await expect( page ).toMatchElement( '.updated.notice', { text: 'Coupon updated.' } );
let couponCode = 'code-' + couponType + new Date().getTime().toString();
const repository = Coupon.restRepository( client );
await repository.create( {
code: couponCode,
discountType: couponType,
amount: couponAmount,
});
return couponCode;
};