-
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.
- Loading branch information
Showing
1 changed file
with
72 additions
and
0 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
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, waitFor } = 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, waitFor } = renderHook(() => useAppAdmin(), { wrapper }); | ||
const admin = result.current; | ||
expect(admin.email).toEqual(expectedEmail); | ||
expect(admin.roles).toEqual(expectedRoles); | ||
}); | ||
|
||
it("provides useAppEmail context hook", () => { | ||
const { result, waitFor } = renderHook(() => useAppEmail(), { wrapper }); | ||
const email = result.current; | ||
expect(email).toEqual(expectedEmail); | ||
}); | ||
|
||
it("provides useCsrfToken context hook", () => { | ||
const { result, waitFor } = renderHook(() => useCsrfToken(), { wrapper }); | ||
const token = result.current; | ||
expect(token).toEqual(expectedCsrfToken); | ||
}); | ||
|
||
it("provides useAppFeatureFlags context hook", () => { | ||
const { result, waitFor } = renderHook(() => useAppFeatureFlags(), { | ||
wrapper, | ||
}); | ||
const flags = result.current; | ||
expect(flags).toEqual(expectedFeatureFlags); | ||
}); | ||
}); |