[FIX] web: allow patching descriptors with getter/setter with value

Previously, it was impossible to patch a property that was defined with
a getter or a setter with a simple value property. This commit fixes
that.

X-original-commit: cd7514e35b458143a948c7b8e73b05760df0a482
Part-of: odoo/odoo#80543
This commit is contained in:
Samuel Degueldre
2021-11-30 12:06:07 +00:00
parent ed80461b66
commit 431e802e31
2 changed files with 34 additions and 1 deletions
+1 -1
View File
@@ -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
@@ -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);