Building a Shopping Cart with Many Requirements through TDD

Introduction to the Shopping Cart Module

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.

Requirements for `ShoppingCart` Class

1. Starting with an Empty Cart

  • 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() returns 0 for an empty cart
    • verify getTotal() returns 0 for the initial state.
  • Examples: a newly created cart should have a count of 0 and a total of 0

2. Adding a Single Item

  • 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.
  • Examples: Adding an item { id: '1', name: 'Book', price: 10 } should result in an item count of 1 and a total cost of 10.

3. Adding Multiple Items

  • 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
  • Examples: adding { id: '1', name: 'Book', price: 10 } and { id: '2', name: 'Pen', price: 5 } should result in a count of 2 and a total of 15
Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal