droboticflow/tests/frontend/clipboard.test.mjs

80 lines
1.8 KiB
JavaScript

import test from "node:test";
import assert from "node:assert/strict";
import { copyText } from "../../src/lib/clipboard.js";
test("uses clipboard API when available", async () => {
let copied = "";
const result = await copyText("payload-path", {
navigator: {
clipboard: {
async writeText(text) {
copied = text;
},
},
},
});
assert.equal(result, true);
assert.equal(copied, "payload-path");
});
test("falls back to execCommand when clipboard API is unavailable", async () => {
let execCommandCalls = 0;
let appendedNode = null;
let removedNode = null;
const textarea = {
value: "",
style: {},
setAttribute(name, value) {
this[name] = value;
},
focusCalled: false,
selectCalled: false,
selectionRange: null,
focus() {
this.focusCalled = true;
},
select() {
this.selectCalled = true;
},
setSelectionRange(start, end) {
this.selectionRange = [start, end];
},
};
const result = await copyText("artifact-path", {
navigator: {},
document: {
createElement(tagName) {
assert.equal(tagName, "textarea");
return textarea;
},
body: {
appendChild(node) {
appendedNode = node;
},
removeChild(node) {
removedNode = node;
},
},
execCommand(command) {
execCommandCalls += 1;
assert.equal(command, "copy");
return true;
},
},
});
assert.equal(result, true);
assert.equal(execCommandCalls, 1);
assert.equal(textarea.value, "artifact-path");
assert.equal(textarea.focusCalled, true);
assert.equal(textarea.selectCalled, true);
assert.deepEqual(textarea.selectionRange, [0, "artifact-path".length]);
assert.equal(appendedNode, textarea);
assert.equal(removedNode, textarea);
});