繁体   English   中英

如何测试剪贴板?

[英]How can i test the 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 = '-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");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM