|
| 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 | +}); |
0 commit comments