diff --git a/cypress/elements/discounts/sales.js b/cypress/elements/discounts/sales.js index 2ba52b48d..d85204c2e 100644 --- a/cypress/elements/discounts/sales.js +++ b/cypress/elements/discounts/sales.js @@ -7,5 +7,7 @@ export const SALES_SELECTORS = { startDateInput: "[name='startDate']", saveButton: "[data-test='button-bar-confirm']", productsTab: "[data-test-id='products-tab']", - assignProducts: "[data-test-id='assign-products']" + variantsTab: "[data-test-id='variants-tab']", + assignProducts: "[data-test-id='assign-products']", + assignVariants: "[data-test-id='assign-variant']" }; diff --git a/cypress/elements/shared/assign-elements-selectors.js b/cypress/elements/shared/assign-elements-selectors.js index 2458a47c4..31995395e 100644 --- a/cypress/elements/shared/assign-elements-selectors.js +++ b/cypress/elements/shared/assign-elements-selectors.js @@ -2,6 +2,7 @@ export const ASSIGN_ELEMENTS_SELECTORS = { searchInput: "[name='query']", tableRow: "[class*='MuiTableRow']", productTableRow: "[data-test-id='assign-product-table-row']", + variantTableRow: "[data-test-id='assign-variant-table-row']", checkbox: "[type='checkbox']", submitButton: "[type='submit']", dialogContent: '[data-test-id="searchQuery"]' diff --git a/cypress/integration/discounts/sales.js b/cypress/integration/discounts/salesForProducts.js similarity index 65% rename from cypress/integration/discounts/sales.js rename to cypress/integration/discounts/salesForProducts.js index 1db94c181..63182d086 100644 --- a/cypress/integration/discounts/sales.js +++ b/cypress/integration/discounts/salesForProducts.js @@ -18,11 +18,12 @@ import filterTests from "../../support/filterTests"; import { assignProducts, createSale, + createSaleWithNewProduct, discountOptions } from "../../support/pages/discounts/salesPage"; filterTests({ definedTags: ["all"] }, () => { - describe("Sales discounts", () => { + describe("Sales discounts for products", () => { const startsWith = "CySales-"; let productType; @@ -80,38 +81,20 @@ filterTests({ definedTags: ["all"] }, () => { const discountValue = 50; const productPrice = 100; - productsUtils - .createProductInChannel({ - name: saleName, - channelId: defaultChannel.id, - warehouseId: warehouse.id, - productTypeId: productType.id, - attributeId: attribute.id, - categoryId: category.id, - price: productPrice - }) - .then(({ product: productResp }) => { - /* Uncomment after fixing SALEOR-3367 bug - cy.clearSessionData() - .loginUserViaRequest("auth", ONE_PERMISSION_USERS.discount) - */ - - cy.visit(urlList.sales); - cy.softExpectSkeletonIsVisible(); - const product = productResp; - createSale({ - saleName, - channelName: defaultChannel.name, - discountValue, - discountOption: discountOptions.PERCENTAGE - }); - assignProducts(product.name); - getProductPrice(product.id, defaultChannel.slug); - }) - .then(price => { - const expectedPrice = (productPrice * discountValue) / 100; - expect(expectedPrice).to.be.eq(price); - }); + createSaleWithNewProduct({ + name: saleName, + channel: defaultChannel, + warehouseId: warehouse.id, + productTypeId: productType.id, + attributeId: attribute.id, + categoryId: category.id, + price: productPrice, + discountOption: discountOptions.PERCENTAGE, + discountValue + }).then(price => { + const expectedPrice = (productPrice * discountValue) / 100; + expect(expectedPrice).to.be.eq(price); + }); }); it("should create fixed price discount", () => { @@ -119,38 +102,20 @@ filterTests({ definedTags: ["all"] }, () => { const discountValue = 50; const productPrice = 100; - productsUtils - .createProductInChannel({ - name: saleName, - channelId: defaultChannel.id, - warehouseId: warehouse.id, - productTypeId: productType.id, - attributeId: attribute.id, - categoryId: category.id, - price: productPrice - }) - .then(({ product: productResp }) => { - /* Uncomment after fixing SALEOR-3367 bug - cy.clearSessionData() - .loginUserViaRequest("auth", ONE_PERMISSION_USERS.discount) - */ - - cy.visit(urlList.sales); - cy.softExpectSkeletonIsVisible(); - const product = productResp; - createSale({ - saleName, - channelName: defaultChannel.name, - discountValue, - discountOption: discountOptions.FIXED - }); - assignProducts(product.name); - getProductPrice(product.id, defaultChannel.slug); - }) - .then(price => { - const expectedPrice = productPrice - discountValue; - expect(expectedPrice).to.be.eq(price); - }); + createSaleWithNewProduct({ + name: saleName, + channel: defaultChannel, + warehouseId: warehouse.id, + productTypeId: productType.id, + attributeId: attribute.id, + categoryId: category.id, + price: productPrice, + discountOption: discountOptions.FIXED, + discountValue + }).then(price => { + const expectedPrice = productPrice - discountValue; + expect(expectedPrice).to.be.eq(price); + }); }); it("should not displayed discount not assign to channel", () => { diff --git a/cypress/integration/discounts/salesForVariants.js b/cypress/integration/discounts/salesForVariants.js new file mode 100644 index 000000000..e7d4d8cab --- /dev/null +++ b/cypress/integration/discounts/salesForVariants.js @@ -0,0 +1,116 @@ +/// +/// + +import faker from "faker"; + +import * as channelsUtils from "../../support/api/utils/channelsUtils"; +import { deleteSalesStartsWith } from "../../support/api/utils/discounts/salesUtils"; +import * as productsUtils from "../../support/api/utils/products/productsUtils"; +import { + createShipping, + deleteShippingStartsWith +} from "../../support/api/utils/shippingUtils"; +import filterTests from "../../support/filterTests"; +import { + createSaleWithNewVariant, + discountOptions +} from "../../support/pages/discounts/salesPage"; + +filterTests({ definedTags: ["all"] }, () => { + describe("Sales discounts for variant", () => { + const startsWith = "CySales-"; + + let productType; + let attribute; + let category; + let defaultChannel; + let warehouse; + + before(() => { + cy.clearSessionData().loginUserViaRequest(); + deleteSalesStartsWith(startsWith); + productsUtils.deleteProductsStartsWith(startsWith); + deleteShippingStartsWith(startsWith); + + const name = `${startsWith}${faker.datatype.number()}`; + productsUtils + .createTypeAttributeAndCategoryForProduct({ name }) + .then( + ({ + productType: productTypeResp, + attribute: attributeResp, + category: categoryResp + }) => { + productType = productTypeResp; + attribute = attributeResp; + category = categoryResp; + + channelsUtils.getDefaultChannel(); + } + ) + .then(channel => { + defaultChannel = channel; + cy.fixture("addresses"); + }) + .then(addresses => { + createShipping({ + channelId: defaultChannel.id, + name, + address: addresses.plAddress, + price: 100 + }); + }) + .then(({ warehouse: warehouseResp }) => { + warehouse = warehouseResp; + }); + }); + + beforeEach(() => { + cy.clearSessionData().loginUserViaRequest(); + }); + + it("should create percentage discount", () => { + const saleName = `${startsWith}${faker.datatype.number()}`; + const discountValue = 50; + const productPrice = 100; + + createSaleWithNewVariant({ + name: saleName, + channel: defaultChannel, + warehouseId: warehouse.id, + productTypeId: productType.id, + attributeId: attribute.id, + categoryId: category.id, + price: productPrice, + discountOption: discountOptions.PERCENTAGE, + discountValue + }).then(({ pricing }) => { + const priceInResponse = pricing.price.gross.amount; + const expectedPrice = (productPrice * discountValue) / 100; + expect(expectedPrice).to.be.eq(priceInResponse); + }); + }); + + it("should create fixed price discount", () => { + const saleName = `${startsWith}${faker.datatype.number()}`; + const discountValue = 50; + const productPrice = 100; + + createSaleWithNewVariant({ + name: saleName, + channel: defaultChannel, + warehouseId: warehouse.id, + productTypeId: productType.id, + attributeId: attribute.id, + categoryId: category.id, + price: productPrice, + discountOption: discountOptions.FIXED, + discountValue + }).then(({ pricing }) => { + const priceInResponse = pricing.price.gross.amount; + const expectedPrice = productPrice - discountValue; + expect(expectedPrice).to.be.eq(priceInResponse); + }); + }); + }); +}); diff --git a/cypress/support/api/requests/Product.js b/cypress/support/api/requests/Product.js index 4cea05bab..01678fc3c 100644 --- a/cypress/support/api/requests/Product.js +++ b/cypress/support/api/requests/Product.js @@ -225,3 +225,31 @@ export function getVariants(variantsList) { }`; return cy.sendRequestWithQuery(query).its("body.data.productVariants"); } + +export function getVariant(id, channel, auth = "auth") { + const query = `query{ + productVariant(id:"${id}" channel:"${channel}"){ + id + name + pricing{ + onSale + discount{ + gross{ + amount + } + } + price{ + gross{ + amount + } + } + priceUndiscounted{ + gross{ + amount + } + } + } + } + }`; + return cy.sendRequestWithQuery(query, auth).its("body.data.productVariant"); +} diff --git a/cypress/support/pages/discounts/salesPage.js b/cypress/support/pages/discounts/salesPage.js index c6b368438..037e0bd64 100644 --- a/cypress/support/pages/discounts/salesPage.js +++ b/cypress/support/pages/discounts/salesPage.js @@ -1,7 +1,11 @@ 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 { formatDate } from "../../../support/formatData/formatDate"; +import { getVariant } from "../../api/requests/Product"; +import { createProductInChannel } from "../../api/utils/products/productsUtils"; +import { getProductPrice } from "../../api/utils/storeFront/storeFrontProductUtils"; import { selectChannelInDetailsPages } from "../channelsPage"; export const discountOptions = { @@ -51,3 +55,98 @@ export function assignProducts(productName) { cy.get(BUTTON_SELECTORS.submit).click(); cy.waitForRequestAndCheckIfNoErrors("@SaleCataloguesAdd"); } + +export function assignVariants(productName, variantName) { + cy.waitForProgressBarToNotBeVisible() + .get(SALES_SELECTORS.variantsTab) + .click() + .get(SALES_SELECTORS.assignVariants) + .click() + .get(ASSIGN_ELEMENTS_SELECTORS.searchInput) + .type(productName) + .get(ASSIGN_ELEMENTS_SELECTORS.variantTableRow) + .should("have.length", 1); + cy.contains(ASSIGN_ELEMENTS_SELECTORS.variantTableRow, variantName) + .find(ASSIGN_ELEMENTS_SELECTORS.checkbox) + .click(); + cy.addAliasToGraphRequest("SaleCataloguesAdd"); + cy.get(BUTTON_SELECTORS.submit).click(); + cy.waitForRequestAndCheckIfNoErrors("@SaleCataloguesAdd"); +} + +export function createSaleWithNewProduct({ + name, + channel, + warehouseId, + productTypeId, + attributeId, + categoryId, + price, + discountOption, + discountValue +}) { + return createProductInChannel({ + name, + channelId: channel.id, + warehouseId, + productTypeId, + attributeId, + categoryId, + price + }).then(({ product: productResp }) => { + /* Uncomment after fixing SALEOR-3367 bug + cy.clearSessionData() + .loginUserViaRequest("auth", ONE_PERMISSION_USERS.discount) + */ + + cy.visit(urlList.sales); + cy.softExpectSkeletonIsVisible(); + const product = productResp; + createSale({ + saleName: name, + channelName: channel.name, + discountValue, + discountOption + }); + assignProducts(product.name); + return getProductPrice(product.id, channel.slug); + }); +} + +export function createSaleWithNewVariant({ + name, + channel, + warehouseId, + productTypeId, + attributeId, + categoryId, + price, + discountValue, + discountOption +}) { + return createProductInChannel({ + name, + channelId: channel.id, + warehouseId, + productTypeId, + attributeId, + categoryId, + price + }).then(({ variantsList: variantsListResp, product }) => { + /* Uncomment after fixing SALEOR-3367 bug + cy.clearSessionData() + .loginUserViaRequest("auth", ONE_PERMISSION_USERS.discount) + */ + cy.visit(urlList.sales); + cy.softExpectSkeletonIsVisible(); + const variant = variantsListResp[0]; + createSale({ + saleName: name, + channelName: channel.name, + discountValue, + discountOption + }); + assignVariants(product.name, variant.name); + return getVariant(variant.id, channel.slug, "token"); + }); +} diff --git a/src/components/AssignVariantDialog/AssignVariantDialog.tsx b/src/components/AssignVariantDialog/AssignVariantDialog.tsx index 47f95922e..77b57ccd7 100644 --- a/src/components/AssignVariantDialog/AssignVariantDialog.tsx +++ b/src/components/AssignVariantDialog/AssignVariantDialog.tsx @@ -220,7 +220,10 @@ const AssignVariantDialog: React.FC = props => { {maybe(() => product.variants, []).map( (variant, variantIndex) => ( - +