Javascript 如何测试剪贴板?
我有这个功能将当前url复制到剪贴板Javascript 如何测试剪贴板?,javascript,reactjs,jestjs,enzyme,clipboard,Javascript,Reactjs,Jestjs,Enzyme,Clipboard,我有这个功能将当前url复制到剪贴板 const copyToClipboard = (str) => { const tmpElm = document.createElement('textarea'); tmpElm.value = window.location.href; tmpElm.setAttribute('readonly', ''); tmpElm.style.position = 'absolute'; tmpElm.style.left = '-
const copyToClipboard = (str) => {
const tmpElm = document.createElement('textarea');
tmpElm.value = window.location.href;
tmpElm.setAttribute('readonly', '');
tmpElm.style.position = 'absolute';
tmpElm.style.left = '-9999px';
document.body.appendChild(tmpElm);
tmpElm.select();
document.execCommand('copy');
document.body.removeChild(tmpElm);
};
如何准确地测试此函数?(使用像jest这样的测试框架,它不嵌套在浏览器中)。我认为你能做的最简单的测试是存根你的
execComand
fn
document.execCommand = jest.fn();
然后您可以编写一个测试,如
expect(document.execCommand).toHaveBeenCalledWith("copy");