From 23185f6a30fa5f44757b8d43d13766b9e11f77f3 Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Sun, 24 Dec 2023 10:54:57 +0100 Subject: [PATCH] test: fixes --- src/use-query.spec.ts | 42 ++++++++++++++++++++++++++++++++++++++++-- src/use-query.ts | 4 ++-- 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/src/use-query.spec.ts b/src/use-query.spec.ts index 1b3f0361..3a9833dd 100644 --- a/src/use-query.spec.ts +++ b/src/use-query.spec.ts @@ -3,6 +3,7 @@ import { UseQueryOptions, useQuery } from './use-query' import { mount } from '@vue/test-utils' import { createPinia } from 'pinia' import { defineComponent, nextTick } from 'vue' +import { GlobalMountOptions } from 'node_modules/@vue/test-utils/dist/types' const delay = (ms: number) => new Promise((r) => setTimeout(r, ms)) @@ -25,7 +26,8 @@ describe('useQuery', () => { } const mountSimple = ( - options: Partial> = {} + options: Partial> = {}, + mountOptions?: GlobalMountOptions ) => { const fetcher = options.fetcher ? vi.fn(options.fetcher) @@ -50,6 +52,7 @@ describe('useQuery', () => { { global: { plugins: [createPinia()], + ...mountOptions, }, } ) @@ -132,7 +135,24 @@ describe('useQuery', () => { expect(wrapper.vm.data).toBe(42) }) - it.todo('new mount does not fetch if staleTime is not elapsed') + it('new mount does not fetch if staleTime is not elaopsed', async () => { + const pinia = createPinia() + const [w1, f1] = mountSimple({ staleTime: 1000 }, { plugins: [pinia] }) + + await runTimers() + + // should not trigger a new fetch because staleTime has not passed + vi.advanceTimersByTime(500) + + const [w2, f2] = mountSimple({ staleTime: 1000 }, { plugins: [pinia] }) + + await runTimers() + + expect(f1).toHaveBeenCalledTimes(1) + expect(f2).toHaveBeenCalledTimes(0) + expect(w1.vm.data).toBe(42) + expect(w2.vm.data).toBe(42) + }) it('when refreshed, fetches the data if the staleTime has been elapsed', async () => { const { wrapper, fetcher } = mountSimple({ staleTime: 1000 }) @@ -149,6 +169,24 @@ describe('useQuery', () => { expect(wrapper.vm.data).toBe(42) }) + it('new mount fetches if staleTime is elapsed', async () => { + const [w1, f1] = mountSimple({ staleTime: 1000 }) + + await runTimers() + + // should not trigger a new fetch because staleTime has not passed + vi.advanceTimersByTime(1001) + + const [w2, f2] = mountSimple({ staleTime: 1000 }) + + await runTimers() + + expect(f1).toHaveBeenCalledTimes(1) + expect(f2).toHaveBeenCalledTimes(1) + expect(w1.vm.data).toBe(42) + expect(w2.vm.data).toBe(42) + }) + it.todo('reuses a pending request even if the staleTime has been elapsed') }) diff --git a/src/use-query.ts b/src/use-query.ts index 43d3c176..283ac9e8 100644 --- a/src/use-query.ts +++ b/src/use-query.ts @@ -95,14 +95,14 @@ export function useQuery( if (options.refetchOnWindowFocus) { useEventListener(document, 'visibilitychange', () => { if (document.visibilityState === 'visible') { - entry.value.refresh() + entry.value.refetch() } }) } if (options.refetchOnReconnect) { useEventListener(window, 'online', () => { - entry.value.refresh() + entry.value.refetch() }) } }