From 7e793140e7d99c7f6b2e9f8d5c5db51efac9bce3 Mon Sep 17 00:00:00 2001 From: Karolina Rakoczy Date: Fri, 26 Feb 2021 16:39:42 +0100 Subject: [PATCH] test for variants --- cypress/apiRequests/Attribute.js | 5 +- cypress/apiRequests/Product.js | 7 +- cypress/apiRequests/utils/Utils.js | 8 +-- .../integration/products/productsVariants.js | 68 +++++++------------ cypress/utils/productsUtils.js | 8 +-- 5 files changed, 35 insertions(+), 61 deletions(-) diff --git a/cypress/apiRequests/Attribute.js b/cypress/apiRequests/Attribute.js index 97a86871d..5cd8ef9d2 100644 --- a/cypress/apiRequests/Attribute.js +++ b/cypress/apiRequests/Attribute.js @@ -1,11 +1,12 @@ class Attribute { - createAttribute(name) { + createAttribute(name, attributeValues = ["value"]) { + attributeValues = attributeValues.map(element => `{name:"${element}"}`); const mutation = `mutation{ attributeCreate(input:{ name:"${name}" valueRequired:false type:PRODUCT_TYPE - values:[{name: "value1"}, {name: "value2"}] + values: [${attributeValues}] }){ attribute{ id diff --git a/cypress/apiRequests/Product.js b/cypress/apiRequests/Product.js index e0eda23c9..a365ae10e 100644 --- a/cypress/apiRequests/Product.js +++ b/cypress/apiRequests/Product.js @@ -1,7 +1,6 @@ -import Utils from "./utils/Utils"; +import { getValueWithDefault } from "./utils/Utils"; class Product { - utils = new Utils(); getFirstProducts(first, search) { const filter = search ? `, filter:{ @@ -97,7 +96,7 @@ class Product { price = 1, costPrice = 1 ) { - const channelListings = this.utils.getValueWithDefault( + const channelListings = getValueWithDefault( channelId, `channelListings:{ channelId:"${channelId}" @@ -106,7 +105,7 @@ class Product { }` ); - const stocks = this.utils.getValueWithDefault( + const stocks = getValueWithDefault( warehouseId, `stocks:{ warehouse:"${warehouseId}" diff --git a/cypress/apiRequests/utils/Utils.js b/cypress/apiRequests/utils/Utils.js index cbb37b751..33373f8d1 100644 --- a/cypress/apiRequests/utils/Utils.js +++ b/cypress/apiRequests/utils/Utils.js @@ -1,6 +1,2 @@ -class Utils { - getValueWithDefault(condition, value, defaultValue = "") { - return condition ? value : defaultValue; - } -} -export default Utils; +export const getValueWithDefault = (condition, value, defaultValue = "") => + condition ? value : defaultValue; diff --git a/cypress/integration/products/productsVariants.js b/cypress/integration/products/productsVariants.js index dcd58f185..1beb5da25 100644 --- a/cypress/integration/products/productsVariants.js +++ b/cypress/integration/products/productsVariants.js @@ -1,9 +1,7 @@ import faker from "faker"; -import { visit } from "graphql"; import Channels from "../../apiRequests/Channels"; import Product from "../../apiRequests/Product"; -import ProductDetails from "../../apiRequests/storeFront/ProductDetails"; import VariantsSteps from "../../steps/products/VariantsSteps"; import { urlList } from "../../url/urlList"; import ChannelsUtils from "../../utils/channelsUtils"; @@ -14,6 +12,7 @@ import StoreFrontProductUtils from "../../utils/storeFront/storeFrontProductUtil // describe("creating variants", () => { const startsWith = "Cy-"; + const attributeValues = ["value1", "value2"]; const productUtils = new ProductsUtils(); const channelsUtils = new ChannelsUtils(); @@ -21,7 +20,6 @@ describe("creating variants", () => { const storeFrontProductUtils = new StoreFrontProductUtils(); const product = new Product(); const channels = new Channels(); - const productDetails = new ProductDetails(); const variantsSteps = new VariantsSteps(); @@ -52,11 +50,13 @@ describe("creating variants", () => { ) .then(() => (warehouse = shippingUtils.getWarehouse())); - productUtils.createTypeAttributeAndCategoryForProduct(name).then(() => { - attribute = productUtils.getAttribute(); - productType = productUtils.getProductType(); - category = productUtils.getCategory(); - }); + productUtils + .createTypeAttributeAndCategoryForProduct(name, attributeValues) + .then(() => { + attribute = productUtils.getAttribute(); + productType = productUtils.getProductType(); + category = productUtils.getCategory(); + }); }); beforeEach(() => { @@ -87,22 +87,17 @@ describe("creating variants", () => { createdProduct.id, defaultChannel.slug ); - // productDetails - // .getProductDetails(createdProduct.id, defaultChannel.slug) }) .then(variants => { - // expect(productDetailsResp.body[0].data.product.name).to.equal(name); expect(variants[0].name).to.equal(attribute.values[0].name); expect(variants[0].pricing.price.gross.amount).to.equal(price); - // expect( - // productDetailsResp.body[0].data.product.variants[0].pricing.price - // .gross.amount - // ).to.equal(price); }); }); it("should create several variants", () => { const name = `${startsWith}${faker.random.number()}`; const secondVariantSku = `${startsWith}${faker.random.number()}`; + const firstVariant = { price: 5, attributeValue: attributeValues[0] }; + const secondVariant = { price: 8, attributeValue: attributeValues[1] }; const variantsPrice = 5; let createdProduct; @@ -114,7 +109,7 @@ describe("creating variants", () => { warehouseId: warehouse.id, productTypeId: productType.id, categoryId: category.id, - price: variantsPrice + price: firstVariant.price }) .then(() => { createdProduct = productUtils.getCreatedProduct(); @@ -123,33 +118,18 @@ describe("creating variants", () => { sku: secondVariantSku, warehouseName: warehouse.name, attributeName: attribute.values[1].name, - price: variantsPrice + price: secondVariant.price }); }) - .then( - () => - storeFrontProductUtils.getProductVariants( - createdProduct.id, - defaultChannel.slug - ) - // productDetails.getProductDetails(createdProduct.id, defaultChannel.slug) + .then(() => + storeFrontProductUtils.getProductVariants( + createdProduct.id, + defaultChannel.slug + ) ) .then(variants => { expect(variants).to.have.length(2); - expect(variants[0].pricing.price.gross.amount).to.equal(variantsPrice); - expect(variants[1].pricing.price.gross.amount).to.equal(variantsPrice); - // expect(productDetailsResp.body[0].data.product.name).to.equal(name); - // expect(productDetailsResp.body[0].data.product.variants).to.have.length( - // 2 - // ); - // expect( - // productDetailsResp.body[0].data.product.variants[0].pricing.price - // .gross.amount - // ).to.equal(variantsPrice); - // expect( - // productDetailsResp.body[0].data.product.variants[1].pricing.price - // .gross.amount - // ).to.equal(variantsPrice); + expect(variants).includes(firstVariant, secondVariant); }); }); it("should create variant for many channels", () => { @@ -183,13 +163,12 @@ describe("creating variants", () => { }) .then(() => { cy.visit(`${urlList.products}${createdProduct.id}`); - variantsSteps.createFirstVariant( + variantsSteps.createFirstVariant({ name, - warehouse.id, - variantsPrice, - attribute.name - ); - // productDetails.getProductDetails(product.id, defaultChannel.slug); + warehouseId: warehouse.id, + price: variantsPrice, + attribute: attribute.values[0].name + }); storeFrontProductUtils.getProductVariants( product.id, defaultChannel.slug @@ -200,7 +179,6 @@ describe("creating variants", () => { }) .then(() => { storeFrontProductUtils.getProductVariants(product.id, newChannel.slug); - // productDetails.getProductDetails(product.id, newChannel.slug); }) .then(variants => { expect(variants[0].pricing.price.gross.amount).to.equal(variantsPrice); diff --git a/cypress/utils/productsUtils.js b/cypress/utils/productsUtils.js index 36c49c080..3de053eca 100644 --- a/cypress/utils/productsUtils.js +++ b/cypress/utils/productsUtils.js @@ -57,14 +57,14 @@ class ProductsUtils { }); } - createTypeAttributeAndCategoryForProduct(name) { - return this.createAttribute(name) + createTypeAttributeAndCategoryForProduct(name, attributeValues) { + return this.createAttribute(name, attributeValues) .then(() => this.createTypeProduct(name, this.attribute.id)) .then(() => this.createCategory(name)); } - createAttribute(name) { + createAttribute(name, attributeValues) { return this.attributeRequest - .createAttribute(name) + .createAttribute(name, attributeValues) .then( resp => (this.attribute = resp.body.data.attributeCreate.attribute) );