CopyToClipboard.js 4.2 KB

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