12 / 12Project
A full flow test from the product list to the cart.
In this step you bring all the earlier pieces together. The user opens the store page. They see the products, add them to the cart, check the total and remove one product.
| Part | Its job | Where you learned it |
|---|---|---|
ProductList | Loads the products from the API and shows them. | Product List |
CartProvider | Shares the cart state. | Provider and Hook |
CartBadge | Shows the number of items in the cart. | Provider and Hook |
CartSummary | Shows the items and the total. | Provider and Hook |
| MSW | Imitates the API response. | Mock |
The working project is on GitHub: examples/vitest/project. Compare against it when you get stuck.
import { CartBadge, CartSummary } from "./cart-components";
import { useCart } from "./cart-context";
import { fetchProducts } from "./fetch-products";
import { ProductList } from "./ProductList";
export function ShopPage() {
const { addToCart } = useCart();
return (
<>
<CartBadge />
<ProductList loadProducts={fetchProducts} onAddToCart={addToCart} />
<CartSummary />
</>
);
}The page uses the real fetchProducts function. In the test, MSW provides the API response. No component is mocked. This is an integration test.
import { screen, within } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { http, HttpResponse } from "msw/http";
import { server } from "./mocks/server";
import { ShopPage } from "./ShopPage";
import { renderWithProviders } from "./test-utils";In MSW 3, http and HttpResponse are imported from the msw/http path.1
The topics: MSW, within,2 findByRole,3 userEvent, Context.
describe("ShopPage", () => {
test("the user adds products to the cart and sees the total", async () => {
const user = userEvent.setup();
renderWithProviders(<ShopPage />);
const keyboard = await screen.findByRole("article", {
name: "Mechanical Keyboard",
});
await user.click(
within(keyboard).getByRole("button", { name: "Add to cart" }),
);
await user.click(
within(keyboard).getByRole("button", { name: "Add to cart" }),
);
const headphones = screen.getByRole("article", {
name: "Wireless Headphones",
});
await user.click(
within(headphones).getByRole("button", { name: "Add to cart" }),
);
expect(screen.getByRole("link", { name: "Cart (3)" })).toBeInTheDocument();
const summary = screen.getByRole("region", { name: "Cart summary" });
expect(within(summary).getByText("Mechanical Keyboard × 2")).toBeInTheDocument();
expect(
within(summary).getByText("Wireless Headphones × 1"),
).toBeInTheDocument();
expect(within(summary).getByText("Total: €5,697.00")).toBeInTheDocument();
});Check the arithmetic: 2 × 2,199 + 1,299 = 5,697. Work the expected value out by hand. Do not compute it with the formula from the code.
Tip
The accessible name of the article and region roles comes from the label you give, not from their content. That is why ProductCard has to have aria-labelledby and CartSummary an aria-label. Without a label, a query with name does not match.4
The topics: the accessible name (aria-label) and the cart emptying.
test("the user removes a product from the cart", async () => {
const user = userEvent.setup();
renderWithProviders(<ShopPage />);
const keyboard = await screen.findByRole("article", {
name: "Mechanical Keyboard",
});
await user.click(
within(keyboard).getByRole("button", { name: "Add to cart" }),
);
await user.click(
screen.getByRole("button", {
name: "Remove Mechanical Keyboard from cart",
}),
);
expect(screen.getByText("Your cart is empty.")).toBeInTheDocument();
expect(screen.getByRole("link", { name: "Cart (0)" })).toBeInTheDocument();
});The topics: toBeVisible and toBeDisabled.5
test("an out-of-stock product cannot be added to the cart", async () => {
renderWithProviders(<ShopPage />);
const mouse = await screen.findByRole("article", { name: "Gaming Mouse" });
expect(within(mouse).getByText("Out of stock")).toBeVisible();
expect(
within(mouse).getByRole("button", { name: "Add to cart" }),
).toBeDisabled();
});The topic: a different response for a single test with server.use.6
test("the cart keeps working when the API fails", async () => {
server.use(
http.get(
"https://api.shop.test/products",
() => new HttpResponse(null, { status: 500 }),
),
);
renderWithProviders(<ShopPage />);
expect(await screen.findByRole("alert")).toHaveTextContent(
"Products could not be loaded.",
);
expect(screen.getByRole("link", { name: "Cart (0)" })).toBeInTheDocument();
expect(screen.getByText("Your cart is empty.")).toBeInTheDocument();
});
});Add a Decrease button to the cart. Write the test first:
Add a decreaseQuantity(productId) function to CartProvider. When the quantity is 1, it should take the product out of the cart.
In CartSummary, show a button for every product whose accessible name is "decrease <product name> quantity".
The test: with 2 Mechanical Keyboard in the cart, clicking Decrease shows Mechanical Keyboard × 1. On the second click the product leaves the list and Your cart is empty. appears.
Sources
The `msw/http` import path for `http` and `HttpResponse`.
Narrowing a query to inside a single element.
The priority of `getByRole`, absence with `queryBy` and waiting with `findBy`.
The name of the `article` and `region` roles comes from the author, and `region` requires one.
The rules of the `toBeVisible` and `toBeDisabled` matchers.
Adding a handler at runtime.
The more your tests resemble the way your software is used, the more confidence they give you.
Mocking network requests with MSW.
Your progress is stored only in this browser.
Last updated: November 3, 2026