Просмотр исходного кода

Merge pull request #1984 from Jose-Tejera/agent/use-native-popstate-events

Handle native popstate events
El RIDO 2 недель назад
Родитель
Сommit
929eba59fb
2 измененных файлов с 19 добавлено и 23 удалено
  1. 2 18
      js/privatebin.js
  2. 17 5
      js/test/UiHelper.js

+ 2 - 18
js/privatebin.js

@@ -1587,11 +1587,11 @@ window.PrivateBin = (function () {
          * @name   UiHelper.historyChange
          * @private
          * @function
-         * @param  {Event} event
+         * @param  {PopStateEvent} event
          */
         function historyChange(event) {
             let currentLocation = Helper.baseUri();
-            if (event.originalEvent.state === null && // no state object passed
+            if (event.state === null && // no state object passed
                 event.target.location.href === currentLocation && // target location is home page
                 window.location.href === currentLocation // and we are not already on the home page
             ) {
@@ -1613,22 +1613,6 @@ window.PrivateBin = (function () {
         };
 
 
-        /**
-         * trigger a history (pop) state change
-         *
-         * used to test the UiHelper.historyChange private function
-         *
-         * @name   UiHelper.mockHistoryChange
-         * @function
-         * @param  {string} state   (optional) state to mock
-         */
-        me.mockHistoryChange = function (state) {
-            if (typeof state === 'undefined') {
-                state = null;
-            }
-            historyChange({ originalEvent: new PopStateEvent('popstate', { state: state }), target: window });
-        };
-
         /**
          * initialize
          *

+ 17 - 5
js/test/UiHelper.js

@@ -3,8 +3,6 @@ const common = require('../common');
 const fc = require('fast-check');
 
 describe('UiHelper', function () {
-    // TODO: As per https://github.com/tmpvar/jsdom/issues/1565 there is no navigation support in jsdom, yet.
-    // for now we use a mock function to trigger the event
     describe('historyChange', function () {
         this.timeout(30000);
         beforeEach(function () {
@@ -12,6 +10,21 @@ describe('UiHelper', function () {
             cleanup();
         });
 
+
+        /** @param {object?} state */
+        function dispatchPopState(state = null) {
+            let eventError = null;
+            const captureError = event => {
+                eventError = event.error;
+                event.preventDefault();
+            };
+            window.addEventListener('error', captureError);
+            PrivateBin.UiHelper.init();
+            window.dispatchEvent(new window.PopStateEvent('popstate', {state: state}));
+            window.removeEventListener('error', captureError);
+            assert.strictEqual(eventError, null);
+        }
+
         it('redirects to home, when the state is null', () => {
             fc.assert(fc.property(
                 common.fcUrl(false, false),
@@ -19,7 +32,7 @@ describe('UiHelper', function () {
                     const expected = common.urlToString(url),
                         clean = globalThis.cleanup('', {url: expected});
 
-                    PrivateBin.UiHelper.mockHistoryChange();
+                    dispatchPopState();
                     PrivateBin.Helper.reset();
                     var result = window.location.href;
                     clean();
@@ -37,7 +50,7 @@ describe('UiHelper', function () {
                     const expected = common.urlToString(url),
                         clean = globalThis.cleanup('', {url: expected});
 
-                    PrivateBin.UiHelper.mockHistoryChange([
+                    dispatchPopState([
                         {type: 'newpaste'}, '', expected
                     ]);
                     PrivateBin.Helper.reset();
@@ -77,4 +90,3 @@ describe('UiHelper', function () {
         */
     });
 });
-