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
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ describe('useMutationState', () => {
function Mutate() {
const { mutate, data } = useMutation({
mutationKey,
mutationFn: (input: number) => sleep(150).then(() => 'data' + input),
mutationFn: (input: number) => sleep(150).then(() => `data${input}`),
})

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const generateInfiniteQueryOptions = (
.mockImplementation(async () => {
const currentPageData = data[currentPage]
if (!currentPageData) {
throw new Error('No data defined for page ' + currentPage)
throw new Error(`No data defined for page ${currentPage}`)
}

await sleep(10)
Expand Down
24 changes: 12 additions & 12 deletions packages/preact-query/src/__tests__/useQueries.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ describe('useQueries', () => {
queryFn: () => 'string',
select: (a) => {
expectTypeOf(a).toEqualTypeOf<string>()
return parseInt(a)
return parseInt(a, 10)
},
},
],
Expand Down Expand Up @@ -219,7 +219,7 @@ describe('useQueries', () => {
queryFn: () => 'string',
select: (a) => {
expectTypeOf(a).toEqualTypeOf<string>()
return parseInt(a)
return parseInt(a, 10)
},
placeholderData: 'string',
// @ts-expect-error (initialData: string)
Expand Down Expand Up @@ -302,7 +302,7 @@ describe('useQueries', () => {
queryFn: () => 'string',
select: (a) => {
expectTypeOf(a).toEqualTypeOf<string>()
return parseInt(a)
return parseInt(a, 10)
},
},
],
Expand Down Expand Up @@ -362,7 +362,7 @@ describe('useQueries', () => {
queryFn: () => 'string',
select: (a) => {
expectTypeOf(a).toEqualTypeOf<string>()
return parseInt(a)
return parseInt(a, 10)
},
placeholderData: 'string',
// @ts-expect-error (initialData: string)
Expand Down Expand Up @@ -404,7 +404,7 @@ describe('useQueries', () => {
queryFn: () => 'string',
select: (a) => {
expectTypeOf(a).toEqualTypeOf<string>()
return parseInt(a)
return parseInt(a, 10)
},
}),
],
Expand Down Expand Up @@ -556,7 +556,7 @@ describe('useQueries', () => {
{
queryKey: key4,
queryFn: () => 'string',
select: (a: string) => parseInt(a),
select: (a: string) => parseInt(a, 10),
},
{
queryKey: key5,
Expand Down Expand Up @@ -596,12 +596,12 @@ describe('useQueries', () => {
{
queryKey: key4,
queryFn: () => 'string',
select: (a: string) => parseInt(a),
select: (a: string) => parseInt(a, 10),
},
{
queryKey: key5,
queryFn: () => 'string',
select: (a: string) => parseInt(a),
select: (a: string) => parseInt(a, 10),
throwOnError,
},
],
Expand Down Expand Up @@ -1138,14 +1138,14 @@ describe('useQueries', () => {
queryKey: key1,
queryFn: async () => {
await sleep(5)
return Promise.resolve('first result ' + count)
return Promise.resolve(`first result ${count}`)
},
},
{
queryKey: key2,
queryFn: async () => {
await sleep(50)
return Promise.resolve('second result ' + count)
return Promise.resolve(`second result ${count}`)
},
},
],
Expand Down Expand Up @@ -1330,14 +1330,14 @@ describe('useQueries', () => {
queryKey: key1,
queryFn: async () => {
await sleep(10)
return 'first result:' + value
return `first result:${value}`
},
},
{
queryKey: key2,
queryFn: async () => {
await sleep(20)
return 'second result:' + value
return `second result:${value}`
},
},
],
Expand Down
38 changes: 18 additions & 20 deletions packages/preact-query/src/__tests__/useQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ describe('useQuery', () => {
queryKey: key,
queryFn: async () => {
await sleep(10)
return 'data: ' + value
return `data: ${value}`
},

gcTime: 0,
Expand Down Expand Up @@ -769,7 +769,7 @@ describe('useQuery', () => {
queryFn: async () => {
await sleep(10)
count++
return 'test' + count
return `test${count}`
},
})

Expand Down Expand Up @@ -3686,9 +3686,8 @@ describe('useQuery', () => {
if (counter < 2) {
counter++
return Promise.reject(new Error('error'))
} else {
return Promise.resolve('data')
}
return Promise.resolve('data')
},
retryDelay: 10,
})
Expand Down Expand Up @@ -5062,9 +5061,8 @@ describe('useQuery', () => {
await sleep(10)
if (id % 2 === 1) {
return Promise.reject(new Error('Error'))
} else {
return 'data'
}
return 'data'
},
retry: false,
retryOnMount: false,
Expand Down Expand Up @@ -5300,7 +5298,7 @@ describe('useQuery', () => {
queryFn: async () => {
count++
await sleep(10)
return 'data' + count
return `data${count}`
},
})

Expand Down Expand Up @@ -5363,7 +5361,7 @@ describe('useQuery', () => {
queryFn: async () => {
count++
await sleep(10)
return 'data' + count
return `data${count}`
},
})

Expand Down Expand Up @@ -5411,7 +5409,7 @@ describe('useQuery', () => {
queryFn: async () => {
count++
await sleep(10)
return 'data' + count
return `data${count}`
},
})

Expand Down Expand Up @@ -5457,7 +5455,7 @@ describe('useQuery', () => {
queryFn: async () => {
count++
await sleep(10)
return 'data' + count
return `data${count}`
},
initialData: 'initial',
})
Expand Down Expand Up @@ -5505,7 +5503,7 @@ describe('useQuery', () => {
queryFn: async () => {
count++
await sleep(10)
return 'data' + count
return `data${count}`
},
initialData: 'initial',
})
Expand Down Expand Up @@ -5565,7 +5563,7 @@ describe('useQuery', () => {
queryFn: async (): Promise<unknown> => {
count++
await sleep(10)
throw new Error('failed' + count)
throw new Error(`failed${count}`)
},
retry: 2,
retryDelay: 10,
Expand Down Expand Up @@ -5678,7 +5676,7 @@ describe('useQuery', () => {
queryFn: async () => {
count++
await sleep(10)
return 'data' + count
return `data${count}`
},
})

Expand Down Expand Up @@ -5735,7 +5733,7 @@ describe('useQuery', () => {
queryFn: async () => {
count++
await sleep(10)
return 'data' + count
return `data${count}`
},
refetchOnReconnect: false,
})
Expand Down Expand Up @@ -5787,7 +5785,7 @@ describe('useQuery', () => {
function Component() {
const state = useQuery({
queryKey: key,
queryFn: async () => {
queryFn: () => {
count++
return `data${count}`
},
Expand Down Expand Up @@ -5862,7 +5860,7 @@ describe('useQuery', () => {
queryFn: async () => {
count++
await sleep(10)
return 'data ' + count
return `data ${count}`
},
networkMode: 'always',
})
Expand Down Expand Up @@ -5898,7 +5896,7 @@ describe('useQuery', () => {
queryFn: async (): Promise<unknown> => {
count++
await sleep(10)
throw new Error('error ' + count)
throw new Error(`error ${count}`)
},
networkMode: 'always',
retry: 1,
Expand Down Expand Up @@ -5942,7 +5940,7 @@ describe('useQuery', () => {
queryFn: async (): Promise<unknown> => {
count++
await sleep(10)
throw new Error('failed' + count)
throw new Error(`failed${count}`)
},
retry: 2,
retryDelay: 1,
Expand Down Expand Up @@ -6067,7 +6065,7 @@ describe('useQuery', () => {
renders++
return (
<div>
<span>{data ? 'has data' + data : 'no data'}</span>
<span>{data ? `has data${data}` : 'no data'}</span>
<button
onClick={() => queryClient.setQueryData<string>(key, 'new data')}
>
Expand Down Expand Up @@ -6697,7 +6695,7 @@ describe('useQuery', () => {
expect(results[3]).toMatchObject({
status: 'error',
fetchStatus: 'idle',
error: error,
error,
errorUpdateCount: 2,
isLoading: false,
failureCount: 1,
Expand Down
74 changes: 71 additions & 3 deletions packages/preact-query/src/__tests__/useSuspenseQueries.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ describe('useSuspenseQueries 2', () => {
function Page({ count, isPending }: { count: number; isPending: boolean }) {
const { data } = useSuspenseQuery({
queryKey: [key, count],
queryFn: () => sleep(10).then(() => 'data' + count),
queryFn: () => sleep(10).then(() => `data${count}`),
})

return <div>{isPending ? 'pending' : data}</div>
Expand Down Expand Up @@ -560,7 +560,7 @@ describe('useSuspenseQueries 2', () => {
queryFn: () =>
sleep(10).then(() => {
queryFnCount++
return 'data' + count
return `data${count}`
}),
})

Expand Down Expand Up @@ -601,7 +601,7 @@ describe('useSuspenseQueries 2', () => {
function Page({ count, isPending }: { count: number; isPending: boolean }) {
const { data } = useSuspenseQuery({
queryKey: [key, count],
queryFn: () => sleep(10).then(() => 'data' + count),
queryFn: () => sleep(10).then(() => `data${count}`),
})

return (
Expand Down Expand Up @@ -1010,6 +1010,74 @@ describe('useSuspenseQueries 2', () => {
expect(queryFn2).toHaveBeenCalledTimes(0)
})

it('should not suspend and only refetch the stale query when one query has fresh and the other has stale 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 key2 becomes stale before mount
vi.advanceTimersByTime(1000)

// Make key1 fresh again by resetting its data
queryClient.setQueryData(key1, 'cached1')

const queryFn1 = vi.fn(() => sleep(20).then(() => 'data1'))

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

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()

// key1 is fresh, no refetch
expect(queryFn1).toHaveBeenCalledTimes(0)

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

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

// key1 is still fresh, no refetch triggered
expect(queryFn1).toHaveBeenCalledTimes(0)

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

expect(rendered.getByText('data1: cached1')).toBeInTheDocument()
expect(rendered.getByText('data2: data2')).toBeInTheDocument()
expect(queryFn1).toHaveBeenCalledTimes(0)
})

it('should not suspend but refetch when all queries have stale cached data', async () => {
const key1 = queryKey()
const key2 = queryKey()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -918,7 +918,6 @@ describe('useSuspenseQuery', () => {
useSuspenseQuery({
queryKey: key,
// @ts-expect-error
// eslint-disable-next-line
queryFn: Math.random() >= 0 ? skipToken : () => Promise.resolve(5),
})

Expand Down
Loading
Loading