[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) <dam@odoo.com>
This commit is contained in:
Julien Mougenot
2023-12-12 23:08:57 +00:00
parent f0924e0b6f
commit 803b4ac29a
2 changed files with 36 additions and 5 deletions
@@ -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)) {
@@ -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"]);
});
});