diff --git a/e2e/tests/api-driven/src/globalHelpers.ts b/e2e/tests/api-driven/src/globalHelpers.ts index 9894013afd..252cbb1e90 100644 --- a/e2e/tests/api-driven/src/globalHelpers.ts +++ b/e2e/tests/api-driven/src/globalHelpers.ts @@ -1,4 +1,4 @@ -import { TEST_EMAIL } from "../../ui-driven/src/helpers"; +import { TEST_EMAIL } from "../../ui-driven/src/globalHelpers"; import { $admin } from "./client"; export function createTeam( diff --git a/e2e/tests/api-driven/src/invite-to-pay/helpers.ts b/e2e/tests/api-driven/src/invite-to-pay/helpers.ts index 343ccb0872..bb906a1656 100644 --- a/e2e/tests/api-driven/src/invite-to-pay/helpers.ts +++ b/e2e/tests/api-driven/src/invite-to-pay/helpers.ts @@ -12,7 +12,7 @@ import { mockPassport, } from "./mocks"; import { $admin } from "../client"; -import { TEST_EMAIL } from "../../../ui-driven/src/helpers"; +import { TEST_EMAIL } from "../../../ui-driven/src/globalHelpers"; import { createTeam, createUser } from "../globalHelpers"; export async function setUpMocks() { diff --git a/e2e/tests/ui-driven/src/context.ts b/e2e/tests/ui-driven/src/context.ts index 88972b80dd..8dffe08045 100644 --- a/e2e/tests/ui-driven/src/context.ts +++ b/e2e/tests/ui-driven/src/context.ts @@ -1,5 +1,5 @@ import assert from "node:assert"; -import { log } from "./helpers"; +import { log } from "./globalHelpers"; import { sign } from "jsonwebtoken"; import { CoreDomainClient } from "@opensystemslab/planx-core"; import { GraphQLClient, gql } from "graphql-request"; diff --git a/e2e/tests/ui-driven/src/create-flow.spec.ts b/e2e/tests/ui-driven/src/create-flow/create-flow.spec.ts similarity index 84% rename from e2e/tests/ui-driven/src/create-flow.spec.ts rename to e2e/tests/ui-driven/src/create-flow/create-flow.spec.ts index 4c522859c6..bb8b3aba97 100644 --- a/e2e/tests/ui-driven/src/create-flow.spec.ts +++ b/e2e/tests/ui-driven/src/create-flow/create-flow.spec.ts @@ -3,14 +3,14 @@ import { contextDefaults, setUpTestContext, tearDownTestContext, -} from "./context"; +} from "../context"; import { - getTeamPage, createAuthenticatedSession, answerQuestion, clickContinue, -} from "./helpers"; -import type { Context } from "./context"; +} from "../globalHelpers"; +import type { Context } from "../context"; +import { getTeamPage, isGetUserRequest } from "./helpers"; test.describe("Navigation", () => { let context: Context = { @@ -43,32 +43,26 @@ test.describe("Navigation", () => { userId: context.user!.id!, }); - let getUserRequestCount = 0; - page.on("request", (req) => { - const isHasuraRequest = req.url().includes("/graphql"); - const isGetUserRequest = - isHasuraRequest && req.postData()?.toString().includes("GetUserById"); + const initialRequest = page.waitForRequest(isGetUserRequest); - if (isGetUserRequest) getUserRequestCount++; - }); + Promise.all([await page.goto("/"), await initialRequest]); - await page.goto("/"); - await page.waitForLoadState("networkidle"); + const team = page.locator("h2", { hasText: context.team.name }); - // Get user data on initial page load - expect(getUserRequestCount).toBe(1); + let isRepeatedRequestMade = false; + page.on( + "request", + (req) => (isRepeatedRequestMade = isGetUserRequest(req)), + ); - const team = page.locator("h2", { hasText: context.team.name }); - team.click(); - await page.waitForLoadState("networkidle"); + Promise.all([ + await team.click(), + expect(isRepeatedRequestMade).toBe(false), + ]); - // User data not refetched on navigation to a new page - expect(getUserRequestCount).toBe(1); + const reloadRequest = page.waitForRequest(isGetUserRequest); - // User data is refetched when page reloaded - await page.reload(); - await page.waitForLoadState("networkidle"); - expect(getUserRequestCount).toBe(2); + Promise.all([await page.reload(), await reloadRequest]); }); test("team data persists on page refresh @regression", async ({ diff --git a/e2e/tests/ui-driven/src/create-flow/helpers.ts b/e2e/tests/ui-driven/src/create-flow/helpers.ts new file mode 100644 index 0000000000..2bc95b7eaf --- /dev/null +++ b/e2e/tests/ui-driven/src/create-flow/helpers.ts @@ -0,0 +1,37 @@ +import { Browser, Page, Request } from "@playwright/test"; +import { createAuthenticatedSession } from "../globalHelpers"; + +export const isGetUserRequest = (req: Request) => { + const isHasuraRequest = req.url().includes("/graphql"); + const isGetUserOperation = req.postData()?.toString().includes("GetUserById"); + return Boolean(isHasuraRequest && isGetUserOperation); +}; + +export async function getAdminPage({ + browser, + userId, +}: { + browser: Browser; + userId: number; +}): Promise { + const page = await createAuthenticatedSession({ browser, userId }); + await page.goto("/"); + await page.waitForResponse((response) => { + return response.url().includes("/graphql"); + }); + return page; +} + +export async function getTeamPage({ + browser, + userId, + teamName, +}: { + browser: Browser; + userId: number; + teamName: string; +}): Promise { + const page = await getAdminPage({ browser, userId }); + await page.locator("h2", { hasText: teamName }).click(); + return page; +} diff --git a/e2e/tests/ui-driven/src/helpers.ts b/e2e/tests/ui-driven/src/globalHelpers.ts similarity index 94% rename from e2e/tests/ui-driven/src/helpers.ts rename to e2e/tests/ui-driven/src/globalHelpers.ts index 63722b46f6..ee9055fbd2 100644 --- a/e2e/tests/ui-driven/src/helpers.ts +++ b/e2e/tests/ui-driven/src/globalHelpers.ts @@ -66,35 +66,6 @@ export async function createAuthenticatedSession({ return page; } -export async function getAdminPage({ - browser, - userId, -}: { - browser: Browser; - userId: number; -}): Promise { - const page = await createAuthenticatedSession({ browser, userId }); - await page.goto("/"); - await page.waitForResponse((response) => { - return response.url().includes("/graphql"); - }); - return page; -} - -export async function getTeamPage({ - browser, - userId, - teamName, -}: { - browser: Browser; - userId: number; - teamName: string; -}): Promise { - const page = await getAdminPage({ browser, userId }); - await page.locator("h2", { hasText: teamName }).click(); - return page; -} - export async function saveSession({ page, context, diff --git a/e2e/tests/ui-driven/src/invite-to-pay/agent.spec.ts b/e2e/tests/ui-driven/src/invite-to-pay/agent.spec.ts index 5aed324a94..2d78f35348 100644 --- a/e2e/tests/ui-driven/src/invite-to-pay/agent.spec.ts +++ b/e2e/tests/ui-driven/src/invite-to-pay/agent.spec.ts @@ -1,5 +1,5 @@ import { test, expect, Page, BrowserContext } from "@playwright/test"; -import { addSessionToContext, modifyFlow } from "../helpers"; +import { addSessionToContext, modifyFlow } from "../globalHelpers"; import inviteToPayFlow from "../mocks/flows/invite-to-pay-flow"; import { Context, @@ -15,9 +15,7 @@ import { navigateToPayComponent, } from "./helpers"; import { mockPaymentRequest, modifiedInviteToPayFlow } from "./mocks"; -import { saveSession } from "../helpers"; -import { returnToSession } from "../helpers"; -import { clickContinue } from "../helpers"; +import { saveSession, returnToSession, clickContinue } from "../globalHelpers"; let context: Context = { ...contextDefaults, diff --git a/e2e/tests/ui-driven/src/invite-to-pay/helpers.ts b/e2e/tests/ui-driven/src/invite-to-pay/helpers.ts index f73de05143..7b84115020 100644 --- a/e2e/tests/ui-driven/src/invite-to-pay/helpers.ts +++ b/e2e/tests/ui-driven/src/invite-to-pay/helpers.ts @@ -5,10 +5,10 @@ import { answerContactInput, addSessionToContext, TEST_EMAIL, -} from "../helpers"; +} from "../globalHelpers"; import type { Page } from "@playwright/test"; import { gql, GraphQLClient } from "graphql-request"; -import { fillInEmail } from "../helpers"; +import { fillInEmail } from "../globalHelpers"; import { PaymentRequest } from "@opensystemslab/planx-core/dist/types"; import { Context } from "../context"; diff --git a/e2e/tests/ui-driven/src/invite-to-pay/mocks.ts b/e2e/tests/ui-driven/src/invite-to-pay/mocks.ts index 5d87a5e95d..bf68efb3a3 100644 --- a/e2e/tests/ui-driven/src/invite-to-pay/mocks.ts +++ b/e2e/tests/ui-driven/src/invite-to-pay/mocks.ts @@ -5,7 +5,7 @@ import { SessionData, } from "@opensystemslab/planx-core/types"; import inviteToPayFlow from "../mocks/flows/invite-to-pay-flow"; -import { TEST_EMAIL } from "../helpers"; +import { TEST_EMAIL } from "../globalHelpers"; export const mockPaymentRequest: Partial = { payeeEmail: TEST_EMAIL, diff --git a/e2e/tests/ui-driven/src/invite-to-pay/nominee.spec.ts b/e2e/tests/ui-driven/src/invite-to-pay/nominee.spec.ts index 39e353095e..d200fc05bb 100644 --- a/e2e/tests/ui-driven/src/invite-to-pay/nominee.spec.ts +++ b/e2e/tests/ui-driven/src/invite-to-pay/nominee.spec.ts @@ -1,6 +1,6 @@ import { test, expect, Page, APIRequestContext } from "@playwright/test"; import { v4 as uuidV4 } from "uuid"; -import { fillGovUkCardDetails, cards } from "../helpers"; +import { fillGovUkCardDetails, cards } from "../globalHelpers"; import inviteToPayFlow from "../mocks/flows/invite-to-pay-flow"; import { Context, diff --git a/e2e/tests/ui-driven/src/login.spec.ts b/e2e/tests/ui-driven/src/login.spec.ts index 75920f3fb9..b3d6a176ad 100644 --- a/e2e/tests/ui-driven/src/login.spec.ts +++ b/e2e/tests/ui-driven/src/login.spec.ts @@ -1,5 +1,5 @@ import { test, expect } from "@playwright/test"; -import { createAuthenticatedSession } from "./helpers"; +import { createAuthenticatedSession } from "./globalHelpers"; import { contextDefaults, setUpTestContext, diff --git a/e2e/tests/ui-driven/src/pay.spec.ts b/e2e/tests/ui-driven/src/pay.spec.ts index 221c2f859a..dde9ea19df 100644 --- a/e2e/tests/ui-driven/src/pay.spec.ts +++ b/e2e/tests/ui-driven/src/pay.spec.ts @@ -5,7 +5,7 @@ import { getSessionId, log, waitForPaymentResponse, -} from "./helpers"; +} from "./globalHelpers"; import type { Page } from "@playwright/test"; import payFlow from "./mocks/flows/pay-flow.json"; import { gql, GraphQLClient } from "graphql-request"; diff --git a/e2e/tests/ui-driven/src/save-and-return.spec.ts b/e2e/tests/ui-driven/src/save-and-return.spec.ts index 496d87bfed..c0b13d3613 100644 --- a/e2e/tests/ui-driven/src/save-and-return.spec.ts +++ b/e2e/tests/ui-driven/src/save-and-return.spec.ts @@ -16,7 +16,7 @@ import { returnToSession, saveSession, modifyFlow, -} from "./helpers"; +} from "./globalHelpers"; import type { Context } from "./context"; test.describe("Save and return", () => { diff --git a/e2e/tests/ui-driven/src/sections.spec.ts b/e2e/tests/ui-driven/src/sections.spec.ts index 1cf605e263..fa0999cd5c 100644 --- a/e2e/tests/ui-driven/src/sections.spec.ts +++ b/e2e/tests/ui-driven/src/sections.spec.ts @@ -17,7 +17,7 @@ import { expectConfirmation, saveSession, returnToSession, -} from "./helpers"; +} from "./globalHelpers"; import { gql } from "graphql-request"; import type { Context } from "./context"; import type { FlowGraph } from "@opensystemslab/planx-core/types";