Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
britt-mo committed Sep 18, 2024
1 parent d1c8af0 commit 79486c6
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 9 deletions.
2 changes: 1 addition & 1 deletion services/ui-src/src/components/layout/FormActions.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ const FormActions = () => {
// Get section IDs and subsection IDs for printing single section
let searchParams = "";
let sectionId = "";

if (currentUser.role === AppRoles.CMS_ADMIN) {
const stateId = window.location.href.split("/")[5];

searchParams = document.location.pathname
.toString()
.replace(`views/sections/${stateId}/`, "")
Expand Down
58 changes: 50 additions & 8 deletions services/ui-src/src/components/layout/FormActions.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,54 +9,96 @@ 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 = (
<Provider store={store}>
<MemoryRouter initialEntries={["/"]}>
<MemoryRouter initialEntries={[firstLocation]}>
<FormActions />
</MemoryRouter>
</Provider>
);
const adminStore = mockStore(adminUserWithReportInProgress);
const adminFormActions = (
<Provider store={adminStore}>
<MemoryRouter initialEntries={["/"]}>
<MemoryRouter initialEntries={[adminFirstLocation]}>
<FormActions />
</MemoryRouter>
</Provider>
);

describe("Fill Form Component", () => {
afterEach(() => {
jest.clearAllMocks();
});

test("should render correctly", () => {
expect(shallow(formActions).exists()).toBe(true);
});
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&sectionId=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&sectionId=2021-00&subsectionId=2021-00-a"
);
expect(printFormButton).toHaveAttribute(
"href",
"/print?year=2021&state=AL"
);
});
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&sectionId=2021-03&subsectionId=2021-03-a"
);
expect(printFormButton).toHaveAttribute(
"href",
"/print?year=2021&state=AL"
);
});
test("should display print section or page on click", () => {
render(formActions);
const printShowButton = screen.getByTestId("print-show");
fireEvent.click(printShowButton);
const printFormButton = screen.getByTestId("print-form");
const printPageButton = screen.getByTestId("print-page");

expect(printPageButton).toHaveTextContent("This Section");
expect(printFormButton).toHaveTextContent("Entire Form");
});
Expand Down

0 comments on commit 79486c6

Please sign in to comment.