Skip to content

Commit

Permalink
Fix bug when pushing to an empty array (longhand) (#168)
Browse files Browse the repository at this point in the history
* Fix bug when pushing to an empty array (longhand)

* Adding a changeset
  • Loading branch information
matthewp authored Sep 28, 2022
1 parent 1091b54 commit 3efbd6a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/small-shrimps-walk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"corset": patch
---

Update when pushing to an empty array using longhand each
4 changes: 2 additions & 2 deletions src/multi-binding.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ export class MultiBinding extends Binding {

// Fill in the defaults by looking at the valueMap for holes.
for(let [key, values] of valueMap) {
if(!dirtyKeys.has(key)) continue;
let isDirty = dirtyKeys.has(key);
// valueMap is always appended from a longhand prop.
let numOfValues = this.numberOfValues;
let keyed = this.defn.feat & features.keyed;
Expand All @@ -298,7 +298,7 @@ export class MultiBinding extends Binding {
values.push(...current);
}
}
yield [/** @type {[K, ...any[]]} */(/** @type {unknown} */(values)), true];
yield [/** @type {[K, ...any[]]} */(/** @type {unknown} */(values)), isDirty];
}

// Yield out to reset to initial state.
Expand Down
25 changes: 25 additions & 0 deletions test/test-each.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,29 @@ QUnit.test('Changes to items result in updates', assert => {
bind().update(root);
assert.equal(ul.firstElementChild.textContent, 'one');
assert.equal(ul.firstElementChild.nextElementSibling.textContent, 'two !!');
});

QUnit.test('Pushing to an empty list (longhand)', assert => {
let root = document.createElement('main');
root.innerHTML = `<ul></ul><template><li></li></template>`;
let items = [];
function bind() {
return sheet`
ul {
each-items: ${items};
each-template: select(template);
}
li {
text: get(item(), label);
}
`;
}
let ul = root.firstElementChild;
bind().update(root);

items.push({ label: 'works' });
bind().update(root);
assert.equal(ul.children.length, 1);
assert.equal(ul.firstElementChild.textContent, 'works');
});

0 comments on commit 3efbd6a

Please sign in to comment.