From 28d12060010229509ec27e7452827899a204354f Mon Sep 17 00:00:00 2001 From: Eric Kwoka <43540491+ekwoka@users.noreply.github.com> Date: Fri, 8 Mar 2024 16:32:39 +0400 Subject: [PATCH] :bug: Prevents Setting from registering a dependency (#4081) --- packages/alpinejs/src/scope.js | 6 +++-- tests/cypress/integration/scope.spec.js | 30 ++++++++++++++++++++++--- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/packages/alpinejs/src/scope.js b/packages/alpinejs/src/scope.js index e1f36feb5..101a8e8d4 100644 --- a/packages/alpinejs/src/scope.js +++ b/packages/alpinejs/src/scope.js @@ -48,6 +48,7 @@ let mergeProxyTrap = { if (name == Symbol.unscopables) return false; return objects.some((obj) => + Object.prototype.hasOwnProperty.call(obj, name) || Reflect.has(obj, name) ); }, @@ -65,8 +66,9 @@ let mergeProxyTrap = { }, set({ objects }, name, value, thisProxy) { - const target = objects.find((obj) => - Reflect.has(obj, name) + const target = + objects.find((obj) => + Object.prototype.hasOwnProperty.call(obj, name) ) || objects[objects.length - 1]; const descriptor = Object.getOwnPropertyDescriptor(target, name); if (descriptor?.set && descriptor?.get) diff --git a/tests/cypress/integration/scope.spec.js b/tests/cypress/integration/scope.spec.js index 3efa0d3fc..acec28111 100644 --- a/tests/cypress/integration/scope.spec.js +++ b/tests/cypress/integration/scope.spec.js @@ -69,12 +69,16 @@ test( this.value++; } } - document.addEventListener("alpine:init", () => + document.addEventListener("alpine:init", () => Alpine.data("counter", () => new Counter()) - ) + );
- +
`, ], @@ -85,3 +89,23 @@ test( } ); +test( + "setting value doesn't register a dependency", + [ + html` +
+ +
+ ; + `, + ], + ({ get }) => { + get("button").should(haveText("effected")); + get("button").click(); + get("button").should(haveText("clicked")); + } +);