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

feat: Adds descriptive warning for undefined x-for key and updates tests. #3498

Merged
merged 1 commit into from
May 10, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
)