Permalink
Browse files

promisify mock_server & test_utils

  • Loading branch information...
VincentSchippefilt committed Nov 8, 2018
1 parent 71289e3 commit b36231033db63be03643f395317f853afabcfe0e
Showing with 36 additions and 35 deletions.
  1. +29 −28 addons/web/static/tests/helpers/mock_server.js
  2. +7 −7 addons/web/static/tests/helpers/test_utils.js
@@ -98,9 +98,9 @@ var MockServer = Class.extend({
*
* @param {string} route
* @param {Object} args
* @returns {Deferred<any>}
* @returns {Promise<any>}
* Resolved with the result of the RPC, stringified then parsed.
* If the RPC should fail, the deferred will be rejected with the
* If the RPC should fail, the promise will be rejected with the
* error object, stringified then parsed.
*/
performRpc: function (route, args) {
@@ -132,12 +132,13 @@ var MockServer = Class.extend({
if (debug) {
console.log('%c[rpc] response (error) ' + route, 'color: orange; font-weight: bold;', JSON.parse(errorString));
}
return $.Deferred().reject(errorString, event || $.Event());
return Promise.reject(errorString, event || $.Event());
});
var promise = def.promise();
promise.abort = abort;
return promise;
// var promise = def.promise();
// promise.abort = abort;
def.abort = abort;
return def;
},
//--------------------------------------------------------------------------
@@ -1058,77 +1059,77 @@ var MockServer = Class.extend({
* @private
* @param {string} route
* @param {Object} args
* @returns {Deferred<any>}
* @returns {Promise<any>}
* Resolved with the result of the RPC. If the RPC should fail, the
* deferred should either be rejected or the call should throw an
* promise should either be rejected or the call should throw an
* exception (@see performRpc for error handling).
*/
_performRpc: function (route, args) {
switch (route) {
case '/web/action/load':
return $.when(this._mockLoadAction(args.kwargs));
return Promise.resolve(this._mockLoadAction(args.kwargs));
case '/web/dataset/search_read':
return $.when(this._mockSearchReadController(args));
return Promise.resolve(this._mockSearchReadController(args));
case '/web/dataset/resequence':
return $.when(this._mockResequence(args));
return Promise.resolve(this._mockResequence(args));
}
if (route.indexOf('/web/image') >= 0 || _.contains(['.png', '.jpg'], route.substr(route.length - 4))) {
return Promise.resolve();
}
switch (args.method) {
case 'copy':
return $.when(this._mockCopy(args.model, args.args[0]));
return Promise.resolve(this._mockCopy(args.model, args.args[0]));
case 'create':
return $.when(this._mockCreate(args.model, args.args[0]));
return Promise.resolve(this._mockCreate(args.model, args.args[0]));
case 'default_get':
return $.when(this._mockDefaultGet(args.model, args.args, args.kwargs));
return Promise.resolve(this._mockDefaultGet(args.model, args.args, args.kwargs));
case 'fields_get':
return $.when(this._mockFieldsGet(args.model, args.args));
return Promise.resolve(this._mockFieldsGet(args.model, args.args));
case 'load_views':
return $.when(this._mockLoadViews(args.model, args.kwargs));
return Promise.resolve(this._mockLoadViews(args.model, args.kwargs));
case 'name_get':
return $.when(this._mockNameGet(args.model, args.args));
return Promise.resolve(this._mockNameGet(args.model, args.args));
case 'name_create':
return $.when(this._mockNameCreate(args.model, args.args));
return Promise.resolve(this._mockNameCreate(args.model, args.args));
case 'name_search':
return $.when(this._mockNameSearch(args.model, args.args, args.kwargs));
return Promise.resolve(this._mockNameSearch(args.model, args.args, args.kwargs));
case 'onchange':
return $.when(this._mockOnchange(args.model, args.args));
return Promise.resolve(this._mockOnchange(args.model, args.args));
case 'read':
return $.when(this._mockRead(args.model, args.args, args.kwargs));
return Promise.resolve(this._mockRead(args.model, args.args, args.kwargs));
case 'read_group':
return $.when(this._mockReadGroup(args.model, args.kwargs));
return Promise.resolve(this._mockReadGroup(args.model, args.kwargs));
case 'read_progress_bar':
return $.when(this._mockReadProgressBar(args.model, args.kwargs));
return Promise.resolve(this._mockReadProgressBar(args.model, args.kwargs));
case 'search_count':
return $.when(this._mockSearchCount(args.model, args.args));
return Promise.resolve(this._mockSearchCount(args.model, args.args));
case 'search_read':
return $.when(this._mockSearchRead(args.model, args.args, args.kwargs));
return Promise.resolve(this._mockSearchRead(args.model, args.args, args.kwargs));
case 'unlink':
return $.when(this._mockUnlink(args.model, args.args));
return Promise.resolve(this._mockUnlink(args.model, args.args));
case 'write':
return $.when(this._mockWrite(args.model, args.args));
return Promise.resolve(this._mockWrite(args.model, args.args));
}
var model = this.data[args.model];
if (model && typeof model[args.method] === 'function') {
return $.when(this.data[args.model][args.method](args.args, args.kwargs));
return Promise.resolve(this.data[args.model][args.method](args.args, args.kwargs));
}
throw new Error("Unimplemented route: " + route);
@@ -94,7 +94,7 @@ function observe(widget) {
* @param {function} [params.mockRPC]
* @returns {ActionManager}
*/
var createActionManager = function (params) {
var createActionManager = async function (params) {
params = params || {};
var $target = $('#qunit-fixture');
if (params.debug) {
@@ -111,7 +111,7 @@ var createActionManager = function (params) {
_.extend(params, {
mockRPC: function (route, args) {
if (args.model === 'ir.attachment') {
return $.when([]);
return Promise.resolve([]);
}
if (mockRPC) {
return mockRPC.apply(this, arguments);
@@ -120,7 +120,7 @@ var createActionManager = function (params) {
},
});
addMockEnvironment(widget, _.defaults(params, { debounce: false }));
widget.prependTo($target);
await widget.prependTo($target);
widget.$el.addClass('o_web_client');
if (config.device.isMobile) {
widget.$el.addClass('o_touch_device');
@@ -134,7 +134,7 @@ var createActionManager = function (params) {
actionManager.destroy = originalDestroy;
widget.destroy();
};
actionManager.appendTo(widget.$el);
await actionManager.appendTo(widget.$el);
return actionManager;
};
@@ -150,10 +150,10 @@ var createDebugManager = function (params) {
_.extend(params, {
mockRPC: function (route, args) {
if (args.method === 'check_access_rights') {
return $.when(true);
return Promise.resolve(true);
}
if (args.method === 'xmlid_to_res_id') {
return $.when(true);
return Promise.resolve(true);
}
if (mockRPC) {
return mockRPC.apply(this, arguments);
@@ -163,7 +163,7 @@ var createDebugManager = function (params) {
session: {
user_has_group: function (group) {
if (group === 'base.group_no_one') {
return $.when(true);
return Promise.resolve(true);
}
return this._super.apply(this, arguments);
},

0 comments on commit b362310

Please sign in to comment.