Skip to content

Commit

Permalink
New tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
tdilauro committed Aug 7, 2024
1 parent b21ec69 commit 4689d3c
Showing 1 changed file with 72 additions and 0 deletions.
72 changes: 72 additions & 0 deletions tests/jest/context/AppContext.test.tsx
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);
});
});

0 comments on commit 4689d3c

Please sign in to comment.