From 6e12614847b5b796611b0b67970b7e8c73818f9a Mon Sep 17 00:00:00 2001 From: Mike Pierce Date: Fri, 14 Jun 2024 16:24:08 +0200 Subject: [PATCH] Add tests for assessment page & PDF download --- ...ergebnis.einschaetzung.$fileName[.pdf].tsx | 16 +- packages/dito/tests/e2e/preCheck.spec.ts | 159 ++++++++++++++++++ 2 files changed, 167 insertions(+), 8 deletions(-) diff --git a/packages/dito/app/routes/vorpruefung_.ergebnis.einschaetzung.$fileName[.pdf].tsx b/packages/dito/app/routes/vorpruefung_.ergebnis.einschaetzung.$fileName[.pdf].tsx index 166a6e1f..a8aa51fa 100644 --- a/packages/dito/app/routes/vorpruefung_.ergebnis.einschaetzung.$fileName[.pdf].tsx +++ b/packages/dito/app/routes/vorpruefung_.ergebnis.einschaetzung.$fileName[.pdf].tsx @@ -3,14 +3,14 @@ import * as fs from "node:fs"; import * as path from "node:path"; import { PDFDocument, PDFForm, PDFTextField } from "pdf-lib"; -const FIELD_NAME_POLICY_TITLE: string = "Titel des Regelungsvorhabens"; -const FIELD_NAME_PRE_CHECK_POSITIVE_1: string = "Vorprüfung positiv - 1"; -const FIELD_NAME_PRE_CHECK_POSITIVE_2: string = "Vorprüfung positiv - 2"; -const FIELD_NAME_PRE_CHECK_POSITIVE_3: string = "Vorprüfung positiv - 3"; -const FIELD_NAME_PRE_CHECK_POSITIVE_4: string = "Vorprüfung positiv - 4"; -const FIELD_NAME_PRE_CHECK_POSITIVE_5: string = "Vorprüfung positiv - 5"; -const FIELD_NAME_PRE_CHECK_NEGATIVE: string = "Vorprüfung negativ"; -const FIELD_NAME_PRE_CHECK_NEGATIVE_ASSESMENT: string = +export const FIELD_NAME_POLICY_TITLE: string = "Titel des Regelungsvorhabens"; +export const FIELD_NAME_PRE_CHECK_POSITIVE_1: string = "Vorprüfung positiv - 1"; +export const FIELD_NAME_PRE_CHECK_POSITIVE_2: string = "Vorprüfung positiv - 2"; +export const FIELD_NAME_PRE_CHECK_POSITIVE_3: string = "Vorprüfung positiv - 3"; +export const FIELD_NAME_PRE_CHECK_POSITIVE_4: string = "Vorprüfung positiv - 4"; +export const FIELD_NAME_PRE_CHECK_POSITIVE_5: string = "Vorprüfung positiv - 5"; +export const FIELD_NAME_PRE_CHECK_NEGATIVE: string = "Vorprüfung negativ"; +export const FIELD_NAME_PRE_CHECK_NEGATIVE_ASSESMENT: string = "Vorprüfung negativ - Erläuterung"; interface PreCheckAnswer { diff --git a/packages/dito/tests/e2e/preCheck.spec.ts b/packages/dito/tests/e2e/preCheck.spec.ts index 10170c3b..237d030e 100644 --- a/packages/dito/tests/e2e/preCheck.spec.ts +++ b/packages/dito/tests/e2e/preCheck.spec.ts @@ -1,10 +1,23 @@ import { expect, test } from "@playwright/test"; +import * as fs from "node:fs"; +import * as path from "node:path"; +import { PDFDocument } from "pdf-lib"; import { preCheck } from "resources/content"; import { + PATH_ASSESSMENT, PATH_DOCUMENTATION, PATH_PRECHECK, PATH_RESULT, } from "resources/staticRoutes"; +import { + FIELD_NAME_POLICY_TITLE, + FIELD_NAME_PRE_CHECK_NEGATIVE, + FIELD_NAME_PRE_CHECK_POSITIVE_1, + FIELD_NAME_PRE_CHECK_POSITIVE_2, + FIELD_NAME_PRE_CHECK_POSITIVE_3, + FIELD_NAME_PRE_CHECK_POSITIVE_4, + FIELD_NAME_PRE_CHECK_POSITIVE_5, +} from "routes/vorpruefung_.ergebnis.einschaetzung.$fileName[.pdf]"; const { questions } = preCheck; @@ -305,6 +318,152 @@ test.describe("test result page", () => { await expect(page.getByRole("main")).not.toContainText("IT-System"); }); + test("assessment page is available", async ({ page }) => { + await page.goto(PATH_PRECHECK); + await page.getByRole("link", { name: "Digitalbezug einschätzen" }).click(); + for (let i = 0; i < 5; i++) { + await page.getByLabel("Ja").click(); + await page.getByRole("button", { name: "Übernehmen" }).click(); + } + await page + .getByRole("link", { name: "Einschätzung als PDF bekommen" }) + .click(); + await expect(page).toHaveURL(PATH_ASSESSMENT); + }); + + test("accepts user input on assessment page", async ({ page }) => { + await page.goto(PATH_PRECHECK); + await page.getByRole("link", { name: "Digitalbezug einschätzen" }).click(); + for (let i = 0; i < 5; i++) { + await page.getByLabel("Ja").click(); + await page.getByRole("button", { name: "Übernehmen" }).click(); + } + await page + .getByRole("link", { name: "Einschätzung als PDF bekommen" }) + .click(); + await page.getByLabel("Arbeitstitel des Vorhabens").fill("Policy #123"); + await expect(page.getByLabel("Arbeitstitel des Vorhabens")).toHaveValue( + "Policy #123", + ); + await page.getByRole("button", { name: "Als PDF herunterladen" }).click(); + }); + + test("generates and downloads PDF with user input", async ({ page }) => { + await page.goto(PATH_PRECHECK); + await page.getByRole("link", { name: "Digitalbezug einschätzen" }).click(); + for (let i = 0; i < 5; i++) { + await page.getByLabel("Ja").click(); + await page.getByRole("button", { name: "Übernehmen" }).click(); + } + await page + .getByRole("link", { name: "Einschätzung als PDF bekommen" }) + .click(); + await page.getByLabel("Arbeitstitel des Vorhabens").fill("Policy #123"); + const downloadPromise = page.waitForEvent("download"); + await page.getByRole("button", { name: "Als PDF herunterladen" }).click(); + const download = await downloadPromise; + expect(download.url()).toContain( + PATH_ASSESSMENT + + "/digitalcheck-vorpruefung.pdf?title=Policy+%23123&it-system=yes&verpflichtungen-fuer-beteiligte=yes&datenaustausch=yes&kommunikation=yes&automatisierung=yes&download", + ); + const filePath = path.resolve(await download.path()); + const fileData = fs.readFileSync(filePath); + const pdfDoc = await PDFDocument.load(fileData); + const form = pdfDoc.getForm(); + + const titleField = form.getTextField(FIELD_NAME_POLICY_TITLE).getText(); + expect(titleField).toBe("Policy #123"); + + const positive_1 = form + .getCheckBox(FIELD_NAME_PRE_CHECK_POSITIVE_1) + .isChecked(); + expect(positive_1).toBe(true); + + const positive_2 = form + .getCheckBox(FIELD_NAME_PRE_CHECK_POSITIVE_2) + .isChecked(); + expect(positive_2).toBe(true); + + const positive_3 = form + .getCheckBox(FIELD_NAME_PRE_CHECK_POSITIVE_3) + .isChecked(); + expect(positive_3).toBe(true); + + const positive_4 = form + .getCheckBox(FIELD_NAME_PRE_CHECK_POSITIVE_4) + .isChecked(); + expect(positive_4).toBe(true); + + const positive_5 = form + .getCheckBox(FIELD_NAME_PRE_CHECK_POSITIVE_5) + .isChecked(); + expect(positive_5).toBe(true); + + const negative = form + .getCheckBox(FIELD_NAME_PRE_CHECK_NEGATIVE) + .isChecked(); + expect(negative).toBe(false); + }); + + test("generates correct PDF in negative case", async ({ page }) => { + await page.goto(PATH_PRECHECK); + await page.getByRole("link", { name: "Digitalbezug einschätzen" }).click(); + for (let i = 0; i < 5; i++) { + await page.getByLabel("Nein").click(); + await page.getByRole("button", { name: "Übernehmen" }).click(); + } + await page + .getByRole("link", { name: "Einschätzung als PDF bekommen" }) + .click(); + await page.getByLabel("Arbeitstitel des Vorhabens").fill("Policy #987"); + const downloadPromise = page.waitForEvent("download"); + await page.getByRole("button", { name: "Als PDF herunterladen" }).click(); + const download = await downloadPromise; + expect(download.url()).toContain( + PATH_ASSESSMENT + + "/digitalcheck-vorpruefung.pdf?title=Policy+%23987&it-system=no&verpflichtungen-fuer-beteiligte=no&datenaustausch=no&kommunikation=no&automatisierung=no&download", + ); + const filePath = path.resolve(await download.path()); + const fileData = fs.readFileSync(filePath); + const pdfDoc = await PDFDocument.load(fileData); + const form = pdfDoc.getForm(); + + const titleField = form.getTextField(FIELD_NAME_POLICY_TITLE).getText(); + expect(titleField).toBe("Policy #987"); + + const positive_1 = form + .getCheckBox(FIELD_NAME_PRE_CHECK_POSITIVE_1) + .isChecked(); + expect(positive_1).toBe(false); + + const positive_2 = form + .getCheckBox(FIELD_NAME_PRE_CHECK_POSITIVE_2) + .isChecked(); + expect(positive_2).toBe(false); + + const positive_3 = form + .getCheckBox(FIELD_NAME_PRE_CHECK_POSITIVE_3) + .isChecked(); + expect(positive_3).toBe(false); + + const positive_4 = form + .getCheckBox(FIELD_NAME_PRE_CHECK_POSITIVE_4) + .isChecked(); + expect(positive_4).toBe(false); + + const positive_5 = form + .getCheckBox(FIELD_NAME_PRE_CHECK_POSITIVE_5) + .isChecked(); + expect(positive_5).toBe(false); + + const negative = form + .getCheckBox(FIELD_NAME_PRE_CHECK_NEGATIVE) + .isChecked(); + expect(negative).toBe(true); + }); + // test("generates PDF with user input", async ({ page }) => { + // test("generates correct PDF in negative case", async ({ page }) => { + test("result page links to documentation", async ({ page }) => { await page.goto(PATH_PRECHECK); await page.getByRole("link", { name: "Digitalbezug einschätzen" }).click();