add promises

This commit is contained in:
Karolina Rakoczy 2021-02-16 15:19:46 +01:00
parent 592993c030
commit ebfb22d9f8
13 changed files with 305 additions and 214 deletions

View file

@ -31,7 +31,9 @@ class Attribute {
}
}
}`;
return cy.sendRequestWithQuery(mutation);
return cy
.sendRequestWithQuery(mutation)
.then(resp => resp.body.data.attributes.edges);
}
deleteAttribute(attributeId) {

View file

@ -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{

View file

@ -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}"

View file

@ -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) {

View file

@ -78,7 +78,9 @@ class ShippingMethod {
}
}
`;
return cy.sendRequestWithQuery(query);
return cy
.sendRequestWithQuery(query)
.then(resp => resp.body.data.shippingZones.edges);
}
}
export default ShippingMethod;

View file

@ -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{

View file

@ -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()
);

View file

@ -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);
});
});
}
);

View file

@ -1,5 +1,6 @@
import "./user";
import "./softAssertions";
import "./deleteElement/index.js";
import { urlList } from "../url/urlList";

View file

@ -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;

View file

@ -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;

View file

@ -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;

View file

@ -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;