Welcome to your third unit for this course dedicated to practicing Test Driven Development (TDD) utilizing TypeScript and Jest. We're going to start building a new system; this time we'll build a shopping cart class with a lot of features.
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: When a new shopping cart is created, it should start without any items, and the total price should be zero.
- Details
- initialize a new shopping cart using the
ShoppingCart
constructor - ensure
getItemCount()
returns0
for an empty cart - verify
getTotal()
returns0
for the initial state.
- initialize a new shopping cart using the
- Examples: a newly created cart should have a count of
0
and a total of0
- Description: Verify that when a single item is added to the cart, the cart's item count increases and the total price reflects the added item's price.
- Details
- Use the
addItem(item)
method to add an item to the cart. - Confirm that
getItemCount()
returns the correct number of items after an item is added. - Ensure
getTotal()
accurately calculates and returns the total price of items in the cart.
- Use the
- Examples: Adding an item
{ id: '1', name: 'Book', price: 10 }
should result in an item count of1
and a total cost of10
.
- Description: When multiple different items are added to the cart, the item count should reflect the total number of unique items, and the total price should be the sum of all individual item prices.
- Details
- accommodate adding different items using the
addItem()
method - ensure
getItemCount()
accurately reflects the number of items added - verify that
getTotal()
correctly calculates the sum of all item prices
- accommodate adding different items using the
- Examples: adding
{ id: '1', name: 'Book', price: 10 }
and{ id: '2', name: 'Pen', price: 5 }
should result in a count of2
and a total of15
- Description: When multiple quantities of the same item are added to the cart, the item count should reflect the total quantity added, and the total price should be the item's unit price multiplied by the quantity.
- Details
- allow items to be added with a specified quantity using an
addItem(item, quantity)
method - ensure
getItemCount()
returns the sum of all quantities added for an item - update
getTotal()
to calculate the total price using the unit price multiplied by the total quantity.
- allow items to be added with a specified quantity using an
- Examples: adding
{ id: '1', name: 'Book', price: 10 }
with a quantity of3
should have a count of3
and a total of30
- Description: When an item is removed from the cart, the item count should decrease accordingly, and the total price should adjust to reflect the removal.
- Details
- provide a
removeItem(id)
method to handle the removal of items from the cart - ensure
getItemCount()
accurately reflects the new count after removal - update
getTotal()
to return the correct total price after an item has been removed.
- provide a
- Examples: adding
{ id: '1', name: 'Book', price: 10 }
in a quantity of2
and then removing one should result in a count of1
and a total of10
.
Looking ahead to the practice exercises, you will have the opportunity to write tests and ensure they pass while practicing the Red-Green-Refactor cycle. Your implementation may differ from the provided solutions as we move forward, and that’s perfectly acceptable. Each practice session will begin from a solution foundation, allowing you to compare your approach with the guided solution and develop your features to ensure test success.
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!