From 0e0374ea0863fd5aa29ac42b79e874fe50fbe808 Mon Sep 17 00:00:00 2001 From: Rory Doak Date: Fri, 29 Nov 2024 14:49:51 +0000 Subject: [PATCH] separate out types and service data --- .../src/create-flow-with-geospatial.spec.ts | 24 +++------ e2e/tests/ui-driven/src/create-flow.spec.ts | 32 +++++------- .../ui-driven/src/helpers/addComponent.ts | 3 +- e2e/tests/ui-driven/src/helpers/context.ts | 51 ++++--------------- .../ui-driven/src/helpers/globalHelpers.ts | 8 +-- .../src/helpers/navigateAndPublish.ts | 2 - .../ui-driven/src/helpers/serviceData.ts | 15 ++++++ e2e/tests/ui-driven/src/helpers/types.ts | 22 ++++++++ .../ui-driven/src/helpers/userActions.ts | 9 ++-- .../ui-driven/src/invite-to-pay/agent.spec.ts | 4 +- .../ui-driven/src/invite-to-pay/helpers.ts | 6 +-- .../src/invite-to-pay/nominee.spec.ts | 6 +-- e2e/tests/ui-driven/src/login.spec.ts | 4 +- e2e/tests/ui-driven/src/pages/Editor.ts | 13 +++++ e2e/tests/ui-driven/src/pay.spec.ts | 4 +- e2e/tests/ui-driven/src/refresh-page.spec.ts | 4 +- .../ui-driven/src/save-and-return.spec.ts | 4 +- e2e/tests/ui-driven/src/sections.spec.ts | 6 +-- 18 files changed, 108 insertions(+), 109 deletions(-) create mode 100644 e2e/tests/ui-driven/src/helpers/serviceData.ts create mode 100644 e2e/tests/ui-driven/src/helpers/types.ts diff --git a/e2e/tests/ui-driven/src/create-flow-with-geospatial.spec.ts b/e2e/tests/ui-driven/src/create-flow-with-geospatial.spec.ts index 991dcd0bc5..e6a1b5f7d0 100644 --- a/e2e/tests/ui-driven/src/create-flow-with-geospatial.spec.ts +++ b/e2e/tests/ui-driven/src/create-flow-with-geospatial.spec.ts @@ -1,5 +1,4 @@ import { expect, test } from "@playwright/test"; -import type { Context } from "./helpers/context"; import { contextDefaults, setUpTestContext, @@ -9,15 +8,14 @@ import { getTeamPage } from "./helpers/getPage"; import { createAuthenticatedSession } from "./helpers/globalHelpers"; import { answerFindProperty, clickContinue } from "./helpers/userActions"; import { PlaywrightEditor } from "./pages/Editor"; +import { publishService, turnServiceOnline } from "./helpers/navigateAndPublish"; +import { TestContext } from "./helpers/types"; +import { serviceProps } from "./helpers/serviceData"; test.describe("Flow creation, publish and preview", () => { - let context: Context = { + let context: TestContext = { ...contextDefaults, }; - const serviceProps = { - name: "A Test Service", - slug: "a-test-service", - }; test.beforeAll(async () => { try { @@ -44,9 +42,6 @@ test.describe("Flow creation, publish and preview", () => { page.on("dialog", (dialog) => dialog.accept(serviceProps.name)); await editor.addNewService(); - // update context to allow flow to be torn down - context.flow = { ...serviceProps }; - await editor.createFindProperty(); await expect(editor.nodeList).toContainText(["Find property"]); await editor.createInternalPortal(); @@ -79,8 +74,7 @@ test.describe("Flow creation, publish and preview", () => { }); // publish flow await page.goto(`/${context.team.slug}/${serviceProps.slug}`); - page.getByRole("button", { name: "CHECK FOR CHANGES TO PUBLISH" }).click(); - page.getByRole("button", { name: "PUBLISH", exact: true }).click(); + publishService(page) let previewLink = page.getByRole("link", { name: "Open published service", @@ -89,13 +83,7 @@ test.describe("Flow creation, publish and preview", () => { await page.goto(`/${context.team.slug}/${serviceProps.slug}`); - // Toggle flow online - page.locator('[aria-label="Service settings"]').click(); - page.getByLabel("Offline").click(); - page.getByRole("button", { name: "Save", disabled: false }).click(); - await expect( - page.getByText("Service settings updated successfully"), - ).toBeVisible(); + turnServiceOnline(page) // Exit back to main Editor page page.locator('[aria-label="Editor"]').click(); diff --git a/e2e/tests/ui-driven/src/create-flow.spec.ts b/e2e/tests/ui-driven/src/create-flow.spec.ts index 2c83eab2c6..afb4860416 100644 --- a/e2e/tests/ui-driven/src/create-flow.spec.ts +++ b/e2e/tests/ui-driven/src/create-flow.spec.ts @@ -1,9 +1,6 @@ import { Browser, expect, test } from "@playwright/test"; -import type { Context } from "./helpers/context"; import { contextDefaults, - externalPortalFlowData, - externalPortalServiceProps, setUpTestContext, tearDownTestContext, } from "./helpers/context"; @@ -23,16 +20,17 @@ import { import { PlaywrightEditor } from "./pages/Editor"; import { createExternalPortal, - createQuestionWithOptions, } from "./helpers/addComponent"; import { navigateToService, publishService, turnServiceOnline, } from "./helpers/navigateAndPublish"; +import { TestContext } from "./helpers/types"; +import { externalPortalFlowData, externalPortalServiceProps } from "./helpers/serviceData"; test.describe("Flow creation, publish and preview", () => { - let context: Context = { + let context: TestContext = { ...contextDefaults, }; const serviceProps = { @@ -131,8 +129,7 @@ test.describe("Flow creation, publish and preview", () => { userId: context.user!.id!, }); - await page.goto(`/${context.team.slug}/${serviceProps.slug}`); - + await navigateToService(page, serviceProps.slug) await publishService(page); const previewLink = page.getByRole("link", { @@ -166,8 +163,7 @@ test.describe("Flow creation, publish and preview", () => { userId: context.user!.id!, }); - await page.goto(`/${context.team.slug}/${serviceProps.slug}`); - + await navigateToService(page, serviceProps.slug) await turnServiceOnline(page); // Exit back to main Editor page @@ -201,22 +197,20 @@ test.describe("Flow creation, publish and preview", () => { // update context to allow new flow to be torn down context.externalPortalFlow = { ...externalPortalServiceProps }; - await createQuestionWithOptions( - page, - editor.firstNode, - externalPortalFlowData.title, - externalPortalFlowData.answers, - ); + const {title, answers} = externalPortalFlowData + + await editor.createQuestionWithOptions(title, answers) + await expect(editor.nodeList).toContainText([ - externalPortalFlowData.title, - externalPortalFlowData.answers[0], - externalPortalFlowData.answers[1], + title, + answers[0], + answers[1], ]); await publishService(page); await turnServiceOnline(page); - navigateToService(page, serviceProps.slug); + await navigateToService(page, serviceProps.slug); await createExternalPortal(page, page.locator("li:nth-child(6)")); diff --git a/e2e/tests/ui-driven/src/helpers/addComponent.ts b/e2e/tests/ui-driven/src/helpers/addComponent.ts index 760f110707..61c0f8ca4e 100644 --- a/e2e/tests/ui-driven/src/helpers/addComponent.ts +++ b/e2e/tests/ui-driven/src/helpers/addComponent.ts @@ -1,6 +1,7 @@ import { ComponentType } from "@opensystemslab/planx-core/types"; import { expect, Locator, Page } from "@playwright/test"; -import { contextDefaults, externalPortalServiceProps } from "./context"; +import { contextDefaults } from "./context"; +import { externalPortalServiceProps } from "./serviceData"; const createBaseComponent = async ( page: Page, diff --git a/e2e/tests/ui-driven/src/helpers/context.ts b/e2e/tests/ui-driven/src/helpers/context.ts index 42c5d6b224..b924ebc4ea 100644 --- a/e2e/tests/ui-driven/src/helpers/context.ts +++ b/e2e/tests/ui-driven/src/helpers/context.ts @@ -4,33 +4,12 @@ import { sign } from "jsonwebtoken"; import assert from "node:assert"; import { log } from "./globalHelpers"; import { $admin } from "../../../api-driven/src/client"; +import { Flow, TestContext } from "./types"; -type NewTeam = Parameters[0]; -export interface Flow { - id?: string; - publishedId?: number; - slug: string; - name: string; - data?: object; -} - -export interface Context { - user: { - id?: number; - firstName: string; - lastName: string; - email: string; - isPlatformAdmin: boolean; - }; - team: { id?: number } & NewTeam; - flow?: Flow; - externalPortalFlow?:Flow; - sessionIds?: string[]; -} - -export const contextDefaults: Context = { +export const contextDefaults: TestContext = { user: { + id: 0, firstName: "Test", lastName: "Test", email: "simulate-delivered@notifications.service.gov.uk", @@ -51,10 +30,10 @@ export const contextDefaults: Context = { }; export async function setUpTestContext( - initialContext: Context, -): Promise { + initialContext: TestContext, +): Promise { const $admin = getCoreDomainClient(); - const context: Context = { ...initialContext }; + const context: TestContext = { ...initialContext }; if (context.user) { context.user.id = await $admin.user.create(context.user); } @@ -96,17 +75,7 @@ export async function setUpTestContext( return context; } -export const externalPortalServiceProps = { - name: "An External Portal Service", - slug: "an-external-portal-service", -}; - -export const externalPortalFlowData = { - title: "Is this an External Portal?", - answers: ["It is an external portal", "No it is not an External Portal"], -}; - -export async function tearDownTestContext(context: Context) { +export async function tearDownTestContext(context: TestContext) { const adminGQLClient = getGraphQLClient(); if (context.flow || context.externalPortalFlow) { await $admin.flow._destroyAll() @@ -268,7 +237,7 @@ async function deleteFlow(adminGQLClient: GraphQLClient, flow: Flow) { } } -async function deleteUser(adminGQLClient: GraphQLClient, context: Context) { +async function deleteUser(adminGQLClient: GraphQLClient, context: TestContext) { if (context.user?.id) { log(`deleting user ${context.user?.id}`); await adminGQLClient.request( @@ -305,7 +274,7 @@ async function deleteUser(adminGQLClient: GraphQLClient, context: Context) { } } -async function deleteTeam(adminGQLClient: GraphQLClient, context: Context) { +async function deleteTeam(adminGQLClient: GraphQLClient, context: TestContext) { if (context.team?.id) { log(`deleting team ${context.team?.id}`); await adminGQLClient.request( @@ -342,7 +311,7 @@ async function deleteTeam(adminGQLClient: GraphQLClient, context: Context) { } } -async function setupGovPaySecret($admin: CoreDomainClient, context: Context) { +async function setupGovPaySecret($admin: CoreDomainClient, context: TestContext) { try { await $admin.client.request( gql` diff --git a/e2e/tests/ui-driven/src/helpers/globalHelpers.ts b/e2e/tests/ui-driven/src/helpers/globalHelpers.ts index a52cee007c..f580449673 100644 --- a/e2e/tests/ui-driven/src/helpers/globalHelpers.ts +++ b/e2e/tests/ui-driven/src/helpers/globalHelpers.ts @@ -1,8 +1,8 @@ import { FlowGraph } from "@opensystemslab/planx-core/types"; import type { Browser, Page, Request } from "@playwright/test"; import { gql } from "graphql-request"; -import type { Context } from "./context"; import { generateAuthenticationToken, getGraphQLClient } from "./context"; +import { TestContext } from "./types"; // Test card numbers to be used in gov.uk sandbox environment // reference: https://docs.payments.service.gov.uk/testing_govuk_pay/#if-you-39-re-using-a-test-39-sandbox-39-account @@ -87,7 +87,7 @@ export async function getSessionId(page: Page): Promise { return sessionId; } -export async function addSessionToContext(page: Page, context: Context) { +export async function addSessionToContext(page: Page, context: TestContext) { const sessionId = await getSessionId(page); context.sessionIds!.push(sessionId); return sessionId; @@ -95,7 +95,7 @@ export async function addSessionToContext(page: Page, context: Context) { export async function waitForPaymentResponse( page: Page, - context: Context, + context: TestContext, ): Promise<{ paymentId: string; state?: { status: string } }> { const { payment_id: paymentId, state } = await page .waitForResponse((response) => { @@ -110,7 +110,7 @@ export async function modifyFlow({ context, modifiedFlow, }: { - context: Context; + context: TestContext; modifiedFlow: FlowGraph; }) { const adminGQLClient = getGraphQLClient(); diff --git a/e2e/tests/ui-driven/src/helpers/navigateAndPublish.ts b/e2e/tests/ui-driven/src/helpers/navigateAndPublish.ts index 1858537eb0..d5d66138fa 100644 --- a/e2e/tests/ui-driven/src/helpers/navigateAndPublish.ts +++ b/e2e/tests/ui-driven/src/helpers/navigateAndPublish.ts @@ -5,8 +5,6 @@ export const navigateToService = async (page: Page, slug: string) => { await page.goto(`/${contextDefaults.team.slug}/${slug}`); await expect(page.getByRole("link", { name: slug })).toBeVisible(); - - return true; }; export const publishService = async (page: Page) => { diff --git a/e2e/tests/ui-driven/src/helpers/serviceData.ts b/e2e/tests/ui-driven/src/helpers/serviceData.ts new file mode 100644 index 0000000000..e20f17f2c1 --- /dev/null +++ b/e2e/tests/ui-driven/src/helpers/serviceData.ts @@ -0,0 +1,15 @@ + +export const serviceProps = { + name: "A Test Service", + slug: "a-test-service", + }; + + export const externalPortalServiceProps = { + name: "An External Portal Service", + slug: "an-external-portal-service", + }; + + export const externalPortalFlowData = { + title: "Is this an External Portal?", + answers: ["It is an external portal", "No it is not an External Portal"], + }; \ No newline at end of file diff --git a/e2e/tests/ui-driven/src/helpers/types.ts b/e2e/tests/ui-driven/src/helpers/types.ts new file mode 100644 index 0000000000..9f94293ff5 --- /dev/null +++ b/e2e/tests/ui-driven/src/helpers/types.ts @@ -0,0 +1,22 @@ +import { CoreDomainClient } from "@opensystemslab/planx-core"; +import { User } from "@opensystemslab/planx-core/dist/types"; + + +type NewTeam = Parameters[0]; + +export interface Flow { + id?: string; + publishedId?: number; + slug: string; + name: string; + data?: object; + } + + +export interface TestContext { + user: Omit; + team: { id?: number } & NewTeam; + flow?: Flow; + externalPortalFlow?:Flow; + sessionIds?: string[]; + } \ No newline at end of file diff --git a/e2e/tests/ui-driven/src/helpers/userActions.ts b/e2e/tests/ui-driven/src/helpers/userActions.ts index 9da223b003..35d24d5ae9 100644 --- a/e2e/tests/ui-driven/src/helpers/userActions.ts +++ b/e2e/tests/ui-driven/src/helpers/userActions.ts @@ -1,17 +1,16 @@ import type { Locator, Page } from "@playwright/test"; import { expect } from "@playwright/test"; import { setupOSMockResponse } from "../mocks/osPlacesResponse"; -import type { Context } from "./context"; import { findSessionId, getGraphQLClient } from "./context"; import { TEST_EMAIL, log, waitForDebugLog } from "./globalHelpers"; -import exp from "node:constants"; +import { TestContext } from "./types"; export async function saveSession({ page, context, }: { page: Page; - context: Context; + context: TestContext; }): Promise { const pageResponsePromise = page.waitForResponse((response) => { return response.url().includes("/send-email/save"); @@ -32,7 +31,7 @@ export async function returnToSession({ shouldContinue = true, }: { page: Page; - context: Context; + context: TestContext; sessionId: string; shouldContinue?: boolean; }) { @@ -82,7 +81,7 @@ export async function fillInEmail({ context, }: { page: Page; - context: Context; + context: TestContext; }) { await page.locator("#email").fill(context.user.email); await page.locator("#confirmEmail").fill(context.user.email); 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 527e788b95..068041b720 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,6 +1,5 @@ import { BrowserContext, Page, expect, test } from "@playwright/test"; import { - Context, contextDefaults, getGraphQLClient, setUpTestContext, @@ -20,8 +19,9 @@ import { navigateToPayComponent, } from "./helpers"; import { mockPaymentRequest, modifiedInviteToPayFlow } from "./mocks"; +import { TestContext } from "../helpers/types"; -let context: Context = { +let context: TestContext = { ...contextDefaults, flow: { 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 4d766f8d35..384bb5eaf0 100644 --- a/e2e/tests/ui-driven/src/invite-to-pay/helpers.ts +++ b/e2e/tests/ui-driven/src/invite-to-pay/helpers.ts @@ -1,7 +1,6 @@ import { PaymentRequest } from "@opensystemslab/planx-core/dist/types"; import { expect, type Page } from "@playwright/test"; import { GraphQLClient, gql } from "graphql-request"; -import { Context } from "../helpers/context"; import { TEST_EMAIL, addSessionToContext, log } from "../helpers/globalHelpers"; import { answerChecklist, @@ -9,11 +8,12 @@ import { answerFindProperty, fillInEmail, } from "../helpers/userActions"; +import { TestContext } from "../helpers/types"; /** * Navigates to pay component whilst completing the minimum requirements for an Invite to Pay flow */ -export async function navigateToPayComponent(page: Page, context: Context) { +export async function navigateToPayComponent(page: Page, context: TestContext) { const previewURL = `/${context.team!.slug!}/${ context.flow?.slug }/published?analytics=false`; @@ -85,7 +85,7 @@ export async function makePaymentRequest({ context, }: { page: Page; - context: Context; + context: TestContext; }) { await navigateToPayComponent(page, context); const sessionId = await addSessionToContext(page, context); 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 15d99d9c0c..f33e499af5 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 @@ -3,7 +3,6 @@ import { APIRequestContext, Page, expect, test } from "@playwright/test"; import { GraphQLClient, gql } from "graphql-request"; import { v4 as uuidV4 } from "uuid"; import { - Context, contextDefaults, getGraphQLClient, setUpTestContext, @@ -14,8 +13,9 @@ import { fillGovUkCardDetails } from "../helpers/userActions"; import inviteToPayFlow from "../mocks/flows/invite-to-pay-flow"; import { getPaymentRequestBySessionId } from "./helpers"; import { mockPaymentRequestDetails, mockSessionData } from "./mocks"; +import { TestContext } from "../helpers/types"; -let context: Context = { +let context: TestContext = { ...contextDefaults, flow: { @@ -148,7 +148,7 @@ async function createSession({ client, sessionId, }: { - context: Context; + context: TestContext; client: GraphQLClient; sessionId: string; }) { diff --git a/e2e/tests/ui-driven/src/login.spec.ts b/e2e/tests/ui-driven/src/login.spec.ts index 23bfbecf83..0f0d73de8d 100644 --- a/e2e/tests/ui-driven/src/login.spec.ts +++ b/e2e/tests/ui-driven/src/login.spec.ts @@ -1,14 +1,14 @@ import { expect, test } from "@playwright/test"; -import type { Context } from "./helpers/context"; import { contextDefaults, setUpTestContext, tearDownTestContext, } from "./helpers/context"; import { createAuthenticatedSession } from "./helpers/globalHelpers"; +import { TestContext } from "./helpers/types"; test.describe("Login", () => { - let context: Context = { + let context: TestContext = { ...contextDefaults, }; diff --git a/e2e/tests/ui-driven/src/pages/Editor.ts b/e2e/tests/ui-driven/src/pages/Editor.ts index 8dd146229f..5e6f5c1ee2 100644 --- a/e2e/tests/ui-driven/src/pages/Editor.ts +++ b/e2e/tests/ui-driven/src/pages/Editor.ts @@ -74,6 +74,19 @@ export class PlaywrightEditor { ).toBeVisible(); } + async createQuestionWithOptions(title:string, answers:string[]) { + await createQuestionWithOptions( + this.page, + this.firstNode, + title, + answers, + ); + await expect( + this.page.locator("a").filter({ hasText: this.answers.questionText }), + ).toBeVisible(); + } + + async createNoticeOnEachBranch() { // Add a notice to the "Yes" path await createNotice( diff --git a/e2e/tests/ui-driven/src/pay.spec.ts b/e2e/tests/ui-driven/src/pay.spec.ts index 7ab35de921..dbb1b950c0 100644 --- a/e2e/tests/ui-driven/src/pay.spec.ts +++ b/e2e/tests/ui-driven/src/pay.spec.ts @@ -2,7 +2,6 @@ import type { SessionData } from "@opensystemslab/planx-core/types"; import type { Page } from "@playwright/test"; import { expect, test } from "@playwright/test"; import { GraphQLClient, gql } from "graphql-request"; -import type { Context } from "./helpers/context"; import { contextDefaults, getGraphQLClient, @@ -17,8 +16,9 @@ import { } from "./helpers/globalHelpers"; import { fillGovUkCardDetails, submitCardDetails } from "./helpers/userActions"; import payFlow from "./mocks/flows/pay-flow.json"; +import { TestContext } from "./helpers/types"; -let context: Context = { +let context: TestContext = { ...contextDefaults, flow: { diff --git a/e2e/tests/ui-driven/src/refresh-page.spec.ts b/e2e/tests/ui-driven/src/refresh-page.spec.ts index 4edd79c190..37481963be 100644 --- a/e2e/tests/ui-driven/src/refresh-page.spec.ts +++ b/e2e/tests/ui-driven/src/refresh-page.spec.ts @@ -1,5 +1,4 @@ import { expect, test } from "@playwright/test"; -import type { Context } from "./helpers/context"; import { contextDefaults, setUpTestContext, @@ -9,9 +8,10 @@ import { createAuthenticatedSession, isGetUserRequest, } from "./helpers/globalHelpers"; +import { TestContext } from "./helpers/types"; test.describe("Refresh page", () => { - let context: Context = { + let context: TestContext = { ...contextDefaults, }; 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 aa7b17c731..1d334d127c 100644 --- a/e2e/tests/ui-driven/src/save-and-return.spec.ts +++ b/e2e/tests/ui-driven/src/save-and-return.spec.ts @@ -1,5 +1,4 @@ import { expect, test } from "@playwright/test"; -import type { Context } from "./helpers/context"; import { contextDefaults, setUpTestContext, @@ -18,9 +17,10 @@ import { modifiedSimpleSendFlow, simpleSendFlow, } from "./mocks/flows/save-and-return-flows"; +import { TestContext } from "./helpers/types"; test.describe("Save and return", () => { - let context: Context = { + let context: TestContext = { ...contextDefaults, flow: { diff --git a/e2e/tests/ui-driven/src/sections.spec.ts b/e2e/tests/ui-driven/src/sections.spec.ts index fe7892aea4..ac95e65f1b 100644 --- a/e2e/tests/ui-driven/src/sections.spec.ts +++ b/e2e/tests/ui-driven/src/sections.spec.ts @@ -1,7 +1,6 @@ import type { FlowGraph } from "@opensystemslab/planx-core/types"; import { expect, test } from "@playwright/test"; import { gql } from "graphql-request"; -import type { Context } from "./helpers/context"; import { contextDefaults, getGraphQLClient, @@ -21,6 +20,7 @@ import { saveSession, } from "./helpers/userActions"; import { flow, updatedQuestionAnswers } from "./mocks/flows/sections-flow"; +import { TestContext } from "./helpers/types"; // TODO: move this type to planx-core // also defined in editor.planx.uk/src/types.ts @@ -34,7 +34,7 @@ export enum SectionStatus { } test.describe("Section statuses", () => { - let context: Context = { + let context: TestContext = { ...contextDefaults, flow: { @@ -533,7 +533,7 @@ async function modifyFlow({ context, flowData, }: { - context: Context; + context: TestContext; flowData: FlowGraph; }) { const adminGQLClient = getGraphQLClient();