Skip to content

Commit 4f9bbf9

Browse files
authored
Merge pull request #198 from codeableorg/e2e-user-create-order
Implement end-to-end test for order creation
2 parents f3639f7 + 41b0c9f commit 4f9bbf9

File tree

5 files changed

+101
-14
lines changed

5 files changed

+101
-14
lines changed

.env

Lines changed: 0 additions & 12 deletions
This file was deleted.

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ dist-ssr
2727
.react-router/
2828

2929
.build/
30+
.env
3031

3132
# Playwright
3233
/test-results/

src/db/migrations/initial.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ CREATE TABLE IF NOT EXISTS cart_items (
5353

5454
CREATE TABLE IF NOT EXISTS orders (
5555
id SERIAL PRIMARY KEY,
56-
user_id INTEGER REFERENCES users(id),
56+
user_id INTEGER REFERENCES users(id) ON DELETE CASCADE,
5757
total_amount NUMERIC(10,2) NOT NULL,
5858

5959
-- Customer and shipping details

src/e2e/user-create-order.spec.ts

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import { test, expect } from "@playwright/test";
2+
3+
import { hashPassword } from "@/lib/security";
4+
import type { CreateUserDTO } from "@/models/user.model";
5+
import {
6+
createUser,
7+
deleteUser,
8+
getUserByEmail,
9+
} from "@/repositories/user.repository";
10+
11+
test.describe("User", () => {
12+
let testUserId: number;
13+
14+
test.beforeAll(async () => {
15+
const testUser: CreateUserDTO = {
16+
17+
name: null,
18+
password: await hashPassword("letmein"),
19+
isGuest: false,
20+
};
21+
22+
const existingUser = await getUserByEmail(testUser.email);
23+
24+
if (existingUser) {
25+
await deleteUser(existingUser.id);
26+
}
27+
28+
const user = await createUser(testUser);
29+
testUserId = user.id;
30+
});
31+
32+
test.afterAll(async () => {
33+
await deleteUser(testUserId);
34+
});
35+
36+
test("User can create an order", async ({ page }) => {
37+
await page.goto("http://localhost:5173/");
38+
39+
await page.getByRole("link", { name: "Iniciar sesión" }).click();
40+
41+
const loginForm = {
42+
"Correo electrónico": "[email protected]",
43+
Contraseña: "letmein",
44+
};
45+
46+
for (const [key, value] of Object.entries(loginForm)) {
47+
const input = await page.getByRole("textbox", { name: key });
48+
await input.click();
49+
await input.fill(value);
50+
}
51+
52+
await page.getByRole("button", { name: "Iniciar sesión" }).click();
53+
54+
// Wait for the user to be logged in
55+
await expect(
56+
page.getByRole("button", { name: "Cerrar sesión" })
57+
).toBeVisible();
58+
59+
await page.getByRole("menuitem", { name: "Polos" }).click();
60+
await page.getByTestId("product-item").first().click();
61+
62+
await page.getByRole("button", { name: "Agregar al Carrito" }).click();
63+
await page.getByRole("link", { name: "Carrito de compras" }).click();
64+
65+
await page.getByRole("link", { name: "Continuar Compra" }).click();
66+
67+
const orderForm = {
68+
Nombre: "Testino",
69+
Apellido: "Diprueba",
70+
Compañia: "",
71+
Dirección: "Calle De Prueba 123",
72+
Ciudad: "Lima",
73+
"Provincia/Estado": "Lima",
74+
"Código Postal": "51111",
75+
Teléfono: "987456321",
76+
};
77+
78+
for (const [key, value] of Object.entries(orderForm)) {
79+
const input = await page.getByRole("textbox", { name: key });
80+
await input.click();
81+
await input.fill(value);
82+
}
83+
84+
await page.getByRole("combobox", { name: "País" }).selectOption("PE");
85+
86+
await page.getByRole("button", { name: "Confirmar Orden" }).click();
87+
88+
await expect(
89+
page.getByText("¡Muchas gracias por tu compra!")
90+
).toBeVisible();
91+
await expect(page.getByTestId("orderId")).toBeVisible();
92+
});
93+
});

src/routes/order-confirmation/index.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,12 @@ export default function OrderConfirmation({
2525
Llegaremos a la puerta de tu domicilio lo antes posible
2626
</p>
2727
<p className="text-sm font-medium mb-2">Código de seguimiento</p>
28-
<p className="text-sm font-medium text-accent-foreground">{orderId}</p>
28+
<p
29+
data-testid="orderId"
30+
className="text-sm font-medium text-accent-foreground"
31+
>
32+
{orderId}
33+
</p>
2934
</Container>
3035
</section>
3136
);

0 commit comments

Comments
 (0)