mirror of
https://github.com/OCA/web.git
synced 2025-02-22 13:21:25 +02:00
[MIG] web_widget_float_formula: Migration to 12.0
This commit is contained in:
@@ -1,161 +1,169 @@
|
||||
/**
|
||||
* Copyright 2016 LasLabs Inc.
|
||||
* License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
|
||||
**/
|
||||
* Copyright 2016 LasLabs Inc.
|
||||
* Copyright 2020 Brainbean Apps (https://brainbeanapps.com)
|
||||
* License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
|
||||
*/
|
||||
odoo.define('web_widget_float_formula.test_web_widget_float_formula', function (require) {
|
||||
"use strict";
|
||||
|
||||
odoo.define_section('web_widget_float_formula', ['web.form_common', 'web.form_widgets', 'web.core'], function(test) {
|
||||
'use strict';
|
||||
var FormView = require('web.FormView');
|
||||
var testUtils = require('web.test_utils');
|
||||
|
||||
window.test_setup = function(self, form_common, form_widgets, core) {
|
||||
core.bus.trigger('web_client_ready');
|
||||
var field_manager = new form_common.DefaultFieldManager(null, {});
|
||||
var filler = {'attrs': {}}; // Needed to instantiate FieldFloat
|
||||
self.field = new form_widgets.FieldFloat(field_manager, filler);
|
||||
self.field.$input = $('<input>');
|
||||
self.field.$label = $('<label>');
|
||||
};
|
||||
QUnit.module('web_widget_float_formula', {}, function () {
|
||||
|
||||
test('Float fields should have a _formula_text property that defaults to an empty string',
|
||||
function(assert, form_common, form_widgets, core) {
|
||||
window.test_setup(this, form_common, form_widgets, core);
|
||||
QUnit.test('float field', async function (assert) {
|
||||
assert.expect(5);
|
||||
|
||||
const form = await testUtils.createAsyncView({
|
||||
View: FormView,
|
||||
model: 'demo_entry',
|
||||
data: {
|
||||
demo_entry: {
|
||||
fields: {
|
||||
test_field: {string: 'Test Field', type: 'float'},
|
||||
},
|
||||
records: [{id: 1, test_field: 0.0}],
|
||||
},
|
||||
},
|
||||
res_id: 1,
|
||||
arch:
|
||||
'<form>' +
|
||||
'<field name="test_field"/>' +
|
||||
'</form>',
|
||||
viewOptions: {
|
||||
mode: 'edit',
|
||||
},
|
||||
});
|
||||
|
||||
var test_field = form.$('.o_field_widget[name="test_field"]');
|
||||
|
||||
testUtils.fields.editInput(test_field, '0.0 + 40.0 + 2.0');
|
||||
assert.strictEqual(test_field.val(), '42.00');
|
||||
|
||||
test_field.triggerHandler('focus');
|
||||
assert.strictEqual(test_field.val(), '0.0 + 40.0 + 2.0');
|
||||
test_field.triggerHandler('blur');
|
||||
assert.strictEqual(test_field.val(), '42.00');
|
||||
|
||||
testUtils.fields.editInput(test_field, '=(1.5+8.0/2.0-(15+5)*0.1)');
|
||||
assert.strictEqual(test_field.val(), '3.50');
|
||||
|
||||
testUtils.fields.editInput(test_field, 'bubblegum');
|
||||
assert.strictEqual(test_field.val(), 'bubblegum');
|
||||
|
||||
form.destroy();
|
||||
});
|
||||
|
||||
QUnit.test('integer field', async function (assert) {
|
||||
assert.expect(5);
|
||||
|
||||
const form = await testUtils.createAsyncView({
|
||||
View: FormView,
|
||||
model: 'demo_entry',
|
||||
data: {
|
||||
demo_entry: {
|
||||
fields: {
|
||||
test_field: {string: 'Test Field', type: 'integer'},
|
||||
},
|
||||
records: [{id: 1, test_field: 0}],
|
||||
},
|
||||
},
|
||||
res_id: 1,
|
||||
arch:
|
||||
'<form>' +
|
||||
'<field name="test_field"/>' +
|
||||
'</form>',
|
||||
viewOptions: {
|
||||
mode: 'edit',
|
||||
},
|
||||
});
|
||||
|
||||
var test_field = form.$('.o_field_widget[name="test_field"]');
|
||||
|
||||
testUtils.fields.editInput(test_field, '0 + 40 + 2');
|
||||
assert.strictEqual(test_field.val(), '42');
|
||||
|
||||
test_field.triggerHandler('focus');
|
||||
assert.strictEqual(test_field.val(), '0 + 40 + 2');
|
||||
test_field.triggerHandler('blur');
|
||||
assert.strictEqual(test_field.val(), '42');
|
||||
|
||||
testUtils.fields.editInput(test_field, '=(1+8/2-(15+5)*0.1)');
|
||||
assert.strictEqual(test_field.val(), '3');
|
||||
|
||||
testUtils.fields.editInput(test_field, 'bubblegum');
|
||||
assert.strictEqual(test_field.val(), 'bubblegum');
|
||||
|
||||
form.destroy();
|
||||
});
|
||||
|
||||
QUnit.test('monetary field', async function (assert) {
|
||||
assert.expect(5);
|
||||
|
||||
const form = await testUtils.createAsyncView({
|
||||
View: FormView,
|
||||
model: 'demo_entry',
|
||||
data: {
|
||||
demo_entry: {
|
||||
fields: {
|
||||
test_field: {string: 'Test Field', type: 'monetary'},
|
||||
currency_id: {string: 'Currency', type: 'many2one', relation: 'currency', searchable: true},
|
||||
},
|
||||
records: [{id: 1, test_field: 0.0, currency_id: 1}],
|
||||
},
|
||||
currency: {
|
||||
fields: {
|
||||
symbol: {string: 'Currency Sumbol', type: 'char', searchable: true},
|
||||
position: {string: 'Currency Position', type: 'char', searchable: true},
|
||||
},
|
||||
records: [{
|
||||
id: 1,
|
||||
display_name: '$',
|
||||
symbol: '$',
|
||||
position: 'before',
|
||||
}]
|
||||
},
|
||||
},
|
||||
res_id: 1,
|
||||
arch:
|
||||
'<form>' +
|
||||
'<field name="test_field" widget="monetary"/>' +
|
||||
'<field name="currency_id" invisible="1"/>' +
|
||||
'</form>',
|
||||
viewOptions: {
|
||||
mode: 'edit',
|
||||
},
|
||||
session: {
|
||||
currencies: {
|
||||
1: {
|
||||
id: 1,
|
||||
display_name: '$',
|
||||
symbol: '$',
|
||||
position: 'before',
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
var test_field = form.$('.o_field_widget[name="test_field"]');
|
||||
var test_field_input = form.$('.o_field_widget[name="test_field"] input');
|
||||
|
||||
testUtils.fields.editInput(test_field_input, '0.0 + 40.0 + 2.0');
|
||||
assert.strictEqual(test_field_input.val(), '42.00');
|
||||
|
||||
test_field.triggerHandler('focusin');
|
||||
assert.strictEqual(test_field_input.val(), '0.0 + 40.0 + 2.0');
|
||||
test_field.triggerHandler('focusout');
|
||||
assert.strictEqual(test_field_input.val(), '42.00');
|
||||
|
||||
testUtils.fields.editInput(test_field_input, '=(1.5+8.0/2.0-(15+5)*0.1)');
|
||||
assert.strictEqual(test_field_input.val(), '3.50');
|
||||
|
||||
testUtils.fields.editInput(test_field_input, 'bubblegum');
|
||||
assert.strictEqual(test_field_input.val(), 'bubblegum');
|
||||
|
||||
form.destroy();
|
||||
});
|
||||
|
||||
assert.strictEqual(this.field._formula_text, '');
|
||||
});
|
||||
|
||||
test('.initialize_content() on float fields should clear the _formula_text property',
|
||||
function(assert, form_common, form_widgets, core) {
|
||||
window.test_setup(this, form_common, form_widgets, core);
|
||||
this.field._formula_text = 'test';
|
||||
this.field.initialize_content();
|
||||
|
||||
assert.strictEqual(this.field._formula_text, '');
|
||||
});
|
||||
|
||||
test('._clean_formula_text() on float fields should clear the _formula_text property',
|
||||
function(assert, form_common, form_widgets, core) {
|
||||
window.test_setup(this, form_common, form_widgets, core);
|
||||
this.field._formula_text = 'test';
|
||||
this.field._clean_formula_text();
|
||||
|
||||
assert.strictEqual(this.field._formula_text, '');
|
||||
});
|
||||
|
||||
test('._process_formula() on float fields should return false when given invalid formulas',
|
||||
function(assert, form_common, form_widgets, core) {
|
||||
window.test_setup(this, form_common, form_widgets, core);
|
||||
|
||||
assert.strictEqual(this.field._process_formula('2*3'), false);
|
||||
assert.strictEqual(this.field._process_formula('=2*3a'), false);
|
||||
});
|
||||
|
||||
test('._process_formula() on float fields should properly process a valid formula',
|
||||
function(assert, form_common, form_widgets, core) {
|
||||
window.test_setup(this, form_common, form_widgets, core);
|
||||
|
||||
assert.strictEqual(this.field._process_formula(' =2*3\n'), '2*3');
|
||||
});
|
||||
|
||||
test('._eval_formula() on float fields should properly evaluate a valid formula',
|
||||
function(assert, form_common, form_widgets, core) {
|
||||
window.test_setup(this, form_common, form_widgets, core);
|
||||
|
||||
assert.equal(this.field._eval_formula('2*3'), 6);
|
||||
});
|
||||
|
||||
test('._eval_formula() on float fields should properly handle alternative decimal points and thousands seps',
|
||||
function(assert, form_common, form_widgets, core) {
|
||||
var translation_params = core._t.database.parameters;
|
||||
translation_params.decimal_point = ',';
|
||||
translation_params.thousands_sep = '.';
|
||||
window.test_setup(this, form_common, form_widgets, core);
|
||||
|
||||
assert.equal(this.field._eval_formula('2.000*3,5'), 7000);
|
||||
});
|
||||
|
||||
test('._eval_formula() on float fields should return false when given an input that evals to undefined',
|
||||
function(assert, form_common, form_widgets, core) {
|
||||
window.test_setup(this, form_common, form_widgets, core);
|
||||
|
||||
assert.equal(this.field._eval_formula(''), false);
|
||||
});
|
||||
|
||||
test('._eval_formula() on float fields should return false when given an input that cannot be evaluated',
|
||||
function(assert, form_common, form_widgets, core) {
|
||||
window.test_setup(this, form_common, form_widgets, core);
|
||||
|
||||
assert.equal(this.field._eval_formula('*/'), false);
|
||||
});
|
||||
|
||||
test('._compute_result() on float fields should always clean up _formula_text',
|
||||
function(assert, form_common, form_widgets, core) {
|
||||
window.test_setup(this, form_common, form_widgets, core);
|
||||
this.field._formula_text = 'test';
|
||||
this.field._compute_result();
|
||||
|
||||
assert.strictEqual(this.field._formula_text, '');
|
||||
});
|
||||
|
||||
test('._compute_result() should not change the value of the associated input when it is not a valid formula',
|
||||
function(assert, form_common, form_widgets, core) {
|
||||
window.test_setup(this, form_common, form_widgets, core);
|
||||
this.field.$input.val('=2*3a');
|
||||
this.field._compute_result();
|
||||
|
||||
assert.strictEqual(this.field.$input.val(), '=2*3a');
|
||||
});
|
||||
|
||||
test('._compute_result() should not change the value of the associated input when it cannot be evaled',
|
||||
function(assert, form_common, form_widgets, core) {
|
||||
window.test_setup(this, form_common, form_widgets, core);
|
||||
this.field.$input.val('=*/');
|
||||
this.field._compute_result();
|
||||
|
||||
assert.strictEqual(this.field.$input.val(), '=*/');
|
||||
});
|
||||
|
||||
test('._compute_result() should behave properly when the current value of the input element is a valid formula',
|
||||
function(assert, form_common, form_widgets, core) {
|
||||
window.test_setup(this, form_common, form_widgets, core);
|
||||
this.field.$input.val('=2*3');
|
||||
this.field._compute_result();
|
||||
|
||||
assert.equal(this.field.$input.val(), '6');
|
||||
assert.strictEqual(this.field._formula_text, '=2*3');
|
||||
});
|
||||
|
||||
test('._display_formula() should update the value of the input element when there is a stored formula',
|
||||
function(assert, form_common, form_widgets, core) {
|
||||
window.test_setup(this, form_common, form_widgets, core);
|
||||
this.field._formula_text = "test";
|
||||
this.field._display_formula();
|
||||
|
||||
assert.equal(this.field.$input.val(), 'test');
|
||||
});
|
||||
|
||||
test('.start() on float fields should add a handler that calls ._compute_result() when the field is blurred',
|
||||
function(assert, form_common, form_widgets, core) {
|
||||
window.test_setup(this, form_common, form_widgets, core);
|
||||
this.field.called = false;
|
||||
this.field._compute_result = function() {
|
||||
this.called = true;
|
||||
};
|
||||
this.field.start();
|
||||
this.field.trigger('blurred');
|
||||
|
||||
assert.strictEqual(this.field.called, true);
|
||||
});
|
||||
|
||||
test('.start() on float fields should add a handler that calls ._display_formula() when the field is focused',
|
||||
function(assert, form_common, form_widgets, core) {
|
||||
window.test_setup(this, form_common, form_widgets, core);
|
||||
this.field.called = false;
|
||||
this.field._display_formula = function() {
|
||||
this.called = true;
|
||||
};
|
||||
this.field.start();
|
||||
this.field.trigger('focused');
|
||||
|
||||
assert.strictEqual(this.field.called, true);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user