-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce modern context provider and hooks (PP-1572) (#123)
- Loading branch information
Showing
5 changed files
with
119 additions
and
39 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
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,28 @@ | ||
import { createContext, useContext } from "react"; | ||
import { FeatureFlags } from "../interfaces"; | ||
import Admin from "../models/Admin"; | ||
|
||
export type AppContextType = { | ||
csrfToken: string; | ||
settingUp: boolean; | ||
admin: Admin; | ||
featureFlags: FeatureFlags; | ||
}; | ||
|
||
// Don't export this, since we always want the error handling behavior of our hook. | ||
const AppContext = createContext<AppContextType | undefined>(undefined); | ||
|
||
export const useAppContext = (): AppContextType => { | ||
const context = useContext(AppContext); | ||
if (context === undefined) { | ||
throw new Error("useAppContext must be used within an AppContext povider."); | ||
} | ||
return context; | ||
}; | ||
|
||
export const useCsrfToken = () => useAppContext().csrfToken; | ||
export const useAppAdmin = () => useAppContext().admin; | ||
export const useAppEmail = () => useAppAdmin().email; | ||
export const useAppFeatureFlags = () => useAppContext().featureFlags; | ||
|
||
export default AppContext.Provider; |
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,72 @@ | ||
import { renderHook } from "@testing-library/react-hooks"; | ||
import { | ||
useAppAdmin, | ||
useAppContext, | ||
useAppEmail, | ||
useAppFeatureFlags, | ||
useCsrfToken, | ||
} from "../../../src/context/appContext"; | ||
import { componentWithProviders } from "../testUtils/withProviders"; | ||
import { ContextProviderProps } from "../../../src/components/ContextProvider"; | ||
import { FeatureFlags } from "../../../src/interfaces"; | ||
|
||
// TODO: These tests may need to be adjusted in the future. | ||
// Currently, an AppContext.Provider is injected into the component tree | ||
// by the ContextProvider, which itself uses a legacy context API. (See | ||
// https://legacy.reactjs.org/docs/legacy-context.html) | ||
// but that will change once uses of that API have been removed. | ||
|
||
describe("AppContext", () => { | ||
const expectedCsrfToken = "token"; | ||
const expectedEmail = "email"; | ||
const expectedFeatureFlags: FeatureFlags = { | ||
// @ts-expect-error - "testTrue" & "testFalse" aren't valid feature flags | ||
testTrue: true, | ||
testFalse: false, | ||
}; | ||
const expectedRoles = [{ role: "system" }]; | ||
|
||
const contextProviderProps: ContextProviderProps = { | ||
csrfToken: expectedCsrfToken, | ||
featureFlags: expectedFeatureFlags, | ||
roles: expectedRoles, | ||
email: expectedEmail, | ||
}; | ||
const wrapper = componentWithProviders({ contextProviderProps }); | ||
|
||
it("provides useAppContext context hook", () => { | ||
const { result } = renderHook(() => useAppContext(), { wrapper }); | ||
const value = result.current; | ||
expect(value.csrfToken).toEqual(expectedCsrfToken); | ||
expect(value.admin.email).toEqual(expectedEmail); | ||
expect(value.admin.roles).toEqual(expectedRoles); | ||
expect(value.featureFlags).toEqual(expectedFeatureFlags); | ||
}); | ||
|
||
it("provides useAppAdmin context hook", () => { | ||
const { result } = renderHook(() => useAppAdmin(), { wrapper }); | ||
const admin = result.current; | ||
expect(admin.email).toEqual(expectedEmail); | ||
expect(admin.roles).toEqual(expectedRoles); | ||
}); | ||
|
||
it("provides useAppEmail context hook", () => { | ||
const { result } = renderHook(() => useAppEmail(), { wrapper }); | ||
const email = result.current; | ||
expect(email).toEqual(expectedEmail); | ||
}); | ||
|
||
it("provides useCsrfToken context hook", () => { | ||
const { result } = renderHook(() => useCsrfToken(), { wrapper }); | ||
const token = result.current; | ||
expect(token).toEqual(expectedCsrfToken); | ||
}); | ||
|
||
it("provides useAppFeatureFlags context hook", () => { | ||
const { result } = renderHook(() => useAppFeatureFlags(), { | ||
wrapper, | ||
}); | ||
const flags = result.current; | ||
expect(flags).toEqual(expectedFeatureFlags); | ||
}); | ||
}); |