Skip to content

Commit 317aa5f

Browse files
NullVoxPopuliclaude
andcommitted
Add test: each over array of objects preserves this on item methods
Iterates over an array of Item instances with {{#each}}, passes item.greet to {{on "click"}} for each, and asserts that each handler fires with the correct Item as this. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
1 parent 4bcd303 commit 317aa5f

1 file changed

Lines changed: 43 additions & 0 deletions

File tree

packages/@ember/-internals/glimmer/tests/integration/this-binding-test.gjs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,5 +113,48 @@ moduleFor(
113113
);
114114
assert.strictEqual(receivedArg, 'did it', 'argument from fn is passed through');
115115
}
116+
117+
['@test each over array of objects preserves this binding on item methods'](assert) {
118+
let seenPairs = [];
119+
120+
class Item {
121+
constructor(name) {
122+
this.name = name;
123+
}
124+
125+
greet() {
126+
seenPairs.push({ self: this, name: this.name });
127+
}
128+
}
129+
130+
let items = [new Item('alice'), new Item('bob'), new Item('carol')];
131+
132+
class Demo extends Component {
133+
constructor(...args) {
134+
super(...args);
135+
this.items = items;
136+
}
137+
138+
<template>
139+
{{#each this.items as |item|}}
140+
<button type="button" class={{item.name}} {{on "click" item.greet}}>{{item.name}}</button>
141+
{{/each}}
142+
</template>
143+
}
144+
145+
this.render(`<this.Demo />`, { Demo });
146+
147+
runTask(() => this.$('button.alice').click());
148+
runTask(() => this.$('button.bob').click());
149+
runTask(() => this.$('button.carol').click());
150+
151+
assert.strictEqual(seenPairs.length, 3, 'all three handlers fired');
152+
assert.strictEqual(seenPairs[0].self, items[0], 'alice: this is the Item instance');
153+
assert.strictEqual(seenPairs[0].name, 'alice', 'alice: this.name is correct');
154+
assert.strictEqual(seenPairs[1].self, items[1], 'bob: this is the Item instance');
155+
assert.strictEqual(seenPairs[1].name, 'bob', 'bob: this.name is correct');
156+
assert.strictEqual(seenPairs[2].self, items[2], 'carol: this is the Item instance');
157+
assert.strictEqual(seenPairs[2].name, 'carol', 'carol: this.name is correct');
158+
}
116159
}
117160
);

0 commit comments

Comments
 (0)