The situation
You work on the storefront of a shop that sells travel and office gear. Customers can search the product list and add items to their cart. Since search shipped, support has seen a stream of returns for items people say they never chose, and complaints that search misses obvious matches.
"I searched for 'Backpack', clicked Add to cart, and a Water Bottle was added instead."
"Typing 'water' finds nothing, but 'Water' works."
Your tasks
Task 1: Fix Add to cart
Fix addToCart(cart, products, productId) in src/utils/cart.js so that:
- It adds the product whose
id is productId, whether or not the product list has been filtered by a search. - Adding a product that is already in the cart increases its quantity by 1.
- It returns a new cart and leaves the cart it was given (and its lines) unchanged. React only re-renders when it gets a new array.
- An unknown id leaves the cart as it was.
Task 2: Fix search
searchProducts must ignore letter case: water, WATER and Water all find Water Bottle.
Make every test in src/utils/cart.test.js pass.
Task 3: Check the page
In the preview, search for back and add the Backpack twice. The cart should show Backpack x2 and nothing else.
Project files
package.json
public/
index.html
src/
index.js
index.css
App.js
App.css
data/
products.js
utils/
cart.js
cart.test.jssrc/App.js: the shop page. It holds the search text and the cart, shows the matching products with an Add to cart button each, and lists the cart.src/App.css: the two-column layout (products beside the cart on wider screens). Everything else is styled with Tailwind classes.src/data/products.js: the four products for sale, each with an id and a name.src/utils/cart.js: searchProducts (filters products by name) and addToCart (returns the cart with a product added).src/utils/cart.test.js: the tests for both helpers.package.json, public/index.html, src/index.js and src/index.css: the standard React app setup. src/index.js starts the app and loads Tailwind CSS, and src/index.css holds the global styles. You don't need to change these.
How to check your work
- Tests: press ▶ Run tests next to the Tests heading in the bottom panel. They don't re-run on their own, so run them again after each change. PASS means every visible test passed.
- Preview: the bottom-left panel shows the running app and rebuilds as you type. Use Open in new tab above it, or Copy link and paste it into a new tab, to see it full size. That tab follows your edits.
- Your work is saved in this browser. Reset (top right) puts the starting code back.