From 6245ed2bce4382c3207baf9c6b2d4394384f0b4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandre=20K=C3=BChn?= Date: Wed, 4 Dec 2019 12:45:33 +0000 Subject: [PATCH] [FIX] bus, mail: navigate from tests with mocked bus services MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Before this commit, when navigating from test environment (e.g. page reload), it crashed with following error: `TypeError: Cannot convert undefined or null to object` This error comes from mocked bus services in tests: even when they have been destroyed, they handle the window event 'unload'. They no longer have a parented parent, so `this.call()` returns `undefined`, hence the crash. This commit prevents mocked bus services to listen on window 'unload' event. Tests must always simulate this window event by explicitly calling the handler. closes odoo/odoo#41378 X-original-commit: fd08d04f0a24fea52e7b668b90aec48b33d349a3 Signed-off-by: Alexandre Kühn (aku) --- addons/bus/static/src/js/crosstab_bus.js | 8 +++++++- addons/mail/static/tests/helpers/test_utils.js | 1 + 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/addons/bus/static/src/js/crosstab_bus.js b/addons/bus/static/src/js/crosstab_bus.js index b939ecab3a5..8f66a64b286 100644 --- a/addons/bus/static/src/js/crosstab_bus.js +++ b/addons/bus/static/src/js/crosstab_bus.js @@ -109,7 +109,7 @@ var CrossTabBus = Longpolling.extend({ peers[this._id] = new Date().getTime(); this._callLocalStorage('setItem', 'peers', peers); - $(window).on('unload.' + this._id, this._onUnload.bind(this)); + this._registerWindowUnload(); if (!this._callLocalStorage('getItem', 'master')) { this._startElection(); @@ -230,6 +230,12 @@ var CrossTabBus = Longpolling.extend({ this._heartbeatTimeout = setTimeout(this._heartbeat.bind(this), hbPeriod); }, + /** + * @private + */ + _registerWindowUnload: function () { + $(window).on('unload.' + this._id, this._onUnload.bind(this)); + }, /** * Check with the local storage if the current tab is the master tab. * If this tab became the master, trigger 'become_master' event diff --git a/addons/mail/static/tests/helpers/test_utils.js b/addons/mail/static/tests/helpers/test_utils.js index af86d3c2e45..d3f7081eef6 100644 --- a/addons/mail/static/tests/helpers/test_utils.js +++ b/addons/mail/static/tests/helpers/test_utils.js @@ -59,6 +59,7 @@ var MockMailService = Class.extend({ bus_service: function () { return BusService.extend({ _poll: function () {}, // Do nothing + _registerWindowUnload: function () {}, // Do nothing isOdooFocused: function () { return true; }, updateOption: function () {}, });