import { randomUUID } from "node:crypto"; import type { Page } from "@playwright/test"; import { expect, test } from "@playwright/test"; import { createCustomerViaUi } from "../../customers/support/create-customer-via-ui"; import { loginViaUi } from "../../support/login-via-ui"; import { createProformaViaUi } from "./support/create-proforma-via-ui"; const selectCandidate = async (page: Page, customer: { id: string; search: string }) => { await page.locator("#proforma-change-customer").click(); const responsePromise = page.waitForResponse((response) => { const url = new URL(response.url()); return ( response.request().method() === "GET" && /\/customers\/?$/.test(url.pathname) && url.searchParams.get("q") === customer.search ); }); await page.getByRole("dialog").locator('input[inputmode="search"]').fill(customer.search); const response = await responsePromise; expect(response.status()).toBe(200); await page.getByTestId(`customer-selection-${customer.id}`).click(); await page.locator("#customer-selection-confirm").click(); }; const confirmCandidate = async (page: Page) => { await expect(page.locator("#proforma-customer-change-confirm")).toBeVisible(); await page.locator("#proforma-customer-change-confirm").click(); }; const save = async (page: Page, id: string) => { const response = page.waitForResponse( (value) => value.request().method() === "PUT" && new URL(value.url()).pathname.endsWith(`/proformas/${id}`) ); await page.getByTestId("proforma-update-submit").click(); const result = await response; expect(result.status()).toBe(200); }; const returnToProforma = async (page: Page, id: string) => { await page.goto(`/proformas/${id}/edit?returnTo=/proformas`); await expect(page.locator("#proforma-change-customer")).toBeVisible(); }; test("CUSTOMER-INVOICES-PROFORMA-CUSTOMER-001: conserva el cliente seleccionado al crear", async ({ page, }, testInfo) => { await loginViaUi(page); const proforma = await createProformaViaUi(page, testInfo); await page.reload(); await expect(page.locator("#proforma-recipient-name")).toHaveText(proforma.customer.name); }); test("CUSTOMER-INVOICES-PROFORMA-CUSTOMER-002: cambia de cliente y lo persiste", async ({ page, }, testInfo) => { await loginViaUi(page); const proforma = await createProformaViaUi(page, testInfo); const search = randomUUID().replaceAll("-", ""); const customer = await createCustomerViaUi(page, testInfo, `E2E Customer B ${search}`); await returnToProforma(page, proforma.id); await selectCandidate(page, { id: customer.id, search }); await confirmCandidate(page); await expect(page.locator("#proforma-recipient-name")).toHaveText(customer.name); await save(page, proforma.id); await page.reload(); await expect(page.locator("#proforma-recipient-name")).toHaveText(customer.name); }); test("CUSTOMER-INVOICES-PROFORMA-CUSTOMER-003: confirma el cambio y refresca el recipient", async ({ page, }, testInfo) => { await loginViaUi(page); const proforma = await createProformaViaUi(page, testInfo); const search = randomUUID().replaceAll("-", ""); const customer = await createCustomerViaUi(page, testInfo, `E2E Customer Derived ${search}`); await returnToProforma(page, proforma.id); await selectCandidate(page, { id: customer.id, search }); await confirmCandidate(page); await expect(page.locator("#proforma-recipient-name")).toHaveText(customer.name); await save(page, proforma.id); await page.reload(); await expect(page.locator("#proforma-recipient-name")).toHaveText(customer.name); }); test("CUSTOMER-INVOICES-PROFORMA-CUSTOMER-004: cancelar conserva el cliente anterior", async ({ page, }, testInfo) => { await loginViaUi(page); const proforma = await createProformaViaUi(page, testInfo); const search = randomUUID().replaceAll("-", ""); const customer = await createCustomerViaUi(page, testInfo, `E2E Customer Cancel ${search}`); await returnToProforma(page, proforma.id); await selectCandidate(page, { id: customer.id, search }); await expect(page.locator("#proforma-customer-change-cancel")).toBeVisible(); await page.locator("#proforma-customer-change-cancel").click(); await expect(page.locator("#proforma-recipient-name")).toHaveText(proforma.customer.name); await page.reload(); await expect(page.locator("#proforma-recipient-name")).toHaveText(proforma.customer.name); });