Skip to content

Commit

Permalink
Re-add tests that were removed during reskin (#1717)
Browse files Browse the repository at this point in the history
* fix: Re-enable CookieConsent portion of Dialogs.test.js

* test: Restore and repair PoliciesModelledPopup test

* chore: Lint
  • Loading branch information
anth-volk authored May 13, 2024
1 parent c839611 commit 9e3c0bd
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 0 deletions.
55 changes: 55 additions & 0 deletions src/__tests__/modals/CookieConsent.test.js
Original file line number Diff line number Diff line change
@@ -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(<CookieConsent />);

// 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(<CookieConsent />);

// 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;
});
});
84 changes: 84 additions & 0 deletions src/__tests__/modals/PoliciesModelledPopup.test.js
Original file line number Diff line number Diff line change
@@ -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(
<BrowserRouter>
<HouseholdOutput
loading={true}
policy={testProps.policy}
metadata={metadataUS}
setHasShownHouseholdPopup={() => false}
householdInput={sampleHousehold}
/>
</BrowserRouter>,
);

await waitFor(() => {
expect(
getByText(
"PolicyEngine results may not constitute exact tax liabilities or benefit entitlements.",
),
).toBeInTheDocument();
});
});
});

0 comments on commit 9e3c0bd

Please sign in to comment.