[FIX] web: error_service: bind the event handlers on browser

Before this commit, the error service bound its listeners onto window.
This may create pollution when executing more than one test instanciating an error_service

After this commit, we bind the handlers onto browser, which is fully in our control in tests.
This commit is contained in:
Lucas Perais (lpe)
2021-06-23 15:47:55 +00:00
parent 77a59f8d6a
commit a2d49d3003
3 changed files with 9 additions and 8 deletions
@@ -1,5 +1,6 @@
/** @odoo-modules */
import { browser } from "@web/core/browser/browser";
import { registry } from "@web/core/registry";
import { errorService } from "@web/core/errors/error_service";
import { dialogService } from "@web/core/dialog/dialog_service";
@@ -36,14 +37,14 @@ QUnit.module("base_automation", {}, function () {
// Both of these are unused but required for the error service to call error handlers
serviceRegistry.add("notification", notificationService);
serviceRegistry.add("rpc", makeFakeRPCService());
const windowAddEventListener = window.addEventListener;
window.addEventListener = (type, cb) => {
const windowAddEventListener = browser.addEventListener;
browser.addEventListener = (type, cb) => {
if (type === "unhandledrejection") {
unhandledRejectionCb = cb;
}
};
registerCleanup(() => {
window.addEventListener = windowAddEventListener;
browser.addEventListener = windowAddEventListener;
});
},
});
@@ -97,7 +97,7 @@ export const errorService = {
}
}
window.addEventListener("error", async (ev) => {
browser.addEventListener("error", async (ev) => {
const { colno, error: originalError, filename, lineno, message } = ev;
let uncaughtError;
if (!filename && !lineno && !colno) {
@@ -119,7 +119,7 @@ export const errorService = {
handleError(uncaughtError, originalError);
});
window.addEventListener("unhandledrejection", async (ev) => {
browser.addEventListener("unhandledrejection", async (ev) => {
const originalError = ev.reason;
const uncaughtError = new UncaughtPromiseError();
uncaughtError.unhandledRejectionEvent = ev;
@@ -36,8 +36,8 @@ QUnit.module("Error Service", {
serviceRegistry.add("notification", notificationService);
serviceRegistry.add("rpc", makeFakeRPCService());
serviceRegistry.add("localization", makeFakeLocalizationService());
const windowAddEventListener = window.addEventListener;
window.addEventListener = (type, cb) => {
const windowAddEventListener = browser.addEventListener;
browser.addEventListener = (type, cb) => {
if (type === "unhandledrejection") {
unhandledRejectionCb = cb;
}
@@ -46,7 +46,7 @@ QUnit.module("Error Service", {
}
};
registerCleanup(() => {
window.addEventListener = windowAddEventListener;
browser.addEventListener = windowAddEventListener;
});
},
});