generated from CDCgov/template
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(RV-426): Adds error framework (#496)
- Loading branch information
1 parent
04bdad6
commit 5c80f8c
Showing
17 changed files
with
447 additions
and
43 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,70 @@ | ||
import { render, screen, fireEvent } from "@testing-library/react"; | ||
import "@testing-library/jest-dom"; | ||
import { ErrorProvider, useError } from "./ErrorContext"; | ||
|
||
function TestComponent() { | ||
const { error, setError, resetError } = useError(); | ||
|
||
return ( | ||
<div> | ||
<p data-testid="errorMessage"> | ||
{error ? error.message : "No error"} | ||
</p> | ||
<button | ||
data-testid="triggerError" | ||
onClick={() => setError(new Error("Test error"))} | ||
> | ||
Trigger Error | ||
</button> | ||
<button data-testid="resetError" onClick={resetError}> | ||
Reset Error | ||
</button> | ||
</div> | ||
); | ||
} | ||
|
||
describe("ErrorContext", () => { | ||
it("should default to null error", () => { | ||
render( | ||
<ErrorProvider> | ||
<TestComponent /> | ||
</ErrorProvider> | ||
); | ||
|
||
const messageEl = screen.getByTestId("errorMessage"); | ||
expect(messageEl).toHaveTextContent("No error"); | ||
}); | ||
|
||
it("should set and display the error message", () => { | ||
render( | ||
<ErrorProvider> | ||
<TestComponent /> | ||
</ErrorProvider> | ||
); | ||
|
||
const triggerButton = screen.getByTestId("triggerError"); | ||
fireEvent.click(triggerButton); | ||
|
||
const messageEl = screen.getByTestId("errorMessage"); | ||
expect(messageEl).toHaveTextContent("Test error"); | ||
}); | ||
|
||
it("should reset the error message", () => { | ||
render( | ||
<ErrorProvider> | ||
<TestComponent /> | ||
</ErrorProvider> | ||
); | ||
|
||
// Trigger the error first | ||
const triggerButton = screen.getByTestId("triggerError"); | ||
fireEvent.click(triggerButton); | ||
|
||
// Reset the error | ||
const resetButton = screen.getByTestId("resetError"); | ||
fireEvent.click(resetButton); | ||
|
||
const messageEl = screen.getByTestId("errorMessage"); | ||
expect(messageEl).toHaveTextContent("No 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { createContext, useContext, useState, ReactNode } from "react"; | ||
|
||
|
||
type Error = typeof ERRORS[keyof typeof ERRORS] | null; | ||
|
||
interface ErrorContextProps { | ||
error: Error | null; | ||
setError: (error: Error | null) => void; | ||
resetError: () => void; | ||
} | ||
|
||
const ErrorContext = createContext<ErrorContextProps>({ | ||
error: null, | ||
setError: () => { | ||
/* no-op */ | ||
}, | ||
resetError: () => { | ||
/* no-op */ | ||
}, | ||
}); | ||
|
||
interface ErrorProviderProps { | ||
children: ReactNode; | ||
} | ||
|
||
/** ErrorProvider component that wraps your entire application */ | ||
export function ErrorProvider({ children }: ErrorProviderProps) { | ||
const [error, setErrorState] = useState<Error | null>(null); | ||
|
||
const setError = (newError: Error | null) => { | ||
setErrorState(newError); | ||
}; | ||
|
||
const resetError = () => { | ||
setErrorState(null); | ||
}; | ||
return ( | ||
<ErrorContext.Provider | ||
value={{ | ||
error, | ||
setError, | ||
resetError, | ||
}} | ||
> | ||
{children} | ||
</ErrorContext.Provider> | ||
); | ||
} | ||
|
||
export function useError() { | ||
return useContext(ErrorContext); | ||
}; | ||
|
||
export const ERRORS = { | ||
NO_ERROR: null, | ||
GENERIC_ERROR: { | ||
title: "An error occurred", | ||
message: "Please try again later", | ||
}, | ||
MISMATCH_ERROR: { | ||
title: 'Mismatch Error', | ||
message: 'The uploaded file has a different number of pages than template. Please upload a file with correct pages pages to proceed or choose different template.' | ||
}, | ||
CSV_ERROR: { | ||
title: 'Error downloading file', | ||
message: 'There was an issue with downloading your file. Please try again.' | ||
} | ||
} |
Oops, something went wrong.