diff --git a/2-copy-of-code/lesson-09/ecommerce-project/src/pages/home/HomePage.test.jsx b/2-copy-of-code/lesson-09/ecommerce-project/src/pages/home/HomePage.test.jsx index 83d52e1..41507ea 100755 --- a/2-copy-of-code/lesson-09/ecommerce-project/src/pages/home/HomePage.test.jsx +++ b/2-copy-of-code/lesson-09/ecommerce-project/src/pages/home/HomePage.test.jsx @@ -9,6 +9,7 @@ vi.mock('axios'); describe('HomePage component', () => { let loadCart; + let user; beforeEach(() => { loadCart = vi.fn(); @@ -41,6 +42,8 @@ describe('HomePage component', () => { }; } }); + + user = userEvent.setup(); }); it('displays the products correct', async () => { @@ -63,4 +66,31 @@ describe('HomePage component', () => { .getByText('Intermediate Size Basketball') ).toBeInTheDocument(); }); + + it('adds a product to the cart', async () => { + render( + + + + ); + 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); + }); }); \ No newline at end of file