-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathCatalogView.test.tsx
58 lines (47 loc) · 1.76 KB
/
CatalogView.test.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import React from 'react';
import { rest } from 'msw';
import { MOCK_API_URL } from '../../../mocks/constants';
import { server } from '../../../mocks/server';
import { render, screen, userEvent, waitFor } from '../../../test/test-utils';
import { CatalogView } from './CatalogView';
const addProduct = jest.fn();
describe('<CatalogView />', () => {
test('renders correctly', async () => {
render(<CatalogView />);
// expect 16 products
const products = await screen.findAllByTestId('product');
expect(products.length).toBe(16);
});
test('renders an error if fetching of the catalog fails', async () => {
// simulate an error when fetching the catalog
server.use(
rest.get(`${MOCK_API_URL}/catalog`, (req, res, ctx) => {
return res(ctx.status(404));
})
);
// suppress console errors
jest.spyOn(console, 'error').mockImplementation(() => {});
render(<CatalogView />);
const errorMessage = await screen.findByText(/404/);
expect(errorMessage).toBeInTheDocument();
// restore console errors
jest.restoreAllMocks();
});
test('when a product is clicked, it is added to the cart', async () => {
// mock add product to cart
server.use(
rest.post(`${MOCK_API_URL}/cart/items`, (req, res, ctx) => {
const { productId } = req.body as { productId: string };
addProduct(productId);
return res(ctx.status(200), ctx.json({ items: [] }));
})
);
render(<CatalogView />);
// click on the first product
const products = await screen.findAllByTestId('product');
userEvent.click(products[0]);
// expect product to be added to the cart
await waitFor(() => expect(addProduct).toBeCalledTimes(1));
expect(addProduct).toBeCalledWith('apple-imac');
});
});