import { expect, test, type Locator, type Page, type Response } from "@playwright/test"; import { bookingTestData, loginAsOperator } from "./fixtures"; const changeInvoiceCollectionActionRegex = /Skift fakturasamling|Change invoice collection|Rechnungssammlung|fakturainnsamling|fakturasamling/i; const findChangeInvoiceCollectionAction = async (page: Page): Promise => { const actionTriggers = page.locator("tbody tr .dropdown-trigger > button"); const triggerCount = await actionTriggers.count(); for (let index = 0; index < triggerCount; index += 1) { const trigger = actionTriggers.nth(index); if (!(await trigger.isVisible().catch(() => false))) { continue; } await trigger.click(); const dropdownRoot = trigger.locator("xpath=ancestor::div[contains(@class,'dropdown')][1]"); const changeInvoiceCollectionAction = dropdownRoot.getByRole("button", { name: changeInvoiceCollectionActionRegex, }); if ((await changeInvoiceCollectionAction.count()) > 0) { const visibleAction = changeInvoiceCollectionAction.first(); if (await visibleAction.isVisible().catch(() => false)) { return visibleAction; } } await page.keyboard.press("Escape"); } return null; }; test.describe("POS order actions", () => { test("change invoice collection opens picker and stays on the same page", async ({ page }) => { await loginAsOperator(page); const departmentId = bookingTestData.departmentId.toString(); await page.goto(`/admin/${departmentId}/modules/pos/orders`); await expect(page).toHaveURL(new RegExp(`/admin/${departmentId}/modules/pos/orders`)); await expect(page.locator("table")).toBeVisible({ timeout: 30000 }); const ordersResponse = await page .waitForResponse( (response: Response) => response.request().method() === "GET" && response.url().includes("/orders?") && response.url().includes("department_id:"), { timeout: 30000 } ) .catch(() => null); test.skip(!ordersResponse, "Orders list request did not complete in time for this environment."); await page.waitForTimeout(500); const changeInvoiceCollectionAction = await findChangeInvoiceCollectionAction(page); test.skip( !changeInvoiceCollectionAction, "No order action menu with 'Skift fakturasamling' is available for current test data/permissions." ); if (!changeInvoiceCollectionAction) { return; } await expect(changeInvoiceCollectionAction).toBeVisible(); const beforeUrl = page.url(); const newTabPromise = page.waitForEvent("popup", { timeout: 2000 }).catch(() => null); await changeInvoiceCollectionAction.click(); const popup = await newTabPromise; expect(popup).toBeNull(); await expect(page).toHaveURL(beforeUrl); const pickerModal = page.locator(".swal2-container .modal-card"); await expect( pickerModal, "Expected invoice collection picker modal to open after clicking the action." ).toBeVisible(); const selectButtons = pickerModal.getByRole("button", { name: /Vælg|Select/i }); const selectableCount = await selectButtons.count(); test.skip(selectableCount === 0, "No invoice collections available for selection in test data."); await selectButtons.first().click(); await expect(page).toHaveURL(beforeUrl); await expect(page.locator(".swal2-container, .swal2-popup")).toContainText(/Faktura samling|invoice collection/i); }); });