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.5 KB
Raw Blame
// @vitest-environment happy-dom
import { createApp, defineComponent, h, nextTick, reactive } from "vue";
import { createI18n } from "vue-i18n";
import { afterEach, describe, expect, it } from "vitest";
import QueryResultViewSwitcher from "@/components/layout/QueryResultViewSwitcher.vue";
import type { RedisResultViewMode, TabOutputView } from "@/types/database";

const mounted: Array<{ unmount: () => void; host: HTMLElement }> = [];

afterEach(() => {
  for (const { unmount, host } of mounted.splice(0)) {
    unmount();
    host.remove();
  }
});

async function mountSwitcher(canShowRedisConsole: boolean, canShowGraph = false) {
  const state = reactive<{ view: TabOutputView; mode?: RedisResultViewMode }>({ view: "result" });
  const host = document.createElement("div");
  document.body.appendChild(host);
  const app = createApp(
    defineComponent({
      setup: () => () =>
        h(QueryResultViewSwitcher, {
          activeView: state.view,
          canShowResult: true,
          canShowSummary: false,
          canShowChart: false,
          canShowMessages: false,
          canShowRedisConsole,
          canShowGraph,
          resultMode: state.mode,
          onSelectView: (view: TabOutputView) => {
            state.view = view;
          },
          onSelectResultMode: (mode: RedisResultViewMode) => {
            state.mode = mode;
          },
        }),
    }),
  );
  app.use(
    createI18n({
      legacy: false,
      locale: "en",
      messages: { en: { tabs: { tableData: "Table Data", executionSummary: "Summary", messages: "Messages" }, chart: { title: "Chart" }, graph: { title: "Graph" }, redis: { commandLine: "Command line" } } },
    }),
  );
  app.mount(host);
  mounted.push({ unmount: () => app.unmount(), host });
  await nextTick();
  return { host, state };
}

describe("QueryResultViewSwitcher Redis mode", () => {
  it("defaults Redis results to the grid and switches through the existing toolbar contract", async () => {
    const { host, state } = await mountSwitcher(true);
    const grid = [...host.querySelectorAll<HTMLButtonElement>("button")].find((button) => button.textContent?.includes("Table Data"));
    const console = host.querySelector<HTMLButtonElement>('button[aria-label="Command line"]');

    expect(state.mode).toBeUndefined();
    expect(grid?.getAttribute("aria-pressed")).toBe("true");
    expect(console?.getAttribute("aria-pressed")).toBe("false");

    console?.click();
    await nextTick();

    expect(state.mode).toBe("console");
    expect(state.view).toBe("result");
    expect(console?.getAttribute("aria-pressed")).toBe("true");

    grid?.click();
    await nextTick();
    expect(state.mode).toBe("grid");
    expect(grid?.getAttribute("aria-pressed")).toBe("true");
  });

  it("does not expose the Redis-only mode on generic result toolbars", async () => {
    const { host } = await mountSwitcher(false);
    expect(host.querySelector('button[aria-label="Command line"]')).toBeNull();
  });

  it("shows the graph view only for graph results and switches back to the table", async () => {
    const { host, state } = await mountSwitcher(false, true);
    const graph = host.querySelector<HTMLButtonElement>('button[aria-label="Graph"]');
    expect(graph).not.toBeNull();
    graph?.click();
    await nextTick();
    expect(state.view).toBe("graph");
    expect(graph?.getAttribute("aria-pressed")).toBe("true");
    host.querySelector<HTMLButtonElement>("button")?.click();
    await nextTick();
    expect(state.view).toBe("result");
  });
});