Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Fixes proxy stack setters bug #3807

Merged
merged 3 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions packages/alpinejs/src/scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,14 @@ let mergeProxyTrap = {
)
},

set({ objects }, name, value) {
return Reflect.set(
objects.find((obj) =>
set({ objects }, name, value, thisProxy) {
const target = objects.find((obj) =>
Object.prototype.hasOwnProperty.call(obj, name)
) || objects[objects.length-1],
name,
value
)
) || objects[objects.length - 1];
const descriptor = Object.getOwnPropertyDescriptor(target, name);
if (descriptor?.set && descriptor?.get)
return Reflect.set(target, name, value, thisProxy);
return Reflect.set(target, name, value);
},
}

Expand Down
58 changes: 58 additions & 0 deletions tests/cypress/integration/scope.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { haveText, html, test } from "../utils";

test(
"properly merges the datastack",
[
html`
<div x-data="{ foo: 'fizz' }">
<div x-data="{ bar: 'buzz' }">
<span x-text="foo + bar"></span>
</div>
</div>
`,
],
({ get }) => {
get("span").should(haveText("fizzbuzz"));
}
);

test(
"merges stack from bottom up",
[
html`
<div x-data="{ foo: 'fizz' }">
<div x-data="{ foo: 'buzz', get bar() { return this.foo } }">
<span id="one" x-text="bar + foo"></span>
</div>
<span id="two" x-text="foo"></span>
</div>
`,
],
({ get }) => {
get("span#one").should(haveText("buzzbuzz"));
get("span#two").should(haveText("fizz"));
}
);

test(
"handles getter setter pairs",
[
html`
<div x-data="{ foo: 'fizzbuzz' }">
<div
x-data="{ get bar() { return this.foo }, set bar(value) { this.foo = value } }"
>
<span id="one" x-text="bar" @click="bar = 'foobar'"></span>
</div>
<span id="two" x-text="foo"></span>
</div>
`,
],
({ get }) => {
get("span#one").should(haveText("fizzbuzz"));
get("span#two").should(haveText("fizzbuzz"));
get("span#one").click();
get("span#one").should(haveText("foobar"));
get("span#two").should(haveText("foobar"));
}
);