From 22e583a8825c514f47fbb4b3e2a4a019aad611c4 Mon Sep 17 00:00:00 2001 From: Julien Mougenot Date: Tue, 17 Mar 2020 15:32:42 +0000 Subject: [PATCH] [FIX] stock: fix traceability action traceback Before this commit, the control panel of the stock traceability report actions was updated before being instantiated. Now, only the props of the control panel are updated before its first rendering. Added a test asserting that the action is properly working. closes odoo/odoo#47823 Signed-off-by: Aaron Bohy (aab) --- .../js/stock_traceability_report_backend.js | 39 ++++------ ...stock_traceability_report_backend_tests.js | 71 +++++++++++++++++++ addons/stock/views/stock_template.xml | 1 + 3 files changed, 87 insertions(+), 24 deletions(-) create mode 100644 addons/stock/static/tests/stock_traceability_report_backend_tests.js diff --git a/addons/stock/static/src/js/stock_traceability_report_backend.js b/addons/stock/static/src/js/stock_traceability_report_backend.js index 9a3b7872947..f723ade6961 100644 --- a/addons/stock/static/src/js/stock_traceability_report_backend.js +++ b/addons/stock/static/src/js/stock_traceability_report_backend.js @@ -16,7 +16,7 @@ var stock_report_generic = AbstractAction.extend({ init: function(parent, action) { this._super.apply(this, arguments); this.actionManager = parent; - this.given_context = session.user_context; + this.given_context = Object.assign({}, session.user_context); this.controller_url = action.context.url; if (action.context.context) { this.given_context = action.context.context; @@ -47,37 +47,28 @@ var stock_report_generic = AbstractAction.extend({ } }); }, - start: function() { - var self = this; - return this._super.apply(this, arguments).then(function () { - self.set_html(); - }); + start: async function() { + this.controlPanelProps.cp_content = { $buttons: this.$buttons }; + await this._super(...arguments); + this.set_html(); }, // Fetches the html and is previous report.context if any, else create it - get_html: function() { - var self = this; - var defs = []; - return this._rpc({ - model: 'stock.traceability.report', - method: 'get_html', - args: [self.given_context], - }) - .then(function (result) { - self.html = result.html; - self.renderButtons(); - defs.push(self.update_cp()); - return Promise.all(defs); - }); + get_html: async function() { + const { html } = await this._rpc({ + args: [this.given_context], + method: 'get_html', + model: 'stock.traceability.report', + }); + this.html = html; + this.renderButtons(); }, // Updates the control panel and render the elements that have yet to be rendered update_cp: function() { if (!this.$buttons) { this.renderButtons(); } - var status = { - cp_content: {$buttons: this.$buttons}, - }; - return this.updateControlPanel(status); + this.controlPanelProps.cp_content = { $buttons: this.$buttons }; + return this.updateControlPanel(); }, renderButtons: function() { var self = this; diff --git a/addons/stock/static/tests/stock_traceability_report_backend_tests.js b/addons/stock/static/tests/stock_traceability_report_backend_tests.js new file mode 100644 index 00000000000..feba8f0f9e5 --- /dev/null +++ b/addons/stock/static/tests/stock_traceability_report_backend_tests.js @@ -0,0 +1,71 @@ +odoo.define('stock.stock_traceability_report_backend_tests', function (require) { + "use strict"; + + const dom = require('web.dom'); + const StockReportGeneric = require('stock.stock_report_generic'); + const testUtils = require('web.test_utils'); + + /** + * Helper function to instantiate a stock report action. + * @param {Object} params + * @param {Object} params.action + * @param {boolean} [params.debug] + * @returns {Promise} + */ + async function createStockReportAction(params) { + const parent = testUtils.createParent(params); + const report = new StockReportGeneric(parent, params.action); + const target = testUtils.prepareTarget(params.debug); + + const _destroy = report.destroy; + report.destroy = function () { + report.destroy = _destroy; + parent.destroy(); + }; + const fragment = document.createDocumentFragment(); + await report.appendTo(fragment); + dom.prepend(target, fragment, { + callbacks: [{ widget: report }], + in_DOM: true, + }); + // Wait for the ReportWidget to be appended + await testUtils.nextTick(); + + return report; + } + + QUnit.module('Stock', {}, function () { + QUnit.module('Traceability report'); + + QUnit.test("Rendering with no lines", async function (assert) { + assert.expect(1); + + const template = ` +
+
+ +

No operation made on this lot.

+
+
+
`; + const report = await createStockReportAction({ + action: { + context: {}, + params: {}, + }, + data: { + 'stock.traceability.report': { + fields: {}, + get_html: () => ({ html: template }), + }, + }, + }); + + // HTML content is nested in a div inside of the content + assert.strictEqual(report.el.querySelector('.o_content > div').innerHTML, template, + "Displayed template should match"); + + report.destroy(); + }); + }); +}); diff --git a/addons/stock/views/stock_template.xml b/addons/stock/views/stock_template.xml index 9c7e7f8407a..dd9f333379c 100644 --- a/addons/stock/views/stock_template.xml +++ b/addons/stock/views/stock_template.xml @@ -21,6 +21,7 @@