From 9d7024a5327939de3c145928782936000d26bd33 Mon Sep 17 00:00:00 2001
From: Victor Feyens <vfe@odoo.com>
Date: Wed, 6 Nov 2019 12:37:14 +0000
Subject: [PATCH] [ADD] purchase_product_matrix: tests

as we cannot do the test in product_matrix, add the purchase equivalent
in purchase_product_matrix to also test the matrix in this case.
---
 .../tests/section_and_note_widget_tests.js    | 106 ++++++++++++++++++
 .../purchase_product_matrix/views/assets.xml  |   5 +
 .../tests/section_and_note_widget_tests.js    |   4 +-
 3 files changed, 113 insertions(+), 2 deletions(-)
 create mode 100644 addons/purchase_product_matrix/static/tests/section_and_note_widget_tests.js

diff --git a/addons/purchase_product_matrix/static/tests/section_and_note_widget_tests.js b/addons/purchase_product_matrix/static/tests/section_and_note_widget_tests.js
new file mode 100644
index 000000000000..0d60fc62a582
--- /dev/null
+++ b/addons/purchase_product_matrix/static/tests/section_and_note_widget_tests.js
@@ -0,0 +1,106 @@
+odoo.define('purchase_product_matrix.section_and_note_widget_tests', function (require) {
+"use strict";
+
+var FormView = require('web.FormView');
+var testUtils = require('web.test_utils');
+var createView = testUtils.createView;
+
+QUnit.module('section_and_note: purchase_product_matrix', {
+    beforeEach: function () {
+        this.data = {
+            purchase_order: {
+                fields: {
+                    order_line_ids: {
+                        string: "Lines",
+                        type: 'one2many',
+                        relation: 'order_line',
+                        relation_field: 'order_id',
+                    },
+                    grid: {string: "Grid", type: 'char'},
+                },
+            },
+            order_line: {
+                fields: {
+                    order_id: {string: "Invoice", type: 'many2one', relation: 'invoice'},
+                    product_template_id: {string: "Product", type: 'many2one', relation: 'product'},
+                },
+            },
+            product: {
+                fields: {
+                    name: {string: "Name", type: 'char'},
+                },
+                records: [
+                    {id: 1, name: 'A configurable product'},
+                ],
+            },
+        };
+
+        this.grid = JSON.stringify({
+            header: [{name: "My Company Tshirt (GRID)"}, {name: "M"}, {name: "L"}],
+            matrix: [[
+                {name: "Men"},
+                {ptav_ids: [10, 13], qty: 0, is_possible_combination: true},
+                {ptav_ids: [11, 13], qty: 0, is_possible_combination: true},
+            ], [
+                {name: "Women"},
+                {ptav_ids: [10, 14], qty: 0, is_possible_combination: true},
+                {ptav_ids: [11, 14], qty: 0, is_possible_combination: true},
+            ]],
+        });
+    },
+}, function () {
+    QUnit.test('can configure a product with the matrix', async function (assert) {
+        assert.expect(4);
+
+        this.data.purchase_order.onchanges = {
+            order_line_ids: obj => {
+                obj.grid = this.grid;
+            },
+            grid: () => {},
+        };
+        var form = await createView({
+            View: FormView,
+            model: 'purchase_order',
+            data: this.data,
+            arch: `<form>
+                    <field name="grid" invisible="1"/>
+                    <field name="order_line_ids" widget="section_and_note_one2many">
+                        <tree editable="bottom">
+                            <field name="product_template_id" widget="matrix_configurator"/>
+                        </tree>
+                    </field>
+                </form>`,
+            mockRPC: function (route, args) {
+                if (args.method === 'onchange' && args.args[2] === 'grid') {
+                    // should trigger an onchange on the grid field and let the
+                    // business logic create rows according to the matrix content
+                    assert.deepEqual(args.args[1].grid, JSON.stringify({
+                        changes: [{qty: 2, ptav_ids: [10, 13]}, {qty: 3, ptav_ids: [11, 14]}],
+                        product_template_id: 1,
+                    }));
+                }
+                if (args.method === 'get_single_product_variant') {
+                    assert.strictEqual(args.args[0], 1);
+                    return Promise.resolve({mode: 'matrix'});
+                }
+                return this._super.apply(this, arguments);
+            },
+        });
+
+        await testUtils.dom.click('.o_field_x2many_list_row_add a');
+        await testUtils.fields.many2one.searchAndClickItem("product_template_id", {item: 'configurable'});
+
+        assert.containsOnce(document.body, '.modal .o_product_variant_matrix');
+        const $matrix = $('.modal .o_product_variant_matrix');
+        assert.strictEqual($matrix.text().replace(/[\n\r\s\u00a0]+/g, ' '),
+            ' My Company Tshirt (GRID) M L Men Women ');
+
+        // select 2 M-Men and 3 L-Women
+        await testUtils.fields.editInput($matrix.find('.o_matrix_input[ptav_ids="10,13"]'), '2');
+        await testUtils.fields.editInput($matrix.find('.o_matrix_input[ptav_ids="11,14"]'), '3');
+        await testUtils.dom.click($('.modal .modal-footer .btn-primary'));
+
+        form.destroy();
+    });
+});
+});
diff --git a/addons/purchase_product_matrix/views/assets.xml b/addons/purchase_product_matrix/views/assets.xml
index 38a6a74bb378..c9aaf58fbedc 100644
--- a/addons/purchase_product_matrix/views/assets.xml
+++ b/addons/purchase_product_matrix/views/assets.xml
@@ -7,4 +7,9 @@
       </xpath>
   </template>
 
+  <template id="qunit_suite" name="purchase_product_matrix tests" inherit_id="web.qunit_suite">
+      <xpath expr="//t[@t-set='head']" position="inside">
+          <script type="text/javascript" src="/purchase_product_matrix/static/tests/section_and_note_widget_tests.js"></script>
+      </xpath>
+  </template>
 </odoo>
diff --git a/addons/sale_product_matrix/static/tests/section_and_note_widget_tests.js b/addons/sale_product_matrix/static/tests/section_and_note_widget_tests.js
index 708102ec835b..939fa2e829f4 100644
--- a/addons/sale_product_matrix/static/tests/section_and_note_widget_tests.js
+++ b/addons/sale_product_matrix/static/tests/section_and_note_widget_tests.js
@@ -1,11 +1,11 @@
-odoo.define('product_matrix.section_and_note_widget_tests', function (require) {
+odoo.define('sale_product_matrix.section_and_note_widget_tests', function (require) {
 "use strict";
 
 var FormView = require('web.FormView');
 var testUtils = require('web.test_utils');
 var createView = testUtils.createView;
 
-QUnit.module('section_and_note: product_matrix', {
+QUnit.module('section_and_note: sale_product_matrix', {
     beforeEach: function () {
         this.data = {
             sale_order: {
-- 
GitLab