diff --git a/addons/web/static/src/core/utils/patch.js b/addons/web/static/src/core/utils/patch.js index 84929850799..8d27121625a 100644 --- a/addons/web/static/src/core/utils/patch.js +++ b/addons/web/static/src/core/utils/patch.js @@ -50,7 +50,7 @@ export function patch(obj, patchName, patchValue, options = {}) { if (prevDesc.value && typeof newDesc.value === "function") { makeIntermediateFunction("value", prevDesc, newDesc, patchedFnName); } - if (prevDesc.get || prevDesc.set) { + if ((newDesc.get || newDesc.set) && (prevDesc.get || prevDesc.set)) { // get and set are defined together. If they are both defined // in the previous descriptor but only one in the new descriptor // then the other will be undefined so we need to apply the diff --git a/addons/web/static/tests/core/utils/patch_tests.js b/addons/web/static/tests/core/utils/patch_tests.js index a675a7ff9a9..20e8213af42 100644 --- a/addons/web/static/tests/core/utils/patch_tests.js +++ b/addons/web/static/tests/core/utils/patch_tests.js @@ -441,6 +441,39 @@ QUnit.module("utils", () => { assert.strictEqual(instance.dynamic, "base"); }); + QUnit.test("patch getter/setter with value", async function (assert) { + assert.expect(4); + + const BaseClass = makeBaseClass(assert, false); + + const originalDescriptor = Object.getOwnPropertyDescriptor( + BaseClass.prototype, + "dynamic" + ); + patch(BaseClass.prototype, "patch", { + dynamic: "patched", + }); + + const instance = new BaseClass(); + + assert.deepEqual(Object.getOwnPropertyDescriptor(BaseClass.prototype, "dynamic"), { + value: "patched", + writable: true, + configurable: true, + enumerable: true, + }); + assert.equal(instance.dynamic, "patched"); + + unpatch(BaseClass.prototype, "patch"); + + instance.dynamic = "base"; + assert.deepEqual( + Object.getOwnPropertyDescriptor(BaseClass.prototype, "dynamic"), + originalDescriptor + ); + assert.strictEqual(instance.dynamic, "base"); + }); + QUnit.test("async function", async function (assert) { assert.expect(3);