-
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 pull request #8 from seroanalytics/session
handle expired sessions
- Loading branch information
Showing
15 changed files
with
216 additions
and
68 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
@@ -1,17 +1,21 @@ | ||
import {Alert, Button} from "react-bootstrap"; | ||
import React, {useContext} from "react"; | ||
import {ActionType, RootContext, RootDispatchContext} from "../RootContext"; | ||
import {ActionType, RootDispatchContext} from "../RootContext"; | ||
import {ErrorDetail} from "../generated"; | ||
|
||
export default function AppError() { | ||
const state = useContext(RootContext); | ||
interface Props { | ||
error: ErrorDetail | ||
} | ||
|
||
export default function AppError({error}: Props) { | ||
const dispatch = useContext(RootDispatchContext); | ||
const message = error.detail ?? `API returned an error: ${error.error}`; | ||
const remove = () => { | ||
dispatch({type: ActionType.ERROR_DISMISSED, payload: null}); | ||
dispatch({type: ActionType.ERROR_DISMISSED, payload: error}); | ||
} | ||
if (state.genericError) { | ||
return <Alert variant={"danger"} className={"rounded-0 border-0"}> | ||
<Button variant={"close"} role={"close"} onClick={remove} className={"mx-2 float-end"}></Button> | ||
{state.genericError.detail ?? `API returned an error: ${state.genericError.error}`} | ||
</Alert> | ||
} else return null | ||
return <Alert variant={"danger"} className={"rounded-0 border-0 mb-1"}> | ||
<Button variant={"close"} role={"close"} onClick={remove} | ||
className={"mx-2 float-end"}></Button> | ||
{message} {error.error === "SESSION_EXPIRED" && <a href={"/"}>Re-upload your data to continue.</a>} | ||
</Alert> | ||
} |
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
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
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
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,46 @@ | ||
import React from "react"; | ||
import {render, screen, waitFor} from "@testing-library/react"; | ||
import { | ||
mockAppState, | ||
mockAxios, | ||
mockDatasetMetadata, | ||
mockError, mockFailure, mockSeriesData, | ||
mockSuccess | ||
} from "../mocks"; | ||
import {RootContext, RootDispatchContext} from "../../src/RootContext"; | ||
import App from "../../src/components/App"; | ||
import {userEvent} from "@testing-library/user-event"; | ||
|
||
describe("<App />", () => { | ||
|
||
beforeEach(() => { | ||
mockAxios.reset(); | ||
}); | ||
|
||
test("should display any generic errors", async () => { | ||
mockAxios.onGet("/datasets/") | ||
.reply(404, mockFailure("bad")); | ||
|
||
render(<App/>); | ||
const errors = await screen.findAllByRole("alert"); | ||
expect(errors.length).toBe(2); | ||
}); | ||
|
||
test("should fetch dataset metadata if dataset selected", async () => { | ||
mockAxios.onGet("/datasets/") | ||
.reply(200, mockSuccess(["d1"])); | ||
|
||
mockAxios.onGet("/dataset/d1/") | ||
.reply(200, mockSuccess(mockDatasetMetadata())); | ||
|
||
mockAxios.onGet("/dataset/d1/trace/ab/?scale=natural") | ||
.reply(200, mockSuccess(mockSeriesData())); | ||
|
||
render(<App/>); | ||
|
||
const go = await screen.findByText("Go"); | ||
await userEvent.click(go); | ||
|
||
await screen.findByText("Detected biomarkers"); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1,57 +1,53 @@ | ||
import React from "react"; | ||
import {fireEvent, render, screen} from "@testing-library/react"; | ||
import AppError from "../../src/components/AppError"; | ||
import {mockAppState, mockError} from "../mocks"; | ||
import {ActionType, RootContext, RootDispatchContext} from "../../src/RootContext"; | ||
import {mockError} from "../mocks"; | ||
import {ActionType, RootDispatchContext} from "../../src/RootContext"; | ||
|
||
describe("<AppError />", () => { | ||
|
||
test("should be null if no error present", () => { | ||
const state = mockAppState(); | ||
const dispatch = jest.fn(); | ||
const {container} = render(<RootContext.Provider value={state}> | ||
<RootDispatchContext.Provider | ||
value={dispatch}><AppError/> | ||
</RootDispatchContext.Provider> | ||
</RootContext.Provider>); | ||
expect(container.firstChild).toBe(null); | ||
}); | ||
|
||
test("should display error detail if present", () => { | ||
const state = mockAppState({genericError: mockError("custom message")}); | ||
const dispatch = jest.fn(); | ||
render(<RootContext.Provider value={state}> | ||
<RootDispatchContext.Provider value={dispatch}> | ||
<AppError/> | ||
</RootDispatchContext.Provider> | ||
</RootContext.Provider>); | ||
const error = mockError("custom message"); | ||
const dispatch = jest.fn(); | ||
render(<RootDispatchContext.Provider value={dispatch}> | ||
<AppError error={error}/> | ||
</RootDispatchContext.Provider>); | ||
const alert = screen.getByRole("alert"); | ||
expect(alert.lastChild?.textContent).toBe("custom message"); | ||
expect(alert.textContent).toBe("custom message "); | ||
}); | ||
|
||
test("should display error type if no detail present", () => { | ||
const state = mockAppState({genericError: mockError(null as any)}); | ||
const error = mockError(null as any); | ||
const dispatch = jest.fn(); | ||
render(<RootDispatchContext.Provider value={dispatch}> | ||
<AppError error={error}/> | ||
</RootDispatchContext.Provider>); | ||
const alert = screen.getByRole("alert"); | ||
expect(alert.textContent).toBe("API returned an error: OTHER_ERROR "); | ||
}); | ||
|
||
test("should display home link if type is SESSION_EXPIRED", () => { | ||
const error = mockError("Session expired.", "SESSION_EXPIRED"); | ||
const dispatch = jest.fn(); | ||
render(<RootContext.Provider value={state}> | ||
<RootDispatchContext.Provider | ||
value={dispatch}><AppError/> | ||
</RootDispatchContext.Provider> | ||
</RootContext.Provider>); | ||
render(<RootDispatchContext.Provider value={dispatch}> | ||
<AppError error={error}/> | ||
</RootDispatchContext.Provider>); | ||
const alert = screen.getByRole("alert"); | ||
expect(alert.lastChild?.textContent).toBe("API returned an error: OTHER_ERROR"); | ||
expect(alert.textContent).toBe("Session expired. Re-upload your data to continue."); | ||
const link = screen.getByRole("link") as HTMLAnchorElement; | ||
expect(link.href).toBe("http://localhost/"); | ||
}); | ||
|
||
test("should dispatch ERROR_DISMISSED action when closed", () => { | ||
const state = mockAppState({genericError: mockError("custom message")}); | ||
const error = mockError("custom message"); | ||
const dispatch = jest.fn(); | ||
render(<RootContext.Provider value={state}> | ||
<RootDispatchContext.Provider | ||
value={dispatch}><AppError/> | ||
</RootDispatchContext.Provider> | ||
</RootContext.Provider>); | ||
render(<RootDispatchContext.Provider value={dispatch}> | ||
<AppError error={error}/> | ||
</RootDispatchContext.Provider>); | ||
const closeButton = screen.getByRole("close"); | ||
fireEvent.click(closeButton); | ||
expect(dispatch.mock.calls.length).toBe(1); | ||
expect(dispatch.mock.calls[0][0].type).toBe(ActionType.ERROR_DISMISSED); | ||
expect(dispatch.mock.calls[0][0].payload).toEqual(error); | ||
}); | ||
}); |
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.