I have been struggling to write a Jest test for a simple component that includes a nested v-data-table
component. While the page renders correctly in the browser, my test keeps failing.
The problem seems to lie with the template I am using for the slot - it was copied directly from the documentation.
Interestingly, when I remove the template with the v-slot
attribute, the test runs without any issues.
Here is an excerpt from People.vue:
<template>
<v-data-table
:headers="headers"
:items="people"
disable-pagination
disable-sort
disable-filtering
hide-default-footer
:loading="!people"
>
<template v-slot:item.id="{ item }">
<v-icon>
mdi-link-variant
</v-icon>
<router-link
:to="{ name: 'assignments', query: { assignee_id: item.id } }"
>Link</router-link
>
</template>
</v-data-table>
</template>
<script>
export default {
name: "People",
data: () => ({
headers: [
{
text: "Name",
value: "first_name"
},
{
text: "Assignment link",
value: "id"
}
]
}),
props: {
people: {
type: Array,
required: true
}
}
};
</script>
And here is a snippet from People.spec.js:
import { shallowMount } from "@vue/test-utils";
import People from "@/components/People.vue";
function getMountedComponent(Component, propsData) {
return shallowMount(Component, { propsData });
}
const propsData = {
people: [{ id: 1 }]
};
describe("headers", () => {
it("contains name and assignment", () => {
expect(getMountedComponent(People, propsData).vm.headers).toEqual([
{
text: "Name",
value: "first_name"
},
{
text: "Assignment link",
value: "id"
}
]);
});
});
The error message I'm receiving is as follows:
console.error node_modules/vue/dist/vue.runtime.common.dev.js:621
[Vue warn]: Error in render: "TypeError: Cannot read property 'item' of undefined"
found in
---> <VDataTable>
<People>
<Root>
console.error node_modules/vue/dist/vue.runtime.common.dev.js:1884
TypeError: Cannot read property 'item' of undefined