import { describe, expect, test } from "vitest";
import { isTextCutOff } from "./truncation";

/** One element of a header as the check sees it: how wide its content is, its box, its overflow. */
const box = (scrollWidth: number, clientWidth: number, overflowX: string) => ({
  scrollWidth,
  clientWidth,
  overflowX,
});

/**
 * Column headers (ENG-6953 point 35): the full header shows in a tooltip only while part of it is cut
 * off, whatever the column's own header renders.
 */
describe("isTextCutOff", () => {
  test("a header whose truncating title is wider than its box is cut off", () => {
    // The cell's wrapper, the header's flex row, and its `truncate` title.
    expect(isTextCutOff([box(200, 200, "visible"), box(200, 200, "visible"), box(380, 176, "hidden")])).toBe(
      true
    );
  });

  test("a title that fits is not cut off", () => {
    expect(isTextCutOff([box(200, 200, "visible"), box(200, 200, "visible"), box(120, 176, "hidden")])).toBe(
      false
    );
  });

  test("text that spills out visibly is not cut off: it can be read", () => {
    expect(isTextCutOff([box(200, 120, "visible"), box(200, 120, "visible")])).toBe(false);
  });

  test("an empty header is not cut off", () => {
    expect(isTextCutOff([])).toBe(false);
  });

  test("text clamped to a few lines and taller than its box is cut off", () => {
    expect(
      isTextCutOff([
        {
          scrollWidth: 200,
          clientWidth: 200,
          overflowX: "hidden",
          scrollHeight: 60,
          clientHeight: 40,
          overflowY: "hidden",
        },
      ])
    ).toBe(true);
  });

  test("a pixel of descender below a line is not cut off", () => {
    expect(
      isTextCutOff([
        {
          scrollWidth: 200,
          clientWidth: 200,
          overflowX: "hidden",
          scrollHeight: 41,
          clientHeight: 40,
          overflowY: "hidden",
        },
      ])
    ).toBe(false);
  });
});
