diff --git a/cypress/e2e/discounts/sales/createSalesForVariants.js b/cypress/e2e/discounts/sales/createSalesForVariants.js index 942edf83d..e39632834 100644 --- a/cypress/e2e/discounts/sales/createSalesForVariants.js +++ b/cypress/e2e/discounts/sales/createSalesForVariants.js @@ -7,7 +7,10 @@ import { createCheckout } from "../../../support/api/requests/Checkout"; import { updateSale } from "../../../support/api/requests/Discounts/Sales"; import { createVariant } from "../../../support/api/requests/Product"; import * as channelsUtils from "../../../support/api/utils/channelsUtils"; -import { createSaleInChannel } from "../../../support/api/utils/discounts/salesUtils"; +import { + createSaleInChannel, + getVariantWithSaleStatus, +} from "../../../support/api/utils/discounts/salesUtils"; import * as productsUtils from "../../../support/api/utils/products/productsUtils"; import { createShipping } from "../../../support/api/utils/shippingUtils"; import { @@ -95,6 +98,8 @@ describe("Sales discounts for variant", () => { { tags: ["@sales", "@allEnv", "@stable"] }, () => { const saleName = `${startsWith}${faker.datatype.number()}`; + const variantSku = `${startsWith}${faker.datatype.number()}`; + const productSku = `${startsWith}${faker.datatype.number()}`; const productName = faker.commerce.product(); const productSlug = productName + faker.datatype.number(); const productPriceOnSale = productPrice - discountValue; @@ -113,13 +118,14 @@ describe("Sales discounts for variant", () => { ...productData, name: productName, slug: productSlug, + sku: productSku, }) .then(({ product, variantsList }) => { variantNotOnSale = variantsList; createVariant({ productId: product.id, - sku: saleName, + sku: variantSku, attributeId: productData.attributeId, attributeName: "value2", warehouseId: warehouse.id, @@ -131,6 +137,11 @@ describe("Sales discounts for variant", () => { }) .then(variantsList => { updateSale({ saleId: sale.id, variants: variantsList }); + getVariantWithSaleStatus({ + channelSlug: defaultChannel.slug, + variantId: variantsList[0].id, + onSaleStatus: true, + }); createCheckout({ channelSlug: defaultChannel.slug, email: "example@example.com", diff --git a/cypress/e2e/discounts/sales/updateSales.js b/cypress/e2e/discounts/sales/updateSales.js index 6a93c6b5e..0401ae648 100644 --- a/cypress/e2e/discounts/sales/updateSales.js +++ b/cypress/e2e/discounts/sales/updateSales.js @@ -8,12 +8,12 @@ import { BUTTON_SELECTORS } from "../../../elements/shared/button-selectors"; import { SHARED_ELEMENTS } from "../../../elements/shared/sharedElements"; import { saleDetailsUrl } from "../../../fixtures/urlList"; import { updateSale } from "../../../support/api/requests/Discounts/Sales"; -import { getVariant } from "../../../support/api/requests/Product"; import { getDefaultChannel } from "../../../support/api/utils/channelsUtils"; import { createSaleInChannel, createSaleInChannelWithProduct, deleteSalesStartsWith, + getVariantWithSaleStatus, } from "../../../support/api/utils/discounts/salesUtils"; import { createProductInChannel, @@ -116,9 +116,10 @@ describe("As an admin I want to update sales", () => { { tags: ["@sales", "@allEnv", "@stable"] }, () => { const productName = `${startsWith}${faker.datatype.number()}`; + const productSku = `${startsWith}${faker.datatype.number()}`; productData.name = productName; - productData.sku = productName; + productData.sku = productSku; createProductInChannel(productData) .then(({ variantsList }) => { @@ -133,9 +134,12 @@ describe("As an admin I want to update sales", () => { }); }) .then(saleResp => { - getVariant(variants[0].id, defaultChannel.slug) + getVariantWithSaleStatus({ + channelSlug: defaultChannel.slug, + variantId: variants[0].id, + onSaleStatus: true, + }) .its("pricing") - .should("include", { onSale: true }) .its("price.gross.amount") .should("eq", productPriceOnSale); cy.visit(saleDetailsUrl(saleResp.id)) @@ -145,9 +149,12 @@ describe("As an admin I want to update sales", () => { .get(BUTTON_SELECTORS.submit) .click() .wait("@SaleDelete"); - getVariant(variants[0].id, defaultChannel.slug) + getVariantWithSaleStatus({ + channelSlug: defaultChannel.slug, + variantId: variants[0].id, + onSaleStatus: false, + }) .its("pricing") - .should("include", { onSale: false }) .its("price.gross.amount") .should("eq", productPrice); }); @@ -173,9 +180,12 @@ describe("As an admin I want to update sales", () => { saleId: sale.id, variants, }); - getVariant(variants[0].id, defaultChannel.slug) + getVariantWithSaleStatus({ + channelSlug: defaultChannel.slug, + variantId: variants[0].id, + onSaleStatus: true, + }) .its("pricing") - .should("include", { onSale: true }) .its("price.gross.amount") .should("eq", productPriceOnSale); cy.visit(saleDetailsUrl(sale.id)) @@ -188,9 +198,12 @@ describe("As an admin I want to update sales", () => { .get(BUTTON_SELECTORS.submit) .click() .wait("@SaleCataloguesRemove"); - getVariant(variants[0].id, defaultChannel.slug) + getVariantWithSaleStatus({ + channelSlug: defaultChannel.slug, + variantId: variants[0].id, + onSaleStatus: false, + }) .its("pricing") - .should("include", { onSale: false }) .its("price.gross.amount") .should("eq", productPrice); }, diff --git a/cypress/support/api/utils/discounts/salesUtils.js b/cypress/support/api/utils/discounts/salesUtils.js index ca4bab407..7adb7b972 100644 --- a/cypress/support/api/utils/discounts/salesUtils.js +++ b/cypress/support/api/utils/discounts/salesUtils.js @@ -1,9 +1,10 @@ +import { getVariant } from "../../../../support/api/requests/Product"; import { addChannelToSale, createSale, deleteSale, getSales, - updateSale + updateSale, } from "../../requests/Discounts/Sales"; export function deleteSalesStartsWith(startsWith) { @@ -15,13 +16,13 @@ export function createSaleInChannel({ type, value, channelId, - discountValue = value + discountValue = value, }) { let sale; return createSale({ name, type, - value + value, }) .then(saleResp => { sale = saleResp; @@ -29,6 +30,26 @@ export function createSaleInChannel({ }) .then(() => sale); } +export function getVariantWithSaleStatus( + { variantId, channelSlug, onSaleStatus }, + retries = 0, +) { + return getVariant(variantId, channelSlug).then(salesResponse => { + if (salesResponse.pricing.onSale === onSaleStatus) { + return; + } else if (retries > 4) { + throw new Error( + `Variant field onSale should have value: ${onSaleStatus} but has opposite. Retried for ${retries} times`, + ); + } else { + cy.wait(5000); + getVariantWithSaleStatus( + { variantId, channelSlug, onSaleStatus }, + retries + 1, + ); + } + }); +} export function createSaleInChannelWithProduct({ name, @@ -36,21 +57,21 @@ export function createSaleInChannelWithProduct({ value, channelId, variants, - productId + productId, }) { let sale; return createSaleInChannel({ name, type, value, - channelId + channelId, }) .then(saleResp => { sale = saleResp; updateSale({ saleId: sale.id, variants, - productId + productId, }); }) .then(() => sale); diff --git a/cypress/support/pages/discounts/salesPage.js b/cypress/support/pages/discounts/salesPage.js index b1b66ef51..208a778b2 100644 --- a/cypress/support/pages/discounts/salesPage.js +++ b/cypress/support/pages/discounts/salesPage.js @@ -2,6 +2,7 @@ import { SALES_SELECTORS } from "../../../elements/discounts/sales"; import { ASSIGN_ELEMENTS_SELECTORS } from "../../../elements/shared/assign-elements-selectors"; import { BUTTON_SELECTORS } from "../../../elements/shared/button-selectors"; import { urlList } from "../../../fixtures/urlList"; +import { getVariantWithSaleStatus } from "../../../support/api/utils/discounts/salesUtils"; import { formatDate } from "../../../support/formatData/formatDate"; import { getVariant } from "../../api/requests/Product"; import { createProductInChannel } from "../../api/utils/products/productsUtils"; @@ -150,6 +151,11 @@ export function createSaleWithNewVariant({ discountOption, }); assignVariants(product.name, variant.name); + getVariantWithSaleStatus({ + channelSlug: channel.slug, + variantId: variant.id, + onSaleStatus: true, + }); return getVariant(variant.id, channel.slug, "token"); }); }