2021-09-27 10:04:21 +00:00
|
|
|
/// <reference types="cypress"/>
|
|
|
|
/// <reference types="../../support"/>
|
|
|
|
|
2021-07-23 09:46:44 +00:00
|
|
|
import faker from "faker";
|
|
|
|
|
2021-10-28 14:43:26 +00:00
|
|
|
import { BUTTON_SELECTORS } from "../../elements/shared/button-selectors";
|
|
|
|
import { urlList, voucherDetailsUrl } from "../../fixtures/urlList";
|
2021-09-27 10:04:21 +00:00
|
|
|
import { ONE_PERMISSION_USERS } from "../../fixtures/users";
|
|
|
|
import { createChannel } from "../../support/api/requests/Channels";
|
2021-11-03 11:29:59 +00:00
|
|
|
import { completeCheckout } from "../../support/api/requests/Checkout";
|
2021-09-27 10:04:21 +00:00
|
|
|
import * as channelsUtils from "../../support/api/utils/channelsUtils";
|
2021-10-28 14:43:26 +00:00
|
|
|
import {
|
|
|
|
createVoucherInChannel,
|
|
|
|
deleteVouchersStartsWith
|
|
|
|
} from "../../support/api/utils/discounts/vouchersUtils";
|
2021-07-23 09:46:44 +00:00
|
|
|
import {
|
2021-11-03 11:29:59 +00:00
|
|
|
addPayment,
|
|
|
|
createCheckoutWithVoucher
|
|
|
|
} from "../../support/api/utils/ordersUtils";
|
|
|
|
import * as productsUtils from "../../support/api/utils/products/productsUtils";
|
2021-12-17 11:07:51 +00:00
|
|
|
import { deleteShippingStartsWith } from "../../support/api/utils/shippingUtils";
|
2021-09-27 10:04:21 +00:00
|
|
|
import filterTests from "../../support/filterTests";
|
|
|
|
import {
|
|
|
|
createVoucher,
|
|
|
|
discountOptions
|
|
|
|
} from "../../support/pages/discounts/vouchersPage";
|
2021-07-23 09:46:44 +00:00
|
|
|
|
2021-09-27 10:04:21 +00:00
|
|
|
filterTests({ definedTags: ["all"] }, () => {
|
2021-07-23 09:46:44 +00:00
|
|
|
describe("Vouchers discounts", () => {
|
|
|
|
const startsWith = "CyVou-";
|
|
|
|
const productPrice = 100;
|
|
|
|
const shippingPrice = 100;
|
|
|
|
|
|
|
|
let defaultChannel;
|
|
|
|
let createdChannel;
|
|
|
|
let shippingMethod;
|
|
|
|
let variants;
|
2021-10-28 14:43:26 +00:00
|
|
|
let product;
|
2021-07-23 09:46:44 +00:00
|
|
|
let address;
|
|
|
|
|
|
|
|
before(() => {
|
|
|
|
cy.clearSessionData().loginUserViaRequest();
|
|
|
|
channelsUtils.deleteChannelsStartsWith(startsWith);
|
|
|
|
deleteVouchersStartsWith(startsWith);
|
2021-12-17 11:07:51 +00:00
|
|
|
deleteShippingStartsWith(startsWith);
|
2021-07-23 09:46:44 +00:00
|
|
|
const name = `${startsWith}${faker.datatype.number()}`;
|
|
|
|
productsUtils
|
2021-11-03 11:29:59 +00:00
|
|
|
.createProductWithShipping({ name, productPrice, shippingPrice })
|
2021-07-23 09:46:44 +00:00
|
|
|
.then(
|
|
|
|
({
|
2021-11-03 11:29:59 +00:00
|
|
|
variantsList: variantsResp,
|
|
|
|
defaultChannel: channel,
|
|
|
|
shippingMethod: shippingMethodResp,
|
|
|
|
address: addressResp,
|
|
|
|
product: productResp
|
2021-07-23 09:46:44 +00:00
|
|
|
}) => {
|
2021-11-03 11:29:59 +00:00
|
|
|
variants = variantsResp;
|
|
|
|
defaultChannel = channel;
|
2021-07-23 09:46:44 +00:00
|
|
|
shippingMethod = shippingMethodResp;
|
2021-11-03 11:29:59 +00:00
|
|
|
address = addressResp;
|
|
|
|
product = productResp;
|
|
|
|
createChannel({ name });
|
2021-07-23 09:46:44 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
.then(channel => {
|
|
|
|
createdChannel = channel;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should create percentage voucher", () => {
|
|
|
|
const voucherValue = 50;
|
2021-11-03 11:29:59 +00:00
|
|
|
const voucherCode = `${startsWith}${faker.datatype.number()}`;
|
|
|
|
const expectedAmount =
|
|
|
|
(productPrice * voucherValue) / 100 + shippingPrice;
|
|
|
|
let checkout;
|
2021-07-23 09:46:44 +00:00
|
|
|
|
|
|
|
loginAndCreateCheckoutForVoucherWithDiscount(
|
|
|
|
discountOptions.PERCENTAGE,
|
2021-11-03 11:29:59 +00:00
|
|
|
voucherValue,
|
|
|
|
voucherCode
|
|
|
|
)
|
|
|
|
.then(amount => {
|
|
|
|
expect(amount).to.be.eq(expectedAmount);
|
|
|
|
createCheckoutForCreatedVoucher(voucherCode);
|
|
|
|
})
|
|
|
|
.then(resp => {
|
|
|
|
expect(resp.addPromoCodeResp.checkout.totalPrice.gross.amount).to.eq(
|
|
|
|
expectedAmount
|
|
|
|
);
|
|
|
|
checkout = resp.checkout;
|
|
|
|
addPayment(checkout.id);
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
completeCheckout(checkout.id);
|
|
|
|
})
|
|
|
|
.then(({ order }) => {
|
|
|
|
expect(order.id).to.be.ok;
|
|
|
|
});
|
2021-07-23 09:46:44 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should create fixed price voucher", () => {
|
|
|
|
const voucherValue = 50;
|
2021-11-03 11:29:59 +00:00
|
|
|
const voucherCode = `${startsWith}${faker.datatype.number()}`;
|
|
|
|
const expectedAmount = productPrice + shippingPrice - voucherValue;
|
|
|
|
let checkout;
|
|
|
|
|
2021-07-23 09:46:44 +00:00
|
|
|
loginAndCreateCheckoutForVoucherWithDiscount(
|
|
|
|
discountOptions.FIXED,
|
2021-11-03 11:29:59 +00:00
|
|
|
voucherValue,
|
|
|
|
voucherCode
|
|
|
|
)
|
|
|
|
.then(amount => {
|
|
|
|
expect(amount).to.be.eq(expectedAmount);
|
|
|
|
createCheckoutForCreatedVoucher(voucherCode);
|
|
|
|
})
|
|
|
|
.then(resp => {
|
|
|
|
expect(resp.addPromoCodeResp.checkout.totalPrice.gross.amount).to.eq(
|
|
|
|
expectedAmount
|
|
|
|
);
|
|
|
|
checkout = resp.checkout;
|
|
|
|
addPayment(checkout.id);
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
completeCheckout(checkout.id);
|
|
|
|
})
|
|
|
|
.then(({ order }) => {
|
|
|
|
expect(order.id).to.be.ok;
|
|
|
|
});
|
2021-07-23 09:46:44 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should create free shipping voucher", () => {
|
2021-11-03 11:29:59 +00:00
|
|
|
const voucherCode = `${startsWith}${faker.datatype.number()}`;
|
|
|
|
const expectedAmount = productPrice;
|
|
|
|
let checkout;
|
|
|
|
|
2021-07-23 09:46:44 +00:00
|
|
|
loginAndCreateCheckoutForVoucherWithDiscount(
|
|
|
|
discountOptions.SHIPPING,
|
2021-11-03 11:29:59 +00:00
|
|
|
null,
|
|
|
|
voucherCode
|
|
|
|
)
|
|
|
|
.then(amount => {
|
|
|
|
expect(amount).to.be.eq(expectedAmount);
|
|
|
|
createCheckoutForCreatedVoucher(voucherCode);
|
|
|
|
})
|
|
|
|
.then(resp => {
|
|
|
|
expect(resp.addPromoCodeResp.checkout.totalPrice.gross.amount).to.eq(
|
|
|
|
expectedAmount
|
|
|
|
);
|
|
|
|
checkout = resp.checkout;
|
|
|
|
addPayment(checkout.id);
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
completeCheckout(checkout.id);
|
|
|
|
})
|
|
|
|
.then(({ order }) => {
|
|
|
|
expect(order.id).to.be.ok;
|
|
|
|
});
|
2021-07-23 09:46:44 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should create voucher not available for selected channel", () => {
|
|
|
|
const randomName = `${startsWith}${faker.datatype.number()}`;
|
|
|
|
const voucherValue = 50;
|
|
|
|
|
|
|
|
cy.clearSessionData()
|
|
|
|
.loginUserViaRequest()
|
|
|
|
.visit(urlList.vouchers);
|
|
|
|
cy.softExpectSkeletonIsVisible();
|
|
|
|
createVoucher({
|
|
|
|
voucherCode: randomName,
|
|
|
|
voucherValue,
|
|
|
|
discountOption: discountOptions.PERCENTAGE,
|
|
|
|
channelName: createdChannel.name
|
|
|
|
});
|
2021-09-29 13:24:47 +00:00
|
|
|
createCheckoutForCreatedVoucher(randomName).then(
|
|
|
|
({ addPromoCodeResp }) => {
|
|
|
|
const errorField = addPromoCodeResp.checkoutErrors[0].field;
|
|
|
|
expect(errorField).to.be.eq("promoCode");
|
|
|
|
}
|
|
|
|
);
|
2021-07-23 09:46:44 +00:00
|
|
|
});
|
|
|
|
|
2021-10-28 14:43:26 +00:00
|
|
|
it("should delete voucher", () => {
|
|
|
|
const name = `${startsWith}${faker.datatype.number()}`;
|
|
|
|
const voucherValue = 50;
|
|
|
|
|
|
|
|
let voucher;
|
|
|
|
|
|
|
|
cy.clearSessionData().loginUserViaRequest();
|
|
|
|
createVoucherInChannel({
|
|
|
|
name,
|
|
|
|
productId: product.id,
|
|
|
|
channelId: defaultChannel.id,
|
|
|
|
value: voucherValue
|
|
|
|
})
|
|
|
|
.then(voucherResp => {
|
|
|
|
voucher = voucherResp;
|
|
|
|
expect(voucher.id).to.be.ok;
|
|
|
|
cy.visit(voucherDetailsUrl(voucher.id))
|
|
|
|
.addAliasToGraphRequest("VoucherDelete")
|
|
|
|
.get(BUTTON_SELECTORS.deleteButton)
|
|
|
|
.click()
|
|
|
|
.get(BUTTON_SELECTORS.submit)
|
|
|
|
.click()
|
|
|
|
.wait("@VoucherDelete");
|
|
|
|
createCheckoutForCreatedVoucher(voucher.code);
|
|
|
|
})
|
|
|
|
.then(({ addPromoCodeResp }) => {
|
|
|
|
const errorField = addPromoCodeResp.checkoutErrors[0].field;
|
|
|
|
expect(errorField).to.be.eq("promoCode");
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-07-23 09:46:44 +00:00
|
|
|
function createCheckoutForCreatedVoucher(voucherCode) {
|
|
|
|
return createCheckoutWithVoucher({
|
|
|
|
channelSlug: defaultChannel.slug,
|
|
|
|
variantsList: variants,
|
|
|
|
address,
|
2021-12-15 12:17:35 +00:00
|
|
|
shippingMethodName: shippingMethod.name,
|
2021-07-23 09:46:44 +00:00
|
|
|
voucherCode,
|
|
|
|
auth: "token"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function loginAndCreateCheckoutForVoucherWithDiscount(
|
|
|
|
discount,
|
2021-11-03 11:29:59 +00:00
|
|
|
voucherValue,
|
|
|
|
voucherCode
|
2021-07-23 09:46:44 +00:00
|
|
|
) {
|
|
|
|
cy.clearSessionData()
|
|
|
|
.loginUserViaRequest("auth", ONE_PERMISSION_USERS.discount)
|
|
|
|
.visit(urlList.vouchers);
|
|
|
|
cy.softExpectSkeletonIsVisible();
|
|
|
|
createVoucher({
|
|
|
|
voucherCode,
|
|
|
|
voucherValue,
|
|
|
|
discountOption: discount,
|
|
|
|
channelName: defaultChannel.name
|
|
|
|
});
|
|
|
|
return createCheckoutForCreatedVoucher(voucherCode).its(
|
2021-09-29 13:24:47 +00:00
|
|
|
"addPromoCodeResp.checkout.totalPrice.gross.amount"
|
2021-07-23 09:46:44 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|