Skip to content

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

3.5 KB
Raw Blame
import { beforeEach, expect, it, vi } from "vitest";
import { act, fireEvent, render, screen } from "@testing-library/react";
import { MantineProvider } from "@mantine/core";
import TeamSection from "@editor/components/shared/config/configSections/TeamSection";

const { post, refreshTeams, refreshSession, teamState } = vi.hoisted(() => ({
  teamState: { available: true, loading: false },
  post: vi.fn(),
  refreshTeams: vi.fn().mockResolvedValue(undefined),
  refreshSession: vi.fn().mockResolvedValue(undefined),
}));

vi.mock("@editor/services/apiClient", () => ({ default: { post } }));
vi.mock("@editor/auth/teamSession", () => ({
  useTeamAuth: () => ({ refreshAfterMembershipChange: refreshSession }),
}));
vi.mock("@editor/contexts/SaaSTeamContext", () => ({
  useSaaSTeam: () => ({
    currentTeam: teamState.available
      ? { teamId: 9, name: "Acme", seatsUsed: 2 }
      : null,
    loading: teamState.loading,
    teamMembers: [
      { id: 1, username: "Alex", email: "[email protected]", role: "LEADER" },
      { id: 2, username: "Jamie", email: "[email protected]", role: "MEMBER" },
    ],
    teamInvitations: [],
    isTeamLeader: true,
    isPersonalTeam: false,
    refreshTeams,
  }),
}));
vi.mock("react-i18next", async (importOriginal) => {
  const actual = await importOriginal<typeof import("react-i18next")>();
  const { createInstance } = await import("i18next");
  const i18n = createInstance();
  await i18n.use(actual.initReactI18next).init({
    lng: "en",
    resources: {},
    interpolation: { escapeValue: false },
  });
  return { ...actual, useTranslation: () => ({ t: i18n.t, i18n }) };
});

beforeEach(() => {
  vi.clearAllMocks();
  teamState.available = true;
  teamState.loading = false;
  post.mockResolvedValue({
    data: {
      teamId: 9,
      teamName: "Acme",
      leaderUserId: 1,
      targetUserId: 2,
      linkedInstances: 0,
      subscribed: true,
      state: "READY",
    },
  });
});

it("keeps the SaaS team settings receipt open until the owner acknowledges it", async () => {
  const view = render(
    <MantineProvider>
      <TeamSection />
    </MantineProvider>,
  );
  refreshSession.mockImplementationOnce(async () => view.unmount());
  fireEvent.click(screen.getByRole("button", { name: "Member actions" }));
  fireEvent.click(await screen.findByRole("menuitem", { name: "Make owner" }));
  fireEvent.click(await screen.findByRole("checkbox"));
  await act(async () => {
    fireEvent.click(screen.getByRole("button", { name: "Transfer ownership" }));
  });
  expect(post).toHaveBeenCalledWith("/api/v1/team/9/ownership/transfer", {
    email: "[email protected]",
    expectedLeaderId: 1,
  });
  expect(screen.getByText("Ownership transferred")).toBeVisible();
  expect(refreshSession).not.toHaveBeenCalled();
  await act(async () => {
    fireEvent.click(screen.getByRole("button", { name: "Done" }));
  });
  expect(refreshSession).toHaveBeenCalledOnce();
  expect(screen.queryByRole("dialog")).toBeNull();
});

it("shows a retry instead of endless loading when no current team is identified", async () => {
  teamState.available = false;
  render(
    <MantineProvider>
      <TeamSection />
    </MantineProvider>,
  );
  expect(screen.getByText(/couldn't identify your current team/)).toBeVisible();
  expect(screen.queryByText("Loading team information...")).toBeNull();
  refreshTeams.mockClear();
  await act(async () => {
    fireEvent.click(screen.getByRole("button", { name: "Try again" }));
  });
  expect(refreshTeams).toHaveBeenCalledOnce();
  expect(post).not.toHaveBeenCalled();
});