CopyToClipboard.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. 'use strict';
  2. const common = require('../common');
  3. describe('CopyToClipboard', function() {
  4. this.timeout(30000);
  5. describe ('Copy document to clipboard', function () {
  6. jsc.property('Copy with button click',
  7. common.jscFormats(),
  8. 'nestring',
  9. async function (format, text) {
  10. var clean = globalThis.cleanup();
  11. common.enableClipboard();
  12. document.body.innerHTML = (
  13. '<div id="placeholder" class="hidden">+++ no document text ' +
  14. '+++</div><div id="prettymessage" class="hidden">' +
  15. '<button type="button" id="prettyMessageCopyBtn"><svg id="copyIcon"></svg>' +
  16. '<svg id="copySuccessIcon"></svg></button><pre ' +
  17. 'id="prettyprint" class="prettyprint linenums:1"></pre>' +
  18. '</div><div id="plaintext" class="hidden"></div>'
  19. );
  20. PrivateBin.PasteViewer.init();
  21. PrivateBin.PasteViewer.setFormat(format);
  22. PrivateBin.PasteViewer.setText(text);
  23. PrivateBin.PasteViewer.run();
  24. PrivateBin.CopyToClipboard.init();
  25. document.getElementById('prettyMessageCopyBtn').click();
  26. const savedToClipboardText = await navigator.clipboard.readText();
  27. clean();
  28. return text === savedToClipboardText;
  29. }
  30. );
  31. /**
  32. * Unfortunately in JSVerify impossible to check if copy with shortcut when user selected some text on the page
  33. * (the copy document to clipboard should not work in this case) due to lacking window.getSelection() in jsdom.
  34. */
  35. jsc.property('Copy with keyboard shortcut',
  36. common.jscFormats(),
  37. 'nestring',
  38. async function (format, text) {
  39. var clean = globalThis.cleanup();
  40. common.enableClipboard();
  41. document.body.innerHTML = (
  42. '<div id="placeholder">+++ no document text ' +
  43. '+++</div><div id="prettymessage" class="hidden">' +
  44. '<button type="button" id="prettyMessageCopyBtn"><svg id="copyIcon"></svg>' +
  45. '<svg id="copySuccessIcon"></svg></button><pre ' +
  46. 'id="prettyprint" class="prettyprint linenums:1"></pre>' +
  47. '</div><div id="plaintext" class="hidden"></div>'
  48. );
  49. PrivateBin.PasteViewer.init();
  50. PrivateBin.PasteViewer.setFormat(format);
  51. PrivateBin.PasteViewer.setText(text);
  52. PrivateBin.PasteViewer.run();
  53. PrivateBin.CopyToClipboard.init();
  54. document.body.dispatchEvent(new Event('copy'));
  55. const copiedTextWithoutSelectedText = await navigator.clipboard.readText();
  56. clean();
  57. return copiedTextWithoutSelectedText === text;
  58. }
  59. );
  60. });
  61. jsc.property('Copy link to clipboard',
  62. 'nestring',
  63. async function (text) {
  64. var clean = globalThis.cleanup();
  65. common.enableClipboard();
  66. document.body.innerHTML = '<button id="copyLink"></button>';
  67. PrivateBin.CopyToClipboard.init();
  68. PrivateBin.CopyToClipboard.setUrl(text);
  69. document.getElementById('copyLink').click();
  70. const copiedText = await navigator.clipboard.readText();
  71. clean();
  72. return text === copiedText;
  73. }
  74. );
  75. describe('Keyboard shortcut hint', function () {
  76. jsc.property('Show hint',
  77. 'nestring',
  78. function (text) {
  79. var clean = globalThis.cleanup();
  80. document.body.innerHTML = '<small id="copyShortcutHintText"></small>';
  81. PrivateBin.CopyToClipboard.init();
  82. PrivateBin.CopyToClipboard.showKeyboardShortcutHint();
  83. const keyboardShortcutHint = document.getElementById('copyShortcutHintText').textContent;
  84. clean();
  85. return keyboardShortcutHint.length > 0;
  86. }
  87. );
  88. jsc.property('Hide hint',
  89. 'nestring',
  90. function (text) {
  91. var clean = globalThis.cleanup();
  92. document.body.innerHTML = '<small id="copyShortcutHintText">' + text + '</small>';
  93. PrivateBin.CopyToClipboard.init();
  94. PrivateBin.CopyToClipboard.hideKeyboardShortcutHint();
  95. const keyboardShortcutHint = document.getElementById('copyShortcutHintText').textContent;
  96. clean();
  97. return keyboardShortcutHint.length === 0;
  98. }
  99. );
  100. });
  101. });