Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

6.2 KB
Raw Blame
import { describe, expect, it } from "vitest";
import { formatGridSnapshotCell, gridSnapshotMetadataControlState, renderGridSnapshotHtml } from "@/lib/gridSnapshot/gridSnapshot";

describe("grid snapshot rendering", () => {
  it("enables field metadata only when field names are visible", () => {
    expect(gridSnapshotMetadataControlState({ showFieldNames: true, hasColumnTypes: true, hasColumnDetails: true })).toEqual({ columnTypesDisabled: false, columnDetailsDisabled: false });
    expect(gridSnapshotMetadataControlState({ showFieldNames: false, hasColumnTypes: true, hasColumnDetails: true })).toEqual({ columnTypesDisabled: true, columnDetailsDisabled: true });
    expect(gridSnapshotMetadataControlState({ showFieldNames: true, hasColumnTypes: false, hasColumnDetails: false })).toEqual({ columnTypesDisabled: true, columnDetailsDisabled: true });
  });

  it("formats primitive values and NULL consistently", () => {
    expect(formatGridSnapshotCell(null)).toBe("NULL");
    expect(formatGridSnapshotCell(true)).toBe("true");
    expect(formatGridSnapshotCell(42)).toBe("42");
  });

  it("renders an escaped, self-contained table with optional headers and row numbers", () => {
    const html = renderGridSnapshotHtml(
      {
        columns: ["id", "name"],
        columnTypes: ["integer", "varchar"],
        columnDetails: ["Primary key", "Display name"],
        rows: [
          [1, "<Ada>"],
          [2, null],
        ],
        title: "Users",
      },
      { appearance: "light", showTrafficLights: false, showFieldNames: true, showRowNumbers: true },
    );
    expect(html).toContain("dbx-grid-snapshot");
    expect(html).toContain("&lt;Ada&gt;");
    expect(html).toContain(">NULL</td>");
    expect(html).toContain(">#</th>");
    expect(html).toContain(">1</td>");
    expect(html).not.toContain("#ff5f57");
    expect(html).not.toContain("integer");
    expect(html).not.toContain("Primary key");
    expect(html).toContain("width: max-content");
    expect(html).toContain("border-collapse: separate");
    expect(html).not.toMatch(/<td[^>]*dbx-grid-snapshot__cell--wrapped/);
  });

  it("optionally renders column types and details below field names", () => {
    const html = renderGridSnapshotHtml(
      {
        columns: ["id"],
        columnTypes: ["integer"],
        columnDetails: ["Primary key"],
        rows: [[1]],
      },
      { appearance: "dark", showColumnTypes: true, showColumnDetails: true, wrapCells: true },
    );
    expect(html).toContain("dbx-grid-snapshot__column-meta");
    expect(html).toContain(">integer</span>");
    expect(html).toContain(">Primary key</span>");
    expect(html).toMatch(/<td[^>]*dbx-grid-snapshot__cell--wrapped/);
  });

  it("can render a compact data-only view", () => {
    const html = renderGridSnapshotHtml({ columns: ["value"], rows: [["x"]] }, { appearance: "dark", showFieldNames: false, showRowNumbers: false, compact: true });
    expect(html).not.toContain("<thead>");
    expect(html).not.toMatch(/<td[^>]*dbx-grid-snapshot__cell--row-number/);
    expect(html).toContain("font-size:12px");
  });

  it("transposes fields into rows while keeping field metadata attached", () => {
    const html = renderGridSnapshotHtml(
      {
        columns: ["id", "name"],
        columnTypes: ["integer", "varchar"],
        columnDetails: ["Primary key", "Display name"],
        rows: [
          [1, "Ada"],
          [2, "Bob"],
        ],
      },
      { appearance: "light", transpose: true, fieldNameLabel: "Field Names", showColumnTypes: true, showColumnDetails: true },
    );
    expect(html).toContain(">Field Names</span>");
    expect(html).toContain(">#1</span>");
    expect(html).toContain(">#2</span>");
    expect(html).toContain(">id</span>");
    expect(html).toContain(">integer</span>");
    expect(html).toContain(">Primary key</span>");
    expect(html).toContain(">Ada</td>");
    expect(html).toContain(">Bob</td>");
  });

  it("hides the field-name placement while preserving transposed row identifiers", () => {
    const source = {
      columns: ["id", "name"],
      columnTypes: ["integer", "varchar"],
      columnDetails: ["Primary key", "Display name"],
      rows: [
        [1, "Ada"],
        [2, "Bob"],
      ],
    };
    const transposed = renderGridSnapshotHtml(source, { appearance: "light", transpose: true, fieldNameLabel: "Field Names", showFieldNames: false, showColumnTypes: true, showColumnDetails: true, showRowNumbers: false });
    expect(transposed).toContain("<thead>");
    expect(transposed).toContain(">#1</span>");
    expect(transposed).toContain(">#2</span>");
    expect(transposed).not.toContain("Field Names");
    expect(transposed).not.toContain(">id</span>");
    expect(transposed).not.toContain(">name</span>");
    expect(transposed).not.toContain("integer");
    expect(transposed).not.toContain("Primary key");
    expect(transposed).toContain(">Ada</td>");
    expect(transposed).toContain(">Bob</td>");

    const regular = renderGridSnapshotHtml(source, { appearance: "light", showFieldNames: false, showColumnTypes: true, showColumnDetails: true });
    expect(regular).not.toContain("<thead>");
    expect(regular).not.toContain("integer");
    expect(regular).not.toContain("Primary key");
  });

  it("supports disabling striped row backgrounds", () => {
    const source = {
      columns: ["id"],
      rows: [[1], [2]],
    };
    const defaultHtml = renderGridSnapshotHtml(source, { appearance: "light" });
    expect(defaultHtml).toContain("background:rgb(240, 240, 240)");

    const unstripedHtml = renderGridSnapshotHtml(source, { appearance: "light", stripedRows: false });
    expect(unstripedHtml).not.toContain("background:rgb(240, 240, 240)");
  });

  it("defines explicit line-height and safe bottom padding on the last row to prevent clipping", () => {
    const source = {
      columns: ["id", "val"],
      rows: [
        [1, "first"],
        [2, "second"],
      ],
    };
    const standardHtml = renderGridSnapshotHtml(source, { appearance: "light" });
    expect(standardHtml).toContain("line-height: 1.4");
    expect(standardHtml).toContain("padding:4px 12px 6px");

    const compactHtml = renderGridSnapshotHtml(source, { appearance: "light", compact: true });
    expect(compactHtml).toContain("padding:3px 8px 5px");
  });
});