From d8b4f3fcd2c5eb56b29c07fff5449f2d26421e37 Mon Sep 17 00:00:00 2001 From: Nicholas Summers Date: Wed, 25 Sep 2024 14:10:57 -0400 Subject: [PATCH] Add basic tests to SynthesizedValue and SynthesizedTable --- .../fields/SynthesizedTable.test.jsx | 139 ++++++++++++++++++ .../components/fields/SynthesizedValue.jsx | 1 - .../fields/SynthesizedValue.test.jsx | 79 ++++++++++ services/ui-src/src/serviceWorker.js | 2 +- services/ui-src/src/setupTests.js | 2 +- services/ui-src/src/store/storeIndex.js | 2 +- services/ui-src/src/util/constants.js | 3 - services/ui-src/src/util/metaEnv.js | 2 + 8 files changed, 223 insertions(+), 7 deletions(-) create mode 100644 services/ui-src/src/components/fields/SynthesizedTable.test.jsx create mode 100644 services/ui-src/src/components/fields/SynthesizedValue.test.jsx create mode 100644 services/ui-src/src/util/metaEnv.js diff --git a/services/ui-src/src/components/fields/SynthesizedTable.test.jsx b/services/ui-src/src/components/fields/SynthesizedTable.test.jsx new file mode 100644 index 000000000..237c75903 --- /dev/null +++ b/services/ui-src/src/components/fields/SynthesizedTable.test.jsx @@ -0,0 +1,139 @@ +import React from "react"; +import { Provider } from "react-redux"; +import { screen, render } from "@testing-library/react"; +import configureMockStore from "redux-mock-store"; +import { lteMask } from "../../util/constants"; +import SynthesizedTable from "./SynthesizedTable"; + +const mockStore = configureMockStore(); +const store = mockStore({ + formData: [ + { + contents: { + section: { + year: 2023, + state: "AL", + }, + }, + }, + ], + global: { + stateName: "AL", + }, + stateUser: { + abbr: "CMS", + }, + enrollmentCounts: { + chipEnrollments: 0, + }, + lastYearFormData: [], + lastYearTotals: {}, +}); + +const defaultProps = { + tableTitle: "Managed Care Costs", + question: { + questions: [], + fieldset_info: { + rows: [ + [ + { + contents: "Eligible children", + }, + { + actions: ["identity"], + targets: ["$..*[?(@ && @.id=='2023-05-a-03-01-a')].answer.entry"], + }, + { + actions: ["identity"], + targets: ["$..*[?(@ && @.id=='2023-05-a-03-01-b')].answer.entry"], + }, + ], + ], + headers: [ + { + contents: "", + }, + { + contents: "FFY 2023", + }, + { + contents: "FFY 2024", + }, + ], + }, + fieldset_type: "synthesized_table", + type: "fieldset", + }, +}; + +const SynthesizedTableComponentWithProps = (testSpecificProps) => { + return ( + + + + ); +}; + +describe("", () => { + test("should render header and labels", () => { + render(SynthesizedTableComponentWithProps()); + + expect(screen.getByText("FFY 2023")).toBeInTheDocument(); + expect(screen.getByText("FFY 2024")).toBeInTheDocument(); + expect(screen.getByText("Eligible children")).toBeInTheDocument(); + }); + + test("should not render in print view with lessThanEleven mask prop", () => { + render( + SynthesizedTableComponentWithProps({ + question: { + ...defaultProps.question, + fieldset_info: { + rows: [ + [ + { + contents: "Eligible children", + mask: lteMask, + }, + { + mask: lteMask, + actions: ["identity"], + targets: [ + "$..*[?(@ && @.id=='2023-05-a-03-01-a')].answer.entry", + ], + }, + { + mask: lteMask, + actions: ["identity"], + targets: [ + "$..*[?(@ && @.id=='2023-05-a-03-01-b')].answer.entry", + ], + }, + ], + ], + headers: [ + { + mask: lteMask, + contents: "", + }, + { + mask: lteMask, + contents: "FFY 2023", + }, + { + mask: lteMask, + contents: "FFY 2024", + }, + ], + }, + }, + printView: true, + }) + ); + + expect(screen.queryByText("FFY 2023")).not.toBeInTheDocument(); + expect(screen.queryByText("FFY 2024")).not.toBeInTheDocument(); + expect(screen.queryByText("Eligible children")).not.toBeInTheDocument(); + }); +}); diff --git a/services/ui-src/src/components/fields/SynthesizedValue.jsx b/services/ui-src/src/components/fields/SynthesizedValue.jsx index df824cf36..b14035ad8 100644 --- a/services/ui-src/src/components/fields/SynthesizedValue.jsx +++ b/services/ui-src/src/components/fields/SynthesizedValue.jsx @@ -22,7 +22,6 @@ const SynthesizedValue = ({ question, printView, ...props }) => { ); const showValue = !(printView && question.fieldset_info.mask === lteMask); - const renderValue = () => { return synthesizeValue( question.fieldset_info, diff --git a/services/ui-src/src/components/fields/SynthesizedValue.test.jsx b/services/ui-src/src/components/fields/SynthesizedValue.test.jsx new file mode 100644 index 000000000..f9bda3071 --- /dev/null +++ b/services/ui-src/src/components/fields/SynthesizedValue.test.jsx @@ -0,0 +1,79 @@ +import React from "react"; +import { Provider } from "react-redux"; +import { screen, render } from "@testing-library/react"; +import configureMockStore from "redux-mock-store"; +import SynthesizedValue from "./SynthesizedValue"; +import { lteMask } from "../../util/constants"; + +const mockStore = configureMockStore(); +const store = mockStore({ + formData: [ + { + contents: { + section: { + year: 2023, + state: "AL", + }, + }, + }, + ], + global: { + stateName: "AL", + }, + stateUser: { + abbr: "CMS", + }, + enrollmentCounts: { + chipEnrollments: 0, + }, + lastYearFormData: [], + lastYearTotals: {}, +}); + +const defaultProps = { + question: { + questions: [], + fieldset_info: { + actions: ["percentage"], + targets: [ + "$..*[?(@ && @.id=='2023-03-i-02-01-01-05')].answer.entry", + "$..*[?(@ && @.id=='2023-03-i-02-01-01-04')].answer.entry", + ], + }, + fieldset_type: "synthesized_value", + type: "fieldset", + }, +}; + +const SynthesizedValueComponentWithProps = (testSpecificProps) => { + return ( + + + + ); +}; + +describe("", () => { + test("should render header and labels", () => { + render(SynthesizedValueComponentWithProps()); + + expect(screen.getByText("Computed:")).toBeInTheDocument(); + }); + + test("should not render in print view with lessThanEleven mask prop", () => { + render( + SynthesizedValueComponentWithProps({ + question: { + ...defaultProps.question, + fieldset_info: { + ...defaultProps.fieldset_info, + mask: lteMask, + }, + }, + printView: true, + }) + ); + + expect(screen.queryByText("Computed:")).not.toBeInTheDocument(); + }); +}); diff --git a/services/ui-src/src/serviceWorker.js b/services/ui-src/src/serviceWorker.js index 03d9eb4e4..5e7236aaa 100644 --- a/services/ui-src/src/serviceWorker.js +++ b/services/ui-src/src/serviceWorker.js @@ -3,7 +3,7 @@ * register() is not called by default. */ -import { MODE, BASE_URL } from "./util/constants"; +import { MODE, BASE_URL } from "./util/metaEnv"; /* * This lets the app load faster on subsequent visits in production, and gives diff --git a/services/ui-src/src/setupTests.js b/services/ui-src/src/setupTests.js index 4c18d3b21..385ebbbb3 100644 --- a/services/ui-src/src/setupTests.js +++ b/services/ui-src/src/setupTests.js @@ -34,7 +34,7 @@ Object.defineProperty(window, "matchMedia", { Object.defineProperty(window, "crypto", { value: require("node:crypto") }); -jest.mock("./util/constants", () => ({ +jest.mock("./util/metaEnv", () => ({ MODE: "production", BASE_URL: "mdctcartsdev.cms.gov", })); diff --git a/services/ui-src/src/store/storeIndex.js b/services/ui-src/src/store/storeIndex.js index 0f2a66ce8..a5b2b530a 100644 --- a/services/ui-src/src/store/storeIndex.js +++ b/services/ui-src/src/store/storeIndex.js @@ -13,7 +13,7 @@ import fiscalYearTemplate from "./fiscalYearTemplate"; import reportStatus from "./reportStatus"; import enrollmentCounts from "./enrollmentCounts"; import { initAuthManager } from "../hooks/authHooks"; -import { MODE } from "../util/constants"; +import { MODE } from "../util/metaEnv"; import { logger } from "redux-logger"; // Consolidate reducers export const reducer = combineReducers({ diff --git a/services/ui-src/src/util/constants.js b/services/ui-src/src/util/constants.js index 6c4dcbebd..1cd267992 100644 --- a/services/ui-src/src/util/constants.js +++ b/services/ui-src/src/util/constants.js @@ -1,4 +1 @@ -// import.meta.env is threaded through here in order to mock it out for jest -export const { MODE, BASE_URL } = import.meta.env; - export const lteMask = "lessThanEleven"; diff --git a/services/ui-src/src/util/metaEnv.js b/services/ui-src/src/util/metaEnv.js new file mode 100644 index 000000000..c34bb3f1a --- /dev/null +++ b/services/ui-src/src/util/metaEnv.js @@ -0,0 +1,2 @@ +// import.meta.env is threaded through here in order to mock it out for jest +export const { MODE, BASE_URL } = import.meta.env;