Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for CMSHomepage #139509

Merged
merged 1 commit into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion services/ui-src/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
"@okta/okta-react": "^4.1.0",
"@reach/accordion": "^0.10.5",
"@redux-devtools/extension": "^3.2.2",
"@testing-library/dom": "^9.3.3",
"@testing-library/jest-dom": "^4.2.4",
"@testing-library/react": "^9.5.0",
"@testing-library/user-event": "^7.2.1",
"@testing-library/user-event": "^14.5.1",
benmartin-coforma marked this conversation as resolved.
Show resolved Hide resolved
"aws-amplify": "^4.3.43",
"axios": "^0.21.1",
"font-awesome": "^4.7.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ const CMSHomepage = ({
let stateStatuses;

if (
filteredStatuses.length > 0 &&
filteredStatuses[0].stateCode === "status"
filteredStatuses?.length > 0 &&
filteredStatuses?.[0]?.stateCode === "status"
) {
stateStatuses = allStateStatuses;
} else {
Expand Down Expand Up @@ -170,15 +170,15 @@ const CMSHomepage = ({
<Button
type="button"
data-cy="cms-homepage-filter-submit"
class="ds-c-button ds-c-button--primary filter-button"
className="ds-c-button ds-c-button--primary filter-button"
onClick={() => filterReports()}
>
Filter
</Button>
<Button
type="button"
data-cy="cms-homepage-filter-clear"
class="ds-c-button ds-c-button--primary filter-button"
className="ds-c-button ds-c-button--primary filter-button"
onClick={() => clearFilter()}
>
Clear
Expand All @@ -198,7 +198,7 @@ const CMSHomepage = ({
</div>
<div data-cy="cms-homepage-reports" className="report-status">
{stateStatuses
.sort((a, b) => (a.lastChanged > b.lastChanged ? -1 : 1))
?.sort((a, b) => (a.lastChanged > b.lastChanged ? -1 : 1))
.map(
({
state,
Expand Down Expand Up @@ -241,10 +241,10 @@ const CMSHomepage = ({
CMSHomepage.propTypes = {
getStatuses: PropTypes.func.isRequired,
allStateStatuses: PropTypes.object.isRequired,
currentYear: PropTypes.object.isRequired,
currentYear: PropTypes.number.isRequired,
currentUserRole: PropTypes.string.isRequired,
stateList: PropTypes.object.isRequired,
yearList: PropTypes.object.isRequired,
stateList: PropTypes.array.isRequired,
yearList: PropTypes.array.isRequired,
benmartin-coforma marked this conversation as resolved.
Show resolved Hide resolved
reportState: PropTypes.object.isRequired,
};

Expand Down
115 changes: 115 additions & 0 deletions services/ui-src/src/components/sections/homepage/CMSHomepage.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import React from "react";
import { render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import configureMockStore from "redux-mock-store";
import { Provider } from "react-redux";
import thunk from "redux-thunk";
import { MemoryRouter } from "react-router";
// components
import CMSHomepage from "./CMSHomepage";
// mocks
import { helpdeskUserWithMultipleReports } from "../../../store/fakeStoreExamples";

const mockStore = configureMockStore([thunk]);
const adminUser = mockStore(helpdeskUserWithMultipleReports);

jest.mock("../../../actions/initial", () => ({
getAllStateStatuses: jest.fn().mockReturnValue(() => async () => {}),
}));

const CmsHomepageComponent = (
<Provider store={adminUser}>
<MemoryRouter path={[]}>
<CMSHomepage></CMSHomepage>
</MemoryRouter>
</Provider>
);

describe("test CMSHomepage", () => {
it("should render", () => {
render(CmsHomepageComponent);
expect(screen.queryAllByText("Alabama").length).toBeGreaterThan(0);
expect(screen.queryAllByText("Alaska").length).toBeGreaterThan(0);
expect(
screen.queryAllByText("Certified and Submitted").length
).toBeGreaterThan(0);
});

it("can be filtered by state", async () => {
const user = userEvent.setup();
render(CmsHomepageComponent);
expect(screen.queryAllByText("Alabama").length).toBeGreaterThan(0);
expect(screen.queryAllByText("Alaska").length).toBeGreaterThan(0);
expect(screen.queryByText("California")).not.toBeInTheDocument();

const stateFilterDropdown = screen.getByText("State", {
selector: "span",
});
await user.click(stateFilterDropdown);
const alabamaDropdownOption = screen.queryByText("Alabama", {
selector: "span",
});
await user.click(alabamaDropdownOption);
await user.click(screen.getByText("Filter", { selector: "button" }));

expect(screen.queryAllByText("Alabama").length).toBeGreaterThan(0);
expect(screen.queryByText("Alaska")).not.toBeInTheDocument();
});

it("can be filtered by year", async () => {
const user = userEvent.setup();
render(CmsHomepageComponent);
expect(screen.queryAllByText("2021").length).toBeGreaterThan(0);
expect(screen.queryAllByText("2020").length).toBeGreaterThan(0);

const yearFilterDropdown = screen.getByText("Year", {
selector: "span",
});
await user.click(yearFilterDropdown);
const year2021DropdownOption = screen.queryByText("2021", {
selector: "span",
});
await user.click(year2021DropdownOption);
await user.click(screen.getByText("Filter", { selector: "button" }));

expect(screen.queryAllByText("2021").length).toBeGreaterThan(0);
expect(screen.queryByText("2020")).not.toBeInTheDocument();
});

it("can be filtered by status", async () => {
const user = userEvent.setup();
render(CmsHomepageComponent);
expect(screen.queryByText("Not Started")).not.toBeInTheDocument();
expect(screen.queryAllByText("In Progress").length).toBeGreaterThan(0);

const yearFilterDropdown = screen.getByText("Status", {
selector: "span",
});
await user.click(yearFilterDropdown);
await user.click(screen.queryByText("Not Started"));
await user.click(screen.getByText("Filter", { selector: "button" }));

expect(screen.queryByText("In Progress")).not.toBeInTheDocument();
});

it("can clear filters", async () => {
const user = userEvent.setup();
render(CmsHomepageComponent);
expect(screen.queryByText("Not Started")).not.toBeInTheDocument();
expect(screen.queryAllByText("In Progress").length).toBeGreaterThan(0);

const yearFilterDropdown = screen.getByText("Status", {
selector: "span",
});
await user.click(yearFilterDropdown);
await user.click(screen.queryByText("Not Started"));
await user.click(screen.getByText("Filter", { selector: "button" }));

expect(screen.queryByText("In Progress")).not.toBeInTheDocument();

await user.click(screen.getByText("Clear", { selector: "button" }));

expect(screen.queryByText("Not Started")).not.toBeInTheDocument();
expect(screen.queryAllByText("In Progress").length).toBeGreaterThan(0);
});
});
Loading