From 9e3c0bda747aca24a6896ee760f361ad8a451f9f Mon Sep 17 00:00:00 2001 From: Anthony V Date: Mon, 13 May 2024 18:24:17 +0200 Subject: [PATCH] Re-add tests that were removed during reskin (#1717) * fix: Re-enable CookieConsent portion of Dialogs.test.js * test: Restore and repair PoliciesModelledPopup test * chore: Lint --- src/__tests__/modals/CookieConsent.test.js | 55 ++++++++++++ .../modals/PoliciesModelledPopup.test.js | 84 +++++++++++++++++++ 2 files changed, 139 insertions(+) create mode 100644 src/__tests__/modals/CookieConsent.test.js create mode 100644 src/__tests__/modals/PoliciesModelledPopup.test.js diff --git a/src/__tests__/modals/CookieConsent.test.js b/src/__tests__/modals/CookieConsent.test.js new file mode 100644 index 000000000..f6dabb9da --- /dev/null +++ b/src/__tests__/modals/CookieConsent.test.js @@ -0,0 +1,55 @@ +import "@testing-library/jest-dom"; +import { render, waitFor } from "@testing-library/react"; +import { screen } from "@testing-library/react"; + +import CookieConsent from "modals/CookieConsent"; + +jest.mock("react-plotly.js", () => jest.fn()); + +jest.mock("react-router-dom", () => { + const originalModule = jest.requireActual("react-router-dom"); + return { + __esModule: true, + ...originalModule, + useSearchParams: jest.fn(), + }; +}); + +describe("Test cookie consent pop-up", () => { + test("Pop-up appears without cookie existing", async () => { + // Launch CookieConsent + const { getByText } = render(); + + // Wait for pop-up to appear + await new Promise((r) => setTimeout(r, 1250)); + + // Ensure that component returns + await waitFor(() => { + expect(getByText("Accept")).toBeInTheDocument(); + }); + }); + + test("Pop-up does not appear if cookies have been accepted", async () => { + // Mock cookie that matches desired + Object.defineProperty(window.document, "cookie", { + configurable: true, + writable: true, + value: "consent=granted;max-age=31536000;path=/", + }); + + // Launch CookieConsent + render(); + + // Wait for pop-up to appear + await new Promise((r) => setTimeout(r, 1250)); + + // Ensure that return is null + await waitFor(() => { + const acceptButton = screen.queryByText("Accept"); + expect(acceptButton).toBeNull(); + }); + + // Remove cookie + delete window.document.cookie; + }); +}); diff --git a/src/__tests__/modals/PoliciesModelledPopup.test.js b/src/__tests__/modals/PoliciesModelledPopup.test.js new file mode 100644 index 000000000..592ed9517 --- /dev/null +++ b/src/__tests__/modals/PoliciesModelledPopup.test.js @@ -0,0 +1,84 @@ +import "@testing-library/jest-dom"; +import { render, waitFor } from "@testing-library/react"; +import { BrowserRouter, useSearchParams } from "react-router-dom"; +// External package imports +import fetch from "node-fetch"; + +import HouseholdOutput from "pages/household/output/HouseholdOutput"; +import { createDefaultHousehold } from "../../api/variables"; + +jest.mock("react-plotly.js", () => jest.fn()); + +jest.mock("react-router-dom", () => { + const originalModule = jest.requireActual("react-router-dom"); + return { + __esModule: true, + ...originalModule, + useSearchParams: jest.fn(), + }; +}); + +let metadataUS = null; +beforeAll(async () => { + const res = await fetch("https://api.policyengine.org/us/metadata"); + const metadataRaw = await res.json(); + metadataUS = metadataRaw.result; +}); + +describe("Test PoliciesModelledPopup", () => { + test("Pop-up appears after beginning calculations", async () => { + const testProps = { + policy: { + reform: { + label: "testVal", + }, + baseline: { + label: "testVal", + }, + }, + }; + + let sampleHousehold = createDefaultHousehold(metadataUS); + // The below is necessary to prevent a litany of 'quiet' errors that + // will materialize in the console + sampleHousehold.people.you.state_name = { + 2024: "az", + }; + sampleHousehold.households["your household"].state_name = { + 2024: "az", + }; + + useSearchParams.mockImplementation(() => { + const get = (param) => { + if (param === "focus") { + return "householdOutput.netIncome"; + } else if (param === "reform") { + return "testVal"; + } else if (param === "baseline") { + return "testVal"; + } + }; + return [{ get }]; + }); + + const { getByText } = render( + + false} + householdInput={sampleHousehold} + /> + , + ); + + await waitFor(() => { + expect( + getByText( + "PolicyEngine results may not constitute exact tax liabilities or benefit entitlements.", + ), + ).toBeInTheDocument(); + }); + }); +});