[FIX] web: rounding error in percentageField

Before this commit, some values entered in the percentage field could
be changed due to a rounding error.
For example, the value 28 was replaced by 28.000000000000004

How to reproduce it:
- Go to a form view with percentage fields
- Enter the value 28 in the percentage field

Before this commit:
The value in the input was replaced by 28.000000000000004

After this commit:
The value in the input remains 28

closes odoo/odoo#100559

Signed-off-by: Aaron Bohy (aab) <aab@odoo.com>
This commit is contained in:
FrancoisGe
2022-09-20 11:34:02 +02:00
parent 3b6f51d9e2
commit 20b8f4c7e8
2 changed files with 22 additions and 4 deletions
@@ -13,7 +13,11 @@ const { Component } = owl;
export class PercentageField extends Component {
setup() {
useInputField({
getValue: () => this.props.value * 100,
getValue: () =>
formatPercentage(this.props.value, {
digits: this.props.digits,
noSymbol: true,
}),
refName: "numpadDecimal",
parse: (v) => parsePercentage(v),
});
@@ -59,7 +59,7 @@ QUnit.module("Fields", (hooks) => {
await click(target.querySelector(".o_form_button_edit"));
assert.strictEqual(
target.querySelector(".o_field_widget[name=float_field] input").value,
"44.444",
"44.4",
"The input should be rendered without the percentage symbol."
);
assert.strictEqual(
@@ -67,8 +67,8 @@ QUnit.module("Fields", (hooks) => {
"%",
"The input should be followed by a span containing the percentage symbol."
);
const field = target.querySelector("input");
await editInput(target, "input", "24");
const field = target.querySelector("[name='float_field'] input");
await editInput(target, "[name='float_field'] input", "24");
assert.strictEqual(field.value, "24", "The value should not be formated yet.");
await click(target.querySelector(".o_form_button_save"));
assert.strictEqual(
@@ -97,4 +97,18 @@ QUnit.module("Fields", (hooks) => {
"Placeholder"
);
});
QUnit.test("PercentageField in form view without rounding error", async function (assert) {
await makeView({
serverData,
type: "form",
resModel: "partner",
arch: `
<form>
<field name="float_field" widget="percentage"/>
</form>`,
});
await editInput(target, "[name='float_field'] input", "28");
assert.strictEqual(target.querySelector("[name='float_field'] input").value, "28");
});
});