NuxtN
Nuxt13mo ago
Yuri

mountSuspended & useCookie

Hello. Why my test show incorrect value for cookie?
Log:
Mock useCookie with value 0
Mock useCookie with value 1

locationConfirmationIsHidden (there isn't cookie): 1
locationConfirmationIsHidden (there is cookie): 1

Part of my component:
const locationConfirmationIsHidden = useCookie<number>(
  CookieLocationConfirmationpIsHidden,
  cookieGenerateOptionsClient(60 * 60 * 24 * 30, () => 0),
)
  await useAsyncData('layoutGetCurrent', () => locationStore.getCurrent())


My test:
describe('LayoutHeaderLocation.vue', () => {
    const createWrapper = async (initialState = { id: 1, canChange: true, title: 'Астрахань' }) => {
      .....
        return await mountSuspended(LayoutHeaderLocation);
    };

    beforeEach(() => {
        vi.clearAllMocks();
        vi.resetAllMocks();
        mockNuxtImport('useCookie', () => () => ({ value: 0 })); 
    });
    it('there isn't cookie', async () => {
        mockNuxtImport('useCookie', () => {
            console.log('Mock useCookie with value 0');
            return () => ({ value: 0 });
        });
        const wrapper = await createWrapper({ id: 1, canChange: true, title: 'Астрахань' });
        await flushPromises();
        await wrapper.vm.$nextTick();
        console.log('locationConfirmationIsHidden (test куки нет):', wrapper.vm.locationConfirmationIsHidden?.value);
    });

    it('there is cookie', async () => {
        mockNuxtImport('useCookie', () => {
            console.log('Mock useCookie with value 1');
            return () => ({ value: 1 });
        });
        const wrapper = await createWrapper({ id: 1, canChange: true, title: 'Астрахань' });
        await flushPromises();
        await wrapper.vm.$nextTick();
        console.log('locationConfirmationIsHidden (there isnt't cookie):', wrapper.vm.locationConfirmationIsHidden?.value);
   
    });
Was this page helpful?