NuxtN
Nuxt13mo ago
Yuri

Change URL in unit-test with useRoute

import {describe, it, expect,} from 'vitest';
import { mockNuxtImport, mountSuspended } from '@nuxt/test-utils/runtime';
import LayoutLogo from '../LayoutLogo.vue';
import {useLayoutStore} from "~/store/layout";
import {NuxtLink} from "#components";

// Mock useRoute and useLayoutStore
mockNuxtImport('useRoute', () => {
    return () => ({
        fullPath: '/' // default mock path
    });
});

const initialLogo = {
    width: 100,
    height: 50,
    alt: '',
    src:"https://example.com/test-image.jpg",
}
describe('LayoutLogo.vue', () => {
    const createWrapper = async () => {
        const layoutStore = useLayoutStore();
        layoutStore.logo = initialLogo
        return await mountSuspended(LayoutLogo);
    };

    it('Рендер корректного компонента в зависимости от страницы', async () => {
        const wrapper = await createWrapper();
        expect(wrapper.find('> div').exists()).toBe(true);
        
        // HOW CHANGE URL ?
        const wrapper2 = await createWrapper();
        expect(wrapper2.findComponent(NuxtLink).exists()).toBe(true);
        expect(wrapper2.find('div').exists()).toBe(false);
    });
});

I need to change the URL to another, for example '/catalog/', before rending wrapper2. How can I do it?
Was this page helpful?