From bc76549a7da9b0d4e80ad0a2e287b5504d46271b Mon Sep 17 00:00:00 2001 From: Vincent Schippefilt Date: Wed, 6 Mar 2019 14:02:28 +0100 Subject: [PATCH] [REF] sale: adapt code after jQuery update Part of task 1896658 Co-authored-by: Aaron Bohy Co-authored-by: Christophe Matthieu Co-authored-by: Mathieu Duckerts-Antoine Co-authored-by: David Monjoie Co-authored-by: Martin Geubelle Co-authored-by: svs-odoo Co-authored-by: Vincent Schippefilt --- .../src/js/product_configurator_controller.js | 6 ++-- .../src/js/product_configurator_mixin.js | 10 +++--- .../src/js/product_configurator_modal.js | 10 +++--- .../src/js/product_configurator_renderer.js | 6 ++-- addons/sale/static/src/js/sale.js | 11 +++--- .../static/tests/product_configurator.test.js | 34 +++++++++++-------- .../tests/sales_team_dashboard_tests.js | 18 +++++----- 7 files changed, 49 insertions(+), 46 deletions(-) diff --git a/addons/sale/static/src/js/product_configurator_controller.js b/addons/sale/static/src/js/product_configurator_controller.js index bf45779bd31..7ca3d32c292 100644 --- a/addons/sale/static/src/js/product_configurator_controller.js +++ b/addons/sale/static/src/js/product_configurator_controller.js @@ -94,14 +94,12 @@ var ProductConfiguratorFormController = FormController.extend({ ]; var productId = parseInt($modal.find(productSelector.join(', ')).first().val(), 10); - var productReady = this.renderer.selectOrCreateProduct( + this.renderer.selectOrCreateProduct( $modal, productId, $modal.find('.product_template_id').val(), false - ); - - productReady.done(function (productId) { + ).then(function (productId) { $modal.find(productSelector.join(', ')).val(productId); var variantValues = self diff --git a/addons/sale/static/src/js/product_configurator_mixin.js b/addons/sale/static/src/js/product_configurator_mixin.js index 1a74764aacc..1bf7cca730a 100644 --- a/addons/sale/static/src/js/product_configurator_mixin.js +++ b/addons/sale/static/src/js/product_configurator_mixin.js @@ -280,25 +280,25 @@ var ProductConfiguratorMixin = { }, /** - * Will return a deferred: + * Will return a promise: * * - If the product already exists, immediately resolves it with the product_id * - If the product does not exist yet ("dynamic" variant creation), this method will - * create the product first and then resolve the deferred with the created product's id + * create the product first and then resolve the promise with the created product's id * * @param {$.Element} $container the container to look into * @param {integer} productId the product id * @param {integer} productTemplateId the corresponding product template id * @param {boolean} useAjax wether the rpc call should be done using ajax.jsonRpc or using _rpc - * @returns {$.Deferred} the deferred that will be resolved with a {integer} productId + * @returns {Promise} the promise that will be resolved with a {integer} productId */ selectOrCreateProduct: function ($container, productId, productTemplateId, useAjax) { var self = this; productId = parseInt(productId); productTemplateId = parseInt(productTemplateId); - var productReady = $.Deferred(); + var productReady = Promise.resolve(); if (productId) { - productReady.resolve(productId); + productReady = Promise.resolve(productId); } else { var params = { product_template_id: productTemplateId, diff --git a/addons/sale/static/src/js/product_configurator_modal.js b/addons/sale/static/src/js/product_configurator_modal.js index c30c10b92b9..e8ad60b66d6 100644 --- a/addons/sale/static/src/js/product_configurator_modal.js +++ b/addons/sale/static/src/js/product_configurator_modal.js @@ -103,7 +103,7 @@ var OptionalProductsModal = Dialog.extend(ServicesMixin, ProductConfiguratorMixi }); var parentInit = self._super.apply(self, arguments); - return $.when(getModalContent, parentInit); + return Promise.all([getModalContent, parentInit]); }, /** @@ -124,7 +124,7 @@ var OptionalProductsModal = Dialog.extend(ServicesMixin, ProductConfiguratorMixi self.$modal.removeAttr("aria-hidden"); self.$modal.modal().appendTo(self.container); self.$modal.focus(); - self._opened.resolve(); + self._openedResolver(); } }); if (options && options.shouldFocusButtons) { @@ -353,14 +353,12 @@ var OptionalProductsModal = Dialog.extend(ServicesMixin, ProductConfiguratorMixi $main_product.after($parent); } - var productReady = this.selectOrCreateProduct( + this.selectOrCreateProduct( $parent, $parent.find('.product_id').val(), productTemplateId, true - ); - - productReady.done(function (productId){ + ).then(function (productId) { $parent.find('.product_id').val(productId); ajax.jsonRpc(self._getUri("/product_configurator/optional_product_items"), 'call', { diff --git a/addons/sale/static/src/js/product_configurator_renderer.js b/addons/sale/static/src/js/product_configurator_renderer.js index 8d16f2a329a..f3f437461ea 100644 --- a/addons/sale/static/src/js/product_configurator_renderer.js +++ b/addons/sale/static/src/js/product_configurator_renderer.js @@ -16,8 +16,10 @@ var ProductConfiguratorFormRenderer = FormRenderer.extend(ProductConfiguratorMix * @override */ start: function () { - this._super.apply(this, arguments); - this.$el.append($('
', {class: 'configurator_container'})); + var self = this; + return this._super.apply(this, arguments).then(function () { + self.$el.append($('
', {class: 'configurator_container'})); + }); }, //-------------------------------------------------------------------------- diff --git a/addons/sale/static/src/js/sale.js b/addons/sale/static/src/js/sale.js index 9d2d9313bac..b81e6ea91c7 100644 --- a/addons/sale/static/src/js/sale.js +++ b/addons/sale/static/src/js/sale.js @@ -39,7 +39,6 @@ KanbanRecord.include({ * Mostly a handler for what happens to the input "this.$target_input" * * @private - * @param {JqueryEvent} ev * */ _onSalesTeamTargetSet: function () { @@ -48,10 +47,12 @@ KanbanRecord.include({ if (isNaN(value)) { this.do_warn(_t("Wrong value entered!"), _t("Only Integer Value should be valid.")); } else { - this.trigger_up('kanban_record_update', {invoiced_target: value}); - this.trigger_up('reload'); - // TODO: previous lines can be refactored as follows (in master) - // self.trigger_up('kanban_record_update', {invoiced_target: value}); + this.trigger_up('kanban_record_update', { + invoiced_target: value, + onSuccess: function () { + self.trigger_up('reload'); + }, + }); } }, }); diff --git a/addons/sale/static/tests/product_configurator.test.js b/addons/sale/static/tests/product_configurator.test.js index df7046b2a20..c751eb6c111 100644 --- a/addons/sale/static/tests/product_configurator.test.js +++ b/addons/sale/static/tests/product_configurator.test.js @@ -91,12 +91,12 @@ odoo.define('sale.product.configurator.tests', function (require) { } } }; - } + }, }, function (){ - QUnit.test('click on "Configure a product" and check for form loading', function (assert) { + QUnit.test('click on "Configure a product" and check for form loading', async function (assert) { assert.expect(2); - var form = createView({ + var form = await createView({ View: FormView, model: 'sale_order', data: this.data, @@ -104,25 +104,26 @@ odoo.define('sale.product.configurator.tests', function (require) { mockRPC: function (route) { if (route === '/web/dataset/call_kw/ir.model.data/xmlid_to_res_id') { assert.ok(true); - return $.Deferred().then(_.constant(1)); + return Promise.resolve(1); } return this._super.apply(this, arguments); }, }); - assert.strictEqual(form.$("a:contains('Configure a product')").length, 1); + assert.containsOnce(form, "a:contains('Configure a product')"); - testUtils.dom.click(form.$("a:contains('Configure a product')")); + await testUtils.dom.click(form.$("a:contains('Configure a product')")); + form.destroy(); }); - QUnit.test('trigger_up the "add_record" event and checks that rows are correctly added to the list', function (assert) { + QUnit.test('trigger_up the "add_record" event and checks that rows are correctly added to the list', async function (assert) { assert.expect(1); - var form = createView({ + var form = await createView({ View: FormView, model: 'sale_order', data: this.data, - arch: getArch() + arch: getArch(), }); var list = form.renderer.allFieldWidgets[form.handle][1]; @@ -132,14 +133,16 @@ odoo.define('sale.product.configurator.tests', function (require) { forceEditable: "bottom" , allowWarning: true }); + await testUtils.nextTick(); - assert.strictEqual(list.$("tr.o_data_row").length, 2); + assert.containsN(list, "tr.o_data_row", 2); + form.destroy(); }); - QUnit.test('Select a product in the list and check for template loading', function (assert){ + QUnit.test('Select a product in the list and check for template loading', async function (assert){ assert.expect(1); - var product_configurator_form = createView({ + var product_configurator_form = await createView({ View: ProductConfiguratorFormView, model: 'sale_product_configurator', data: this.data, @@ -156,13 +159,14 @@ odoo.define('sale.product.configurator.tests', function (require) { mockRPC: function (route) { if (route === '/product_configurator/configure') { assert.ok(true); - return $.Deferred().then(_.constant(1)); + return Promise.resolve('
plop
'); } return this._super.apply(this, arguments); } }); - testUtils.dom.click(product_configurator_form.$('.o_input')); - testUtils.dom.click($("ul.ui-autocomplete li a:contains('Customizable Desk')").mouseenter()); + await testUtils.dom.click(product_configurator_form.$('.o_input')); + await testUtils.dom.click($("ul.ui-autocomplete li a:contains('Customizable Desk')").mouseenter()); + product_configurator_form.destroy(); }); }); }); diff --git a/addons/sale/static/tests/sales_team_dashboard_tests.js b/addons/sale/static/tests/sales_team_dashboard_tests.js index 5a79c1ff4d4..a9ab7604d93 100644 --- a/addons/sale/static/tests/sales_team_dashboard_tests.js +++ b/addons/sale/static/tests/sales_team_dashboard_tests.js @@ -22,10 +22,10 @@ QUnit.module('Sales Team Dashboard', { } }); -QUnit.test('edit target with several o_kanban_primary_bottom divs [REQUIRE FOCUS]', function (assert) { +QUnit.test('edit target with several o_kanban_primary_bottom divs [REQUIRE FOCUS]', async function (assert) { assert.expect(6); - var kanban = createView({ + var kanban = await createView({ View: KanbanView, model: 'crm.team', data: this.data, @@ -60,24 +60,24 @@ QUnit.test('edit target with several o_kanban_primary_bottom divs [REQUIRE FOCUS assert.containsN(kanban, '.o_kanban_primary_bottom', 2, "should have two divs with classname 'o_kanban_primary_bottom'"); - testUtils.dom.click(kanban.$('a.sales_team_target_definition')); + await testUtils.dom.click(kanban.$('a.sales_team_target_definition')); assert.containsOnce(kanban, '.o_kanban_primary_bottom:last input', "should have rendered an input in the last o_kanban_primary_bottom div"); kanban.$('.o_kanban_primary_bottom:last input').focus(); kanban.$('.o_kanban_primary_bottom:last input').val('123'); - kanban.$('.o_kanban_primary_bottom:last input').blur(); - + kanban.$('.o_kanban_primary_bottom:last input').trigger('blur'); + await testUtils.nextTick(); assert.strictEqual(kanban.$('.o_kanban_record').text(), "123Click to define a target", 'The kanban record should display the updated target value'); kanban.destroy(); }); -QUnit.test('edit target supports push Enter', function (assert) { +QUnit.test('edit target supports push Enter', async function (assert) { assert.expect(3); - var kanban = createView({ + var kanban = await createView({ View: KanbanView, model: 'crm.team', data: this.data, @@ -107,12 +107,12 @@ QUnit.test('edit target supports push Enter', function (assert) { }, }); - kanban.$('a.sales_team_target_definition').click(); + await testUtils.dom.click(kanban.$('a.sales_team_target_definition')); kanban.$('.o_kanban_primary_bottom:last input').focus(); kanban.$('.o_kanban_primary_bottom:last input').val('123'); kanban.$('.o_kanban_primary_bottom:last input').trigger($.Event('keydown', {which: $.ui.keyCode.ENTER, keyCode: $.ui.keyCode.ENTER})); - + await testUtils.nextTick(); assert.strictEqual(kanban.$('.o_kanban_record').text(), "123Click to define a target", 'The kanban record should display the updated target value');