CopyToClipboard.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. 'use strict';
  2. var 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', function (format, text) {
  7. var clean = jsdom();
  8. $('body').html(
  9. '<div id="placeholder" class="hidden">+++ no paste text ' +
  10. '+++</div><div id="prettymessage" class="hidden">' +
  11. '<button type="button" id="prettyMessageCopyBtn"><svg id="copyIcon"></svg>' +
  12. '<svg id="copySuccessIcon"></svg></button><pre ' +
  13. 'id="prettyprint" class="prettyprint linenums:1"></pre>' +
  14. '</div><div id="plaintext" class="hidden"></div>'
  15. );
  16. $.PrivateBin.PasteViewer.init();
  17. $.PrivateBin.PasteViewer.setFormat(format);
  18. $.PrivateBin.PasteViewer.setText(text);
  19. $.PrivateBin.PasteViewer.run();
  20. $.PrivateBin.CopyToClipboard.init();
  21. $.PrivateBin.CopyToClipboard.enableTestMode();
  22. $('#prettyMessageCopyBtn').trigger('click');
  23. const copiedText = $.PrivateBin.CopyToClipboard.readFromClipboard();
  24. clean();
  25. return text === copiedText;
  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', function (format, text) {
  32. var clean = jsdom();
  33. $('body').html(
  34. '<div id="placeholder">+++ no paste text ' +
  35. '+++</div><div id="prettymessage" class="hidden">' +
  36. '<button type="button" id="prettyMessageCopyBtn"><svg id="copyIcon"></svg>' +
  37. '<svg id="copySuccessIcon"></svg></button><pre ' +
  38. 'id="prettyprint" class="prettyprint linenums:1"></pre>' +
  39. '</div><div id="plaintext" class="hidden"></div>'
  40. );
  41. $.PrivateBin.PasteViewer.init();
  42. $.PrivateBin.PasteViewer.setFormat(format);
  43. $.PrivateBin.PasteViewer.setText(text);
  44. $.PrivateBin.PasteViewer.run();
  45. $.PrivateBin.CopyToClipboard.init();
  46. $.PrivateBin.CopyToClipboard.enableTestMode();
  47. $('body').trigger('copy');
  48. const copiedTextWithoutSelectedText = $.PrivateBin.CopyToClipboard.readFromClipboard();
  49. clean();
  50. return copiedTextWithoutSelectedText === text;
  51. });
  52. });
  53. jsc.property('Copy link to clipboard', 'nestring', function (text) {
  54. var clean = jsdom();
  55. $('body').html('<button id="copyLink"></button>');
  56. $.PrivateBin.CopyToClipboard.init();
  57. $.PrivateBin.CopyToClipboard.enableTestMode();
  58. $.PrivateBin.CopyToClipboard.setUrl(text);
  59. $('#copyLink').trigger('click');
  60. const copiedText = $.PrivateBin.CopyToClipboard.readFromClipboard();
  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. });