Skip to content
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
32 changes: 32 additions & 0 deletions test/test-template-engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -1241,6 +1241,38 @@ describe('html updaters', () => {
assert(container.querySelector('#c').textContent === 'fuzz');
container.remove();
});

it('repeat callbacks are provided args from underlying “.map” call', () => {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI —Added this test first to prove that we could repro failure in our test suite. Then, added the related fix.

// The identify callback is written in a bizarre way to test that all the
// expected function arguments are actually passed in. If they weren’t you
// would get duplicated key errors or undefined key errors.
const getTemplate = ({ items }) => {
return html`
<div id="target">
${repeat(items, (_, index, array) => array?.[index]?.id, (_, index, array) => {
return html`<div id="${array?.[index]?.id}" class="item">${array?.[index]?.id}</div>`;
})}
</div>
`;
};
const container = document.createElement('div');
document.body.append(container);
render(container, getTemplate({ items: [{ id: 'foo' }, { id: 'bar'}, { id: 'baz' }] }));
const foo = container.querySelector('#foo');
const bar = container.querySelector('#bar');
const baz = container.querySelector('#baz');
assert(container.querySelector('#target').childElementCount === 3);
assert(!!foo);
assert(!!bar);
assert(!!baz);
assert(container.querySelector('#target').children[0] === foo);
assert(container.querySelector('#target').children[1] === bar);
assert(container.querySelector('#target').children[2] === baz);
assert(foo.textContent === 'foo');
assert(bar.textContent === 'bar');
assert(baz.textContent === 'baz');
container.remove();
});
});

describe('updater errors', () => {
Expand Down
4 changes: 2 additions & 2 deletions x-template.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ class TemplateEngine {
throw new Error(`Unexpected repeat callback "${callback}" provided, expected a function.`);
}
return identify
? items.map(item => [identify(item), callback(item)])
: items.map(item => callback(item)); // Just a basic array.
? items.map((...args) => [identify(...args), callback(...args)])
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Super simple fix to just pass everything through as-is.

: items.map((...args) => callback(...args)); // Just a basic array.
}

// Deprecated. Will remove in future release.
Expand Down