Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ vi.mock('axios');

describe('HomePage component', () => {
let loadCart;
let user;

beforeEach(() => {
loadCart = vi.fn();
Expand Down Expand Up @@ -41,6 +42,8 @@ describe('HomePage component', () => {
};
}
});

user = userEvent.setup();
});

it('displays the products correct', async () => {
Expand All @@ -63,4 +66,31 @@ describe('HomePage component', () => {
.getByText('Intermediate Size Basketball')
).toBeInTheDocument();
});

it('adds a product to the cart', async () => {
render(
<MemoryRouter>
<HomePage cart={[]} loadCart={loadCart} />
</MemoryRouter>
);
const productContainers = await screen.findAllByTestId('product-container');

const addToCartButton1 = within(productContainers[0])
.getByTestId('add-to-cart-button');
await user.click(addToCartButton1);

const addToCartButton2 = within(productContainers[1])
.getByTestId('add-to-cart-button');
await user.click(addToCartButton2);

expect(axios.post).toHaveBeenNthCalledWith(1, '/api/cart-items', {
productId: 'e43638ce-6aa0-4b85-b27f-e1d07eb678c6',
quantity: 1
});
expect(axios.post).toHaveBeenNthCalledWith(2, '/api/cart-items', {
productId: '15b6fc6f-327a-4ec4-896f-486349e85a3d',
quantity: 1
});
expect(loadCart).toHaveBeenCalledTimes(2);
});
});