From ee5f1e277dd92cfcd1d980bc526989ed2b54a3b6 Mon Sep 17 00:00:00 2001 From: Nicolas Lempereur Date: Wed, 6 Mar 2019 15:58:45 +0000 Subject: [PATCH] [FIX] web: pivot no result no remove groupbys Since f8b38f64d dictionaries "groupbys" are the same object between the pivot model instance and the headers menu. But this causes an issue in 12.0 since when there is no results, the headers menu groupbys is truncated and now being the same object, the pivot model instance is also truncated. So succedding search that find results would possibly have lost the groupbys which is unexpected and breaks some 12.0 tests not expecting it. With this changeset, the groupbys are not truncated anymore. Without the change, the added test fails with: Column groupby not lost after reload after empty results expected: { "pivot_column_groupby": [ "customer" ], "pivot_measures": [ "__count" ], "pivot_row_groupby": [] } result: { "pivot_column_groupby": [], "pivot_measures": [ "__count" ], "pivot_row_groupby": [] } closes #31644 Signed-off-by: Nicolas Lempereur (nle) --- .../static/src/js/views/pivot/pivot_model.js | 15 -------- addons/web/static/tests/views/pivot_tests.js | 35 +++++++++++++++++++ 2 files changed, 35 insertions(+), 15 deletions(-) diff --git a/addons/web/static/src/js/views/pivot/pivot_model.js b/addons/web/static/src/js/views/pivot/pivot_model.js index 1e669824ec8..7c4ddff9d22 100644 --- a/addons/web/static/src/js/views/pivot/pivot_model.js +++ b/addons/web/static/src/js/views/pivot/pivot_model.js @@ -274,12 +274,10 @@ var PivotModel = AbstractModel.extend({ // if we don't have the key 'groupBy' in params. In that case, // we want to have the full open state for the groupbys. self._updateTree(old_row.root, self.data.main_row.root); - self._updateMainGroupBys(old_row, self.data.main_row); } if (!('pivot_column_groupby' in (params.context || {}))) { self._updateTree(old_col.root, self.data.main_col.root); - self._updateMainGroupBys(old_col, self.data.main_col); } }); }, @@ -711,19 +709,6 @@ var PivotModel = AbstractModel.extend({ this._traverseTree(root.children[i], f, arg1, arg2, arg3); } }, - /** - * Updates the groupBys that the main[row | col] holds - * - * @private - * @param {Object} old: The main abstract header before reload - * @param {Object} main: The main abstract header after reload - * - */ - _updateMainGroupBys: function (old, main) { - var new_groupby_length = this._getHeaderDepth(main.root) - 1; - main.root.groupbys = old.root.groupbys; - main.root.groupbys.splice(new_groupby_length); - }, /** * @param {Object} old_tree * @param {Object} new_tree diff --git a/addons/web/static/tests/views/pivot_tests.js b/addons/web/static/tests/views/pivot_tests.js index 1eb9fa94e30..dbe4395f2b1 100644 --- a/addons/web/static/tests/views/pivot_tests.js +++ b/addons/web/static/tests/views/pivot_tests.js @@ -1030,6 +1030,41 @@ QUnit.module('Views', { pivot.destroy(); }); + QUnit.test('Empty results keep groupbys', function (assert) { + assert.expect(2); + + var pivot = createView({ + View: PivotView, + model: "partner", + data: this.data, + arch: '', + }); + + // Set a column groupby + pivot.$('thead .o_pivot_header_cell_closed').click(); + pivot.$('.o_field_selection li[data-field=customer] a').click(); + + // Set a domain for empty results + pivot.update({domain: [['id', '=', false]]}); + + var expectedContext = {pivot_column_groupby: undefined, + pivot_measures: undefined, + pivot_row_groupby: undefined}; + assert.deepEqual(pivot.getContext(), expectedContext, + 'Column groupby not lost after empty results'); + + // Set a domain for not empty results + pivot.update({domain: [['product_id', '=', 37]]}); + + var expectedContext = {pivot_column_groupby: ['customer'], + pivot_measures: ['__count'], + pivot_row_groupby: []}; + assert.deepEqual(pivot.getContext(), expectedContext, + 'Column groupby not lost after reload after empty results'); + + pivot.destroy(); + }); + QUnit.test('correctly uses pivot_ keys from the context', function (assert) { assert.expect(7);