From bc326fa21ea264ebe6d07b5b1817f0bc7ef6f023 Mon Sep 17 00:00:00 2001 From: Hubert Van De Walle Date: Fri, 8 Mar 2024 14:21:57 +0000 Subject: [PATCH] [FIX] web: prevent multiple updates when pressing enter Steps to reproduce ================== In 16.4: - Go to Accounting > Reconcile 8 items - Click on the first line on the right - Edit the label and then press Enter - Switch to another line => Odoo Server Error Cause of the issue ================== When pressing Enter, two events are triggered: keydown and change. In the useInputField hook, there is a listener for both of those events, and they both end up calling `record.update` with the current value from the input. In the relational model, the update is locked inside a mutex. So the first update (triggered by the keydown) does an onchange. This onchange deletes the current records (Command.CLEAR) and returns new records. Once this is done, the mutex is released. The second update (triggered by the change event) then tries the same update. Since the value was obtained when queuing for the mutex, it uses the old (now deleted) record. Solution ======== This adds a test for https://github.com/odoo/odoo/pull/154991 as part of the fix got lost in a conflict resolution for previous versions. opw-3726818 closes odoo/odoo#157155 X-original-commit: fbebffe446de9dd80db2dbe5ca7bd2746dc2a54f Signed-off-by: Simon Genin (ges@odoo) Signed-off-by: Hubert Van De Walle --- .../web/static/tests/views/list_view_tests.js | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/addons/web/static/tests/views/list_view_tests.js b/addons/web/static/tests/views/list_view_tests.js index ef5aad42df4..8ac94b40b69 100644 --- a/addons/web/static/tests/views/list_view_tests.js +++ b/addons/web/static/tests/views/list_view_tests.js @@ -19970,4 +19970,37 @@ QUnit.module("Views", (hooks) => { assert.verifySteps(["switch to form - resId: 5 activeIds: 5,1,2,3,4"]); } ); + + QUnit.test("onchange should only be called once after pressing enter on a field", async function (assert) { + serverData.models.foo.onchanges = { + foo(record) { + if (record.foo) { + record.int_field = 1; + } + }, + }; + await makeView({ + type: "list", + resModel: "foo", + serverData, + arch: ` + + + + `, + async mockRPC(_, { method }) { + if (method === "onchange") { + assert.step(method); + } + }, + }); + await click(target.querySelector(".o_data_cell")); + target.querySelector(".o_field_widget[name=foo] input").value = "1"; + await triggerEvents(target, ".o_field_widget[name=foo] input", [ + ["keydown", { key: "Enter" }], + ["change"], + ]); + await nextTick(); + assert.verifySteps(["onchange"], "There should only be one onchange call"); + }); });