Przeglądaj źródła

test: fix clipboard copy error/event

rugk 3 miesięcy temu
rodzic
commit
6b07d458e8
2 zmienionych plików z 26 dodań i 3 usunięć
  1. 1 0
      js/common.js
  2. 25 3
      js/test/CopyToClipboard.js

+ 1 - 0
js/common.js

@@ -207,6 +207,7 @@ exports.urlToString = function (url) {
 };
 
 exports.enableClipboard = function () {
+    // @ts-ignore
     navigator.clipboard = (function () {
         let savedText = "";
 

+ 25 - 3
js/test/CopyToClipboard.js

@@ -1,10 +1,10 @@
 'use strict';
 const common = require('../common');
 
-describe('CopyToClipboard', function() {
+describe('CopyToClipboard', function () {
     this.timeout(30000);
 
-    describe ('Copy document to clipboard', function () {
+    describe('Copy document to clipboard', function () {
         jsc.property('Copy with button click',
             common.jscFormats(),
             'nestring',
@@ -65,7 +65,7 @@ describe('CopyToClipboard', function() {
 
                 PrivateBin.CopyToClipboard.init();
 
-                document.body.dispatchEvent(new Event('copy'));
+                document.body.dispatchEvent(getClipboardEvent());
 
                 const copiedTextWithoutSelectedText = await navigator.clipboard.readText();
 
@@ -74,6 +74,28 @@ describe('CopyToClipboard', function() {
                 return copiedTextWithoutSelectedText === text;
             }
         );
+
+        /**
+         * ClipboardEvent is not supported in jsdom yet, so this creates a mock event to trigger the copy event listener.
+         *
+         * See https://github.com/jsdom/jsdom/issues/1568
+         *
+         * @returns {ClipboardEvent}
+         */
+        function getClipboardEvent() {
+            /** {@type ClipboardEvent} */
+            const clipboardEvent = new Event('copy', {
+                bubbles: true,
+                cancelable: true,
+                composed: true
+            });
+            clipboardEvent['clipboardData'] = {
+                getData: function () {
+                    return '';
+                }
+            }
+            return clipboardEvent;
+        }
     });