CopyToClipboard unit tests

This commit is contained in:
Ribas160 2025-01-07 17:40:18 +02:00
parent 42b7b9060d
commit 6c651ea676
3 changed files with 172 additions and 8 deletions

View File

@ -5454,12 +5454,14 @@ jQuery.PrivateBin = (function($, RawDeflate) {
const CopyToClipboard = (function () {
const me = {};
let copyButton = $('#prettyMessageCopyBtn'),
copyLinkButton = $('#copyLink'),
copyIcon = $('#copyIcon'),
successIcon = $('#copySuccessIcon'),
shortcutHint = $('#copyShortcutHintText'),
url;
let copyButton,
copyLinkButton,
copyIcon,
successIcon,
shortcutHint,
url,
testMode,
testClipboard;
/**
* Handle copy to clipboard button click
@ -5540,7 +5542,11 @@ jQuery.PrivateBin = (function($, RawDeflate) {
* @function
*/
function saveToClipboard(text) {
navigator.clipboard.writeText(text);
if (testMode) {
testClipboard = text;
} else {
navigator.clipboard.writeText(text);
}
};
/**
@ -5606,6 +5612,35 @@ jQuery.PrivateBin = (function($, RawDeflate) {
url = newUrl;
};
/**
* Enable test mode, using for unit tests
*
* @name CopyToClipboard.testMode
* @function
*/
me.enableTestMode = function () {
testMode = true;
};
/**
* Read text from user's clipboard
*
* @name CopyToClipboard.readFromClipboard
* @function
* @returns {string}
*/
me.readFromClipboard = function () {
let clipboardData = "";
if (testMode) {
clipboardData = testClipboard;
} else {
clipboardData = navigator.clipboard.readText();
}
return clipboardData;
};
/**
* Initialize
*
@ -5613,6 +5648,12 @@ jQuery.PrivateBin = (function($, RawDeflate) {
* @function
*/
me.init = function() {
copyButton = $('#prettyMessageCopyBtn');
copyLinkButton = $('#copyLink');
copyIcon = $('#copyIcon');
successIcon = $('#copySuccessIcon');
shortcutHint = $('#copyShortcutHintText');
handleCopyButtonClick();
handleCopyLinkButtonClick();
handleKeyboardShortcut();

123
js/test/CopyToClipboard.js Normal file
View File

@ -0,0 +1,123 @@
'use strict';
var common = require('../common');
describe('CopyToClipboard', function() {
this.timeout(30000);
describe ('Copy paste co clipboard', function () {
jsc.property('Copy with button click', common.jscFormats(), 'nestring', function (format, text) {
var clean = jsdom();
$('body').html(
'<div id="placeholder" class="hidden">+++ no paste text ' +
'+++</div><div id="prettymessage" class="hidden">' +
'<button type="button" id="prettyMessageCopyBtn"><svg id="copyIcon"></svg>' +
'<svg id="copySuccessIcon"></svg></button><pre ' +
'id="prettyprint" class="prettyprint linenums:1"></pre>' +
'</div><div id="plaintext" class="hidden"></div>'
);
$.PrivateBin.PasteViewer.init();
$.PrivateBin.PasteViewer.setFormat(format);
$.PrivateBin.PasteViewer.setText(text);
$.PrivateBin.PasteViewer.run();
$.PrivateBin.CopyToClipboard.init();
$.PrivateBin.CopyToClipboard.enableTestMode();
$('#prettyMessageCopyBtn').trigger('click');
const copiedText = $.PrivateBin.CopyToClipboard.readFromClipboard();
clean();
return text === copiedText;
});
/**
* Unfortunately in JSVerify impossible to check if copy with shortcut when user selected some text on the page
* (the copy paste to clipboard should not work in this case) due to lucking window.getSelection() in jsdom.
*/
jsc.property('Copy with keyboard shortcut', common.jscFormats(), 'nestring', function (format, text) {
var clean = jsdom();
$('body').html(
'<div id="placeholder">+++ no paste text ' +
'+++</div><div id="prettymessage" class="hidden">' +
'<button type="button" id="prettyMessageCopyBtn"><svg id="copyIcon"></svg>' +
'<svg id="copySuccessIcon"></svg></button><pre ' +
'id="prettyprint" class="prettyprint linenums:1"></pre>' +
'</div><div id="plaintext" class="hidden"></div>'
);
$.PrivateBin.PasteViewer.init();
$.PrivateBin.PasteViewer.setFormat(format);
$.PrivateBin.PasteViewer.setText(text);
$.PrivateBin.PasteViewer.run();
$.PrivateBin.CopyToClipboard.init();
$.PrivateBin.CopyToClipboard.enableTestMode();
$('body').trigger('copy');
const copiedTextWithoutSelectedText = $.PrivateBin.CopyToClipboard.readFromClipboard();
clean();
return copiedTextWithoutSelectedText === text;
});
});
jsc.property('Copy link to clipboard', 'nestring', function (text) {
var clean = jsdom();
$('body').html('<button id="copyLink"></button>');
$.PrivateBin.CopyToClipboard.init();
$.PrivateBin.CopyToClipboard.enableTestMode();
$.PrivateBin.CopyToClipboard.setUrl(text);
$('#copyLink').trigger('click');
const copiedText = $.PrivateBin.CopyToClipboard.readFromClipboard();
clean();
return text === copiedText;
});
describe('Keyboard shortcut hint', function () {
jsc.property('Show hint', 'nestring', function (text) {
var clean = jsdom();
$('body').html('<small id="copyShortcutHintText"></small>');
$.PrivateBin.CopyToClipboard.init();
$.PrivateBin.CopyToClipboard.showKeyboardShortcutHint();
const keyboardShortcutHint = $('#copyShortcutHintText').text();
clean();
return keyboardShortcutHint.length > 0;
});
jsc.property('Hide hint', 'nestring', function (text) {
var clean = jsdom();
$('body').html('<small id="copyShortcutHintText">' + text + '</small>');
$.PrivateBin.CopyToClipboard.init();
$.PrivateBin.CopyToClipboard.hideKeyboardShortcutHint();
const keyboardShortcutHint = $('#copyShortcutHintText').text();
clean();
return keyboardShortcutHint.length === 0;
});
});
});

View File

@ -108,7 +108,7 @@ class Configuration
'js/kjua-0.9.0.js' => 'sha512-CVn7af+vTMBd9RjoS4QM5fpLFEOtBCoB0zPtaqIDC7sF4F8qgUSRFQQpIyEDGsr6yrjbuOLzdf20tkHHmpaqwQ==',
'js/legacy.js' => 'sha512-UxW/TOZKon83n6dk/09GsYKIyeO5LeBHokxyIq+r7KFS5KMBeIB/EM7NrkVYIezwZBaovnyNtY2d9tKFicRlXg==',
'js/prettify.js' => 'sha512-puO0Ogy++IoA2Pb9IjSxV1n4+kQkKXYAEUtVzfZpQepyDPyXk8hokiYDS7ybMogYlyyEIwMLpZqVhCkARQWLMg==',
'js/privatebin.js' => 'sha512-/hDE31uyN+PB84PJ+C7fCFcdbfPy3TeV4slm6eSFrvqNhVMn/FPh/wsjyKLdQGW7fqBdxlGfq5+CDwUfF8fY0w==',
'js/privatebin.js' => 'sha512-RbRvz3FPpNO23J/RQUvPfH118Yugz0nBSsqGnMlbr0rfTYR0G0/PIhJTymlR8P2p3sdmtd80m1fUrbdY7/gbEA==',
'js/purify-3.2.3.js' => 'sha512-m8Wa/I//YoYMiIahBxDDwYfTnycl+i2DwH58nR8ps1o4KWqXzF8k1K4qHDgAz2HSQFNCNNKH/Qcbfu/jLOuhuQ==',
'js/rawinflate-0.3.js' => 'sha512-g8uelGgJW9A/Z1tB6Izxab++oj5kdD7B4qC7DHwZkB6DGMXKyzx7v5mvap2HXueI2IIn08YlRYM56jwWdm2ucQ==',
'js/showdown-2.1.0.js' => 'sha512-WYXZgkTR0u/Y9SVIA4nTTOih0kXMEd8RRV6MLFdL6YU8ymhR528NLlYQt1nlJQbYz4EW+ZsS0fx1awhiQJme1Q==',