From 803b4ac29a14e77880e8ad4e9ca6e6c5e3871711 Mon Sep 17 00:00:00 2001 From: Julien Mougenot Date: Tue, 12 Dec 2023 13:20:20 +0100 Subject: [PATCH] [FIX] web: do not apply same datetime picker value Before this commit, the equality check performed by the datetime service relied on a JSON-strigified version of the value on one end, and on an array of ISO strings on the other end. This meant that the value was never equal to itself and that the `onApply` callback would be called even if the value remained unchanged. This commit fixes that by ensuring that the compared values have the same shape. closes odoo/odoo#145929 Signed-off-by: Mathieu Duckerts-Antoine (dam) --- .../core/datetime/datetimepicker_service.js | 9 +++--- .../core/datetime/datetime_hook_tests.js | 32 +++++++++++++++++++ 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/addons/web/static/src/core/datetime/datetimepicker_service.js b/addons/web/static/src/core/datetime/datetimepicker_service.js index 9c7242c9e1c..cf03620ba17 100644 --- a/addons/web/static/src/core/datetime/datetimepicker_service.js +++ b/addons/web/static/src/core/datetime/datetimepicker_service.js @@ -76,8 +76,7 @@ export const datetimePickerService = { * value has changed, and set other internal variables accordingly. */ const apply = () => { - const serializedValue = JSON.stringify(pickerProps.value); - if (areDatesEqual(lastInitialProps?.value, serializedValue)) { + if (areDatesEqual(lastInitialProps?.value, deepCopy(pickerProps.value))) { return; } @@ -89,13 +88,13 @@ export const datetimePickerService = { const computeBasePickerProps = () => { const nextInitialProps = markValuesRaw(hookParams.pickerProps); - const serializedProps = deepCopy(nextInitialProps); + const propsCopy = deepCopy(nextInitialProps); - if (lastInitialProps && arePropsEqual(lastInitialProps, serializedProps)) { + if (lastInitialProps && arePropsEqual(lastInitialProps, propsCopy)) { return; } - lastInitialProps = serializedProps; + lastInitialProps = propsCopy; inputsChanged = ensureArray(lastInitialProps.value).map(() => false); for (const [key, value] of Object.entries(nextInitialProps)) { diff --git a/addons/web/static/tests/core/datetime/datetime_hook_tests.js b/addons/web/static/tests/core/datetime/datetime_hook_tests.js index c2548d2a88c..0bad565c800 100644 --- a/addons/web/static/tests/core/datetime/datetime_hook_tests.js +++ b/addons/web/static/tests/core/datetime/datetime_hook_tests.js @@ -147,4 +147,36 @@ QUnit.module("Components", ({ beforeEach }) => { assert.strictEqual(pickerProps.value.toSQL().split(" ")[0], "2023-06-06"); }); + + QUnit.test("value is not updated if it did not change", async (assert) => { + const getShortDate = (date) => date.toSQL().split(" ")[0]; + + let pickerProps; + const defaultPickerProps = { + value: DateTime.fromSQL("2023-06-06"), + type: "date", + }; + + const input = await mountInput(() => { + pickerProps = useDateTimePicker({ + pickerProps: defaultPickerProps, + onApply: (value) => { + assert.step(getShortDate(value)); + }, + }).state; + }); + + assert.strictEqual(input.value, "06/06/2023"); + assert.strictEqual(getShortDate(pickerProps.value), "2023-06-06"); + + await editInput(input, null, "06/06/2023"); + + assert.strictEqual(getShortDate(pickerProps.value), "2023-06-06"); + assert.verifySteps([]); + + await editInput(input, null, "07/06/2023"); + + assert.strictEqual(getShortDate(pickerProps.value), "2023-06-07"); + assert.verifySteps(["2023-06-07"]); + }); });