CopyToClipboard.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. 'use strict';
  2. const common = require('../common');
  3. const fc = require('fast-check');
  4. describe('CopyToClipboard', function () {
  5. afterEach(() => {
  6. globalThis.cleanup();
  7. });
  8. describe('Copy document to clipboard', function () {
  9. jsc.property('Copy with button click',
  10. common.jscFormats(),
  11. 'nestring',
  12. async function (format, text) {
  13. common.enableClipboard();
  14. document.body.innerHTML = (
  15. '<div id="placeholder" class="hidden">+++ no document text ' +
  16. '+++</div><div id="prettymessage" class="hidden">' +
  17. '<button type="button" id="prettyMessageCopyBtn"><svg id="copyIcon"></svg>' +
  18. '<svg id="copySuccessIcon"></svg></button><pre ' +
  19. 'id="prettyprint" class="prettyprint linenums:1"></pre>' +
  20. '</div><div id="plaintext" class="hidden"></div>'
  21. );
  22. PrivateBin.PasteViewer.init();
  23. PrivateBin.PasteViewer.setFormat(format);
  24. PrivateBin.PasteViewer.setText(text);
  25. PrivateBin.PasteViewer.run();
  26. PrivateBin.CopyToClipboard.init();
  27. document.getElementById('prettyMessageCopyBtn').click();
  28. const savedToClipboardText = await navigator.clipboard.readText();
  29. return text === savedToClipboardText;
  30. }
  31. );
  32. /**
  33. * Unfortunately in JSVerify impossible to check if copy with shortcut when user selected some text on the page
  34. * (the copy document to clipboard should not work in this case) due to lacking window.getSelection() in jsdom.
  35. */
  36. jsc.property('Copy with keyboard shortcut',
  37. common.jscFormats(),
  38. 'nestring',
  39. async function (format, text) {
  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(getClipboardEvent());
  55. const copiedTextWithoutSelectedText = await navigator.clipboard.readText();
  56. return copiedTextWithoutSelectedText === text;
  57. }
  58. );
  59. /**
  60. * ClipboardEvent is not supported in jsdom yet, so this creates a mock event to trigger the copy event listener.
  61. *
  62. * See https://github.com/jsdom/jsdom/issues/1568
  63. *
  64. * @returns {ClipboardEvent}
  65. */
  66. function getClipboardEvent() {
  67. /** {@type ClipboardEvent} */
  68. const clipboardEvent = new Event('copy', {
  69. bubbles: true,
  70. cancelable: true,
  71. composed: true
  72. });
  73. clipboardEvent['clipboardData'] = {
  74. getData: function () {
  75. return '';
  76. }
  77. }
  78. return clipboardEvent;
  79. }
  80. });
  81. it('Copy link to clipboard', async function () {
  82. await fc.assert(fc.asyncProperty(fc.string(),
  83. async function (text) {
  84. common.enableClipboard();
  85. document.body.innerHTML = '<button id="copyLink"></button>';
  86. PrivateBin.Alert.init();
  87. PrivateBin.CopyToClipboard.init();
  88. PrivateBin.CopyToClipboard.setUrl(text);
  89. document.getElementById('copyLink').click();
  90. const copiedText = await navigator.clipboard.readText();
  91. return text === copiedText;
  92. })
  93. );
  94. });
  95. describe('Keyboard shortcut hint', function () {
  96. it('shows hint', () => {
  97. fc.assert(fc.property(fc.string(),
  98. function (text) {
  99. document.body.innerHTML = '<small id="copyShortcutHintText"></small>';
  100. PrivateBin.CopyToClipboard.init();
  101. PrivateBin.CopyToClipboard.showKeyboardShortcutHint();
  102. const keyboardShortcutHint = document.getElementById('copyShortcutHintText').textContent;
  103. return keyboardShortcutHint.length > 0;
  104. }
  105. ));
  106. });
  107. });
  108. jsc.property('Hide hint',
  109. 'nestring',
  110. function (text) {
  111. document.body.innerHTML = '<small id="copyShortcutHintText">' + text + '</small>';
  112. PrivateBin.CopyToClipboard.init();
  113. PrivateBin.CopyToClipboard.hideKeyboardShortcutHint();
  114. const keyboardShortcutHint = document.getElementById('copyShortcutHintText').textContent;
  115. return keyboardShortcutHint.length === 0;
  116. }
  117. );
  118. });