Show the subtotal in the checkout order summary
React · React · beginner · modification
The checkout review page listed the items in the order but no subtotal, so customers added up the line totals in their heads before going on to payment. `src/orders/OrderSummary.jsx` now computes the subtotal from the order's lines and shows it in a footer row under the items, formatted like the line totals. `src/orders/OrderSummary.test.jsx` (new) covers the item rows and the subtotal row.
Shop SPA on React 18.3 (Vite, function components). Tests run with `npm test`: Vitest 2 with @testing-library/react 16 in jsdom. Existing and unchanged: `CheckoutReview.jsx` renders `<OrderSummary lines={cart.lines} />` once the cart has loaded and shows the loading, error and empty-cart states itself, so `lines` always holds at least one line. `lines` is an array of at most 50 `{ id, name, quantity, unitPriceCents }` objects: `id` is a unique non-empty string, `name` a non-empty display name, `quantity` an integer from 1 to 99 and `unitPriceCents` the price of one unit in US cents, an integer from 0 to 500000 (the cart API validates both). All prices are in US dollars. `formatCents` in `src/money.js` formats a whole number of cents as dollars with `Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' })` (`1650` gives `$16.50`). `OrderSummary.css` styles the table.
Requirements
- The checkout review page's order summary (`OrderSummary`, `lines` prop) gains a Subtotal row under the items. The subtotal is the sum of the line totals shown in the Total column, where a line's total is its unit price times its quantity (`unitPriceCents * quantity`). It is formatted with `formatCents`, like the line totals.
- Shipping and tax are not part of the subtotal: the payment step (unchanged) adds them and computes the amount charged on the server. The Subtotal row is for display only.
- Unchanged: the table keeps its caption ("Order summary"), its Item, Qty and Total columns and one row per line, keyed by the line's `id`; the `lines` array and its objects are not modified.
- The summary has tests that run with the existing `npm test` (Vitest).
Files touched
- src/orders/OrderSummary.jsx
- src/orders/OrderSummary.test.jsx