-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(react-query): useSuspenseQueries type not compatible with queryOp…
…tion (#7194) Co-authored-by: Dominik Dorfmeister <office@dorfmeister.cc>
- Loading branch information
Showing
2 changed files
with
120 additions
and
2 deletions.
There are no files selected for viewing
116 changes: 116 additions & 0 deletions
116
packages/react-query/src/__tests__/useSuspenseQueries.test-d.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import { describe, expectTypeOf, it } from 'vitest' | ||
import { useSuspenseQueries } from '..' | ||
import { queryOptions } from '../queryOptions' | ||
import type { OmitKeyof } from '..' | ||
import type { UseQueryOptions } from '../types' | ||
|
||
describe('UseSuspenseQueries config object overload', () => { | ||
it('TData should always be defined', () => { | ||
const query1 = { | ||
queryKey: ['key1'], | ||
queryFn: () => { | ||
return { | ||
wow: true, | ||
} | ||
}, | ||
initialData: { | ||
wow: false, | ||
}, | ||
} | ||
|
||
const query2 = { | ||
queryKey: ['key2'], | ||
queryFn: () => 'Query Data', | ||
} | ||
|
||
const queryResults = useSuspenseQueries({ queries: [query1, query2] }) | ||
|
||
const query1Data = queryResults[0].data | ||
const query2Data = queryResults[1].data | ||
|
||
expectTypeOf(query1Data).toEqualTypeOf<{ wow: boolean }>() | ||
expectTypeOf(query2Data).toEqualTypeOf<string>() | ||
}) | ||
|
||
it('TData should be defined when passed through queryOptions', () => { | ||
const options = queryOptions({ | ||
queryKey: ['key'], | ||
queryFn: () => { | ||
return { | ||
wow: true, | ||
} | ||
}, | ||
}) | ||
const queryResults = useSuspenseQueries({ queries: [options] }) | ||
|
||
const data = queryResults[0].data | ||
|
||
expectTypeOf(data).toEqualTypeOf<{ wow: boolean }>() | ||
}) | ||
|
||
it('it should be possible to define a different TData than TQueryFnData using select with queryOptions spread into useQuery', () => { | ||
const query1 = queryOptions({ | ||
queryKey: ['key'], | ||
queryFn: () => Promise.resolve(1), | ||
select: (data) => data > 1, | ||
}) | ||
|
||
const query2 = { | ||
queryKey: ['key'], | ||
queryFn: () => Promise.resolve(1), | ||
select: (data: number) => data > 1, | ||
} | ||
|
||
const queryResults = useSuspenseQueries({ queries: [query1, query2] }) | ||
const query1Data = queryResults[0].data | ||
const query2Data = queryResults[1].data | ||
|
||
expectTypeOf(query1Data).toEqualTypeOf<boolean>() | ||
expectTypeOf(query2Data).toEqualTypeOf<boolean>() | ||
}) | ||
|
||
it('TData should have undefined in the union when initialData is provided as a function which can return undefined', () => { | ||
const queryResults = useSuspenseQueries({ | ||
queries: [ | ||
{ | ||
queryKey: ['key'], | ||
queryFn: () => { | ||
return { | ||
wow: true, | ||
} | ||
}, | ||
initialData: () => undefined as { wow: boolean } | undefined, | ||
}, | ||
], | ||
}) | ||
|
||
const data = queryResults[0].data | ||
|
||
expectTypeOf(data).toEqualTypeOf<{ wow: boolean }>() | ||
}) | ||
|
||
describe('custom hook', () => { | ||
it('should allow custom hooks using UseQueryOptions', () => { | ||
type Data = string | ||
|
||
const useCustomQueries = ( | ||
options?: OmitKeyof<UseQueryOptions<Data>, 'queryKey' | 'queryFn'>, | ||
) => { | ||
return useSuspenseQueries({ | ||
queries: [ | ||
{ | ||
...options, | ||
queryKey: ['todos-key'], | ||
queryFn: () => Promise.resolve('data'), | ||
}, | ||
], | ||
}) | ||
} | ||
|
||
const queryResults = useCustomQueries() | ||
const data = queryResults[0].data | ||
|
||
expectTypeOf(data).toEqualTypeOf<Data>() | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters