diff options
| author | Aaron Bohy <aab@odoo.com> | 2017-12-21 12:29:50 +0100 |
|---|---|---|
| committer | Alexandre Kühn <alex.e.kuhn@gmail.com> | 2018-02-01 14:51:58 +0100 |
| commit | 02ec09cb1c3e2d7bc7968f40c18f2208d7f3f498 (patch) | |
| tree | 10d91136786b13666e78cfb94b12f1609d6f883d /addons/web/static/tests | |
| parent | 0cd656b5af38051df3f7d6319e99705150bb1cd8 (diff) | |
[REF][IMP] *: mail js refactoring
This commit improves the JS code of the mail module,
which comes maily from the new coding guidelines
and the addition of "JS services".
JS services are important objects that do not fit
well in the component tree, such as chat_manager
or ajax.
The benefits of JS services are improved readability
of the code, reduced coupling, and more testable
modules. In particular, discuss was hard to
Summary of the changes:
- ClientAction has been renamed into Discuss
- Clear instantiation of chatManager
- New coding guidelines in most mail modules
- JS Services can interact with each other
- Chat Manager and Window Manager are services
- Window Manager renamed to Chat Window Manager
- bus.bus is now encapsulated in Bus Service (a service)
- The test infrastructure has been tweaked with JS Services
- Chat Mixin has been removed
A future improvement would be to translate some 'trigger' into 'trigger_up'.
Diffstat (limited to 'addons/web/static/tests')
| -rw-r--r-- | addons/web/static/tests/chrome/action_manager_tests.js | 12 | ||||
| -rw-r--r-- | addons/web/static/tests/helpers/mock_server.js | 4 | ||||
| -rw-r--r-- | addons/web/static/tests/helpers/test_utils.js | 58 |
3 files changed, 59 insertions, 15 deletions
diff --git a/addons/web/static/tests/chrome/action_manager_tests.js b/addons/web/static/tests/chrome/action_manager_tests.js index eaadcdc180a..f6a0007c1a9 100644 --- a/addons/web/static/tests/chrome/action_manager_tests.js +++ b/addons/web/static/tests/chrome/action_manager_tests.js @@ -1,9 +1,11 @@ odoo.define('web.action_manager_tests', function (require) { "use strict"; +var ReportClientAction = require('report.client_action'); + var ControlPanelMixin = require('web.ControlPanelMixin'); var core = require('web.core'); -var ReportClientAction = require('report.client_action'); +var ReportService = require('web.ReportService'); var testUtils = require('web.test_utils'); var Widget = require('web.Widget'); @@ -1338,6 +1340,7 @@ QUnit.module('ActionManager', { } return this._super.apply(this, arguments); }, + services: [ReportService], session: { get_file: function (params) { assert.step(params.url); @@ -1347,7 +1350,9 @@ QUnit.module('ActionManager', { }, }); actionManager.doAction(7, { - on_close: assert.step.bind(assert, 'on_close'), + on_close: function () { + assert.step('on_close'); + }, }); assert.verifySteps([ @@ -1374,6 +1379,7 @@ QUnit.module('ActionManager', { } return this._super.apply(this, arguments); }, + services: [ReportService], session: { get_file: function (params) { assert.step(params.url); @@ -1388,7 +1394,6 @@ QUnit.module('ActionManager', { }, }); actionManager.doAction(7); - assert.verifySteps([ '/web/action/load', '/report/check_wkhtmltopdf', @@ -1430,6 +1435,7 @@ QUnit.module('ActionManager', { } return this._super.apply(this, arguments); }, + services: [ReportService], session: { get_file: function (params) { assert.step(params.url); // should not be called diff --git a/addons/web/static/tests/helpers/mock_server.js b/addons/web/static/tests/helpers/mock_server.js index a80aa2ff05b..fb5af01fdcd 100644 --- a/addons/web/static/tests/helpers/mock_server.js +++ b/addons/web/static/tests/helpers/mock_server.js @@ -430,7 +430,7 @@ var MockServer = Class.extend({ return result; }, /** - * Simulate a 'field_get' operation + * Simulate a 'fields_get' operation * * @private * @param {string} modelName @@ -464,7 +464,7 @@ var MockServer = Class.extend({ if (!action) { // when the action doesn't exist, the real server doesn't crash, it // simply returns false - console.warn("No action found for ID " + event.data.actionID); + console.warn("No action found for ID " + kwargs.action_id); } return action || false; }, diff --git a/addons/web/static/tests/helpers/test_utils.js b/addons/web/static/tests/helpers/test_utils.js index 468b25e5733..6b2c09d2631 100644 --- a/addons/web/static/tests/helpers/test_utils.js +++ b/addons/web/static/tests/helpers/test_utils.js @@ -19,6 +19,7 @@ var core = require('web.core'); var dom = require('web.dom'); var session = require('web.session'); var MockServer = require('web.MockServer'); +var utils = require('web.utils'); var Widget = require('web.Widget'); var DebouncedField = basic_fields.DebouncedField; @@ -97,12 +98,6 @@ var createActionManager = function (params) { }, }); addMockEnvironment(widget, _.defaults(params, {debounce: false})); - intercept(widget, 'call_service', function (event) { - if (event.data.service === 'report') { - var state = widget._rpc({route: '/report/check_wkhtmltopdf'}); - event.data.callback(state); - } - }, true); widget.appendTo($target); widget.$el.addClass('o_web_client'); @@ -330,6 +325,7 @@ function addMockEnvironment(widget, params) { currentDate: params.currentDate, debug: params.debug, }); + // make sure the debounce value for input fields is set to 0 var initialDebounceValue = DebouncedField.prototype.DEBOUNCE; DebouncedField.prototype.DEBOUNCE = params.fieldDebounce || 0; @@ -406,11 +402,53 @@ function addMockEnvironment(widget, params) { widgetDestroy.call(this); }; - intercept(widget, 'call_service', function (event) { - if (event.data.service === 'ajax') { - var result = mockServer.performRpc(event.data.args[0], event.data.args[1]); - event.data.callback(result); + // Dispatch service calls + // Note: some services could call other services at init, + // Which is why we have to init services after that + var services = {}; + intercept(widget, 'call_service', function (ev) { + var args, result; + if (ev.data.service === 'ajax') { + // ajax service is already mocked by the server + var route = ev.data.args[0]; + args = ev.data.args[1]; + result = mockServer.performRpc(route, args); + } else if (services[ev.data.service]) { + var service = services[ev.data.service]; + args = (ev.data.args || []); + result = service[ev.data.method].apply(service, args); } + ev.data.callback(result); + }); + // Instantiate services + // Note: ensure topological sort of services based on their dependencies + var sortServices = function (services) { + // Create nodes (services), with ajax already loaded + var nodes = { ajax: [] }; + _.each(services, function (Service) { + nodes[Service.prototype.name] = Service.prototype.dependencies; + }); + var sorted; + try { + sorted = utils.topologicalSort(nodes); + } catch (err) { + console.warn('topologicalSort Error:', err.message); + sorted = nodes; + } + // Remove ajax from sorted + sorted = _.without(sorted, 'ajax'); + // Sort services based on sorted + // Note: we convert sorted to an object key=>index for efficiency + var sortedObj = _.invert(_.object(_.pairs(sorted))); + sorted = _.sortBy(services, function (Service) { + return sortedObj[Service.prototype.name]; + }); + return sorted; + }; + var sortedServices = sortServices(params.services); + _.each(sortedServices, function (Service) { + var service = new Service(widget); + services[service.name] = service; }); intercept(widget, 'load_action', function (event) { |
