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。

3.1 KB
Raw Blame
// @vitest-environment happy-dom
import { createApp, nextTick } from "vue";
import { beforeEach, describe, expect, it, vi } from "vitest";

vi.mock("../ContentArea.vue", () => ({
  default: {
    name: "ContentAreaStub",
    props: ["activeTab", "activeConnection", "executableSql", "activeOutputView", "formatSqlRequest", "compressSqlRequest", "selectedSql", "cursorPos", "blockDangerousRedisCommands"],
    emits: ["previewStatement", "focusStatement", "reload"],
    template: `<div><button data-test="emit-preview" @click="$emit('previewStatement', 'tab-1', { from: 0, to: 8 })">preview</button><button data-test="emit-focus" @click="$emit('focusStatement', 'tab-1', { from: 0, to: 8 })">focus</button><button data-test="emit-reload" @click="$emit('reload', 'SELECT 1')">reload</button></div>`,
  },
}));

import QueryResultSurface from "../QueryResultSurface.vue";

function createHost(): HTMLDivElement {
  const host = document.createElement("div");
  document.body.appendChild(host);
  return host;
}

describe("QueryResultSurface mount contract", () => {
  beforeEach(() => {
    document.body.innerHTML = "";
  });

  it("forwards preview and focus statement events to the parent", async () => {
    const host = createHost();
    const onPreviewStatement = vi.fn();
    const onFocusStatement = vi.fn();

    const app = createApp(QueryResultSurface, {
      activeTab: {
        id: "tab-1",
        title: "SQL",
        connectionId: "conn-1",
        database: "db",
        sql: "SELECT 1",
        mode: "query",
      },
      activeConnection: undefined,
      executableSql: "SELECT 1",
      activeOutputView: "result",
      formatSqlRequest: null,
      compressSqlRequest: null,
      selectedSql: "",
      cursorPos: 0,
      blockDangerousRedisCommands: false,
      onPreviewStatement,
      onFocusStatement,
    });
    app.mount(host);
    await nextTick();

    host.querySelector<HTMLButtonElement>('[data-test="emit-preview"]')?.click();
    host.querySelector<HTMLButtonElement>('[data-test="emit-focus"]')?.click();
    await nextTick();

    expect(onPreviewStatement).toHaveBeenCalledWith("tab-1", { from: 0, to: 8 });
    expect(onFocusStatement).toHaveBeenCalledWith("tab-1", { from: 0, to: 8 });

    app.unmount();
    host.remove();
  });

  it("forwards generic reload events to the parent", async () => {
    const host = createHost();
    const onReload = vi.fn();

    const app = createApp(QueryResultSurface, {
      activeTab: {
        id: "tab-1",
        title: "SQL",
        connectionId: "conn-1",
        database: "db",
        sql: "SELECT 1",
        mode: "query",
      },
      activeConnection: undefined,
      executableSql: "SELECT 1",
      activeOutputView: "result",
      formatSqlRequest: null,
      compressSqlRequest: null,
      selectedSql: "",
      cursorPos: 0,
      blockDangerousRedisCommands: false,
      onReload,
    });
    app.mount(host);
    await nextTick();

    host.querySelector<HTMLButtonElement>('[data-test="emit-reload"]')?.click();
    await nextTick();

    expect(onReload).toHaveBeenCalledWith("SELECT 1");

    app.unmount();
    host.remove();
  });
});