summaryrefslogtreecommitdiff
path: root/addons/web/static/tests/views/basic_model_tests.js
diff options
context:
space:
mode:
authorLucas Perais (lpe) <lpe@odoo.com>2020-08-07 08:48:55 +0000
committerRaphael Collet <rco@odoo.com>2020-08-20 13:39:25 +0000
commit38b7462dcf7541bbcb51ac75cc1f7b6715f13ace (patch)
tree624ff1d52b3dcc253d4b1562510957afcb8ecdcd /addons/web/static/tests/views/basic_model_tests.js
parent488e334fc8fd6601e304635fa577c98b9bfd18e6 (diff)
[REF] web: BasicModel: combine default_get and onchange
This commit adapts the BasicModel to combine calls to `default_get` and (first) `onchange`. When creating a new record, we now only call `onchange`, which thus return default values and potential onchange values. Tests (and the MockServer) have been adapted accordingly. NOTE 1. If the `default_get` within the `onchange` returns a value for a field that is not in the view, we ignore it, and it won't be saved. Before, that value was kept and sent upon save. This change in behavior may prove problematic, although the overall risk is small. Decision has been made to keep heavy comments and code snippets if we were to revert back somehow to the previous situation. NOTE 2. Putting a context on a many2one field may change the value returned by `name_get` for that field. By default, the calls to `name_get` are done by `onchange`. If the context on a field must be used for `name_get`, one has to set the option `always_reload` to `True` on the field. In that case, every `onchange` that changes the value will trigger an extra `name_get`. NOTE 3. Suppose that a one2many field has a list view with field A, and a form view without field A. When adding a line, we now send all known fields (main view and inline views) to the `onchange`, which may return a default value for A. The value will appear on the list view, but not in the form view. The former behavior was to call `default_get` with the fields that occur in the form view only, and therefore field A would be left to value `False`. NOTE 4. A test surprisingly adds an extra call to `read`. The test was actually wrong before. With the changes in MockServer, we now correctly receive a command `[6, false, [1]]`, whereas before we received `[1]`, which isn't a valid command, and which was ignored. As a consequence, an extra `read` is done, whereas the test asserts it shouldn't. But it already didn't work before (I checked by sending the correct command). This needs to be bugfixed elsewhere (task-id-2323491): in a o2m with a onchange and default order records on an other page than the first should not trigger a `read`. Task 2261084 Co-authored-by: Raphael Collet <rco@odoo.com> Co-authored-by: Aaron Bohy <aab@odoo.com>
Diffstat (limited to 'addons/web/static/tests/views/basic_model_tests.js')
-rw-r--r--addons/web/static/tests/views/basic_model_tests.js37
1 files changed, 12 insertions, 25 deletions
diff --git a/addons/web/static/tests/views/basic_model_tests.js b/addons/web/static/tests/views/basic_model_tests.js
index abd91ca17cf..909cdfaa905 100644
--- a/addons/web/static/tests/views/basic_model_tests.js
+++ b/addons/web/static/tests/views/basic_model_tests.js
@@ -81,7 +81,9 @@ odoo.define('web.basic_model_tests', function (require) {
QUnit.module('BasicModel');
QUnit.test('can process x2many commands', async function (assert) {
- assert.expect(5);
+ assert.expect(6);
+
+ this.data.partner.fields.product_ids.default = [[0, 0, { category: [] }]];
const form = await createView({
View: FormView,
@@ -111,22 +113,16 @@ odoo.define('web.basic_model_tests', function (require) {
},
mockRPC(route, args) {
assert.step(args.method);
- if (args.method === 'default_get' && args.model === 'partner') {
- return Promise.resolve({
- product_ids: [
- [0, 0, {category: []}],
- ]
- });
- }
return this._super.apply(this, arguments);
},
});
assert.verifySteps([
'load_views',
- 'default_get',
+ 'onchange',
]);
assert.containsOnce(form, '.o_field_x2many_list', 'should have rendered a x2many list');
+ assert.containsOnce(form, '.o_data_row', 'should have added 1 record as default');
assert.containsOnce(form, '.o_field_x2many_list_row_add', 'should have rendered a x2many add row on list');
form.destroy();
});
@@ -691,7 +687,7 @@ odoo.define('web.basic_model_tests', function (require) {
model.destroy();
});
- QUnit.test('can make a default_record, no onchange', async function (assert) {
+ QUnit.test('can make a default_record with the help of onchange', async function (assert) {
assert.expect(5);
this.params.context = {};
@@ -714,8 +710,7 @@ odoo.define('web.basic_model_tests', function (require) {
assert.deepEqual(record.data.product_ids.data, [], "o2m default should be []");
assert.deepEqual(record.data.category.data, [], "m2m default should be []");
- assert.verifySteps(['default_get'],
- "there should be default_get");
+ assert.verifySteps(['onchange']);
model.destroy();
});
@@ -756,7 +751,7 @@ odoo.define('web.basic_model_tests', function (require) {
});
QUnit.test('can make a default_record with default relational values', async function (assert) {
- assert.expect(7);
+ assert.expect(6);
this.data.partner.fields.product_id.default = 37;
this.data.partner.fields.product_ids.default = [
@@ -805,8 +800,7 @@ odoo.define('web.basic_model_tests', function (require) {
assert.deepEqual(record.data.category.res_ids, [12, 14],
"m2m default should be [12, 14]");
- assert.verifySteps(['default_get', 'name_get'],
- "there should be default_get and name_get");
+ assert.verifySteps(['onchange']);
model.destroy();
});
@@ -872,7 +866,7 @@ odoo.define('web.basic_model_tests', function (require) {
"should have fetched correct name");
assert.strictEqual(record.data.other_product_id.data.display_name, "xpad",
"should have fetched correct name");
- assert.strictEqual(rpcCount, 2, "should have done 2 rpcs: default_get and 1 name_get");
+ assert.strictEqual(rpcCount, 1, "should have done 1 rpc: onchange");
model.destroy();
});
@@ -1402,7 +1396,7 @@ odoo.define('web.basic_model_tests', function (require) {
});
QUnit.test('default_get: fetch many2one with default (empty & not) inside x2manys', async function (assert) {
- assert.expect(4);
+ assert.expect(3);
this.data.partner.fields.category_m2o = {
type: 'many2one',
@@ -1419,12 +1413,6 @@ odoo.define('web.basic_model_tests', function (require) {
var model = await createModel({
Model: BasicModel,
data: this.data,
- mockRPC: function (route, args) {
- if (args.method === 'name_get' && args.model === 'partner_type') {
- assert.deepEqual(args.args, [[12]], "should name_get on category 12");
- }
- return this._super(route, args);
- },
});
var params = {
@@ -2199,8 +2187,7 @@ odoo.define('web.basic_model_tests', function (require) {
var recordID = await model.load(this.params);
recordID = await model.reload(recordID);
- assert.verifySteps(['default_get', 'default_get'],
- "two default_get RPCs should have been done");
+ assert.verifySteps(['onchange', 'onchange']);
var record = model.get(recordID);
assert.strictEqual(record.data.product_id, false,
"m2o default value should be false");