Skip to content

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

3.5 KB
Raw Blame
// @vitest-environment node
import { describe, it, expect } from "vitest";
import type {
  ConfigNavSection,
  NavKey,
} from "@editor/components/shared/config/types";
import {
  buildPortalSettingsSections,
  portalSupersededSectionKeys,
} from "@editor/components/settings/portalSettingsNav";
import { mergeSettingsGroups } from "@editor/components/settings/mergeSettingsGroups";

const group = (
  id: string,
  keys: NavKey[],
  mergeAt?: "lead" | "append",
): ConfigNavSection => ({
  id,
  title: id,
  mergeAt,
  items: keys.map((key) => ({ key, label: key, icon: "x", component: null })),
});

const translate = ((_: string, fallback: string) => fallback) as never;

describe("buildPortalSettingsSections", () => {
  it("keeps independently enabled processor sections when the roster is hidden", () => {
    const sections = buildPortalSettingsSections(translate, {
      includeRoster: false,
      includeBilling: true,
      includeAccountLink: false,
      includeApiKeys: false,
    });

    expect(
      sections.flatMap((section) => section.items.map((item) => item.key)),
    ).toEqual(["billing"]);
  });
});

describe("portalSupersededSectionKeys", () => {
  it("retires People and Teams once the roster is served", () => {
    const keys = portalSupersededSectionKeys([group("workspace", ["users"])]);
    expect(keys).toEqual(expect.arrayContaining(["people", "teams", "users"]));
  });

  it("keeps the build's own API Keys when the processor supplies none", () => {
    // A build without the processor gets the roster but not the keys tab, and
    // dropping a key nothing replaces would delete the section outright.
    expect(
      portalSupersededSectionKeys([group("workspace", ["users"])]),
    ).not.toContain("api-keys");
  });

  it("retires the build's API Keys only when the processor's is present", () => {
    const keys = portalSupersededSectionKeys([
      group("workspace", ["users"]),
      group("preferences", ["api-keys"]),
    ]);
    expect(keys).toContain("api-keys");
  });

  it("retires legacy plan sections only when billing is present", () => {
    expect(
      portalSupersededSectionKeys([group("workspace", ["users"])]),
    ).not.toEqual(expect.arrayContaining(["plan", "adminPlan"]));

    const keys = portalSupersededSectionKeys([
      group("workspace", ["users", "billing"]),
    ]);
    expect(keys).toEqual(expect.arrayContaining(["plan", "adminPlan"]));
  });

  it("supersedes nothing when no roster was built", () => {
    expect(portalSupersededSectionKeys([])).toEqual([]);
  });
});

describe("mergeSettingsGroups", () => {
  it("drops a superseded section rather than showing both rosters", () => {
    const merged = mergeSettingsGroups(
      [group("workspace", ["people", "teams", "adminPlan"])],
      [group("workspace", ["users"])],
      portalSupersededSectionKeys([group("workspace", ["users"])]),
    );
    expect(merged[0].items.map((i) => i.key)).toEqual(["users", "adminPlan"]);
  });

  it("appends where the group asks, so an addition lands after what it joins", () => {
    const merged = mergeSettingsGroups(
      [group("preferences", ["general"])],
      [group("preferences", ["api-keys"], "append")],
      [],
    );
    expect(merged[0].items.map((i) => i.key)).toEqual(["general", "api-keys"]);
  });

  it("leads by default, so a replacement holds the place it took over", () => {
    const merged = mergeSettingsGroups(
      [group("workspace", ["people", "adminPlan"])],
      [group("workspace", ["users"])],
      ["people"],
    );
    expect(merged[0].items.map((i) => i.key)).toEqual(["users", "adminPlan"]);
  });
});