summaryrefslogtreecommitdiff
path: root/addons/web/static/tests/fields/basic_fields_tests.js
diff options
context:
space:
mode:
authorAaron Bohy <aab@odoo.com>2017-06-26 09:04:25 +0200
committerAaron Bohy <aab@odoo.com>2017-06-27 14:12:31 +0200
commit7ea6f653df0863e9c0dbaf0b07800c0277b962b1 (patch)
treea2111a89146312ce37f51814467f526bfa4b19ce /addons/web/static/tests/fields/basic_fields_tests.js
parent816a3cd2c1edeff2ffa2e90c0f8a25795804f84d (diff)
[FIX] web: MonetaryField: rerender with currency
The MonetaryField can be rendered with or without a currency. The currency can change during the lifecyle of the widget (e.g. currency field in the view, whose value is set by an onchange). More precisely, the widget can be instantiated without currency, then a change on another field can trigger an onchange which sets the currency, and our monetary widget is re-rendered with a currency. Before this rev., this wasn't supported in edit mode because the $el's root node was different if there were a currency or not (it was an input if there were no currency, and a div containing an input and a span otherwise). This root node being rendered only once (at the creation of the widget), the widget wasn't re-rendered correctly if the currency was suddenly set or unset. This fix makes the widget behave uniformly whether or not there is a currency, so now it is always a div containing an input (and optionally a span if there is a currency).
Diffstat (limited to 'addons/web/static/tests/fields/basic_fields_tests.js')
-rw-r--r--addons/web/static/tests/fields/basic_fields_tests.js57
1 files changed, 57 insertions, 0 deletions
diff --git a/addons/web/static/tests/fields/basic_fields_tests.js b/addons/web/static/tests/fields/basic_fields_tests.js
index c492537c63f..8f26c922a83 100644
--- a/addons/web/static/tests/fields/basic_fields_tests.js
+++ b/addons/web/static/tests/fields/basic_fields_tests.js
@@ -1973,6 +1973,63 @@ QUnit.module('basic_fields', {
form.destroy();
});
+ QUnit.test('monetary field with currency set by an onchange', function (assert) {
+ // this test ensures that the monetary field can be re-rendered with and
+ // without currency (which can happen as the currency can be set by an
+ // onchange)
+ assert.expect(8);
+
+ this.data.partner.onchanges = {
+ int_field: function (obj) {
+ obj.currency_id = obj.int_field ? 2 : null;
+ },
+ };
+
+ var list = createView({
+ View: ListView,
+ model: 'partner',
+ data: this.data,
+ arch: '<tree editable="top">' +
+ '<field name="int_field"/>' +
+ '<field name="qux" widget="monetary"/>' +
+ '<field name="currency_id" invisible="1"/>' +
+ '</tree>',
+ session: {
+ currencies: _.indexBy(this.data.currency.records, 'id'),
+ },
+ });
+
+ list.$buttons.find('.o_list_button_add').click();
+ assert.strictEqual(list.$('div.o_field_widget[name=qux] input').length, 1,
+ "monetary field should have been rendered correctly (without currency)");
+ assert.strictEqual(list.$('.o_field_widget[name=qux] span').length, 0,
+ "monetary field should have been rendered correctly (without currency)");
+
+ // set a value for int_field -> should set the currency and re-render qux
+ list.$('.o_field_widget[name=int_field]').click().val('7').trigger('input');
+ assert.strictEqual(list.$('div.o_field_widget[name=qux] input').length, 1,
+ "monetary field should have been re-rendered correctly (with currency)");
+ assert.strictEqual(list.$('.o_field_widget[name=qux] span:contains(€)').length, 1,
+ "monetary field should have been re-rendered correctly (with currency)");
+ var $quxInput = list.$('.o_field_widget[name=qux] input');
+ $quxInput.click(); // check that the field is focusable
+ assert.strictEqual(document.activeElement, $quxInput[0],
+ "focus should be on the qux field's input");
+
+ // unset the value of int_field -> should unset the currency and re-render qux
+ list.$('.o_field_widget[name=int_field]').click().val('0').trigger('input');
+ $quxInput = list.$('div.o_field_widget[name=qux] input');
+ assert.strictEqual($quxInput.length, 1,
+ "monetary field should have been re-rendered correctly (without currency)");
+ assert.strictEqual(list.$('.o_field_widget[name=qux] span').length, 0,
+ "monetary field should have been re-rendered correctly (without currency)");
+ $quxInput.click(); // check that the field is still focusable
+ assert.strictEqual(document.activeElement, $quxInput[0],
+ "focus should be on the qux field's input");
+
+ list.destroy();
+ });
+
QUnit.module('FieldInteger');
QUnit.test('integer field when unset', function (assert) {