Files
odoo_source/addons/bus/static/tests/helpers/mock_server.js
T
tsm-odoo d998c54feb [FIX] mail, bus: fix mock sendone/many crash during tests
The mock of sendone/many was relying on the bus_service. Since owl.Component.env
is changing during tests setup, the bus_service was not always defined. In order
to make this more reliable, the longpolling/poll route is now mocked, returning a
promise that can be resolved by the sendone/many methods.

closes odoo/odoo#97298

Related: odoo/enterprise#30059
Signed-off-by: Alexandre Kühn (aku) <aku@odoo.com>
2022-08-05 16:35:45 +02:00

63 lines
2.0 KiB
JavaScript

/** @odoo-module **/
import { patch } from "@web/core/utils/patch";
import { MockServer } from "@web/../tests/helpers/mock_server";
import { makeDeferred } from "@web/../tests/helpers/utils";
patch(MockServer.prototype, 'bus', {
init() {
this._super(...arguments);
this.pendingLongpollingPromise = null;
this.notificationsToBeResolved = [];
},
//--------------------------------------------------------------------------
// Private
//--------------------------------------------------------------------------
/**
* @override
*/
async _performRPC(route, args) {
if (route === '/longpolling/poll') {
const longpollingPromise = makeDeferred();
if (this.notificationsToBeResolved.length) {
longpollingPromise.resolve(this.notificationsToBeResolved);
this.notificationsToBeResolved = [];
} else {
this.pendingLongpollingPromise = longpollingPromise;
}
return longpollingPromise;
}
return this._super(route, args);
},
/**
* Simulates `_sendone` on `bus.bus`.
*
* @param {string} channel
* @param {string} notificationType
* @param {any} message
*/
_mockBusBus__sendone(channel, notificationType, message) {
this._mockBusBus__sendmany([[channel, notificationType, message]]);
},
/**
* Simulates `_sendmany` on `bus.bus`.
*
* @param {Array} notifications
*/
_mockBusBus__sendmany(notifications) {
const values = [];
for (const notification of notifications) {
const [type, payload] = notification.slice(1, notification.length);
values.push({ message: { payload, type } });
}
if (this.pendingLongpollingPromise) {
this.pendingLongpollingPromise.resolve(values);
this.pendingLongpollingPromise = null;
} else {
this.notificationsToBeResolved.push(...values);
}
},
});