-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.test.tsx
More file actions
103 lines (82 loc) · 2.57 KB
/
app.test.tsx
File metadata and controls
103 lines (82 loc) · 2.57 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import { cleanup, render, screen, waitFor } from "@/utils/test-utils";
import App from "./app";
import { it, expect, beforeAll, afterEach, afterAll, vi } from "vitest";
import { setupServer } from "msw/node";
import { http, HttpResponse } from "msw";
import userEvent from "@testing-library/user-event";
const products = [
{
id: 0,
name: "Special Burger",
price: 1500, // $15.00
},
];
const paymentEndpointMock = vi.fn();
export const restHandlers = [
http.get("http://localhost:3000/api/products", () => {
return HttpResponse.json(products);
}),
http.post("http://localhost:3000/api/payment", async ({ request }) => {
const body = await request.json();
paymentEndpointMock(body);
return HttpResponse.json({});
}),
];
const server = setupServer(...restHandlers);
// Start server before all tests
beforeAll(() => server.listen({ onUnhandledRequest: "error" }));
// Close server after all tests
afterAll(() => server.close());
// Reset handlers after each test for test isolation
afterEach(() => {
cleanup();
server.resetHandlers();
paymentEndpointMock.mockClear();
});
it("Renders correctly, displays list of products", async () => {
render(<App />);
expect(screen.getByText("Products")).toBeVisible();
// Wait for the products to load
await waitFor(() => {
expect(screen.getByText("Special Burger")).toBeVisible();
});
});
it("Adds a product and shows correct total", async () => {
render(<App />);
await waitFor(() => {
expect(screen.getByText("Special Burger")).toBeVisible();
});
await userEvent.click(
screen.getByRole("button", { name: "Add Special Burger to order" })
);
// TODO: Check the total
});
it("Adds a product twice and shows correct total", async () => {
render(<App />);
await waitFor(() => {
expect(screen.getByText("Special Burger")).toBeVisible();
});
await userEvent.click(
screen.getByRole("button", { name: "Add Special Burger to order" })
);
await userEvent.click(
screen.getByRole("button", { name: "Add Special Burger to order" })
);
// TODO: Check the total
});
it("Adds a product and submits correct data to API", async () => {
render(<App />);
await waitFor(() => {
expect(screen.getByText("Special Burger")).toBeVisible();
});
await userEvent.click(
screen.getByRole("button", { name: "Add Special Burger to order" })
);
// TODO: Implement application logic to submit the payment
expect(paymentEndpointMock).toHaveBeenCalledWith(
expect.objectContaining({
product: expect.objectContaining(products[0]),
quantity: 1,
})
);
});