Skip to content

#1 PDF Application on GitHub that lets you edit PDFs on any device anywhere

3.9 KB
Raw Blame
import type { ReactNode } from "react";
import { cleanup, render, screen } from "@testing-library/react";
import { afterEach, expect, it, vi } from "vitest";
import { MemoryRouter } from "react-router-dom";
import { MantineProvider } from "@mantine/core";
import { PortalProviders } from "@processor/PortalProviders";
import { PlatformProviders } from "@editor/components/PlatformProviders";

const h = vi.hoisted(() => ({
  get: vi.fn(),
  auth: {
    user: { id: "admin", orgOwner: true },
    loading: false,
    isAdmin: true,
    isAnonymous: false,
    signOut: vi.fn(),
  },
}));
vi.mock("react-i18next", async (importOriginal) => ({
  ...(await importOriginal<typeof import("react-i18next")>()),
  useTranslation: () => ({
    t: (key: string) => key,
    i18n: { language: "en-US" },
  }),
}));
vi.mock("@editor/auth", () => ({ useAuth: () => h.auth }));
vi.mock("@editor/auth/UseSession", () => ({
  AuthProvider: ({ children }: { children: ReactNode }) => children,
  useAuth: () => h.auth,
}));
vi.mock("@editor/services/apiClient", () => ({ default: { get: h.get } }));
vi.mock("@editor/extensions/accountLogout", () => ({
  useAccountLogout: () => vi.fn(),
}));
vi.mock("@editor/contexts/LicenseContext", () => ({
  LicenseProvider: ({ children }: { children: ReactNode }) => children,
  useLicense: () => ({ licenseInfo: null, loading: false }),
}));
vi.mock("@editor/contexts/CheckoutContext", () => ({
  CheckoutProvider: ({ children }: { children: ReactNode }) => children,
}));
vi.mock("@processor/contexts/TierContext", () => ({
  TierProvider: ({ children }: { children: ReactNode }) => children,
}));
vi.mock("@processor/contexts/LinkContext", () => ({
  LinkProvider: ({ children }: { children: ReactNode }) => children,
}));
vi.mock("@processor/contexts/AccountLinkContext", () => ({
  AccountLinkProvider: ({ children }: { children: ReactNode }) => children,
}));
vi.mock("@processor/contexts/UIContext", () => ({
  UIProvider: ({ children }: { children: ReactNode }) => children,
  useUI: () => ({ linkModalOpen: false }),
}));
vi.mock("@processor/components/account-link/LinkAccountModal", () => ({
  LinkAccountModalHost: () => null,
}));
vi.mock("@processor/components/account-link/ConnectCallbackHost", () => ({
  ConnectCallbackHost: () => null,
}));
vi.mock("@processor/hooks/useFreeTierExhaustedPrompt", () => ({
  useFreeTierExhaustedPrompt: () => {},
}));
vi.mock("@processor/components/PortalChrome", () => ({
  PortalChrome: () => <div>Processor</div>,
}));
vi.mock("@editor/components/onboarding/StaticOnboardingSlide", () => ({
  default: ({ slideId }: { slideId: string }) => (
    <div role="dialog" aria-label={slideId} />
  ),
}));
vi.mock("@editor/hooks/useCookieConsentInitialization", () => ({
  useCookieConsentInitialization: () => {},
}));
vi.mock("@editor/hooks/usePosthogTracking", () => ({
  usePosthogTracking: () => {},
}));
vi.mock("@editor/hooks/useScarfTracking", () => ({
  useScarfTracking: () => {},
}));

afterEach(cleanup);

it("mounts mandatory setup on a cold Processor entry without editor providers", async () => {
  h.get.mockImplementation(async (url: string) => {
    if (url === "/api/v1/config/app-config")
      return {
        data: { enableLogin: true, isAdmin: true, enableAnalytics: false },
      };
    if (url === "/api/v1/proprietary/ui-data/account")
      return {
        data: { username: "admin", changeCredsFlag: true, settings: "{}" },
      };
    if (url === "/api/v1/proprietary/ui-data/login")
      return { data: { showDefaultCredentials: true } };
    throw new Error(`Unexpected request: ${url}`);
  });
  const view = render(
    <MemoryRouter initialEntries={["/processor/policies"]}>
      <MantineProvider>
        <PlatformProviders>
          <PortalProviders />
        </PlatformProviders>
      </MantineProvider>
    </MemoryRouter>,
  );
  expect(
    await screen.findByRole("dialog", { name: "first-login" }),
  ).toBeVisible();
  expect(screen.getByText("Processor")).toBeVisible();
  view.unmount();
});