test for variants

This commit is contained in:
Karolina Rakoczy 2021-02-26 16:39:42 +01:00
parent 089a2c428d
commit 7e793140e7
5 changed files with 35 additions and 61 deletions

View file

@ -1,11 +1,12 @@
class Attribute { class Attribute {
createAttribute(name) { createAttribute(name, attributeValues = ["value"]) {
attributeValues = attributeValues.map(element => `{name:"${element}"}`);
const mutation = `mutation{ const mutation = `mutation{
attributeCreate(input:{ attributeCreate(input:{
name:"${name}" name:"${name}"
valueRequired:false valueRequired:false
type:PRODUCT_TYPE type:PRODUCT_TYPE
values:[{name: "value1"}, {name: "value2"}] values: [${attributeValues}]
}){ }){
attribute{ attribute{
id id

View file

@ -1,7 +1,6 @@
import Utils from "./utils/Utils"; import { getValueWithDefault } from "./utils/Utils";
class Product { class Product {
utils = new Utils();
getFirstProducts(first, search) { getFirstProducts(first, search) {
const filter = search const filter = search
? `, filter:{ ? `, filter:{
@ -97,7 +96,7 @@ class Product {
price = 1, price = 1,
costPrice = 1 costPrice = 1
) { ) {
const channelListings = this.utils.getValueWithDefault( const channelListings = getValueWithDefault(
channelId, channelId,
`channelListings:{ `channelListings:{
channelId:"${channelId}" channelId:"${channelId}"
@ -106,7 +105,7 @@ class Product {
}` }`
); );
const stocks = this.utils.getValueWithDefault( const stocks = getValueWithDefault(
warehouseId, warehouseId,
`stocks:{ `stocks:{
warehouse:"${warehouseId}" warehouse:"${warehouseId}"

View file

@ -1,6 +1,2 @@
class Utils { export const getValueWithDefault = (condition, value, defaultValue = "") =>
getValueWithDefault(condition, value, defaultValue = "") { condition ? value : defaultValue;
return condition ? value : defaultValue;
}
}
export default Utils;

View file

@ -1,9 +1,7 @@
import faker from "faker"; import faker from "faker";
import { visit } from "graphql";
import Channels from "../../apiRequests/Channels"; import Channels from "../../apiRequests/Channels";
import Product from "../../apiRequests/Product"; import Product from "../../apiRequests/Product";
import ProductDetails from "../../apiRequests/storeFront/ProductDetails";
import VariantsSteps from "../../steps/products/VariantsSteps"; import VariantsSteps from "../../steps/products/VariantsSteps";
import { urlList } from "../../url/urlList"; import { urlList } from "../../url/urlList";
import ChannelsUtils from "../../utils/channelsUtils"; import ChannelsUtils from "../../utils/channelsUtils";
@ -14,6 +12,7 @@ import StoreFrontProductUtils from "../../utils/storeFront/storeFrontProductUtil
// <reference types="cypress" /> // <reference types="cypress" />
describe("creating variants", () => { describe("creating variants", () => {
const startsWith = "Cy-"; const startsWith = "Cy-";
const attributeValues = ["value1", "value2"];
const productUtils = new ProductsUtils(); const productUtils = new ProductsUtils();
const channelsUtils = new ChannelsUtils(); const channelsUtils = new ChannelsUtils();
@ -21,7 +20,6 @@ describe("creating variants", () => {
const storeFrontProductUtils = new StoreFrontProductUtils(); const storeFrontProductUtils = new StoreFrontProductUtils();
const product = new Product(); const product = new Product();
const channels = new Channels(); const channels = new Channels();
const productDetails = new ProductDetails();
const variantsSteps = new VariantsSteps(); const variantsSteps = new VariantsSteps();
@ -52,7 +50,9 @@ describe("creating variants", () => {
) )
.then(() => (warehouse = shippingUtils.getWarehouse())); .then(() => (warehouse = shippingUtils.getWarehouse()));
productUtils.createTypeAttributeAndCategoryForProduct(name).then(() => { productUtils
.createTypeAttributeAndCategoryForProduct(name, attributeValues)
.then(() => {
attribute = productUtils.getAttribute(); attribute = productUtils.getAttribute();
productType = productUtils.getProductType(); productType = productUtils.getProductType();
category = productUtils.getCategory(); category = productUtils.getCategory();
@ -87,22 +87,17 @@ describe("creating variants", () => {
createdProduct.id, createdProduct.id,
defaultChannel.slug defaultChannel.slug
); );
// productDetails
// .getProductDetails(createdProduct.id, defaultChannel.slug)
}) })
.then(variants => { .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].name).to.equal(attribute.values[0].name);
expect(variants[0].pricing.price.gross.amount).to.equal(price); 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", () => { it("should create several variants", () => {
const name = `${startsWith}${faker.random.number()}`; const name = `${startsWith}${faker.random.number()}`;
const secondVariantSku = `${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; const variantsPrice = 5;
let createdProduct; let createdProduct;
@ -114,7 +109,7 @@ describe("creating variants", () => {
warehouseId: warehouse.id, warehouseId: warehouse.id,
productTypeId: productType.id, productTypeId: productType.id,
categoryId: category.id, categoryId: category.id,
price: variantsPrice price: firstVariant.price
}) })
.then(() => { .then(() => {
createdProduct = productUtils.getCreatedProduct(); createdProduct = productUtils.getCreatedProduct();
@ -123,33 +118,18 @@ describe("creating variants", () => {
sku: secondVariantSku, sku: secondVariantSku,
warehouseName: warehouse.name, warehouseName: warehouse.name,
attributeName: attribute.values[1].name, attributeName: attribute.values[1].name,
price: variantsPrice price: secondVariant.price
}); });
}) })
.then( .then(() =>
() =>
storeFrontProductUtils.getProductVariants( storeFrontProductUtils.getProductVariants(
createdProduct.id, createdProduct.id,
defaultChannel.slug defaultChannel.slug
) )
// productDetails.getProductDetails(createdProduct.id, defaultChannel.slug)
) )
.then(variants => { .then(variants => {
expect(variants).to.have.length(2); expect(variants).to.have.length(2);
expect(variants[0].pricing.price.gross.amount).to.equal(variantsPrice); expect(variants).includes(firstVariant, secondVariant);
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);
}); });
}); });
it("should create variant for many channels", () => { it("should create variant for many channels", () => {
@ -183,13 +163,12 @@ describe("creating variants", () => {
}) })
.then(() => { .then(() => {
cy.visit(`${urlList.products}${createdProduct.id}`); cy.visit(`${urlList.products}${createdProduct.id}`);
variantsSteps.createFirstVariant( variantsSteps.createFirstVariant({
name, name,
warehouse.id, warehouseId: warehouse.id,
variantsPrice, price: variantsPrice,
attribute.name attribute: attribute.values[0].name
); });
// productDetails.getProductDetails(product.id, defaultChannel.slug);
storeFrontProductUtils.getProductVariants( storeFrontProductUtils.getProductVariants(
product.id, product.id,
defaultChannel.slug defaultChannel.slug
@ -200,7 +179,6 @@ describe("creating variants", () => {
}) })
.then(() => { .then(() => {
storeFrontProductUtils.getProductVariants(product.id, newChannel.slug); storeFrontProductUtils.getProductVariants(product.id, newChannel.slug);
// productDetails.getProductDetails(product.id, newChannel.slug);
}) })
.then(variants => { .then(variants => {
expect(variants[0].pricing.price.gross.amount).to.equal(variantsPrice); expect(variants[0].pricing.price.gross.amount).to.equal(variantsPrice);

View file

@ -57,14 +57,14 @@ class ProductsUtils {
}); });
} }
createTypeAttributeAndCategoryForProduct(name) { createTypeAttributeAndCategoryForProduct(name, attributeValues) {
return this.createAttribute(name) return this.createAttribute(name, attributeValues)
.then(() => this.createTypeProduct(name, this.attribute.id)) .then(() => this.createTypeProduct(name, this.attribute.id))
.then(() => this.createCategory(name)); .then(() => this.createCategory(name));
} }
createAttribute(name) { createAttribute(name, attributeValues) {
return this.attributeRequest return this.attributeRequest
.createAttribute(name) .createAttribute(name, attributeValues)
.then( .then(
resp => (this.attribute = resp.body.data.attributeCreate.attribute) resp => (this.attribute = resp.body.data.attributeCreate.attribute)
); );