diff --git a/features/add_discount_to_cart.feature b/features/add_discount_to_cart.feature index bb4b756..358514d 100644 --- a/features/add_discount_to_cart.feature +++ b/features/add_discount_to_cart.feature @@ -8,8 +8,8 @@ Feature: Add discount to cart | sku | name | price | | 001 | potatoes | 2.5 | | 002 | water | 0.95 | - And there is a cart discount "free shipping" for 1 euros with code "FREE-SHIPPING" - And there is a cart discount "special offer" for 10 euros with code "SPECIAL-OFFER" + And there is a cart discount "free shipping" for 10 % with code "FREE-SHIPPING" + And there is a cart discount "special offer" for 95 % with code "SPECIAL-OFFER" And I have a cart Scenario: No discounts @@ -20,14 +20,14 @@ Feature: Add discount to cart Scenario: Add discount Given I add 2 units of product "001" to my cart When I apply "FREE-SHIPPING" discount to my cart - Then the cart's total cost should be 4.0 euros + Then the cart's total cost should be 4.5 euros And there should be discount "free shipping" in my cart Scenario: Add discount only apply once Given I add 2 units of product "001" to my cart And I apply "FREE-SHIPPING" discount to my cart When I apply "FREE-SHIPPING" discount to my cart - Then the cart's total cost should be 4.0 euros + Then the cart's total cost should be 4.5 euros And there should be discount "free shipping" in my cart Scenario: Cart total should be always positive @@ -36,4 +36,20 @@ Feature: Add discount to cart When I apply "SPECIAL-OFFER" discount to my cart Then the cart's total cost should be 0.0 euros And there should be discount "free shipping" in my cart - And there should be discount "special offer" in my cart \ No newline at end of file + And there should be discount "special offer" in my cart + + Scenario Outline: Add discount examples + Given the following products exist: + | sku | name | price | + | 003 | orange juice | | + And there is a cart discount "super discount" for % with code "SUPER-DISCOUNT" + Given I add units of product "003" to my cart + When I apply "SUPER-DISCOUNT" discount to my cart + Then the cart's total cost should be euros + And there should be discount "super discount" in my cart + Examples: + | product_price | product_quantity | percentage | cart_total | + | 21.0 | 1 | 10 | 18.90 | + | 20.0 | 2 | 15 | 34.0 | + | 5.0 | 3 | 3 | 14.55 | + | 5.0 | 3 | 0 | 15.00 | \ No newline at end of file diff --git a/src/entity/Cart.ts b/src/entity/Cart.ts index 7db7426..8f9dbbb 100644 --- a/src/entity/Cart.ts +++ b/src/entity/Cart.ts @@ -55,16 +55,17 @@ class Cart { .reduce((carry: number, current: number) => carry + current, 0) } - private totalDiscountsPrice(): number { + private totalDiscountsPercentage(): number { return this.discounts .map((discount: Discount) => discount.value) .reduce((carry: number, current: number) => carry + current, 0) } public totalPrice(): number { + const totalLinesPrice: number = this.totalLinesPrice() return Math.max( 0, - Math.round((this.totalLinesPrice() - this.totalDiscountsPrice()) * 100) / 100 + Math.round((totalLinesPrice - (totalLinesPrice * this.totalDiscountsPercentage() / 100)) * 100) / 100 ) } diff --git a/src/tests/steps/DiscountSteps.ts b/src/tests/steps/DiscountSteps.ts index 3584526..7ba535f 100644 --- a/src/tests/steps/DiscountSteps.ts +++ b/src/tests/steps/DiscountSteps.ts @@ -11,7 +11,7 @@ class DiscountSteps { this.discountRepository = myConnection.getRepository(Discount) } - @given("there is a cart discount {string} for {double} euros with code {string}") + @given("there is a cart discount {string} for {double} % with code {string}") public async thereIsACartDiscountWithCode(name: string, value: number, code: string): Promise { const discount: Discount = new Discount(name, code, value) await this.discountRepository.save(discount)