-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch '3.1.0' into CRDCDH-1443-II
- Loading branch information
Showing
11 changed files
with
1,675 additions
and
11 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,196 @@ | ||
import React, { FC, useMemo } from "react"; | ||
import { render, waitFor } from "@testing-library/react"; | ||
import { MemoryRouter, Routes, Route } from "react-router-dom"; | ||
import { MockedProvider, MockedResponse } from "@apollo/client/testing"; | ||
import { | ||
Context as AuthContext, | ||
ContextState as AuthContextState, | ||
Status as AuthContextStatus, | ||
} from "../../components/Contexts/AuthContext"; | ||
import StudiesController from "./Controller"; | ||
import { SearchParamsProvider } from "../../components/Contexts/SearchParamsContext"; | ||
import { | ||
GET_APPROVED_STUDY, | ||
GetApprovedStudyInput, | ||
GetApprovedStudyResp, | ||
LIST_APPROVED_STUDIES, | ||
ListApprovedStudiesInput, | ||
ListApprovedStudiesResp, | ||
} from "../../graphql"; | ||
|
||
// NOTE: Omitting fields depended on by the component | ||
const baseUser: Omit<User, "role"> = { | ||
_id: "", | ||
firstName: "", | ||
lastName: "", | ||
userStatus: "Active", | ||
IDP: "nih", | ||
email: "", | ||
organization: null, | ||
dataCommons: [], | ||
createdAt: "", | ||
updateAt: "", | ||
studies: null, | ||
}; | ||
|
||
type ParentProps = { | ||
role: User["role"]; | ||
initialEntry?: string; | ||
mocks?: MockedResponse[]; | ||
ctxStatus?: AuthContextStatus; | ||
children: React.ReactNode; | ||
}; | ||
|
||
const TestParent: FC<ParentProps> = ({ | ||
role, | ||
initialEntry = "/studies", | ||
mocks = [], | ||
ctxStatus = AuthContextStatus.LOADED, | ||
children, | ||
}: ParentProps) => { | ||
const baseAuthCtx: AuthContextState = useMemo<AuthContextState>( | ||
() => ({ | ||
status: ctxStatus, | ||
isLoggedIn: role !== null, | ||
user: { ...baseUser, role }, | ||
}), | ||
[role, ctxStatus] | ||
); | ||
|
||
return ( | ||
<MockedProvider mocks={mocks} showWarnings> | ||
<MemoryRouter initialEntries={[initialEntry]}> | ||
<Routes> | ||
<Route | ||
path="/studies/:studyId?" | ||
element={ | ||
<AuthContext.Provider value={baseAuthCtx}> | ||
<SearchParamsProvider>{children}</SearchParamsProvider> | ||
</AuthContext.Provider> | ||
} | ||
/> | ||
<Route path="/" element={<div>Root Page</div>} /> | ||
</Routes> | ||
</MemoryRouter> | ||
</MockedProvider> | ||
); | ||
}; | ||
|
||
describe("StudiesController", () => { | ||
it("should render the page without crashing", async () => { | ||
const listApprovedStudiesMock: MockedResponse< | ||
ListApprovedStudiesResp, | ||
ListApprovedStudiesInput | ||
> = { | ||
request: { | ||
query: LIST_APPROVED_STUDIES, | ||
}, | ||
variableMatcher: () => true, | ||
result: { | ||
data: { | ||
listApprovedStudies: { | ||
total: 1, | ||
studies: [ | ||
{ | ||
_id: "study-id-1", | ||
studyName: "Study Name 1", | ||
studyAbbreviation: "SN1", | ||
dbGaPID: "db123456", | ||
controlledAccess: true, | ||
openAccess: false, | ||
PI: "Dr. Smith", | ||
ORCID: "0000-0001-2345-6789", | ||
createdAt: "2022-01-01T00:00:00Z", | ||
originalOrg: "", | ||
}, | ||
], | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
const { getByTestId } = render( | ||
<TestParent | ||
role="Admin" | ||
ctxStatus={AuthContextStatus.LOADED} | ||
mocks={[listApprovedStudiesMock]} | ||
> | ||
<StudiesController /> | ||
</TestParent> | ||
); | ||
|
||
await waitFor(() => { | ||
expect(getByTestId("list-studies-container")).toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
it("should show a loading spinner when the AuthCtx is loading", async () => { | ||
const { getByTestId } = render( | ||
<TestParent role="Admin" ctxStatus={AuthContextStatus.LOADING}> | ||
<StudiesController /> | ||
</TestParent> | ||
); | ||
|
||
await waitFor(() => { | ||
expect(getByTestId("studies-suspense-loader")).toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
it.each<UserRole>([ | ||
"Data Curator", | ||
"Data Commons POC", | ||
"Federal Lead", | ||
"User", | ||
"fake role" as User["role"], | ||
])("should redirect the user role %p to the home page", (role) => { | ||
const { getByText } = render( | ||
<TestParent role={role}> | ||
<StudiesController /> | ||
</TestParent> | ||
); | ||
|
||
expect(getByText("Root Page")).toBeInTheDocument(); | ||
}); | ||
|
||
it("should render the StudyView when a studyId param is provided", async () => { | ||
const studyId = "study-id-1"; | ||
|
||
const getApprovedStudyMock: MockedResponse<GetApprovedStudyResp, GetApprovedStudyInput> = { | ||
request: { | ||
query: GET_APPROVED_STUDY, | ||
}, | ||
variableMatcher: () => true, | ||
result: { | ||
data: { | ||
getApprovedStudy: { | ||
_id: studyId, | ||
studyName: "Study Name 1", | ||
studyAbbreviation: "SN1", | ||
dbGaPID: "db123456", | ||
controlledAccess: true, | ||
openAccess: false, | ||
PI: "Dr. Smith", | ||
ORCID: "0000-0001-2345-6789", | ||
createdAt: "2022-01-01T00:00:00Z", | ||
originalOrg: "", | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
const { getByTestId } = render( | ||
<TestParent | ||
role="Admin" | ||
ctxStatus={AuthContextStatus.LOADED} | ||
mocks={[getApprovedStudyMock]} | ||
initialEntry={`/studies/${studyId}`} | ||
> | ||
<StudiesController /> | ||
</TestParent> | ||
); | ||
|
||
await waitFor(() => { | ||
expect(getByTestId("study-view-container")).toBeInTheDocument(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.