Welcome to your fourth unit for this course dedicated to practicing Test Driven Development (TDD) utilizing TypeScript and Jest. We're going to continue building a our ShoppingCart
system by jamming even more features into our class.
In this course, emphasis is placed on hands-on practice, where you'll receive requirements through tests, one at a time. Your task is to write tests AND implement code that makes each test pass, simulating a real-world TDD environment. Where I wrote the tests for you last time, this time it's all up to you!
Remember to use the core concepts of the Red-Green-Refactor cycle while completing these coding exercises. I'm still here to help! Just ask.
- Description: Removing an item that does not exist in the cart should result in an error being thrown, indicating the item is not found.
- Details
- support item removal through a
removeItem(id)
method - ensure the method throws an appropriate error message if an item does not exist in the cart
- support item removal through a
- Examples: attempting to remove an item with
id: '999'
from the cart should throw an error with the message'Item not found'
- Description: When a percentage discount is applied, the total price of the items in the cart should reflect this discount.
- Details
- use
applyDiscount(percentage)
method to apply a percentage discount to the total - ensure
getTotal()
returns the discounted price after applying the discount.
- use
- Examples: applying a 10% discount to an item with a total price of
100
should result in a new total of90
- Description: When the total price of items in the cart exceeds $150, a bulk discount of 10% should be applied to the total.
- Details
- if the total exceeds 150, a 10% discount should be applied automatically
- the
getTotal()
method should return the discounted total when applicable
- Examples: adding an item
{ id: '1', name: 'Book', price: 200 }
should result in a total of180
after applying the bulk discount
- Description: The cart should be able to remove all items, resetting the item count and total price to zero.
- Details
- implement a
clear()
method to remove all items from the cart - ensure
getItemCount()
returns0
after clearing - verify
getTotal()
is0
after clearing.
- implement a
- Examples: if there are multiple items in the cart, calling
clear()
should leave the count as0
and the total as0
.
- Description: When the quantity of an item in the cart is updated, the item count and total price should reflect the updated quantity.
- Details
- allow item quantities to be updated using an
updateQuantity()
method - ensure
getItemCount()
returns the correct count after updating the quantity - update
getTotal()
to return the correct total price after updating the quantity
- allow item quantities to be updated using an
- Examples: updating the quantity of
{ id: '1', name: 'Book', price: 10 }
from2
to3
should result in a count of3
and a total of30
In this unit, you explored the design of test cases for a new ShoppingCart class, focusing on essential features like handling an empty cart, adding items, managing item quantities, and removing items. Now its your turn to ensure that the described functionality is implemented by writing comprehensive test cases and ensuring that the test passes with the least amount of code needed.
As you undertake these exercises, remember to engage in the Red-Green-Refactor cycle. Be sure to practice writing tests first and do not write implementation code unless the test asks for it.
Red! Green! Refactor!