Skip to content

The platform for continuously improving AI agents.

2.4 KB
Raw Blame
// @vitest-environment jsdom
// covers: console-logic
import { cleanup, fireEvent, render, screen, within } from "@testing-library/react";
import { afterEach, expect, it, vi } from "vitest";

import type { DatasetUpload } from "@/hooks/use-uploads";
import { FileImport } from "./file-import";

afterEach(cleanup);

function entry(name: string, patch: Partial<DatasetUpload> = {}): DatasetUpload {
  return {
    file: new File(["a;b\n1;2\n"], name),
    id: name,
    inspection: {
      bytes: 8,
      columns: ["a", "b"],
      filename: name,
      options: { delimiter: "semicolon", encoding: "utf-8-sig", header: true },
      preview: [{ a: "1", b: "2" }],
      rows: 1,
      sheets: [],
    },
    percent: 100,
    reviewed: false,
    rows: 1,
    status: "ready",
    uploadId: "upload",
    ...patch,
  };
}

function show(upload: DatasetUpload) {
  const handlers = { onContinue: vi.fn(), onRead: vi.fn(), onRemove: vi.fn(), onRetry: vi.fn() };
  render(<FileImport entry={upload} {...handlers} />);
  return handlers;
}

it("previews the first rows and is continued once", () => {
  const handlers = show(entry("menu.csv"));
  const card = screen.getByRole("region", { name: "Import menu.csv" });
  expect(within(card).getByRole("columnheader", { name: "a" })).toBeTruthy();
  expect(within(card).getByText("1 rows")).toBeTruthy();
  fireEvent.click(within(card).getByRole("button", { name: "Continue" }));
  expect(handlers.onContinue).toHaveBeenCalledOnce();
});

it("rereads a delimited file with the chosen separator, keeping its other options", () => {
  const handlers = show(entry("menu.csv"));
  fireEvent.click(screen.getByRole("button", { name: "How the file is read: Semicolon · UTF-8" }));
  fireEvent.click(screen.getByRole("radio", { name: "Pipe" }));
  expect(handlers.onRead).toHaveBeenCalledWith({
    delimiter: "pipe",
    encoding: "utf-8-sig",
    header: true,
  });
});

it("reads every sheet of a workbook until one sheet is chosen", () => {
  const book = entry("book.xlsx");
  book.inspection = { ...book.inspection!, options: { header: true }, sheets: ["orders", "notes"] };
  show(book);
  expect(screen.getByRole("button", { name: "How the file is read: All sheets" })).toBeTruthy();
});

it("shows Ready instead of Continue once reviewed", () => {
  show(entry("menu.csv", { reviewed: true }));
  expect(screen.getByText("Ready")).toBeTruthy();
  expect(screen.queryByRole("button", { name: "Continue" })).toBeNull();
});