Skip to content

Commit

Permalink
feat: Adds descriptive warning for undefined x-for key and updates te…
Browse files Browse the repository at this point in the history
…sts. (#3498)
  • Loading branch information
nickdawes committed May 10, 2023
1 parent 3a75b62 commit 9ad43f1
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 9 deletions.
18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions packages/alpinejs/src/directives/x-for.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ function loop(el, iteratorNames, evaluateItems, evaluateKey) {
let marker = document.createElement('div')

mutateDom(() => {
if (!elForSpot) {
throw new Error("AlpineJS x-for Error: ':key' is undefined or invalid. Please ensure you're providing a valid key attribute.");
}
elForSpot.after(marker)
elInSpot.after(elForSpot)
elForSpot._x_currentIfEl && elForSpot.after(elForSpot._x_currentIfEl)
Expand Down
26 changes: 26 additions & 0 deletions tests/cypress/integration/directives/x-for.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -559,3 +559,29 @@ test('renders children using directives injected by x-html correctly',
get('p:nth-of-type(2) span').should(haveText('bar'))
}
)

test('x-for throws descriptive error when key is undefined',
html`
<div x-data="{ items: [
{
id: 1,
name: 'foo',
},
{
id: 2,
name: 'bar',
},
{
id: 3,
name: 'baz',
},
]}">
<template x-for="item in items" :key="item.doesntExist">
<span x-text="i"></span>
</template>
</div>
`,
({ get }) => {
},
true
)

0 comments on commit 9ad43f1

Please sign in to comment.