Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions packages/preact-query/src/__tests__/useSuspenseQueries.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1078,6 +1078,74 @@ describe('useSuspenseQueries 2', () => {
expect(queryFn1).toHaveBeenCalledTimes(0)
})

it('should not suspend and only refetch the stale query when one query has stale and the other has fresh cached data', async () => {
const key1 = queryKey()
const key2 = queryKey()

queryClient.setQueryData(key1, 'cached1')
queryClient.setQueryData(key2, 'cached2')

// Advance past staleTime (min 1000ms in suspense) so key1 becomes stale before mount
vi.advanceTimersByTime(1000)

// Make key2 fresh again by resetting its data
queryClient.setQueryData(key2, 'cached2')

const queryFn2 = vi.fn(() => sleep(20).then(() => 'data2'))

function Page() {
const [result1, result2] = useSuspenseQueries({
queries: [
{
queryKey: key1,
queryFn: () => sleep(10).then(() => 'data1'),
},
{
queryKey: key2,
queryFn: queryFn2,
},
],
})

return (
<div>
<div>data1: {result1.data}</div>
<div>data2: {result2.data}</div>
</div>
)
}

const rendered = renderWithClient(
queryClient,
<Suspense fallback={<div>loading</div>}>
<Page />
</Suspense>,
)

// No suspend, cached data shown immediately
expect(rendered.getByText('data1: cached1')).toBeInTheDocument()
expect(rendered.getByText('data2: cached2')).toBeInTheDocument()

// key2 is fresh, no refetch
expect(queryFn2).toHaveBeenCalledTimes(0)

// key1 background refetch completes
await vi.advanceTimersByTimeAsync(11)

expect(rendered.getByText('data1: data1')).toBeInTheDocument()
expect(rendered.getByText('data2: cached2')).toBeInTheDocument()

// key2 is still fresh, no refetch triggered
expect(queryFn2).toHaveBeenCalledTimes(0)

// after key1 refetch completes, key2 is still fresh with no refetch triggered
await vi.advanceTimersByTimeAsync(10)

expect(rendered.getByText('data1: data1')).toBeInTheDocument()
expect(rendered.getByText('data2: cached2')).toBeInTheDocument()
expect(queryFn2).toHaveBeenCalledTimes(0)
})

it('should not suspend but refetch when all queries have stale cached data', async () => {
const key1 = queryKey()
const key2 = queryKey()
Expand Down
68 changes: 68 additions & 0 deletions packages/react-query/src/__tests__/useSuspenseQueries.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1056,6 +1056,74 @@ describe('useSuspenseQueries 2', () => {
expect(queryFn1).toHaveBeenCalledTimes(0)
})

it('should not suspend and only refetch the stale query when one query has stale and the other has fresh cached data', async () => {
const key1 = queryKey()
const key2 = queryKey()

queryClient.setQueryData(key1, 'cached1')
queryClient.setQueryData(key2, 'cached2')

// Advance past staleTime (min 1000ms in suspense) so key1 becomes stale before mount
vi.advanceTimersByTime(1000)

// Make key2 fresh again by resetting its data
queryClient.setQueryData(key2, 'cached2')

const queryFn2 = vi.fn(() => sleep(20).then(() => 'data2'))

function Page() {
const [result1, result2] = useSuspenseQueries({
queries: [
{
queryKey: key1,
queryFn: () => sleep(10).then(() => 'data1'),
},
{
queryKey: key2,
queryFn: queryFn2,
},
],
})

return (
<div>
<div>data1: {result1.data}</div>
<div>data2: {result2.data}</div>
</div>
)
}

const rendered = renderWithClient(
queryClient,
<React.Suspense fallback={<div>loading</div>}>
<Page />
</React.Suspense>,
)

// No suspend, cached data shown immediately
expect(rendered.getByText('data1: cached1')).toBeInTheDocument()
expect(rendered.getByText('data2: cached2')).toBeInTheDocument()

// key2 is fresh, no refetch
expect(queryFn2).toHaveBeenCalledTimes(0)

// key1 background refetch completes
await act(() => vi.advanceTimersByTimeAsync(11))

expect(rendered.getByText('data1: data1')).toBeInTheDocument()
expect(rendered.getByText('data2: cached2')).toBeInTheDocument()

// key2 is still fresh, no refetch triggered
expect(queryFn2).toHaveBeenCalledTimes(0)

// after key1 refetch completes, key2 is still fresh with no refetch triggered
await act(() => vi.advanceTimersByTimeAsync(10))

expect(rendered.getByText('data1: data1')).toBeInTheDocument()
expect(rendered.getByText('data2: cached2')).toBeInTheDocument()
expect(queryFn2).toHaveBeenCalledTimes(0)
})

it('should not suspend but refetch when all queries have stale cached data', async () => {
const key1 = queryKey()
const key2 = queryKey()
Expand Down
Loading