diff --git a/cypress/apiRequests/Attribute.js b/cypress/apiRequests/Attribute.js index a7253f8b8..f1c180d01 100644 --- a/cypress/apiRequests/Attribute.js +++ b/cypress/apiRequests/Attribute.js @@ -31,7 +31,9 @@ class Attribute { } } }`; - return cy.sendRequestWithQuery(mutation); + return cy + .sendRequestWithQuery(mutation) + .then(resp => resp.body.data.attributes.edges); } deleteAttribute(attributeId) { diff --git a/cypress/apiRequests/Category.js b/cypress/apiRequests/Category.js index 7ea65eb7a..ad5d4f0dd 100644 --- a/cypress/apiRequests/Category.js +++ b/cypress/apiRequests/Category.js @@ -26,7 +26,9 @@ class Category { } } }`; - return cy.sendRequestWithQuery(mutation); + return cy + .sendRequestWithQuery(mutation) + .then(resp => resp.body.data.categories.edges); } deleteCategory(categoryId) { const mutation = `mutation{ diff --git a/cypress/apiRequests/Checkout.js b/cypress/apiRequests/Checkout.js index 0b611f883..28f9fbf69 100644 --- a/cypress/apiRequests/Checkout.js +++ b/cypress/apiRequests/Checkout.js @@ -1,10 +1,9 @@ class Checkout { createCheckout(channelSlug, email, productQuantity, variantsList) { - const lines = []; - variantsList.forEach(variant => { - lines.push(`{quantity:${productQuantity} - variantId:"${variant.id}"}`); - }); + const lines = variantsList.map( + variant => `{quantity:${productQuantity} + variantId:"${variant.id}"}` + ); const mutation = `mutation{ checkoutCreate(input:{ channel:"${channelSlug}" diff --git a/cypress/apiRequests/Product.js b/cypress/apiRequests/Product.js index b747d238f..09dca8bf6 100644 --- a/cypress/apiRequests/Product.js +++ b/cypress/apiRequests/Product.js @@ -1,11 +1,10 @@ class Product { getFirstProducts(first, search) { - let filter = ""; - if (search) { - filter = `, filter:{ - search:"${search}" - }`; - } + const filter = search + ? `, filter:{ + search:"${search}" + }` + : ""; const query = `query{ products(first:${first}${filter}){ edges{ @@ -20,7 +19,9 @@ class Product { } } `; - return cy.sendRequestWithQuery(query); + return cy + .sendRequestWithQuery(query) + .then(resp => resp.body.data.products.edges); } updateChannelInProduct(productId, channelId) { @@ -159,7 +160,9 @@ class Product { } } }`; - return cy.sendRequestWithQuery(query); + return cy + .sendRequestWithQuery(query) + .then(resp => resp.body.data.productTypes.edges); } deleteProductType(productTypeId) { diff --git a/cypress/apiRequests/ShippingMethod.js b/cypress/apiRequests/ShippingMethod.js index 93ecb8f80..6c9deb3de 100644 --- a/cypress/apiRequests/ShippingMethod.js +++ b/cypress/apiRequests/ShippingMethod.js @@ -78,7 +78,9 @@ class ShippingMethod { } } `; - return cy.sendRequestWithQuery(query); + return cy + .sendRequestWithQuery(query) + .then(resp => resp.body.data.shippingZones.edges); } } export default ShippingMethod; diff --git a/cypress/apiRequests/Warehouse.js b/cypress/apiRequests/Warehouse.js index 478d28726..8237a9333 100644 --- a/cypress/apiRequests/Warehouse.js +++ b/cypress/apiRequests/Warehouse.js @@ -38,7 +38,9 @@ class Warehouse { } } }`; - return cy.sendRequestWithQuery(query); + return cy + .sendRequestWithQuery(query) + .then(resp => resp.body.data.warehouses.edges); } deleteWarehouse(warehouseId) { const mutation = `mutation{ diff --git a/cypress/integration/homePage.js b/cypress/integration/homePage.js index c1f7e5cfc..eab169707 100644 --- a/cypress/integration/homePage.js +++ b/cypress/integration/homePage.js @@ -31,9 +31,9 @@ describe("User authorization", () => { before(() => { cy.clearSessionData().loginUserViaRequest(); + productsUtils.deleteProperProducts(startsWith); customer.deleteCustomers(startsWith); shippingUtils.deleteShipping(startsWith); - productsUtils.deleteProducts(startsWith); channelsUtils.getDefaultChannel().then(channel => { defaultChannel = channel; @@ -50,21 +50,21 @@ describe("User authorization", () => { shippingPrice ) .then(() => { - const warehouseId = shippingUtils.getWarehouseId(); + const warehouse = shippingUtils.getWarehouse(); productsUtils .createTypeAttributeAndCategoryForProduct(randomName) .then(() => { - const productTypeId = productsUtils.getProductTypeId(); - const attributeId = productsUtils.getAttributeId(); - const categoryId = productsUtils.getCategoryId(); + const productType = productsUtils.getProductType(); + const attribute = productsUtils.getAttribute(); + const category = productsUtils.getCategory(); productsUtils.createProductInChannel( randomName, defaultChannel.id, - warehouseId, + warehouse.id, 20, - productTypeId, - attributeId, - categoryId, + productType.id, + attribute.id, + category.id, productPrice ); }); @@ -96,7 +96,7 @@ describe("User authorization", () => { ordersUtils.createReadyToFulfillOrder( customerId, - shippingUtils.getShippingMethodId(), + shippingUtils.getShippingMethod().id, defaultChannel.id, productsUtils.getCreatedVariants() ); @@ -113,14 +113,13 @@ describe("User authorization", () => { homePageUtils .getOrdersReadyForCapture(defaultChannel.slug) .as("ordersReadyForCapture"); - const shippingId = shippingUtils.getShippingMethodId(); const variantsList = productsUtils.getCreatedVariants(); ordersUtils.createWaitingForCaptureOrder( defaultChannel.slug, randomEmail, variantsList, - shippingId + shippingUtils.getShippingMethod().id ); cy.get("@ordersReadyForCapture").then(ordersReadyForCapture => { @@ -138,19 +137,19 @@ describe("User authorization", () => { .as("productsOutOfStock"); const productOutOfStockRandomName = startsWith + faker.random.number(); const productsOutOfStockUtils = new ProductsUtils(); - const warehouseId = shippingUtils.getWarehouseId(); - const productTypeId = productsUtils.getProductTypeId(); - const attributeId = productsUtils.getAttributeId(); - const categoryId = productsUtils.getCategoryId(); + const warehouse = shippingUtils.getWarehouse(); + const productType = productsUtils.getProductType(); + const attribute = productsUtils.getAttribute(); + const category = productsUtils.getCategory(); productsOutOfStockUtils.createProductInChannel( productOutOfStockRandomName, defaultChannel.id, - warehouseId, + warehouse.id, 0, - productTypeId, - attributeId, - categoryId, + productType.id, + attribute.id, + category.id, productPrice ); @@ -168,7 +167,7 @@ describe("User authorization", () => { ordersUtils.createReadyToFulfillOrder( customerId, - shippingUtils.getShippingMethodId(), + shippingUtils.getShippingMethod().id, defaultChannel.id, productsUtils.getCreatedVariants() ); @@ -190,7 +189,7 @@ describe("User authorization", () => { ordersUtils.createReadyToFulfillOrder( customerId, - shippingUtils.getShippingMethodId(), + shippingUtils.getShippingMethod().id, defaultChannel.id, productsUtils.getCreatedVariants() ); diff --git a/cypress/support/deleteElement/index.js b/cypress/support/deleteElement/index.js new file mode 100644 index 000000000..ff15bd8b3 --- /dev/null +++ b/cypress/support/deleteElement/index.js @@ -0,0 +1,18 @@ +Cypress.Commands.add( + "handleDeleteElement", + (element, deleteFunction, startsWith) => { + if (element.node.name.includes(startsWith)) { + deleteFunction(element.node.id); + } + } +); +Cypress.Commands.add( + "deleteProperElements", + (deleteFunction, getFunction, startsWith, name) => { + getFunction(100, startsWith).then(elements => { + elements.forEach(element => { + cy.handleDeleteElement(element, deleteFunction, startsWith); + }); + }); + } +); diff --git a/cypress/support/index.js b/cypress/support/index.js index 98c9d30d8..f6115623a 100644 --- a/cypress/support/index.js +++ b/cypress/support/index.js @@ -1,5 +1,6 @@ import "./user"; import "./softAssertions"; +import "./deleteElement/index.js"; import { urlList } from "../url/urlList"; diff --git a/cypress/support/promises/promises.js b/cypress/support/promises/promises.js new file mode 100644 index 000000000..a32228a66 --- /dev/null +++ b/cypress/support/promises/promises.js @@ -0,0 +1,10 @@ +class Promises { + createPromise(requestFunction) { + return new Promise(resolve => { + requestFunction + .then(resp => resp.body.data) + .then(result => resolve(result)); + }); + } +} +export default Promises; diff --git a/cypress/utils/ordersUtils.js b/cypress/utils/ordersUtils.js index e1416cbc7..88184881b 100644 --- a/cypress/utils/ordersUtils.js +++ b/cypress/utils/ordersUtils.js @@ -1,46 +1,81 @@ import Checkout from "../apiRequests/Checkout"; import Order from "../apiRequests/Order"; +import Promises from "../support/promises/promises"; class OrdersUtils { - createWaitingForCaptureOrder( + promises = new Promises(); + checkoutRequest = new Checkout(); + orderRequest = new Order(); + + checkout; + order; + + async createWaitingForCaptureOrder( channelSlug, email, variantsList, shippingMethodId ) { - const checkout = new Checkout(); - return checkout - .createCheckout(channelSlug, email, 1, variantsList) - .then(createCheckoutResp => { - const checkoutId = - createCheckoutResp.body.data.checkoutCreate.checkout.id; - return checkout - .addShippingMethod(checkoutId, shippingMethodId) - .then(() => - checkout - .addPayment(checkoutId, "mirumee.payments.dummy", "not-charged") - .then(() => checkout.completeCheckout(checkoutId)) - ); - }); + await this.createCheckout(channelSlug, email, variantsList); + this.addShippingMethod(this.checkout.id, shippingMethodId); + this.addPayment(this.checkout.id); + this.completeCheckout(this.checkout.id); } - createReadyToFulfillOrder( + async createReadyToFulfillOrder( customerId, shippingMethodId, channelId, variantsList ) { - const order = new Order(); - return order - .createDraftOrder(customerId, shippingMethodId, channelId) - .then(draftOrderResp => { - const orderId = draftOrderResp.body.data.draftOrderCreate.order.id; - variantsList.forEach(variantElement => { - order.addProductToOrder(orderId, variantElement.id); - }); - return order - .markOrderAsPaid(orderId) - .then(() => order.completeOrder(orderId)); - }); + await this.createDraftOrder(customerId, shippingMethodId, channelId); + variantsList.forEach(variantElement => { + this.orderRequest.addProductToOrder(this.order.id, variantElement.id); + }); + this.markOrderAsPaid(this.order.id); + this.completeOrder(this.order.id); + } + async createDraftOrder(customerId, shippingMethodId, channelId) { + const respProm = await this.promises.createPromise( + this.orderRequest.createDraftOrder( + customerId, + shippingMethodId, + channelId + ) + ); + this.order = respProm.draftOrderCreate.order; + } + async completeOrder(orderId) { + await this.promises.createPromise(this.orderRequest.completeOrder(orderId)); + } + async markOrderAsPaid(orderId) { + await this.promises.createPromise( + this.orderRequest.markOrderAsPaid(orderId) + ); + } + async createCheckout(channelSlug, email, variantsList) { + const respProm = await this.promises.createPromise( + this.checkoutRequest.createCheckout(channelSlug, email, 1, variantsList) + ); + this.checkout = respProm.checkoutCreate.checkout; + } + async addShippingMethod(checkoutId, shippingMethodId) { + await this.promises.createPromise( + this.checkoutRequest.addShippingMethod(checkoutId, shippingMethodId) + ); + } + async addPayment(checkoutId) { + await this.promises.createPromise( + this.checkoutRequest.addPayment( + checkoutId, + "mirumee.payments.dummy", + "not-charged" + ) + ); + } + async completeCheckout(checkoutId) { + await this.promises.createPromise( + this.checkoutRequest.completeCheckout(checkoutId) + ); } } export default OrdersUtils; diff --git a/cypress/utils/productsUtils.js b/cypress/utils/productsUtils.js index af6601b19..7c834b7a7 100644 --- a/cypress/utils/productsUtils.js +++ b/cypress/utils/productsUtils.js @@ -1,24 +1,21 @@ import Attribute from "../apiRequests/Attribute"; import Category from "../apiRequests/Category"; import Product from "../apiRequests/Product"; +import Promises from "../support/promises/promises.js"; class ProductsUtils { - createdVariantId; - productTypeId; - attributeId; - categoryId; + promises = new Promises(); + productRequest = new Product(); + attributeRequest = new Attribute(); + categoryRequest = new Category(); - updateChannelInProduct(productsList, channelId) { - const product = new Product(); - productsList.forEach(productElement => { - product.updateChannelInProduct(productElement.node.id, channelId); - const variants = productElement.node.variants; - variants.forEach(variant => { - product.updateChannelPriceInVariant(variant.id, channelId); - }); - }); - } - createProductInChannel( + product; + variants; + productType; + attribute; + category; + + async createProductInChannel( name, channelId, warehouseId, @@ -28,97 +25,112 @@ class ProductsUtils { categoryId, price ) { - const product = new Product(); - return product - .createProduct(attributeId, name, productTypeId, categoryId) - .then(createProductResp => { - const productId = createProductResp.body.data.productCreate.product.id; - return product.updateChannelInProduct(productId, channelId).then(() => - product - .createVariant( - productId, - name, - warehouseId, - quantityInWarehouse, - channelId, - price - ) - .then(createVariantResp => { - this.createdVariantId = - createVariantResp.body.data.productVariantBulkCreate.productVariants; - }) - ); - }); + await this.createProduct(attributeId, name, productTypeId, categoryId); + this.updateChannelInProduct(this.product.id, channelId); + await this.createVariant( + this.product.id, + name, + warehouseId, + quantityInWarehouse, + channelId, + price + ); } - createTypeAttributeAndCategoryForProduct(name) { - const attribute = new Attribute(); - const category = new Category(); - const product = new Product(); - return attribute.createAttribute(name).then(createAttributeResp => { - this.attributeId = - createAttributeResp.body.data.attributeCreate.attribute.id; - return product - .createTypeProduct(name, this.attributeId) - .then(createTypeProductResp => { - this.productTypeId = - createTypeProductResp.body.data.productTypeCreate.productType.id; - return category.createCategory(name).then(categoryResp => { - this.categoryId = categoryResp.body.data.categoryCreate.category.id; - }); - }); - }); + async createTypeAttributeAndCategoryForProduct(name) { + await this.createAttribute(name); + await this.createTypeProduct(name, this.attribute.id); + await this.createCategory(name); + } + async createAttribute(name) { + const respProm = await this.promises.createPromise( + this.attributeRequest.createAttribute(name) + ); + this.attribute = respProm.attributeCreate.attribute; + } + async createTypeProduct(name, attributeId) { + const respProm = await this.promises.createPromise( + this.productRequest.createTypeProduct(name, attributeId) + ); + this.productType = respProm.productTypeCreate.productType; + } + async createCategory(name) { + const respProm = await this.promises.createPromise( + this.categoryRequest.createCategory(name) + ); + this.category = respProm.categoryCreate.category; + } + async createProduct(attributeId, name, productTypeId, categoryId) { + const respProm = await this.promises.createPromise( + this.productRequest.createProduct( + attributeId, + name, + productTypeId, + categoryId + ) + ); + this.product = respProm.productCreate.product; + } + async updateChannelInProduct(productId, channelId) { + await this.promises.createPromise( + this.productRequest.updateChannelInProduct(productId, channelId) + ); + } + async createVariant( + productId, + name, + warehouseId, + quantityInWarehouse, + channelId, + price + ) { + const respProm = await this.promises.createPromise( + this.productRequest.createVariant( + productId, + name, + warehouseId, + quantityInWarehouse, + channelId, + price + ) + ); + this.variants = respProm.productVariantBulkCreate.productVariants; } getCreatedVariants() { - return this.createdVariantId; + return this.variants; } - getProductTypeId() { - return this.productTypeId; + getProductType() { + return this.productType; } - getAttributeId() { - return this.attributeId; + getAttribute() { + return this.attribute; } - getCategoryId() { - return this.categoryId; + getCategory() { + return this.category; } - - deleteProducts(startsWith) { + deleteProperProducts(startsWith) { const product = new Product(); const attribute = new Attribute(); const category = new Category(); - product.getProductTypes(100, startsWith).then(resp => { - const productTypes = resp.body.data.productTypes.edges; - productTypes.forEach(productType => { - if (productType.node.name.includes(startsWith)) { - product.deleteProductType(productType.node.id); - } - }); - }); - attribute.getAttributes(100, startsWith).then(resp => { - const attributes = resp.body.data.attributes.edges; - attributes.forEach(attributeElement => { - if (attributeElement.node.name.includes(startsWith)) { - attribute.deleteAttribute(attributeElement.node.id); - } - }); - }); - category.getCategories(100, startsWith).then(resp => { - const categories = resp.body.data.categories.edges; - categories.forEach(categoryElement => { - if (categoryElement.node.name.includes(startsWith)) { - category.deleteCategory(categoryElement.node.id); - } - }); - }); - product.getFirstProducts(100, startsWith).then(getProductResp => { - const products = getProductResp.body.data.products.edges; - products.forEach(productElement => { - if (productElement.node.name.includes(startsWith)) { - product.deleteProducts(productElement.node.id); - } - }); - }); + cy.deleteProperElements( + product.deleteProductType, + product.getProductTypes, + startsWith, + "productType" + ); + cy.deleteProperElements( + attribute.deleteAttribute, + attribute.getAttributes, + startsWith, + "attributes" + ); + cy.deleteProperElements( + category.deleteCategory, + category.getCategories, + startsWith, + "categories" + ); } } export default ProductsUtils; diff --git a/cypress/utils/shippingUtils.js b/cypress/utils/shippingUtils.js index 5806ac82d..977c1b981 100644 --- a/cypress/utils/shippingUtils.js +++ b/cypress/utils/shippingUtils.js @@ -1,71 +1,77 @@ import ShippingMethod from "../apiRequests/ShippingMethod"; import Warehouse from "../apiRequests/Warehouse"; +import Promises from "../support/promises/promises.js"; class ShippingUtils { - shippingMethodId; - shippingZoneId; - warehouseId; + promises = new Promises(); + shippingMethodRequest = new ShippingMethod(); + warehouseRequest = new Warehouse(); - createShipping(channelId, name, address, price) { - const shippingMethod = new ShippingMethod(); - const warehouse = new Warehouse(); - return shippingMethod - .createShippingZone(name, address.country) - .then(shippingZoneResp => { - this.shippingZoneId = - shippingZoneResp.body.data.shippingZoneCreate.shippingZone.id; - return warehouse - .createWarehouse(name, this.shippingZoneId, address) - .then(createWarehouseResp => { - this.warehouseId = - createWarehouseResp.body.data.createWarehouse.warehouse.id; - return shippingMethod - .createShippingRate(name, this.shippingZoneId) - .then(rateResp => { - this.shippingMethodId = - rateResp.body.data.shippingPriceCreate.shippingMethod.id; - return shippingMethod.addChannelToShippingMethod( - this.shippingMethodId, - channelId, - price - ); - }); - }); - }); + shippingMethod; + shippingZone; + warehouse; + + async createShipping(channelId, name, address, price) { + await this.createShippingZone(name, address.country); + await this.createWarehouse(name, this.shippingZone.id, address); + await this.createShippingRate(name, this.shippingZone.id); + this.addChannelToShippingMethod(this.shippingMethod.id, channelId, price); } - getShippingMethodId() { - return this.shippingMethodId; + async createShippingZone(name, country) { + const respProm = await this.promises.createPromise( + this.shippingMethodRequest.createShippingZone(name, country) + ); + this.shippingZone = respProm.shippingZoneCreate.shippingZone; + } + async createWarehouse(name, shippingZoneId, address) { + const respProm = await this.promises.createPromise( + this.warehouseRequest.createWarehouse(name, shippingZoneId, address) + ); + this.warehouse = respProm.createWarehouse.warehouse; + } + async createShippingRate(name, shippingZoneId) { + const respProm = await this.promises.createPromise( + this.shippingMethodRequest.createShippingRate(name, shippingZoneId) + ); + this.shippingMethod = respProm.shippingPriceCreate.shippingMethod; + } + async addChannelToShippingMethod(shippingMethodId, channelId, price) { + await this.promises.createPromise( + this.shippingMethodRequest.addChannelToShippingMethod( + shippingMethodId, + channelId, + price + ) + ); } - getShippingZoneId() { - return this.shippingZoneId; + getShippingMethod() { + return this.shippingMethod; } - getWarehouseId() { - return this.warehouseId; + getShippingZone() { + return this.shippingZone; + } + + getWarehouse() { + return this.warehouse; } deleteShipping(startsWith) { const shippingMethod = new ShippingMethod(); const warehouse = new Warehouse(); - shippingMethod.getShippingZones().then(resp => { - if (resp.body.data.shippingZones) { - const shippingZone = resp.body.data.shippingZones.edges; - shippingZone.forEach(element => { - if (element.node.name.includes(startsWith)) { - shippingMethod.deleteShippingZone(element.node.id); - } - }); - } - }); - warehouse.getWarehouses(100, startsWith).then(resp => { - const warehouses = resp.body.data.warehouses.edges; - warehouses.forEach(warehouseElement => { - if (warehouseElement.node.name.includes(startsWith)) { - warehouse.deleteWarehouse(warehouseElement.node.id); - } - }); - }); + cy.deleteProperElements( + shippingMethod.deleteShippingZone, + shippingMethod.getShippingZones, + startsWith, + "shippingZONE" + ); + cy.deleteProperElements( + warehouse.deleteWarehouse, + warehouse.getWarehouses, + startsWith, + "Warehouse" + ); } } export default ShippingUtils;