Skip to content
This repository was archived by the owner on Apr 26, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions features/add_discount_to_cart.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
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 | <product_price> |
And there is a cart discount "super discount" for <percentage> % with code "SUPER-DISCOUNT"
Given I add <product_quantity> units of product "003" to my cart
When I apply "SUPER-DISCOUNT" discount to my cart
Then the cart's total cost should be <cart_total> 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 |
5 changes: 3 additions & 2 deletions src/entity/Cart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}

Expand Down
2 changes: 1 addition & 1 deletion src/tests/steps/DiscountSteps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
const discount: Discount = new Discount(name, code, value)
await this.discountRepository.save(discount)
Expand Down