Skip to content

Commit 939e42f

Browse files
committed
Setup tests. Fix small issues
1 parent 5c9a441 commit 939e42f

File tree

14 files changed

+247
-1324
lines changed

14 files changed

+247
-1324
lines changed

package-lock.json

Lines changed: 40 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"name": "store",
3-
"version": "0.1.0",
2+
"name": "my-store-app",
3+
"version": "1.0.0",
44
"private": true,
55
"scripts": {
66
"start": "vite",
@@ -48,6 +48,7 @@
4848
"@typescript-eslint/eslint-plugin": "^5.30.5",
4949
"@typescript-eslint/parser": "^5.30.5",
5050
"@vitejs/plugin-react": "^1.3.2",
51+
"@vitest/ui": "^0.18.0",
5152
"eslint": "^8.19.0",
5253
"eslint-config-prettier": "^8.5.0",
5354
"eslint-plugin-prettier": "^4.2.1",

src/components/App/App.test.tsx

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,53 @@
1-
import { render } from "@testing-library/react";
1+
import { MemoryRouter } from "react-router-dom";
22
import { test, expect } from "vitest";
33
import App from "~/components/App/App";
4+
import { server } from "~/mocks/server";
5+
import { rest } from "msw";
6+
import API_PATHS from "~/constants/apiPaths";
7+
import { CartItem } from "~/models/CartItem";
8+
import { AvailableProduct } from "~/models/Product";
9+
import { renderWithProviders } from "~/testUtils";
10+
import { screen, waitForElementToBeRemoved } from "@testing-library/react";
11+
import { formatAsPrice } from "~/utils/utils";
412

5-
test("renders correctly", () => {
6-
const container = render(<App />);
7-
expect(container).toMatchSnapshot();
13+
test("Renders products list", async () => {
14+
const products: AvailableProduct[] = [
15+
{
16+
id: "1",
17+
title: "Product 1",
18+
description: "Product 1 description",
19+
price: 1,
20+
count: 1,
21+
},
22+
{
23+
id: "2",
24+
title: "Product 2",
25+
description: "Product 2 description",
26+
price: 2,
27+
count: 2,
28+
},
29+
];
30+
server.use(
31+
rest.get(`${API_PATHS.bff}/product/available`, (req, res, ctx) => {
32+
return res(
33+
ctx.status(200),
34+
ctx.delay(),
35+
ctx.json<AvailableProduct[]>(products)
36+
);
37+
}),
38+
rest.get(`${API_PATHS.cart}/profile/cart`, (req, res, ctx) => {
39+
return res(ctx.status(200), ctx.json<CartItem[]>([]));
40+
})
41+
);
42+
renderWithProviders(
43+
<MemoryRouter initialEntries={["/"]}>
44+
<App />
45+
</MemoryRouter>
46+
);
47+
48+
await waitForElementToBeRemoved(() => screen.queryByText(/Loading/));
49+
products.forEach((product) => {
50+
expect(screen.getByText(product.title)).toBeInTheDocument();
51+
expect(screen.getByText(formatAsPrice(product.price))).toBeInTheDocument();
52+
});
853
});

src/components/App/App.tsx

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,32 @@
1-
import PageProducts from "~/components/pages/PageProducts/PageProducts";
2-
import MainLayout from "~/components/MainLayout/MainLayout";
31
import { Routes, Route } from "react-router-dom";
2+
import MainLayout from "~/components/MainLayout/MainLayout";
43
import PageProductForm from "~/components/pages/PageProductForm/PageProductForm";
5-
import PageCart from "~/components/pages/PageCart/PageCart";
64
import PageOrders from "~/components/pages/PageOrders/PageOrders";
75
import PageOrder from "~/components/pages/PageOrder/PageOrder";
86
import PageProductImport from "~/components/pages/admin/PageProductImport/PageProductImport";
7+
import PageCart from "~/components/pages/PageCart/PageCart";
8+
import PageProducts from "~/components/pages/PageProducts/PageProducts";
9+
import { Typography } from "@mui/material";
910

1011
function App() {
1112
return (
1213
<MainLayout>
1314
<Routes>
1415
<Route path="/" element={<PageProducts />} />
1516
<Route path="cart" element={<PageCart />} />
16-
<Route path="admin">
17-
<Route path="orders" element={<PageOrders />} />
18-
<Route path="orders/:id" element={<PageOrder />} />
19-
<Route path="products" element={<PageProductImport />} />
20-
<Route path="product-form" element={<PageProductForm />}>
21-
<Route path=":id" element={<PageProductForm />} />
22-
</Route>
17+
<Route path="admin/orders">
18+
<Route index element={<PageOrders />} />
19+
<Route path=":id" element={<PageOrder />} />
20+
</Route>
21+
<Route path="admin/products" element={<PageProductImport />} />
22+
<Route path="product-form">
23+
<Route index element={<PageProductForm />} />
24+
<Route path=":id" element={<PageProductForm />} />
2325
</Route>
26+
<Route
27+
path="*"
28+
element={<Typography variant="h1">Not found</Typography>}
29+
/>
2430
</Routes>
2531
</MainLayout>
2632
);

0 commit comments

Comments
 (0)