diff --git a/addons/web/static/tests/helpers/mock_server.js b/addons/web/static/tests/helpers/mock_server.js index 0cb9fa5682cd9..ab62ddb69c94f 100644 --- a/addons/web/static/tests/helpers/mock_server.js +++ b/addons/web/static/tests/helpers/mock_server.js @@ -1028,15 +1028,7 @@ var MockServer = Class.extend({ kwargs.orderby = kwargs.orderby.split(',')[0]; var fieldName = kwargs.orderby.split(' ')[0]; var order = kwargs.orderby.split(' ')[1]; - result.sort(function (g1, g2) { - if (g1[fieldName] < g2[fieldName]) { - return order === 'ASC' ? -1 : 1; - } - if (g1[fieldName] > g2[fieldName]) { - return order === 'ASC' ? 1 : -1; - } - return 0; - }); + result = this._sortByField(result, model, fieldName, order); } if (kwargs.limit) { @@ -1175,19 +1167,11 @@ var MockServer = Class.extend({ return result; }); if (args.sort) { - // deal with sort on multiple fields (i.e. only consider the first) + // warning: only consider first level of sort args.sort = args.sort.split(',')[0]; var fieldName = args.sort.split(' ')[0]; var order = args.sort.split(' ')[1]; - processedRecords.sort(function (r1, r2) { - if (r1[fieldName] < r2[fieldName]) { - return order === 'ASC' ? -1 : 1; - } - if (r1[fieldName] > r2[fieldName]) { - return order === 'ASC' ? 1 : -1; - } - return 0; - }); + processedRecords = this._sortByField(processedRecords, args.model, fieldName, order); } var result = { length: nbRecords, @@ -1375,6 +1359,41 @@ var MockServer = Class.extend({ throw new Error("Unimplemented route: " + route); }, + /** + * @private + * @param {Object[]} records the records to sort + * @param {string} model the model of records + * @param {string} fieldName the field to sort on + * @param {string} [order="DESC"] "ASC" or "DESC" + * @returns {Object} + */ + _sortByField: function (records, model, fieldName, order) { + const field = this.data[model].fields[fieldName]; + records.sort((r1, r2) => { + let v1 = r1[fieldName]; + let v2 = r2[fieldName]; + if (field.type === 'many2one') { + const coRecords = this.data[field.relation].records; + if (this.data[field.relation].fields.sequence) { + // use sequence field of comodel to sort records + v1 = coRecords.find(r => r.id === v1[0]).sequence; + v2 = coRecords.find(r => r.id === v2[0]).sequence; + } else { + // sort by id + v1 = v1[0]; + v2 = v2[0]; + } + } + if (v1 < v2) { + return order === 'ASC' ? -1 : 1; + } + if (v1 > v2) { + return order === 'ASC' ? 1 : -1; + } + return 0; + }); + return records; + }, /** * helper function: traverse a tree and apply the function f to each of its * nodes.