From a8c1b5de0352426732943665861e6e86e36af6a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20G=2E=20Rodr=C3=ADguez?= Date: Mon, 13 Jun 2022 10:53:03 +0200 Subject: [PATCH] implementing feature steps (step1) --- src/tests/steps/CartSteps.ts | 19 ++++++++++++++++--- src/tests/steps/ProductSteps.ts | 10 +++++++++- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/tests/steps/CartSteps.ts b/src/tests/steps/CartSteps.ts index 2f385c1..06fb788 100644 --- a/src/tests/steps/CartSteps.ts +++ b/src/tests/steps/CartSteps.ts @@ -5,6 +5,7 @@ import Cart from "../../entity/Cart" import axios, { Axios } from 'axios' import assert from "assert" import CartLine from "../../entity/CartLine" +import Discount from "../../entity/Discount" @binding() class CartSteps { @@ -43,17 +44,29 @@ class CartSteps { @when("I remove product {string} of my cart") public async removeProductOfMyCart(sku: string): Promise { - throw "Step method not implemented" + await this.addProductUnitsToMyCart(0, sku) } @then("the cart's total cost should be {double} euro(s)") public async cartTotalCost(totalCost: number) { - throw "Step method not implemented" + const cart: Cart = await this.currentCart() + const totalProducts: number = cart + .lines + .map((cartLine: CartLine) => cartLine.quantity * cartLine.product.price) + .reduce((carry: number, current: number) => carry + current, 0) + const totalDiscounts = cart + .discounts + .map((discount: Discount) => discount.value) + .reduce((carry: number, current: number) => carry + current, 0) + assert.equal(Math.round((totalProducts - totalDiscounts) * 100) / 100, totalCost) } @then("there should be {int} unit(s) of product {string} in my cart") public async thereShouldBeProductUnitsInMyCart(quantity: number, sku: string) { - throw "Step method not implemented" + const cart: Cart = await this.currentCart() + const cartLine: CartLine|undefined = cart.lines.find((cartLine: CartLine) => cartLine.product.sku == sku) + assert.equal(cartLine ? true : false, true, `Product ${sku} not found`) + assert.equal(cartLine?.quantity, quantity) } @then("there shouldn't be product {string} in my cart") diff --git a/src/tests/steps/ProductSteps.ts b/src/tests/steps/ProductSteps.ts index 498e7b8..e088ac8 100644 --- a/src/tests/steps/ProductSteps.ts +++ b/src/tests/steps/ProductSteps.ts @@ -14,7 +14,15 @@ class ProductSteps { @given("the following products exist:") public async theFollowingProductsExists(table: DataTable): Promise { - throw "Step method not implemented" + var products: Product[] = [] + table.hashes().forEach(async (row: any) => { + const product = new Product() + product.sku = row.sku + product.name = row.name + product.price = row.price + products.push(product) + }) + await this.productRepository.save(products) } }