|
| 1 | +import React from "react" |
| 2 | +import { renderWithProviders, screen } from "@/test-utils" |
| 3 | +import { setMockResponse } from "api/test-utils" |
| 4 | +import { factories } from "api/mitxonline-test-utils" |
| 5 | +import { useFeatureFlagEnabled } from "posthog-js/react" |
| 6 | +import { allowConsoleErrors } from "ol-test-utilities" |
| 7 | +import { ForbiddenError } from "@/common/errors" |
| 8 | +import ContractAdminPage from "./ContractAdminPage" |
| 9 | + |
| 10 | +jest.mock("posthog-js/react", () => ({ |
| 11 | + ...jest.requireActual("posthog-js/react"), |
| 12 | + useFeatureFlagEnabled: jest.fn(), |
| 13 | +})) |
| 14 | +const mockedUseFeatureFlagEnabled = jest.mocked(useFeatureFlagEnabled) |
| 15 | + |
| 16 | +const API_BASE_URL = process.env.NEXT_PUBLIC_MITX_ONLINE_BASE_URL |
| 17 | +const managerOrgsUrl = `${API_BASE_URL}/api/v0/b2b/manager/organizations/` |
| 18 | +const managerContractDetailUrl = (orgId: number, contractId: number) => |
| 19 | + `${API_BASE_URL}/api/v0/b2b/manager/organizations/${orgId}/contracts/${contractId}/` |
| 20 | + |
| 21 | +const makeOrgWithContract = () => { |
| 22 | + const contract = factories.contracts.contract() |
| 23 | + const org = factories.organizations.organization({ contracts: [contract] }) |
| 24 | + return { org, contract } |
| 25 | +} |
| 26 | + |
| 27 | +describe("ContractAdminPage", () => { |
| 28 | + beforeEach(() => { |
| 29 | + mockedUseFeatureFlagEnabled.mockReturnValue(undefined) |
| 30 | + }) |
| 31 | + |
| 32 | + test("throws ForbiddenError when feature flag is explicitly false", () => { |
| 33 | + mockedUseFeatureFlagEnabled.mockReturnValue(false) |
| 34 | + allowConsoleErrors() |
| 35 | + |
| 36 | + expect(() => |
| 37 | + renderWithProviders( |
| 38 | + <ContractAdminPage orgSlug="any-org" contractSlug="any-contract" />, |
| 39 | + ), |
| 40 | + ).toThrow(ForbiddenError) |
| 41 | + }) |
| 42 | + |
| 43 | + test("renders nothing while feature flag is loading (undefined)", () => { |
| 44 | + mockedUseFeatureFlagEnabled.mockReturnValue(undefined) |
| 45 | + |
| 46 | + const { view } = renderWithProviders( |
| 47 | + <ContractAdminPage orgSlug="any-org" contractSlug="any-contract" />, |
| 48 | + ) |
| 49 | + |
| 50 | + expect(view.container.firstChild).toBeNull() |
| 51 | + }) |
| 52 | + |
| 53 | + test("shows 'Organization not found' when user is not a manager for the requested org", async () => { |
| 54 | + mockedUseFeatureFlagEnabled.mockReturnValue(true) |
| 55 | + |
| 56 | + const otherOrg = factories.organizations.organization({}) |
| 57 | + setMockResponse.get(managerOrgsUrl, [otherOrg]) |
| 58 | + |
| 59 | + renderWithProviders( |
| 60 | + <ContractAdminPage orgSlug="not-my-org" contractSlug="some-contract" />, |
| 61 | + ) |
| 62 | + |
| 63 | + await screen.findByRole("heading", { name: "Organization not found" }) |
| 64 | + }) |
| 65 | + |
| 66 | + test("shows 'Contract not found' when org is found but contract slug does not match", async () => { |
| 67 | + mockedUseFeatureFlagEnabled.mockReturnValue(true) |
| 68 | + |
| 69 | + const { org } = makeOrgWithContract() |
| 70 | + setMockResponse.get(managerOrgsUrl, [org]) |
| 71 | + |
| 72 | + renderWithProviders( |
| 73 | + <ContractAdminPage |
| 74 | + orgSlug={org.slug} |
| 75 | + contractSlug="wrong-contract-slug" |
| 76 | + />, |
| 77 | + ) |
| 78 | + |
| 79 | + await screen.findByRole("heading", { name: "Contract not found" }) |
| 80 | + }) |
| 81 | + |
| 82 | + test("renders org name and contract name when flag is on and user is a manager", async () => { |
| 83 | + mockedUseFeatureFlagEnabled.mockReturnValue(true) |
| 84 | + |
| 85 | + const { org, contract } = makeOrgWithContract() |
| 86 | + setMockResponse.get(managerOrgsUrl, [org]) |
| 87 | + setMockResponse.get(managerContractDetailUrl(org.id, contract.id), { |
| 88 | + ...contract, |
| 89 | + attachment_percentage: null, |
| 90 | + total_enrollments: 0, |
| 91 | + total_codes: 50, |
| 92 | + }) |
| 93 | + |
| 94 | + renderWithProviders( |
| 95 | + <ContractAdminPage orgSlug={org.slug} contractSlug={contract.slug} />, |
| 96 | + ) |
| 97 | + |
| 98 | + await screen.findByRole("heading", { name: org.name }) |
| 99 | + expect(screen.getByText(contract.name)).toBeInTheDocument() |
| 100 | + }) |
| 101 | + |
| 102 | + test("renders seat count from contract detail", async () => { |
| 103 | + mockedUseFeatureFlagEnabled.mockReturnValue(true) |
| 104 | + |
| 105 | + const { org, contract } = makeOrgWithContract() |
| 106 | + setMockResponse.get(managerOrgsUrl, [org]) |
| 107 | + setMockResponse.get(managerContractDetailUrl(org.id, contract.id), { |
| 108 | + ...contract, |
| 109 | + attachment_percentage: null, |
| 110 | + total_enrollments: 12, |
| 111 | + total_codes: 75, |
| 112 | + }) |
| 113 | + |
| 114 | + renderWithProviders( |
| 115 | + <ContractAdminPage orgSlug={org.slug} contractSlug={contract.slug} />, |
| 116 | + ) |
| 117 | + |
| 118 | + await screen.findByText("75 seats") |
| 119 | + }) |
| 120 | +}) |
0 commit comments