Custom clipboard object added to the unit tests

This commit is contained in:
ribas160 2025-01-10 16:23:04 +02:00
parent 6c651ea676
commit 48eb6ef87a
4 changed files with 34 additions and 50 deletions

View file

@ -152,3 +152,22 @@ exports.urlToString = function (url) {
encodeURI(url.query.join('').replace(/^&+|&+$/gm,'')) : '') + encodeURI(url.query.join('').replace(/^&+|&+$/gm,'')) : '') +
(url.fragment ? '#' + encodeURI(url.fragment) : ''); (url.fragment ? '#' + encodeURI(url.fragment) : '');
}; };
exports.enableClipboard = function () {
navigator.clipboard = (function () {
let savedText = "";
async function writeText(text) {
savedText = text;
};
async function readText() {
return savedText;
};
return {
writeText,
readText,
};
})();
};

View file

@ -5459,9 +5459,7 @@ jQuery.PrivateBin = (function($, RawDeflate) {
copyIcon, copyIcon,
successIcon, successIcon,
shortcutHint, shortcutHint,
url, url;
testMode,
testClipboard;
/** /**
* Handle copy to clipboard button click * Handle copy to clipboard button click
@ -5542,11 +5540,7 @@ jQuery.PrivateBin = (function($, RawDeflate) {
* @function * @function
*/ */
function saveToClipboard(text) { function saveToClipboard(text) {
if (testMode) {
testClipboard = text;
} else {
navigator.clipboard.writeText(text); navigator.clipboard.writeText(text);
}
}; };
/** /**
@ -5612,35 +5606,6 @@ jQuery.PrivateBin = (function($, RawDeflate) {
url = newUrl; 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 * Initialize
* *

View file

@ -1,12 +1,13 @@
'use strict'; 'use strict';
var common = require('../common'); const common = require('../common');
describe('CopyToClipboard', function() { describe('CopyToClipboard', function() {
this.timeout(30000); this.timeout(30000);
describe ('Copy paste co clipboard', function () { describe ('Copy paste co clipboard', function () {
jsc.property('Copy with button click', common.jscFormats(), 'nestring', function (format, text) { jsc.property('Copy with button click', common.jscFormats(), 'nestring', async function (format, text) {
var clean = jsdom(); var clean = jsdom();
common.enableClipboard();
$('body').html( $('body').html(
'<div id="placeholder" class="hidden">+++ no paste text ' + '<div id="placeholder" class="hidden">+++ no paste text ' +
@ -23,23 +24,23 @@ describe('CopyToClipboard', function() {
$.PrivateBin.PasteViewer.run(); $.PrivateBin.PasteViewer.run();
$.PrivateBin.CopyToClipboard.init(); $.PrivateBin.CopyToClipboard.init();
$.PrivateBin.CopyToClipboard.enableTestMode();
$('#prettyMessageCopyBtn').trigger('click'); $('#prettyMessageCopyBtn').trigger('click');
const copiedText = $.PrivateBin.CopyToClipboard.readFromClipboard(); const savedToClipboardText = await navigator.clipboard.readText();
clean(); clean();
return text === copiedText; return text === savedToClipboardText;
}); });
/** /**
* Unfortunately in JSVerify impossible to check if copy with shortcut when user selected some text on the page * 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. * (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) { jsc.property('Copy with keyboard shortcut', common.jscFormats(), 'nestring', async function (format, text) {
var clean = jsdom(); var clean = jsdom();
common.enableClipboard();
$('body').html( $('body').html(
'<div id="placeholder">+++ no paste text ' + '<div id="placeholder">+++ no paste text ' +
@ -56,11 +57,10 @@ describe('CopyToClipboard', function() {
$.PrivateBin.PasteViewer.run(); $.PrivateBin.PasteViewer.run();
$.PrivateBin.CopyToClipboard.init(); $.PrivateBin.CopyToClipboard.init();
$.PrivateBin.CopyToClipboard.enableTestMode();
$('body').trigger('copy'); $('body').trigger('copy');
const copiedTextWithoutSelectedText = $.PrivateBin.CopyToClipboard.readFromClipboard(); const copiedTextWithoutSelectedText = await navigator.clipboard.readText();
clean(); clean();
@ -69,18 +69,18 @@ describe('CopyToClipboard', function() {
}); });
jsc.property('Copy link to clipboard', 'nestring', function (text) { jsc.property('Copy link to clipboard', 'nestring', async function (text) {
var clean = jsdom(); var clean = jsdom();
common.enableClipboard();
$('body').html('<button id="copyLink"></button>'); $('body').html('<button id="copyLink"></button>');
$.PrivateBin.CopyToClipboard.init(); $.PrivateBin.CopyToClipboard.init();
$.PrivateBin.CopyToClipboard.enableTestMode();
$.PrivateBin.CopyToClipboard.setUrl(text); $.PrivateBin.CopyToClipboard.setUrl(text);
$('#copyLink').trigger('click'); $('#copyLink').trigger('click');
const copiedText = $.PrivateBin.CopyToClipboard.readFromClipboard(); const copiedText = await navigator.clipboard.readText();
clean(); clean();

View file

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