How to test components that depend on the (internal) layout composable? #18076
Replies: 7 comments 4 replies
-
|
Seems like i'm struggling with the same issue. That I get this error on a 'app-bar' component? Anyone have an answer for this issue? @backfromexile you already have a solution? |
Beta Was this translation helpful? Give feedback.
-
|
@backfromexile If you provide more context, I will be able to find final solution more easily. |
Beta Was this translation helpful? Give feedback.
-
|
Hello, this is the solution for now import { h } from "vue";
import { VApp } from "vuetify/components";
import MyComponent from "./MyComponent.vue";
describe("Test", () => {
it("should", () => {
const wrapper = mount(VApp, {
slots: {
default: h(MyComponent),
}
})
)}
)} |
Beta Was this translation helpful? Give feedback.
-
|
Components like VMain, VNavigationDrawer, VBottomNavigation, VAppBar and VLayoutItem are asynchronous, so the v-app waits for them to render. await new Promise((res) => {
setTimeout(() => res(''), 1)
})I created a problem, maybe there will be some more detailed information #19895 |
Beta Was this translation helpful? Give feedback.
-
|
Here is a full Nuxt example of a unit test that worked for me. I provide this since I needed a longer timeout to make it work and it took me a bit to assemble the information provided by different answers here. import { mountSuspended } from '@nuxt/test-utils/runtime';
import { VApp } from 'vuetify/components';
describe('v-navigation-drawer', () => {
it('renders the test component', async () => {
const wrapper = await mountSuspended(VApp, {
slots: {
default: {template: `<v-navigation-drawer
v-model="showMenu"
temporary
location="right"
color="rgba(75, 84, 86, 0.5)"
>
some content
</v-navigation-drawer>`},
},
});
await new Promise((resolve) => {
setTimeout(() => resolve(''), 500);
});
expect(wrapper.html()).toMatchInlineSnapshot(`
"<div class="v-application v-theme--light v-layout v-layout--full-height v-locale--is-ltr">
<div class="v-application__wrap">
<v-navigation-drawer temporary="" location="right" color="rgba(75, 84, 86, 0.5)"> some content </v-navigation-drawer>
</div>
</div>"
`);
});
}); |
Beta Was this translation helpful? Give feedback.
-
|
Hello, I get the same error in my unit tests "Could not find injected layout" |
Beta Was this translation helpful? Give feedback.
-
|
Where 'YourComponent' uses 'v-navigation-drawer': import { mount } from "@vue/test-utils";
import { createVuetify } from "vuetify";
import YourComponent from "@/components/YourComponent.vue";
import {
VNavigationDrawer,
VApp,
} from "vuetify/components";
const Component = {
template: "<v-app><YourComponent></YourComponent></v-app>",
};
global.ResizeObserver = vi.fn().mockImplementation(() => ({
observe: vi.fn(),
unobserve: vi.fn(),
disconnect: vi.fn(),
}));
describe("YourComponent", async () => {
const wrapper = mount(Component, {
global: {
plugins: [createVuetify()],
components: {
"v-app": VApp,
"v-navigation-drawer": VNavigationDrawer,
YourComponent: YourComponent,
},
},
});
}); |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
For any Vue component I want to unit test that uses any Vuetify component which itself depends on the layout composable (these components are VMain, VFooter, VNavigationDrawer, VBottomNavigation, VSystemBar, VAppBar, VLayoutItem), I get the error
Error: Could not find injected Vuetify layoutwhen mounting the component isolated.How are we supposed to test our components? Of course I could wrap the component in a VApp or VLayout for the test, but for example
vue-test-utilshas no proper way to do that (afaik only cypress does), and also it kind of defeats the point of isolated testing.Beta Was this translation helpful? Give feedback.
All reactions