Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

2.3 KB
Raw Blame
import { render, screen } from "@testing-library/react";
import { beforeEach, describe, expect, test, vi } from "vitest";
import { RequestExportButton } from "./RequestExportButton";

const mocks = vi.hoisted(() => ({
  loadSubscription: vi.fn(),
  allowJustInTimeAccess: true,
}));

vi.mock("react-i18next", () => ({
  useTranslation: () => ({ t: (key: string) => key }),
}));

vi.mock("@/components/FeatureBadge", () => ({
  FeatureBadge: () => null,
}));

vi.mock("@/components/PermissionGuard", () => ({
  PermissionGuard: ({
    children,
  }: {
    children: (props: { disabled: boolean }) => React.ReactNode;
  }) => <>{children({ disabled: false })}</>,
}));

vi.mock("@/hooks/useAppProject", () => ({
  useAppProject: () => ({
    name: "projects/proj1",
    allowJustInTimeAccess: mocks.allowJustInTimeAccess,
  }),
}));

vi.mock("@/modules/sql-editor/store/editor", () => ({
  useSQLEditorEditorState: (selector: (state: { project: string }) => unknown) =>
    selector({ project: "projects/proj1" }),
}));

vi.mock("@/stores/app", () => ({
  useAppStore: (
    selector: (state: {
      loadSubscription: typeof mocks.loadSubscription;
      hasFeature: () => boolean;
    }) => unknown
  ) =>
    selector({
      loadSubscription: mocks.loadSubscription,
      hasFeature: () => true,
    }),
}));

vi.mock("./AccessGrantRequestDrawer", () => ({
  AccessGrantRequestDrawer: () => null,
}));

vi.mock("./RequestDrawerHost", () => ({
  useRequestDrawerHost: () => undefined,
}));

describe("RequestExportButton", () => {
  beforeEach(() => {
    mocks.allowJustInTimeAccess = true;
  });

  test("labels the export action instead of the access-grant mechanism", () => {
    render(
      <RequestExportButton
        statement="SELECT * FROM t"
        targets={["instances/i/databases/d"]}
      />
    );

    expect(screen.getByRole("button")).toHaveTextContent(
      "sql-editor.request-export"
    );
    expect(screen.getByRole("button")).not.toHaveTextContent(
      "sql-editor.request-access-grant"
    );
  });

  test("is hidden when the project does not allow just-in-time access", () => {
    mocks.allowJustInTimeAccess = false;
    const { container } = render(
      <RequestExportButton
        statement="SELECT * FROM t"
        targets={["instances/i/databases/d"]}
      />
    );

    expect(container).toBeEmptyDOMElement();
  });
});