diff --git a/services/ui-src/src/components/fields/Integer.jsx b/services/ui-src/src/components/fields/Integer.jsx index 14d2c9c68..ecd344142 100644 --- a/services/ui-src/src/components/fields/Integer.jsx +++ b/services/ui-src/src/components/fields/Integer.jsx @@ -31,17 +31,17 @@ const getPrevYearValue = (question, lastYearFormData) => { // Get questions from last years JSON const questions = - lastYearFormData[3].contents.section.subsections[2].parts[partIndex] + lastYearFormData?.[3]?.contents.section.subsections[2].parts[partIndex] .questions; // Filter down to this question - const matchingQuestion = questions.filter( + const matchingQuestion = questions?.filter( (question) => fieldsetId === question?.fieldset_info?.id ); // The first will always be correct - if (matchingQuestion[0]) { - prevYearValue = parseInt(matchingQuestion[0].questions[0].answer?.entry); + if (matchingQuestion?.[0]) { + prevYearValue = matchingQuestion[0].questions[0].answer?.entry; } } return prevYearValue; diff --git a/services/ui-src/src/components/layout/FormActions.jsx b/services/ui-src/src/components/layout/FormActions.jsx index 8013054bd..f63403fd0 100644 --- a/services/ui-src/src/components/layout/FormActions.jsx +++ b/services/ui-src/src/components/layout/FormActions.jsx @@ -23,14 +23,27 @@ const FormActions = () => { const printDialogeRef = useRef(null); // Get section IDs and subsection IDs for printing single section - let searchParams = document.location.pathname - .toString() - .replace("/sections/", "") - .replace(formYear + "/", ""); + let searchParams = ""; + let sectionId = ""; - const sectionId = formYear + "-" + searchParams.substring(0, 2); - let subsectionId = sectionId + "-"; + if (currentUser.role === AppRoles.CMS_ADMIN) { + const stateId = window.location.href.split("/")[5]; + searchParams = document.location.pathname + .toString() + .replace(`views/sections/${stateId}/`, "") + .replace(formYear + "/", ""); + + sectionId = formYear + "-" + searchParams.substring(1, 3); + } else { + searchParams = document.location.pathname + .toString() + .replace("/sections/", "") + .replace(formYear + "/", ""); + sectionId = formYear + "-" + searchParams.substring(0, 2); + } + + let subsectionId = sectionId + "-"; if (sectionId.slice(-2) === "03") { subsectionId += searchParams.slice(-1); } else { diff --git a/services/ui-src/src/components/layout/FormActions.test.jsx b/services/ui-src/src/components/layout/FormActions.test.jsx index 77967b362..fdf31f5db 100644 --- a/services/ui-src/src/components/layout/FormActions.test.jsx +++ b/services/ui-src/src/components/layout/FormActions.test.jsx @@ -9,12 +9,14 @@ import { stateUserWithReportInProgress, } from "../../store/fakeStoreExamples"; import { MemoryRouter } from "react-router"; +const firstLocation = "/sections/2021/00"; +const adminFirstLocation = "/views/sections/AL/2021/00"; const mockStore = configureMockStore(); const store = mockStore(stateUserWithReportInProgress); const formActions = ( - + @@ -22,36 +24,76 @@ const formActions = ( const adminStore = mockStore(adminUserWithReportInProgress); const adminFormActions = ( - + ); + describe("Fill Form Component", () => { - it("should render correctly", () => { + afterEach(() => { + jest.clearAllMocks(); + }); + + test("should render correctly", () => { expect(shallow(formActions).exists()).toBe(true); }); - it("should add hrefs given a section and subsection", () => { + test("should add hrefs given a section and subsection", () => { render(formActions); + window.history.pushState({}, "Title", "/sections/00"); + const printShowButton = screen.getByTestId("print-show"); + fireEvent.click(printShowButton); + const printFormButton = screen.getByTestId("print-form"); + const printPageButton = screen.getByTestId("print-page"); + expect(printPageButton).toHaveAttribute( + "href", + "/print?year=2021&state=AL§ionId=2021-00&subsectionId=2021-00-a" + ); + expect(printFormButton).toHaveAttribute( + "href", + "/print?year=2021&state=AL" + ); + }); + + test("should add hrefs given a section and subsection for admin user", () => { + const setLocation = (path = "/") => { + delete window.location; + window.location = new URL("https://www.example.com" + path); + }; + + setLocation(adminFirstLocation); + render(adminFormActions); + window.history.pushState({}, "Title", "/00/a"); const printShowButton = screen.getByTestId("print-show"); fireEvent.click(printShowButton); const printFormButton = screen.getByTestId("print-form"); const printPageButton = screen.getByTestId("print-page"); - expect(printPageButton).toHaveAttribute("href"); - expect(printFormButton).toHaveAttribute("href"); + expect(printPageButton).toHaveAttribute( + "href", + "/print?year=2021&state=AL§ionId=2021-00&subsectionId=2021-00-a" + ); + expect(printFormButton).toHaveAttribute( + "href", + "/print?year=2021&state=AL" + ); }); - it("should build the component when looking at section 3 subsections", () => { - window.history.pushState({}, "Title", "/sections/03"); + test("should build the component when looking at section 3 subsections", () => { render(adminFormActions); - window.history.pushState({}, "Title", "/sections/03"); + window.history.pushState({}, "Title", "/03/a"); const printShowButton = screen.getByTestId("print-show"); fireEvent.click(printShowButton); const printFormButton = screen.getByTestId("print-form"); const printPageButton = screen.getByTestId("print-page"); - expect(printPageButton).toHaveAttribute("href"); - expect(printFormButton).toHaveAttribute("href"); + expect(printPageButton).toHaveAttribute( + "href", + "/print?year=2021&state=AL§ionId=2021-03&subsectionId=2021-03-a" + ); + expect(printFormButton).toHaveAttribute( + "href", + "/print?year=2021&state=AL" + ); }); - it("should display print section or page on click", () => { + test("should display print section or page on click", () => { render(formActions); const printShowButton = screen.getByTestId("print-show"); fireEvent.click(printShowButton); @@ -60,7 +102,7 @@ describe("Fill Form Component", () => { expect(printPageButton).toHaveTextContent("This Section"); expect(printFormButton).toHaveTextContent("Entire Form"); }); - it("should clear on click", () => { + test("should clear on click", () => { render(formActions); const printShowButton = screen.getByTestId("print-show"); fireEvent.click(printShowButton); @@ -72,7 +114,7 @@ describe("Fill Form Component", () => { expect(printPageButton).toBeNull(); expect(printFormButton).toBeNull(); }); - it("should not clear on internal click, then clear on outside click", () => { + test("should not clear on internal click, then clear on outside click", () => { const map = {}; document.addEventListener = jest.fn((event, cb) => {