Files
odoo_source/addons/bus/static/tests/websocket_worker_tests.js
T
Matthieu Stockbauer fe699a241d [IMP] *: adapt client code to use websocket instead of longpolling
*: bus, calendar, hr_attendance, iap_mail, im_livechat, project, snailmail,
snailmail_account, survey, web, web_editor, website_livechat.

This commit is part of the websocket integration in Odoo.
This bus service now communicates with a shared worker in order to provide
a single websocket connection for multiple tabs. It is designed to
be used as a websocket except that events are slightly different,
re-connection is handled automatically. If the browser does not support shared
worker (Safari), the service fallback on a simple web worker.

Available events are:
   - connect : fired upon a successful connection.
   - disconnect : fired upon reception of the websocket close event.
     The close code and reason are given to the listeners callback.
   - reconnect : fired upon a successful re-connection.
   - reconnecting : triggered when the worker starts to try reconnecting.
   - notification : fired upon the reception of notifications.

Since multiple tabs are now handled by a worker, the cross_tab bus is no longer
required and has been removed.

Part-of: odoo/odoo#75510
2022-08-23 17:55:10 +02:00

99 lines
2.7 KiB
JavaScript

/** @odoo-module */
import { WEBSOCKET_CLOSE_CODES } from "@bus/workers/websocket_worker";
import { patchWebsocketWorkerWithCleanup } from '@bus/../tests/helpers/mock_websocket';
import { nextTick, patchWithCleanup } from "@web/../tests/helpers/utils";
QUnit.module('Websocket Worker');
QUnit.test('connect event is broadcasted', async function (assert) {
assert.expect(2);
patchWebsocketWorkerWithCleanup({
broadcast(type) {
assert.step(`broadcast ${type}`);
},
});
// Wait for the websocket to connect.
await nextTick();
assert.verifySteps(['broadcast connect']);
});
QUnit.test('disconnect event is broadcasted', async function (assert) {
assert.expect(3);
const worker = patchWebsocketWorkerWithCleanup({
broadcast(type) {
assert.step(`broadcast ${type}`);
},
});
// Wait for the websocket to connect.
await nextTick();
worker.websocket.close(WEBSOCKET_CLOSE_CODES.CLEAN);
// Wait for the websocket to disconnect.
await nextTick();
assert.verifySteps([
'broadcast connect',
'broadcast disconnect',
]);
});
QUnit.test('reconnecting/reconnect event is broadcasted', async function (assert) {
assert.expect(5);
// Patch setTimeout in order for the worker to reconnect immediatly.
patchWithCleanup(window, {
setTimeout: fn => fn(),
});
const worker = patchWebsocketWorkerWithCleanup({
broadcast(type) {
assert.step(`broadcast ${type}`);
},
});
// Wait for the websocket to connect.
await nextTick();
worker.websocket.close(WEBSOCKET_CLOSE_CODES.ABNORMAL_CLOSURE);
// Wait for the disconnect/reconnecting/reconnect events.
await nextTick();
assert.verifySteps([
'broadcast connect',
'broadcast disconnect',
'broadcast reconnecting',
'broadcast reconnect',
]);
});
QUnit.test('notification event is broadcasted', async function (assert) {
assert.expect(3);
const notifications = [{
id: 70,
message: {
type: "bundle_changed",
payload: {
server_version: '15.5alpha1+e',
},
},
}];
const worker = patchWebsocketWorkerWithCleanup({
broadcast(type, message) {
if (type === 'notification') {
assert.step(`broadcast ${type}`);
assert.deepEqual(message, notifications.map(notif => notif.message));
}
},
});
// Wait for the websocket to connect.
await nextTick();
worker.websocket.dispatchEvent(new MessageEvent('message', {
data: JSON.stringify(notifications),
}));
assert.verifySteps([
'broadcast notification',
]);
});