Skip to content

Commit

Permalink
Add tests for AuthProvider.
Browse files Browse the repository at this point in the history
Signed-off-by: Mirjam Aulbach <mirjam.aulbach@aiven.io>
  • Loading branch information
programmiri committed Feb 16, 2024
1 parent ae856b3 commit d93c472
Showing 1 changed file with 74 additions and 0 deletions.
74 changes: 74 additions & 0 deletions coral/src/app/context-provider/AuthProvider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {
} from "src/app/context-provider/AuthProvider";
import { waitForElementToBeRemoved } from "@testing-library/react/pure";
import { testAuthUser } from "src/domain/auth-user/auth-user-test-helper";
import { setupFeatureFlagMock } from "src/services/feature-flags/test-helper";
import { FeatureFlag } from "src/services/feature-flags/types";

const getAuthMock = jest.fn();
jest.mock("src/domain/auth-user", () => ({
Expand Down Expand Up @@ -81,4 +83,76 @@ describe("AuthProvider.tsx", () => {
expect(authUserFromContext).toBeVisible();
});
});

describe("does not render the auth provider with given children when auth user is SUPERADMIN", () => {
beforeEach(async () => {
getAuthMock.mockReturnValue({ mockAuthUser, userrole: "SUPERADMIN" });
customRender(
<AuthProvider>
<ChildComponent />
</AuthProvider>,
{
queryClient: true,
}
);

await waitForElementToBeRemoved(screen.getByText("Loading Klaw"));
});

afterEach(() => {
jest.resetAllMocks();
cleanup();
});

it("does not returns context provider with given children", () => {
const childElement = screen.queryByTestId("auth-provider-child");
expect(childElement).not.toBeInTheDocument();
});

it("shows a dialog informing superadmin they can not access coral", async () => {
const dialog = await screen.findByRole("dialog", {
name: "You're currently logged in as superadmin.",
});

expect(dialog).toBeVisible();
});
});

describe("render the auth provider with given children when auth user is SUPERADMIN and feature flag is enabled", () => {
beforeEach(async () => {
setupFeatureFlagMock(
FeatureFlag.FEATURE_FLAG_SUPER_ADMIN_ACCESS_CORAL,
true
);
getAuthMock.mockReturnValue({ mockAuthUser, userrole: "SUPERADMIN" });
customRender(
<AuthProvider>
<ChildComponent />
</AuthProvider>,
{
queryClient: true,
}
);

await waitForElementToBeRemoved(screen.getByText("Loading Klaw"));
});

afterEach(() => {
jest.resetAllMocks();
cleanup();
});

it("returns context provider with given children", () => {
const childElement = screen.getByTestId("auth-provider-child");
expect(childElement).toBeVisible();
});

it("does not show dialog informing superadmin they can not access coral", () => {
const dialog = screen.queryByText(
"You're currently logged in as superadmin."
);

expect(dialog).not.toBeInTheDocument();
});
});
});

0 comments on commit d93c472

Please sign in to comment.