Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,20 @@ import { ErrorBoundary } from './ErrorBoundary'
import { renderWithClient } from './utils'

describe('QueryErrorResetBoundary', () => {
let queryCache: QueryCache
let queryClient: QueryClient

beforeEach(() => {
vi.useFakeTimers()
queryCache = new QueryCache()
queryClient = new QueryClient({ queryCache })
})

afterEach(() => {
vi.useRealTimers()
queryClient.clear()
})

const queryCache = new QueryCache()
const queryClient = new QueryClient({ queryCache })

describe('useQuery', () => {
it('should retry fetch if the reset error boundary has been reset', async () => {
const consoleMock = vi
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,20 @@ import { QueryCache, QueryClient, hashKey, useQuery } from '..'
import { renderWithClient } from './utils'

describe('fine grained persister', () => {
let queryCache: QueryCache
let queryClient: QueryClient

beforeEach(() => {
vi.useFakeTimers()
queryCache = new QueryCache()
queryClient = new QueryClient({ queryCache })
})

afterEach(() => {
vi.useRealTimers()
queryClient.clear()
})

const queryCache = new QueryCache()
const queryClient = new QueryClient({ queryCache })

it('should restore query state from persister and not refetch', async () => {
const key = queryKey()
const hash = hashKey(key)
Expand Down
13 changes: 1 addition & 12 deletions packages/preact-query/src/__tests__/ssr.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ describe('Server Side Rendering', () => {

afterEach(() => {
vi.useRealTimers()
queryClient.clear()
})

it('should not trigger fetch', () => {
Expand All @@ -53,8 +54,6 @@ describe('Server Side Rendering', () => {

expect(markup).toContain('status pending')
expect(queryFn).toHaveBeenCalledTimes(0)

queryCache.clear()
})

it('should add prefetched data to cache', async () => {
Expand All @@ -70,8 +69,6 @@ describe('Server Side Rendering', () => {

expect(data).toBe('data')
expect(queryCache.find({ queryKey: key })?.state.data).toBe('data')

queryCache.clear()
})

it('should return existing data from the cache', async () => {
Expand Down Expand Up @@ -101,8 +98,6 @@ describe('Server Side Rendering', () => {

expect(markup).toContain('status success')
expect(queryFn).toHaveBeenCalledTimes(1)

queryCache.clear()
})

it('should add initialData to the cache', () => {
Expand Down Expand Up @@ -133,8 +128,6 @@ describe('Server Side Rendering', () => {
const keys = queryCache.getAll().map((query) => query.queryKey)

expect(keys).toEqual([[key, 1]])

queryCache.clear()
})

it('useMutationState should return empty array', () => {
Expand All @@ -151,8 +144,6 @@ describe('Server Side Rendering', () => {
)

expect(markup).toContain('mutationState: 0')

queryCache.clear()
})

it('useInfiniteQuery should return the correct state', async () => {
Expand Down Expand Up @@ -190,7 +181,5 @@ describe('Server Side Rendering', () => {

expect(markup).toContain('page 1')
expect(queryFn).toHaveBeenCalledTimes(1)

queryCache.clear()
})
})
1 change: 1 addition & 0 deletions packages/preact-query/src/__tests__/suspense.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ describe('Suspense Timer Tests', () => {

afterEach(() => {
vi.useRealTimers()
queryClient.clear()
})

it('should enforce minimum staleTime of 1000ms when using suspense with number', async () => {
Expand Down
23 changes: 13 additions & 10 deletions packages/preact-query/src/__tests__/useInfiniteQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,25 @@ const fetchItems = async (
}

describe('useInfiniteQuery', () => {
let queryCache: QueryCache
let queryClient: QueryClient

beforeEach(() => {
vi.useFakeTimers()
queryCache = new QueryCache()
queryClient = new QueryClient({
queryCache,
defaultOptions: {
queries: {
experimental_prefetchInRender: true,
},
},
})
})

afterEach(() => {
vi.useRealTimers()
})

const queryCache = new QueryCache()
const queryClient = new QueryClient({
queryCache,
defaultOptions: {
queries: {
experimental_prefetchInRender: true,
},
},
queryClient.clear()
})

it('should return the correct states for a successful query', async () => {
Expand Down
1 change: 1 addition & 0 deletions packages/preact-query/src/__tests__/useMutation.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ describe('useMutation', () => {

afterEach(() => {
vi.useRealTimers()
queryClient.clear()
})

it('should be able to reset `data`', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,21 @@ const generateInfiniteQueryOptions = (
}

describe('usePrefetchInfiniteQuery', () => {
let queryCache: QueryCache
let queryClient: QueryClient

beforeEach(() => {
vi.useFakeTimers()
queryCache = new QueryCache()
queryClient = new QueryClient({ queryCache })
})

afterEach(() => {
vi.useRealTimers()
queryClient.clear()
Fallback.mockClear()
vi.useRealTimers()
})

const queryCache = new QueryCache()
const queryClient = new QueryClient({ queryCache })

const Fallback = vi.fn().mockImplementation(() => <div>Loading...</div>)

function Suspended<T = unknown>(props: {
Expand Down
7 changes: 5 additions & 2 deletions packages/preact-query/src/__tests__/usePrefetchQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,18 @@ const generateQueryFn = (data: string) =>
})

describe('usePrefetchQuery', () => {
const queryCache = new QueryCache()
const queryClient = new QueryClient({ queryCache })
let queryCache: QueryCache
let queryClient: QueryClient

beforeEach(() => {
vi.useFakeTimers()
queryCache = new QueryCache()
queryClient = new QueryClient({ queryCache })
})

afterEach(() => {
vi.useRealTimers()
queryClient.clear()
})

function Suspended<TData = unknown>(props: {
Expand Down
9 changes: 6 additions & 3 deletions packages/preact-query/src/__tests__/useQueries.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,20 @@ import { ErrorBoundary } from './ErrorBoundary'
import { renderWithClient } from './utils'

describe('useQueries', () => {
let queryCache: QueryCache
let queryClient: QueryClient

beforeEach(() => {
vi.useFakeTimers()
queryCache = new QueryCache()
queryClient = new QueryClient({ queryCache })
})

afterEach(() => {
vi.useRealTimers()
queryClient.clear()
})

const queryCache = new QueryCache()
const queryClient = new QueryClient({ queryCache })

it('should return the correct states', async () => {
const key1 = queryKey()
const key2 = queryKey()
Expand Down
1 change: 1 addition & 0 deletions packages/preact-query/src/__tests__/useQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ describe('useQuery', () => {

afterEach(() => {
vi.useRealTimers()
queryClient.clear()
})

// See https://github.com/tannerlinsley/react-query/issues/105
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { queryKey } from '@tanstack/query-test-utils'
import { Suspense } from 'preact/compat'
import { describe, expect, it, vi } from 'vitest'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'

import {
QueryCache,
Expand All @@ -11,8 +11,17 @@ import {
import { renderWithClient } from './utils'

describe('useSuspenseInfiniteQuery', () => {
const queryCache = new QueryCache()
const queryClient = new QueryClient({ queryCache })
let queryCache: QueryCache
let queryClient: QueryClient

beforeEach(() => {
queryCache = new QueryCache()
queryClient = new QueryClient({ queryCache })
})

afterEach(() => {
queryClient.clear()
})

it('should log an error when skipToken is passed as queryFn', () => {
const consoleErrorSpy = vi
Expand Down
9 changes: 6 additions & 3 deletions packages/preact-query/src/__tests__/useSuspenseQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,20 @@ import { ErrorBoundary } from './ErrorBoundary'
import { renderWithClient } from './utils'

describe('useSuspenseQuery', () => {
let queryCache: QueryCache
let queryClient: QueryClient

beforeEach(() => {
vi.useFakeTimers()
queryCache = new QueryCache()
queryClient = new QueryClient({ queryCache })
})

afterEach(() => {
vi.useRealTimers()
queryClient.clear()
})

const queryCache = new QueryCache()
const queryClient = new QueryClient({ queryCache })

/**
* Preact Suspense handles the rerenders differently than React.
* This test only checks for 4 renders (vs. React -> 6)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,20 @@ import {
import { renderWithClient } from './utils'

describe('QueryErrorResetBoundary', () => {
let queryCache: QueryCache
let queryClient: QueryClient

beforeEach(() => {
vi.useFakeTimers()
queryCache = new QueryCache()
queryClient = new QueryClient({ queryCache })
})

afterEach(() => {
vi.useRealTimers()
queryClient.clear()
})

const queryCache = new QueryCache()
const queryClient = new QueryClient({ queryCache })

describe('useQuery', () => {
it('should retry fetch if the reset error boundary has been reset', async () => {
const consoleMock = vi
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,20 @@ import { QueryCache, QueryClient, hashKey, useQuery } from '..'
import { renderWithClient } from './utils'

describe('fine grained persister', () => {
let queryCache: QueryCache
let queryClient: QueryClient

beforeEach(() => {
vi.useFakeTimers()
queryCache = new QueryCache()
queryClient = new QueryClient({ queryCache })
})

afterEach(() => {
vi.useRealTimers()
queryClient.clear()
})

const queryCache = new QueryCache()
const queryClient = new QueryClient({ queryCache })

it('should restore query state from persister and not refetch', async () => {
const key = queryKey()
const hash = hashKey(key)
Expand Down
Loading
Loading