Skip to content

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

1.5 KB
Raw Blame
import { act, renderHook, waitFor } from "@testing-library/react";
import { expect, it, vi } from "vitest";
import { useGoogleDrivePicker } from "@editor/hooks/useGoogleDrivePicker";
import { allowConsole } from "@editor/tests/failOnConsole";

const service = vi.hoisted(() => ({
  initialize: vi.fn().mockResolvedValue(undefined),
  openPicker: vi.fn(),
}));
vi.mock("@editor/contexts/AppConfigContext", () => ({
  useAppConfig: () => ({ config: { googleDriveEnabled: true } }),
}));
vi.mock("@editor/services/googleDrivePickerService", () => ({
  extractGoogleDriveBackendConfig: () => ({}),
  isGoogleDriveConfigured: () => true,
  getGoogleDriveConfig: () => ({}),
  getGoogleDrivePickerService: () => service,
}));

it("clears a failed Drive attempt and can retry without reinitializing the service", async () => {
  allowConsole.error(/Google Drive picker error/);
  const file = new File(["PDF"], "Drive.pdf");
  service.openPicker
    .mockRejectedValueOnce(new Error("Access denied"))
    .mockResolvedValueOnce([file]);
  const { result } = renderHook(useGoogleDrivePicker);
  await waitFor(() => expect(result.current.isEnabled).toBe(true));
  await act(async () => {
    expect(await result.current.openPicker()).toEqual([]);
  });
  expect(result.current.error).toBe("Access denied");
  expect(result.current.isLoading).toBe(false);
  act(() => result.current.clearError());
  expect(result.current.error).toBeNull();
  await act(async () => {
    expect(await result.current.openPicker()).toEqual([file]);
  });
  expect(service.initialize).toHaveBeenCalledOnce();
});