From b7347a31fd3f409fd41ccd1a47b0c67c2ebc0b33 Mon Sep 17 00:00:00 2001 From: Philipp Metzner Date: Mon, 9 Feb 2026 14:26:30 +0100 Subject: [PATCH] Stabilize test assertions Credits: Mauro in #2474 --- .../views/Box/BoxViewHistoryOverlay.test.tsx | 3 --- .../views/BoxCreate/BoxCreateView.test.tsx | 4 ++++ front/src/views/Boxes/BoxesView.test.tsx | 24 +++++++------------ .../src/views/Boxes/BoxesViewActions.test.tsx | 6 +---- .../CreateShipmentView.test.tsx | 5 +--- 5 files changed, 14 insertions(+), 28 deletions(-) diff --git a/front/src/views/Box/BoxViewHistoryOverlay.test.tsx b/front/src/views/Box/BoxViewHistoryOverlay.test.tsx index fc010afe8..a5afab52d 100644 --- a/front/src/views/Box/BoxViewHistoryOverlay.test.tsx +++ b/front/src/views/Box/BoxViewHistoryOverlay.test.tsx @@ -113,9 +113,6 @@ describe("3.1.12 - Box HistoryOverlay on BoxView", () => { expect(may2023).toBeInTheDocument(); expect(jan2023).toBeInTheDocument(); - // Wait a moment to ensure DOM is fully settled before position checking - await new Promise((resolve) => setTimeout(resolve, 50)); - // Verify that the 2024 entry appears before the 2023 entries // by checking the document position of the elements const march2024Position = march2024.compareDocumentPosition(may2023); diff --git a/front/src/views/BoxCreate/BoxCreateView.test.tsx b/front/src/views/BoxCreate/BoxCreateView.test.tsx index 7276ffad4..d6c5321c0 100644 --- a/front/src/views/BoxCreate/BoxCreateView.test.tsx +++ b/front/src/views/BoxCreate/BoxCreateView.test.tsx @@ -193,12 +193,16 @@ describe("BoxCreateView", () => { // Fill in the form await selectOptionInSelectField(user, /product/i, "Snow trousers (Boy)", "Create New Box"); + expect(await screen.findByText("Snow trousers (Boy)")).toBeInTheDocument(); await selectOptionInSelectField(user, /size/i, "S", "Create New Box"); + expect(await screen.findByText("S")).toBeInTheDocument(); await selectOptionInSelectField(user, /location/i, "Warehouse", "Create New Box"); + expect(await screen.findByText("Warehouse")).toBeInTheDocument(); const numberOfItemsInput = screen.getByRole("spinbutton"); await user.clear(numberOfItemsInput); await user.type(numberOfItemsInput, "5"); + await waitFor(() => expect(numberOfItemsInput).toHaveValue("5")); //added new Tags await user.type(screen.getByLabelText(/Tags/), "epic"); diff --git a/front/src/views/Boxes/BoxesView.test.tsx b/front/src/views/Boxes/BoxesView.test.tsx index a0a387f85..f95199a0e 100644 --- a/front/src/views/Boxes/BoxesView.test.tsx +++ b/front/src/views/Boxes/BoxesView.test.tsx @@ -647,9 +647,6 @@ describe("4.8.2 - Selecting rows and performing bulk actions", () => { await user.click(checkbox2); await waitFor(() => expect(checkbox2).toBeChecked(), { timeout: 10000 }); - // Add a delay to ensure state propagation in CI environments - await new Promise((resolve) => setTimeout(resolve, 500)); - // Wait for the action buttons to be available (not in error state) await waitFor( () => { @@ -668,21 +665,16 @@ describe("4.8.2 - Selecting rows and performing bulk actions", () => { await user.click(moveBoxesButton); - expect( - await screen.findByRole( - "menuitem", - { - name: /wh1/i, - }, - { timeout: 10000 }, - ), - ).toBeInTheDocument(); - - await user.click( - screen.getByRole("menuitem", { + // There may be multiple menuitems with the name 'WH1', so use findAllByRole + const wh1MenuItems = await screen.findAllByRole( + "menuitem", + { name: /wh1/i, - }), + }, + { timeout: 10000 }, ); + expect(wh1MenuItems.length).toBeGreaterThan(0); + await user.click(wh1MenuItems[0]); // Wait for the UI to update after the action await waitFor( diff --git a/front/src/views/Boxes/BoxesViewActions.test.tsx b/front/src/views/Boxes/BoxesViewActions.test.tsx index 74f0af329..7866e15f0 100644 --- a/front/src/views/Boxes/BoxesViewActions.test.tsx +++ b/front/src/views/Boxes/BoxesViewActions.test.tsx @@ -880,11 +880,7 @@ boxesViewActionsTests.forEach(({ name, mocks, clicks, toast, searchParams, trigg if (clicks[1]) { // Wait until the sub-action is present, ensuring all Menu updates are flushed - const subButton = await waitFor( - () => screen.getByText(clicks[1]), - {}, - { timeout: 10000 }, - ); + const subButton = await screen.findByText(clicks[1]); expect(subButton).toBeInTheDocument(); await user.click(subButton); } diff --git a/front/src/views/Transfers/CreateShipment/CreateShipmentView.test.tsx b/front/src/views/Transfers/CreateShipment/CreateShipmentView.test.tsx index 7836cfe89..d3ff0a370 100644 --- a/front/src/views/Transfers/CreateShipment/CreateShipmentView.test.tsx +++ b/front/src/views/Transfers/CreateShipment/CreateShipmentView.test.tsx @@ -179,14 +179,11 @@ it("4.3.1 - Initial load of Page", async () => { // Test case 4.3.1.2 - Content: Displays Partner Orgs Select Options await assertOptionsInSelectField(user, /organisation/i, [/boxcare/i], title); await selectOptionInSelectField(user, /organisation/i, "BoxCare"); - // Add a delay to ensure state propagation after selection - await new Promise((resolve) => setTimeout(resolve, 300)); expect(await screen.findByText("BoxCare", {}, { timeout: 10000 })).toBeInTheDocument(); // Test case 4.3.1.3 - Content: Displays Partner Bases Select Options When Partner Organisation Selected await assertOptionsInSelectField(user, /base/i, [/samos/i, /thessaloniki/i, /athens/i], title); await selectOptionInSelectField(user, /base/i, "Samos"); - // Add a delay to ensure state propagation after selection - await new Promise((resolve) => setTimeout(resolve, 300)); + expect(await screen.findByText("Samos", {}, { timeout: 10000 })).toBeInTheDocument(); // Breadcrumbs are there expect(screen.getByRole("link", { name: /back to manage shipments/i })).toBeInTheDocument();