summaryrefslogtreecommitdiff
path: root/addons/web/static/tests/fields/basic_fields_tests.js
diff options
context:
space:
mode:
authorCedric Snauwaert <csn@openerp.com>2019-07-03 08:58:30 +0000
committerCedric Snauwaert <csn@openerp.com>2019-07-09 07:45:42 +0000
commit9cab694bf594f6c7c8d795174d9159ac82f526b2 (patch)
tree59c3726d041e72f92eb40d7b123d96e63eb26445 /addons/web/static/tests/fields/basic_fields_tests.js
parent38753f95d9890171e8c9d81f29ae0b1b1eafa9cc (diff)
[IMP] web: add simple calculator on numeric fields
Add a new behavior to numeric fields, possibility to enter value manually or to use a simple arithmetic expression that will be evaluated. Example: =20+3*2 In order for the computation of the arithmetic expression to be done, the value needs to starts with = and then an expression can be entered. We only support the basics mathematical operations + - * / ( ) ^ User locale and digits separator are taken into account. If the value entered can't be evaluated to a numeric, the field will be displayed in error. closes odoo/odoo#34538 Signed-off-by: Aaron Bohy (aab) <aab@odoo.com>
Diffstat (limited to 'addons/web/static/tests/fields/basic_fields_tests.js')
-rw-r--r--addons/web/static/tests/fields/basic_fields_tests.js130
1 files changed, 130 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 4e5deb56780..c65873cc2c6 100644
--- a/addons/web/static/tests/fields/basic_fields_tests.js
+++ b/addons/web/static/tests/fields/basic_fields_tests.js
@@ -564,6 +564,86 @@ QUnit.module('basic_fields', {
form.destroy();
});
+ QUnit.test('float field using formula in form view', async function (assert) {
+ assert.expect(4);
+
+ var form = await createView({
+ View: FormView,
+ model: 'partner',
+ data: this.data,
+ arch:'<form string="Partners">' +
+ '<sheet>' +
+ '<field name="qux" widget="float" digits="[5,3]"/>' +
+ '</sheet>' +
+ '</form>',
+ res_id: 2,
+ });
+
+ // Test computation with priority of operation
+ await testUtils.form.clickEdit(form);
+ await testUtils.fields.editInput(form.$('input[name=qux]'), '=20+3*2');
+ await testUtils.form.clickSave(form);
+ assert.strictEqual(form.$('.o_field_widget').first().text(), '26.000',
+ 'The new value should be calculated properly.');
+
+ // Test computation with ** operand
+ await testUtils.form.clickEdit(form);
+ await testUtils.fields.editInput(form.$('input[name=qux]'), '=2**3');
+ await testUtils.form.clickSave(form);
+ assert.strictEqual(form.$('.o_field_widget').first().text(), '8.000',
+ 'The new value should be calculated properly.');
+
+ // Test computation with ^ operant which should do the same as **
+ await testUtils.form.clickEdit(form);
+ await testUtils.fields.editInput(form.$('input[name=qux]'), '=2^3');
+ await testUtils.form.clickSave(form);
+ assert.strictEqual(form.$('.o_field_widget').first().text(), '8.000',
+ 'The new value should be calculated properly.');
+
+ // Test computation and rounding
+ await testUtils.form.clickEdit(form);
+ await testUtils.fields.editInput(form.$('input[name=qux]'), '=100/3');
+ await testUtils.form.clickSave(form);
+ assert.strictEqual(form.$('.o_field_widget').first().text(), '33.333',
+ 'The new value should be calculated properly.');
+
+ form.destroy();
+ });
+
+ QUnit.test('float field using incorrect formula in form view', async function (assert) {
+ assert.expect(4);
+
+ var form = await createView({
+ View: FormView,
+ model: 'partner',
+ data: this.data,
+ arch:'<form string="Partners">' +
+ '<sheet>' +
+ '<field name="qux" widget="float" digits="[5,3]"/>' +
+ '</sheet>' +
+ '</form>',
+ res_id: 2,
+ });
+
+ // Test that incorrect value is not computed
+ await testUtils.form.clickEdit(form);
+ await testUtils.fields.editInput(form.$('input[name=qux]'), '=abc');
+ await testUtils.form.clickSave(form);
+ assert.hasClass(form.$('.o_form_view'),'o_form_editable',
+ "form view should still be editable");
+ assert.hasClass(form.$('input[name=qux]'),'o_field_invalid',
+ "fload field should be displayed as invalid");
+
+ await testUtils.fields.editInput(form.$('input[name=qux]'), '=3:2?+4');
+ await testUtils.form.clickSave(form);
+ assert.hasClass(form.$('.o_form_view'),'o_form_editable',
+ "form view should still be editable");
+ assert.hasClass(form.$('input[name=qux]'),'o_field_invalid',
+ "float field should be displayed as invalid");
+
+ form.destroy();
+ });
+
QUnit.test('float field in editable list view', async function (assert) {
assert.expect(4);
@@ -3390,6 +3470,35 @@ QUnit.module('basic_fields', {
form.destroy();
});
+ QUnit.test('monetary field rounding using formula in form view', async function (assert) {
+ assert.expect(1);
+
+ var form = await createView({
+ View: FormView,
+ model: 'partner',
+ data: this.data,
+ arch:'<form string="Partners">' +
+ '<sheet>' +
+ '<field name="qux" widget="monetary"/>' +
+ '<field name="currency_id" invisible="1"/>' +
+ '</sheet>' +
+ '</form>',
+ res_id: 5,
+ session: {
+ currencies: _.indexBy(this.data.currency.records, 'id'),
+ },
+ });
+
+ // Test computation and rounding
+ await testUtils.form.clickEdit(form);
+ await testUtils.fields.editInput(form.$('.o_field_monetary input'), '=100/3');
+ await testUtils.form.clickSave(form);
+ assert.strictEqual(form.$('.o_field_widget').first().text(), '$\u00a033.33',
+ 'The new value should be calculated and rounded properly.');
+
+ form.destroy();
+ });
+
QUnit.test('monetary field with currency symbol after', async function (assert) {
assert.expect(5);
@@ -3800,6 +3909,27 @@ QUnit.module('basic_fields', {
form.destroy();
});
+ QUnit.test('integer field rounding using formula in form view', async function (assert) {
+ assert.expect(1);
+
+ var form = await createView({
+ View: FormView,
+ model: 'partner',
+ data: this.data,
+ arch:'<form string="Partners"><field name="int_field"/></form>',
+ res_id: 2,
+ });
+
+ // Test computation and rounding
+ await testUtils.form.clickEdit(form);
+ await testUtils.fields.editInput(form.$('input[name=int_field]'), '=100/3');
+ await testUtils.form.clickSave(form);
+ assert.strictEqual(form.$('.o_field_widget').first().text(), '33',
+ 'The new value should be calculated properly.');
+
+ form.destroy();
+ });
+
QUnit.test('integer field in form view with virtual id', async function (assert) {
assert.expect(1);
var params = {