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

CRDCDH-1738 Data Submission List - Unlock Organization dropdown #516

Merged
merged 18 commits into from
Nov 5, 2024
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
142 changes: 112 additions & 30 deletions src/components/DataSubmissions/DataSubmissionListFilters.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
ContextState as AuthContextState,
Status as AuthContextStatus,
} from "../Contexts/AuthContext";
import { useOrganizationListContext } from "../Contexts/OrganizationListContext";
import { Column } from "../GenericTable";
import { ListSubmissionsResp } from "../../graphql";

Expand Down Expand Up @@ -82,28 +81,15 @@ describe("DataSubmissionListFilters Component", () => {

const submitterNames = ["Submitter1", "Submitter2"];
const dataCommons = ["DataCommon1", "DataCommon2"];
const organizations: Organization[] = [
{ _id: "Org1", name: "Organization 1" } as Organization,
{ _id: "Org2", name: "Organization 2" } as Organization,
];
const columnVisibilityModel = { name: true, status: true };

const mockOnChange = jest.fn();
const mockOnColumnVisibilityModelChange = jest.fn();

beforeEach(() => {
// Set up default mock for useOrganizationListContext
(useOrganizationListContext as jest.Mock).mockReturnValue({
activeOrganizations: [
{ _id: "Org1", name: "Organization 1" },
{ _id: "Org2", name: "Organization 2" },
],
status: "LOADED",
data: {
activeOrganizations: [
{ _id: "Org1", name: "Organization 1" },
{ _id: "Org2", name: "Organization 2" },
],
},
});
});

afterEach(() => {
jest.clearAllMocks();
jest.useRealTimers();
Expand All @@ -114,6 +100,7 @@ describe("DataSubmissionListFilters Component", () => {
<TestParent>
<DataSubmissionListFilters
columns={columns}
organizations={organizations}
submitterNames={submitterNames}
dataCommons={dataCommons}
columnVisibilityModel={columnVisibilityModel}
Expand All @@ -133,6 +120,7 @@ describe("DataSubmissionListFilters Component", () => {
<TestParent userRole="Admin">
<DataSubmissionListFilters
columns={columns}
organizations={organizations}
submitterNames={submitterNames}
dataCommons={dataCommons}
columnVisibilityModel={columnVisibilityModel}
Expand Down Expand Up @@ -162,6 +150,7 @@ describe("DataSubmissionListFilters Component", () => {
<TestParent>
<DataSubmissionListFilters
columns={columns}
organizations={organizations}
submitterNames={submitterNames}
dataCommons={dataCommons}
columnVisibilityModel={columnVisibilityModel}
Expand Down Expand Up @@ -189,6 +178,7 @@ describe("DataSubmissionListFilters Component", () => {
<TestParent userRole="Admin">
<DataSubmissionListFilters
columns={columns}
organizations={organizations}
submitterNames={submitterNames}
dataCommons={dataCommons}
columnVisibilityModel={columnVisibilityModel}
Expand Down Expand Up @@ -221,11 +211,12 @@ describe("DataSubmissionListFilters Component", () => {
});
});

it("prevents non-admin users from changing the organization select", async () => {
it("allows non-admin users to select an organization", async () => {
const { getByTestId } = render(
<TestParent userRole="Submitter">
<DataSubmissionListFilters
columns={columns}
organizations={organizations}
submitterNames={submitterNames}
dataCommons={dataCommons}
columnVisibilityModel={columnVisibilityModel}
Expand All @@ -242,14 +233,15 @@ describe("DataSubmissionListFilters Component", () => {
const organizationSelectInput = getByTestId("organization-select");

const button = within(organizationSelectInput).getByRole("button");
expect(button).toHaveClass("Mui-readOnly");
expect(button).not.toHaveClass("Mui-readOnly");
});

it("resets all filters and clears URL searchParams when reset button is clicked", async () => {
const { getByTestId, getByRole } = render(
<TestParent userRole="Admin">
<DataSubmissionListFilters
columns={columns}
organizations={organizations}
submitterNames={submitterNames}
dataCommons={dataCommons}
columnVisibilityModel={columnVisibilityModel}
Expand Down Expand Up @@ -377,6 +369,7 @@ describe("DataSubmissionListFilters Component", () => {
<TestParent userRole="Admin">
<DataSubmissionListFilters
columns={columns}
organizations={organizations}
submitterNames={submitterNames}
dataCommons={dataCommons}
columnVisibilityModel={columnVisibilityModel}
Expand Down Expand Up @@ -404,6 +397,7 @@ describe("DataSubmissionListFilters Component", () => {
<TestParent userRole="Admin">
<DataSubmissionListFilters
columns={columns}
organizations={organizations}
submitterNames={submitterNames}
dataCommons={dataCommons}
columnVisibilityModel={columnVisibilityModel}
Expand Down Expand Up @@ -439,6 +433,7 @@ describe("DataSubmissionListFilters Component", () => {
<TestParent userRole="Admin">
<DataSubmissionListFilters
columns={columns}
organizations={organizations}
submitterNames={submitterNames}
dataCommons={dataCommons}
columnVisibilityModel={columnVisibilityModel}
Expand Down Expand Up @@ -468,6 +463,7 @@ describe("DataSubmissionListFilters Component", () => {
<TestParent userRole="Admin">
<DataSubmissionListFilters
columns={columns}
organizations={organizations}
submitterNames={submitterNames}
dataCommons={dataCommons}
columnVisibilityModel={columnVisibilityModel}
Expand Down Expand Up @@ -503,6 +499,7 @@ describe("DataSubmissionListFilters Component", () => {
<TestParent userRole="Admin">
<DataSubmissionListFilters
columns={columns}
organizations={organizations}
submitterNames={submitterNames}
dataCommons={dataCommons}
columnVisibilityModel={columnVisibilityModel}
Expand Down Expand Up @@ -540,6 +537,7 @@ describe("DataSubmissionListFilters Component", () => {
<TestParent userRole="Admin">
<DataSubmissionListFilters
columns={columns}
organizations={organizations}
submitterNames={submitterNames}
dataCommons={dataCommons}
columnVisibilityModel={columnVisibilityModel}
Expand Down Expand Up @@ -581,6 +579,7 @@ describe("DataSubmissionListFilters Component", () => {
<TestParent userRole="Admin">
<DataSubmissionListFilters
columns={columns}
organizations={organizations}
submitterNames={submitterNames}
dataCommons={dataCommons}
columnVisibilityModel={columnVisibilityModel}
Expand Down Expand Up @@ -624,6 +623,7 @@ describe("DataSubmissionListFilters Component", () => {
<TestParent initialEntries={initialEntries} userRole="Admin">
<DataSubmissionListFilters
columns={columns}
organizations={organizations}
submitterNames={submitterNames}
dataCommons={dataCommons}
columnVisibilityModel={columnVisibilityModel}
Expand Down Expand Up @@ -667,6 +667,7 @@ describe("DataSubmissionListFilters Component", () => {
<TestParent initialEntries={initialEntries} userRole="Admin">
<DataSubmissionListFilters
columns={columns}
organizations={organizations}
submitterNames={submitterNames}
dataCommons={dataCommons}
columnVisibilityModel={columnVisibilityModel}
Expand Down Expand Up @@ -707,6 +708,7 @@ describe("DataSubmissionListFilters Component", () => {
<TestParent initialEntries={initialEntries} userRole="Admin">
<DataSubmissionListFilters
columns={columns}
organizations={organizations}
submitterNames={submitterNames}
dataCommons={dataCommons}
columnVisibilityModel={columnVisibilityModel}
Expand Down Expand Up @@ -739,6 +741,7 @@ describe("DataSubmissionListFilters Component", () => {
<TestParent initialEntries={initialEntries} userRole="Admin">
<DataSubmissionListFilters
columns={columns}
organizations={organizations}
submitterNames={submitterNames}
dataCommons={[]} // Empty dataCommons
columnVisibilityModel={columnVisibilityModel}
Expand Down Expand Up @@ -782,6 +785,7 @@ describe("DataSubmissionListFilters Component", () => {
<TestParent>
<DataSubmissionListFilters
columns={columns}
organizations={organizations}
submitterNames={submitterNames}
dataCommons={["DataCommon1", "DataCommon2"]} // Non-empty dataCommons
columnVisibilityModel={columnVisibilityModel}
Expand Down Expand Up @@ -809,13 +813,12 @@ describe("DataSubmissionListFilters Component", () => {

await waitFor(() => {
expect(getByTestId("data-commons-select-input")).toHaveValue("DataCommon1");
expect(mockOnChange).toHaveBeenCalledWith(
expect.objectContaining({
dataCommons: "DataCommon1",
})
);
});

expect(mockOnChange).toHaveBeenCalledWith(
expect.objectContaining({
dataCommons: "DataCommon1",
})
);
});

it("sets submitterNames select to 'All' when submitterNames prop is empty", async () => {
Expand All @@ -826,6 +829,7 @@ describe("DataSubmissionListFilters Component", () => {
<TestParent>
<DataSubmissionListFilters
columns={columns}
organizations={organizations}
submitterNames={[]} // Empty submitterNames
dataCommons={dataCommons}
columnVisibilityModel={columnVisibilityModel}
Expand Down Expand Up @@ -863,6 +867,7 @@ describe("DataSubmissionListFilters Component", () => {
<TestParent>
<DataSubmissionListFilters
columns={columns}
organizations={organizations}
submitterNames={["Submitter1", "Submitter2"]} // Non-empty submitterNames
dataCommons={dataCommons}
columnVisibilityModel={columnVisibilityModel}
Expand Down Expand Up @@ -890,12 +895,89 @@ describe("DataSubmissionListFilters Component", () => {

await waitFor(() => {
expect(getByTestId("submitter-name-select-input")).toHaveValue("Submitter1");
expect(mockOnChange).toHaveBeenCalledWith(
expect.objectContaining({
submitterName: "Submitter1",
})
);
});
});

expect(mockOnChange).toHaveBeenCalledWith(
expect.objectContaining({
submitterName: "Submitter1",
})
it("sets organization select to 'All' when organizations prop is empty", async () => {
const mockOnChange = jest.fn();
const mockOnColumnVisibilityModelChange = jest.fn();

const { getByTestId, getByRole } = render(
<TestParent>
<DataSubmissionListFilters
columns={columns}
organizations={[]}
submitterNames={submitterNames}
dataCommons={dataCommons}
columnVisibilityModel={columnVisibilityModel}
onColumnVisibilityModelChange={mockOnColumnVisibilityModelChange}
onChange={mockOnChange}
/>
</TestParent>
);

await waitFor(() => {
expect(getByTestId("organization-select-input")).toHaveValue("All");
});

const organizationSelect = within(getByTestId("organization-select")).getByRole("button");
userEvent.click(organizationSelect);

const organizationList = within(getByRole("listbox", { hidden: true }));

await waitFor(() => {
expect(organizationList.getByTestId("organization-option-All")).toBeInTheDocument();
expect(organizationList.queryByTestId("organization-option-Org1")).not.toBeInTheDocument();
expect(organizationList.queryByTestId("organization-option-Org2")).not.toBeInTheDocument();
});
});

it("sets organization select to field.value when organizations prop is non-empty", async () => {
const mockOnChange = jest.fn();
const mockOnColumnVisibilityModelChange = jest.fn();

const { getByTestId, getByRole } = render(
<TestParent>
<DataSubmissionListFilters
columns={columns}
organizations={organizations}
submitterNames={submitterNames}
dataCommons={dataCommons}
columnVisibilityModel={columnVisibilityModel}
onColumnVisibilityModelChange={mockOnColumnVisibilityModelChange}
onChange={mockOnChange}
/>
</TestParent>
);

await waitFor(() => {
expect(getByTestId("organization-select-input")).toHaveValue("All");
});

const organizationSelect = within(getByTestId("organization-select")).getByRole("button");
userEvent.click(organizationSelect);

const organizationList = within(getByRole("listbox", { hidden: true }));

await waitFor(() => {
expect(organizationList.getByTestId("organization-option-Org1")).toBeInTheDocument();
expect(organizationList.getByTestId("organization-option-Org2")).toBeInTheDocument();
});

userEvent.click(getByTestId("organization-option-Org1"));

await waitFor(() => {
expect(getByTestId("organization-select-input")).toHaveValue("Org1");
expect(mockOnChange).toHaveBeenCalledWith(
expect.objectContaining({
organization: "Org1",
})
);
});
});
});
Loading