74 lines
2.3 KiB
TypeScript
74 lines
2.3 KiB
TypeScript
import { config, mount, flushPromises } from "@vue/test-utils";
|
|
import { describe, expect, it, vi, afterEach } from "vitest";
|
|
import { NConfigProvider, NMessageProvider, zhCN, dateZhCN } from "naive-ui";
|
|
import { defineComponent, h } from "vue";
|
|
import SecretOnceAlert from "./SecretOnceAlert.vue";
|
|
|
|
vi.mock("@/utils/notify", () => ({
|
|
message: {
|
|
success: vi.fn(),
|
|
error: vi.fn(),
|
|
warning: vi.fn(),
|
|
},
|
|
}));
|
|
|
|
import { message } from "@/utils/notify";
|
|
|
|
config.global.stubs = { teleport: true };
|
|
|
|
function wrap() {
|
|
return defineComponent({
|
|
setup() {
|
|
return () =>
|
|
h(NConfigProvider, { locale: zhCN, dateLocale: dateZhCN, size: "small" }, {
|
|
default: () =>
|
|
h(NMessageProvider, null, {
|
|
default: () =>
|
|
h(SecretOnceAlert, {
|
|
title: "令牌只显示一次",
|
|
secret: "nxm_abc",
|
|
filename: "api-token.txt",
|
|
}),
|
|
}),
|
|
});
|
|
},
|
|
});
|
|
}
|
|
|
|
describe("SecretOnceAlert 复制", () => {
|
|
afterEach(() => {
|
|
vi.mocked(message.success).mockClear();
|
|
vi.mocked(message.error).mockClear();
|
|
});
|
|
|
|
it("不支持 clipboard 时回退 execCommand 并提示成功", async () => {
|
|
Object.defineProperty(navigator, "clipboard", { value: undefined, configurable: true });
|
|
Object.defineProperty(document, "execCommand", { value: vi.fn(() => true), configurable: true });
|
|
|
|
const w = mount(wrap(), { attachTo: document.body });
|
|
await flushPromises();
|
|
await w.find('[data-testid="secret-copy"]').trigger("click");
|
|
await flushPromises();
|
|
|
|
expect(document.execCommand).toHaveBeenCalledWith("copy");
|
|
expect(message.success).toHaveBeenCalledWith("已复制");
|
|
w.unmount();
|
|
});
|
|
|
|
it("writeText 失败时提示错误", async () => {
|
|
Object.defineProperty(navigator, "clipboard", {
|
|
value: { writeText: vi.fn().mockRejectedValue(new Error("denied")) },
|
|
configurable: true,
|
|
});
|
|
Object.defineProperty(document, "execCommand", { value: vi.fn(() => false), configurable: true });
|
|
|
|
const w = mount(wrap(), { attachTo: document.body });
|
|
await flushPromises();
|
|
await w.find('[data-testid="secret-copy"]').trigger("click");
|
|
await flushPromises();
|
|
|
|
expect(message.error).toHaveBeenCalledWith("复制失败,请手动选择文本");
|
|
w.unmount();
|
|
});
|
|
});
|