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.2 KB
Raw Blame
// @vitest-environment happy-dom

import { createApp, nextTick } from "vue";
import { createI18n } from "vue-i18n";
import { afterEach, describe, expect, it } from "vitest";
import ErrorBanner from "@/components/ui/ErrorBanner.vue";
import { eventTargetAllowsNativeClipboard } from "@/lib/common/clipboard";

const mountedContainers: HTMLDivElement[] = [];

function findNativeClipboardRegion(container: HTMLElement, text: string): HTMLElement {
  const region = Array.from(container.querySelectorAll<HTMLElement>("[data-native-clipboard]")).find((element) => element.textContent?.trim() === text);
  if (!region) throw new Error(`Native clipboard region not found: ${text}`);
  return region;
}

// The centered variant's clipboard region wraps title + message in one node,
// so match by containment instead of exact text.
function findClipboardRegionContaining(container: HTMLElement, text: string): HTMLElement {
  const region = Array.from(container.querySelectorAll<HTMLElement>("[data-native-clipboard]")).find((element) => element.textContent?.includes(text));
  if (!region) throw new Error(`Native clipboard region not found containing: ${text}`);
  return region;
}

function selectRegion(region: HTMLElement) {
  const selection = window.getSelection();
  const range = document.createRange();
  range.selectNodeContents(region);
  selection?.removeAllRanges();
  selection?.addRange(range);
}

async function mountBanner(variant: "card" | "centered", message: string) {
  const container = document.createElement("div");
  mountedContainers.push(container);
  document.body.append(container);

  const app = createApp(ErrorBanner, {
    variant,
    title: "Save failed",
    message,
  });
  app.use(
    createI18n({
      legacy: false,
      locale: "en",
      messages: { en: { grid: { copy: "Copy", dismiss: "Dismiss" } } },
      missingWarn: false,
      fallbackWarn: false,
    }),
  );
  app.mount(container);
  await nextTick();
  return container;
}

afterEach(() => {
  window.getSelection()?.removeAllRanges();
  for (const container of mountedContainers.splice(0)) container.remove();
});

describe("ErrorBanner native clipboard selection", () => {
  it.each(["Save failed", "Conditional request failed"])("preserves native copy for selected %s text", async (text) => {
    const container = await mountBanner("card", "Conditional request failed");
    selectRegion(findNativeClipboardRegion(container, text));

    expect(
      eventTargetAllowsNativeClipboard({
        key: "c",
        metaKey: true,
        target: container,
      }),
    ).toBe(true);
  });

  it("preserves native copy for selected centered error message", async () => {
    const container = await mountBanner("centered", "fatal error\n\nlong detail line");
    selectRegion(findClipboardRegionContaining(container, "long detail line"));

    expect(
      eventTargetAllowsNativeClipboard({
        key: "c",
        metaKey: true,
        target: container,
      }),
    ).toBe(true);
  });

  it("leaves grid copy shortcuts active without a text selection", async () => {
    const container = await mountBanner("card", "Conditional request failed");

    expect(
      eventTargetAllowsNativeClipboard({
        key: "c",
        metaKey: true,
        target: container,
      }),
    ).toBe(false);
  });
});