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) { 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) { deleteCategory(categoryId) {
const mutation = `mutation{ const mutation = `mutation{

View file

@ -1,10 +1,9 @@
class Checkout { class Checkout {
createCheckout(channelSlug, email, productQuantity, variantsList) { createCheckout(channelSlug, email, productQuantity, variantsList) {
const lines = []; const lines = variantsList.map(
variantsList.forEach(variant => { variant => `{quantity:${productQuantity}
lines.push(`{quantity:${productQuantity} variantId:"${variant.id}"}`
variantId:"${variant.id}"}`); );
});
const mutation = `mutation{ const mutation = `mutation{
checkoutCreate(input:{ checkoutCreate(input:{
channel:"${channelSlug}" channel:"${channelSlug}"

View file

@ -1,11 +1,10 @@
class Product { class Product {
getFirstProducts(first, search) { getFirstProducts(first, search) {
let filter = ""; const filter = search
if (search) { ? `, filter:{
filter = `, filter:{ search:"${search}"
search:"${search}" }`
}`; : "";
}
const query = `query{ const query = `query{
products(first:${first}${filter}){ products(first:${first}${filter}){
edges{ 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) { 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) { 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; 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) { deleteWarehouse(warehouseId) {
const mutation = `mutation{ const mutation = `mutation{

View file

@ -31,9 +31,9 @@ describe("User authorization", () => {
before(() => { before(() => {
cy.clearSessionData().loginUserViaRequest(); cy.clearSessionData().loginUserViaRequest();
productsUtils.deleteProperProducts(startsWith);
customer.deleteCustomers(startsWith); customer.deleteCustomers(startsWith);
shippingUtils.deleteShipping(startsWith); shippingUtils.deleteShipping(startsWith);
productsUtils.deleteProducts(startsWith);
channelsUtils.getDefaultChannel().then(channel => { channelsUtils.getDefaultChannel().then(channel => {
defaultChannel = channel; defaultChannel = channel;
@ -50,21 +50,21 @@ describe("User authorization", () => {
shippingPrice shippingPrice
) )
.then(() => { .then(() => {
const warehouseId = shippingUtils.getWarehouseId(); const warehouse = shippingUtils.getWarehouse();
productsUtils productsUtils
.createTypeAttributeAndCategoryForProduct(randomName) .createTypeAttributeAndCategoryForProduct(randomName)
.then(() => { .then(() => {
const productTypeId = productsUtils.getProductTypeId(); const productType = productsUtils.getProductType();
const attributeId = productsUtils.getAttributeId(); const attribute = productsUtils.getAttribute();
const categoryId = productsUtils.getCategoryId(); const category = productsUtils.getCategory();
productsUtils.createProductInChannel( productsUtils.createProductInChannel(
randomName, randomName,
defaultChannel.id, defaultChannel.id,
warehouseId, warehouse.id,
20, 20,
productTypeId, productType.id,
attributeId, attribute.id,
categoryId, category.id,
productPrice productPrice
); );
}); });
@ -96,7 +96,7 @@ describe("User authorization", () => {
ordersUtils.createReadyToFulfillOrder( ordersUtils.createReadyToFulfillOrder(
customerId, customerId,
shippingUtils.getShippingMethodId(), shippingUtils.getShippingMethod().id,
defaultChannel.id, defaultChannel.id,
productsUtils.getCreatedVariants() productsUtils.getCreatedVariants()
); );
@ -113,14 +113,13 @@ describe("User authorization", () => {
homePageUtils homePageUtils
.getOrdersReadyForCapture(defaultChannel.slug) .getOrdersReadyForCapture(defaultChannel.slug)
.as("ordersReadyForCapture"); .as("ordersReadyForCapture");
const shippingId = shippingUtils.getShippingMethodId();
const variantsList = productsUtils.getCreatedVariants(); const variantsList = productsUtils.getCreatedVariants();
ordersUtils.createWaitingForCaptureOrder( ordersUtils.createWaitingForCaptureOrder(
defaultChannel.slug, defaultChannel.slug,
randomEmail, randomEmail,
variantsList, variantsList,
shippingId shippingUtils.getShippingMethod().id
); );
cy.get("@ordersReadyForCapture").then(ordersReadyForCapture => { cy.get("@ordersReadyForCapture").then(ordersReadyForCapture => {
@ -138,19 +137,19 @@ describe("User authorization", () => {
.as("productsOutOfStock"); .as("productsOutOfStock");
const productOutOfStockRandomName = startsWith + faker.random.number(); const productOutOfStockRandomName = startsWith + faker.random.number();
const productsOutOfStockUtils = new ProductsUtils(); const productsOutOfStockUtils = new ProductsUtils();
const warehouseId = shippingUtils.getWarehouseId(); const warehouse = shippingUtils.getWarehouse();
const productTypeId = productsUtils.getProductTypeId(); const productType = productsUtils.getProductType();
const attributeId = productsUtils.getAttributeId(); const attribute = productsUtils.getAttribute();
const categoryId = productsUtils.getCategoryId(); const category = productsUtils.getCategory();
productsOutOfStockUtils.createProductInChannel( productsOutOfStockUtils.createProductInChannel(
productOutOfStockRandomName, productOutOfStockRandomName,
defaultChannel.id, defaultChannel.id,
warehouseId, warehouse.id,
0, 0,
productTypeId, productType.id,
attributeId, attribute.id,
categoryId, category.id,
productPrice productPrice
); );
@ -168,7 +167,7 @@ describe("User authorization", () => {
ordersUtils.createReadyToFulfillOrder( ordersUtils.createReadyToFulfillOrder(
customerId, customerId,
shippingUtils.getShippingMethodId(), shippingUtils.getShippingMethod().id,
defaultChannel.id, defaultChannel.id,
productsUtils.getCreatedVariants() productsUtils.getCreatedVariants()
); );
@ -190,7 +189,7 @@ describe("User authorization", () => {
ordersUtils.createReadyToFulfillOrder( ordersUtils.createReadyToFulfillOrder(
customerId, customerId,
shippingUtils.getShippingMethodId(), shippingUtils.getShippingMethod().id,
defaultChannel.id, defaultChannel.id,
productsUtils.getCreatedVariants() 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 "./user";
import "./softAssertions"; import "./softAssertions";
import "./deleteElement/index.js";
import { urlList } from "../url/urlList"; 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 Checkout from "../apiRequests/Checkout";
import Order from "../apiRequests/Order"; import Order from "../apiRequests/Order";
import Promises from "../support/promises/promises";
class OrdersUtils { class OrdersUtils {
createWaitingForCaptureOrder( promises = new Promises();
checkoutRequest = new Checkout();
orderRequest = new Order();
checkout;
order;
async createWaitingForCaptureOrder(
channelSlug, channelSlug,
email, email,
variantsList, variantsList,
shippingMethodId shippingMethodId
) { ) {
const checkout = new Checkout(); await this.createCheckout(channelSlug, email, variantsList);
return checkout this.addShippingMethod(this.checkout.id, shippingMethodId);
.createCheckout(channelSlug, email, 1, variantsList) this.addPayment(this.checkout.id);
.then(createCheckoutResp => { this.completeCheckout(this.checkout.id);
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))
);
});
} }
createReadyToFulfillOrder( async createReadyToFulfillOrder(
customerId, customerId,
shippingMethodId, shippingMethodId,
channelId, channelId,
variantsList variantsList
) { ) {
const order = new Order(); await this.createDraftOrder(customerId, shippingMethodId, channelId);
return order variantsList.forEach(variantElement => {
.createDraftOrder(customerId, shippingMethodId, channelId) this.orderRequest.addProductToOrder(this.order.id, variantElement.id);
.then(draftOrderResp => { });
const orderId = draftOrderResp.body.data.draftOrderCreate.order.id; this.markOrderAsPaid(this.order.id);
variantsList.forEach(variantElement => { this.completeOrder(this.order.id);
order.addProductToOrder(orderId, variantElement.id); }
}); async createDraftOrder(customerId, shippingMethodId, channelId) {
return order const respProm = await this.promises.createPromise(
.markOrderAsPaid(orderId) this.orderRequest.createDraftOrder(
.then(() => order.completeOrder(orderId)); 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; export default OrdersUtils;

View file

@ -1,24 +1,21 @@
import Attribute from "../apiRequests/Attribute"; import Attribute from "../apiRequests/Attribute";
import Category from "../apiRequests/Category"; import Category from "../apiRequests/Category";
import Product from "../apiRequests/Product"; import Product from "../apiRequests/Product";
import Promises from "../support/promises/promises.js";
class ProductsUtils { class ProductsUtils {
createdVariantId; promises = new Promises();
productTypeId; productRequest = new Product();
attributeId; attributeRequest = new Attribute();
categoryId; categoryRequest = new Category();
updateChannelInProduct(productsList, channelId) { product;
const product = new Product(); variants;
productsList.forEach(productElement => { productType;
product.updateChannelInProduct(productElement.node.id, channelId); attribute;
const variants = productElement.node.variants; category;
variants.forEach(variant => {
product.updateChannelPriceInVariant(variant.id, channelId); async createProductInChannel(
});
});
}
createProductInChannel(
name, name,
channelId, channelId,
warehouseId, warehouseId,
@ -28,97 +25,112 @@ class ProductsUtils {
categoryId, categoryId,
price price
) { ) {
const product = new Product(); await this.createProduct(attributeId, name, productTypeId, categoryId);
return product this.updateChannelInProduct(this.product.id, channelId);
.createProduct(attributeId, name, productTypeId, categoryId) await this.createVariant(
.then(createProductResp => { this.product.id,
const productId = createProductResp.body.data.productCreate.product.id; name,
return product.updateChannelInProduct(productId, channelId).then(() => warehouseId,
product quantityInWarehouse,
.createVariant( channelId,
productId, price
name, );
warehouseId,
quantityInWarehouse,
channelId,
price
)
.then(createVariantResp => {
this.createdVariantId =
createVariantResp.body.data.productVariantBulkCreate.productVariants;
})
);
});
} }
createTypeAttributeAndCategoryForProduct(name) { async createTypeAttributeAndCategoryForProduct(name) {
const attribute = new Attribute(); await this.createAttribute(name);
const category = new Category(); await this.createTypeProduct(name, this.attribute.id);
const product = new Product(); await this.createCategory(name);
return attribute.createAttribute(name).then(createAttributeResp => { }
this.attributeId = async createAttribute(name) {
createAttributeResp.body.data.attributeCreate.attribute.id; const respProm = await this.promises.createPromise(
return product this.attributeRequest.createAttribute(name)
.createTypeProduct(name, this.attributeId) );
.then(createTypeProductResp => { this.attribute = respProm.attributeCreate.attribute;
this.productTypeId = }
createTypeProductResp.body.data.productTypeCreate.productType.id; async createTypeProduct(name, attributeId) {
return category.createCategory(name).then(categoryResp => { const respProm = await this.promises.createPromise(
this.categoryId = categoryResp.body.data.categoryCreate.category.id; 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() { getCreatedVariants() {
return this.createdVariantId; return this.variants;
} }
getProductTypeId() { getProductType() {
return this.productTypeId; return this.productType;
} }
getAttributeId() { getAttribute() {
return this.attributeId; return this.attribute;
} }
getCategoryId() { getCategory() {
return this.categoryId; return this.category;
} }
deleteProperProducts(startsWith) {
deleteProducts(startsWith) {
const product = new Product(); const product = new Product();
const attribute = new Attribute(); const attribute = new Attribute();
const category = new Category(); const category = new Category();
product.getProductTypes(100, startsWith).then(resp => { cy.deleteProperElements(
const productTypes = resp.body.data.productTypes.edges; product.deleteProductType,
productTypes.forEach(productType => { product.getProductTypes,
if (productType.node.name.includes(startsWith)) { startsWith,
product.deleteProductType(productType.node.id); "productType"
} );
}); cy.deleteProperElements(
}); attribute.deleteAttribute,
attribute.getAttributes(100, startsWith).then(resp => { attribute.getAttributes,
const attributes = resp.body.data.attributes.edges; startsWith,
attributes.forEach(attributeElement => { "attributes"
if (attributeElement.node.name.includes(startsWith)) { );
attribute.deleteAttribute(attributeElement.node.id); cy.deleteProperElements(
} category.deleteCategory,
}); category.getCategories,
}); startsWith,
category.getCategories(100, startsWith).then(resp => { "categories"
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);
}
});
});
} }
} }
export default ProductsUtils; export default ProductsUtils;

View file

@ -1,71 +1,77 @@
import ShippingMethod from "../apiRequests/ShippingMethod"; import ShippingMethod from "../apiRequests/ShippingMethod";
import Warehouse from "../apiRequests/Warehouse"; import Warehouse from "../apiRequests/Warehouse";
import Promises from "../support/promises/promises.js";
class ShippingUtils { class ShippingUtils {
shippingMethodId; promises = new Promises();
shippingZoneId; shippingMethodRequest = new ShippingMethod();
warehouseId; warehouseRequest = new Warehouse();
createShipping(channelId, name, address, price) { shippingMethod;
const shippingMethod = new ShippingMethod(); shippingZone;
const warehouse = new Warehouse(); warehouse;
return shippingMethod
.createShippingZone(name, address.country) async createShipping(channelId, name, address, price) {
.then(shippingZoneResp => { await this.createShippingZone(name, address.country);
this.shippingZoneId = await this.createWarehouse(name, this.shippingZone.id, address);
shippingZoneResp.body.data.shippingZoneCreate.shippingZone.id; await this.createShippingRate(name, this.shippingZone.id);
return warehouse this.addChannelToShippingMethod(this.shippingMethod.id, channelId, price);
.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
);
});
});
});
} }
getShippingMethodId() { async createShippingZone(name, country) {
return this.shippingMethodId; 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() { getShippingMethod() {
return this.shippingZoneId; return this.shippingMethod;
} }
getWarehouseId() { getShippingZone() {
return this.warehouseId; return this.shippingZone;
}
getWarehouse() {
return this.warehouse;
} }
deleteShipping(startsWith) { deleteShipping(startsWith) {
const shippingMethod = new ShippingMethod(); const shippingMethod = new ShippingMethod();
const warehouse = new Warehouse(); const warehouse = new Warehouse();
shippingMethod.getShippingZones().then(resp => { cy.deleteProperElements(
if (resp.body.data.shippingZones) { shippingMethod.deleteShippingZone,
const shippingZone = resp.body.data.shippingZones.edges; shippingMethod.getShippingZones,
shippingZone.forEach(element => { startsWith,
if (element.node.name.includes(startsWith)) { "shippingZONE"
shippingMethod.deleteShippingZone(element.node.id); );
} cy.deleteProperElements(
}); warehouse.deleteWarehouse,
} warehouse.getWarehouses,
}); startsWith,
warehouse.getWarehouses(100, startsWith).then(resp => { "Warehouse"
const warehouses = resp.body.data.warehouses.edges; );
warehouses.forEach(warehouseElement => {
if (warehouseElement.node.name.includes(startsWith)) {
warehouse.deleteWarehouse(warehouseElement.node.id);
}
});
});
} }
} }
export default ShippingUtils; export default ShippingUtils;