Skip to content

Commit c6188b9

Browse files
committed
react query options testing
1 parent 6d630b0 commit c6188b9

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

packages/react-query/src/__tests__/infiniteQueryOptions.test-d.tsx

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,20 @@ describe('infiniteQueryOptions', () => {
5050
InfiniteData<string, unknown> | undefined
5151
>()
5252
})
53+
it('should work when passed to useInfiniteQuery with select', () => {
54+
const options = infiniteQueryOptions({
55+
queryKey: ['key'],
56+
queryFn: () => Promise.resolve('string'),
57+
getNextPageParam: () => 1,
58+
initialPageParam: 1,
59+
select: (data) => data.pages,
60+
})
61+
62+
const { data } = useInfiniteQuery(options)
63+
64+
// known issue: type of pageParams is unknown when returned from useInfiniteQuery
65+
expectTypeOf(data).toEqualTypeOf<string[] | undefined>()
66+
})
5367
it('should work when passed to useSuspenseInfiniteQuery', () => {
5468
const options = infiniteQueryOptions({
5569
queryKey: ['key'],
@@ -62,6 +76,61 @@ describe('infiniteQueryOptions', () => {
6276

6377
expectTypeOf(data).toEqualTypeOf<InfiniteData<string, unknown>>()
6478
})
79+
it('should work when passed to useInfiniteQuery with select', () => {
80+
const options = infiniteQueryOptions({
81+
queryKey: ['key'],
82+
queryFn: () => Promise.resolve('string'),
83+
getNextPageParam: () => 1,
84+
initialPageParam: 1,
85+
select: (data) => data.pages,
86+
})
87+
88+
const { data } = useInfiniteQuery(options)
89+
90+
// known issue: type of pageParams is unknown when returned from useInfiniteQuery
91+
expectTypeOf(data).toEqualTypeOf<string[] | undefined>()
92+
})
93+
it('should work when passed to infiniteQuery', async () => {
94+
const options = infiniteQueryOptions({
95+
queryKey: ['key'],
96+
queryFn: () => Promise.resolve('string'),
97+
getNextPageParam: () => 1,
98+
initialPageParam: 1,
99+
})
100+
101+
options.select
102+
103+
const data = await new QueryClient().infiniteQuery(options)
104+
105+
expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
106+
})
107+
it('should work when passed to fetchInfiniteQuery', async () => {
108+
const options = infiniteQueryOptions({
109+
queryKey: ['key'],
110+
queryFn: () => Promise.resolve('string'),
111+
getNextPageParam: () => 1,
112+
initialPageParam: 1,
113+
})
114+
115+
const data = await new QueryClient().fetchInfiniteQuery(options)
116+
117+
expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
118+
})
119+
it('should work when passed to infiniteQuery with select', async () => {
120+
const options = infiniteQueryOptions({
121+
queryKey: ['key'],
122+
queryFn: () => Promise.resolve('string'),
123+
getNextPageParam: () => 1,
124+
initialPageParam: 1,
125+
select: (data) => data.pages,
126+
})
127+
128+
options.select
129+
130+
const data = await new QueryClient().infiniteQuery(options)
131+
132+
expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
133+
})
65134
it('should work when passed to fetchInfiniteQuery', async () => {
66135
const options = infiniteQueryOptions({
67136
queryKey: ['key'],

packages/react-query/src/__tests__/queryOptions.test-d.tsx

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,15 @@ describe('queryOptions', () => {
5555
const { data } = useSuspenseQuery(options)
5656
expectTypeOf(data).toEqualTypeOf<number>()
5757
})
58+
it('should work when passed to query', async () => {
59+
const options = queryOptions({
60+
queryKey: ['key'],
61+
queryFn: () => Promise.resolve(5),
62+
})
5863

64+
const data = await new QueryClient().query(options)
65+
expectTypeOf(data).toEqualTypeOf<number>()
66+
})
5967
it('should work when passed to fetchQuery', async () => {
6068
const options = queryOptions({
6169
queryKey: ['key'],
@@ -65,6 +73,26 @@ describe('queryOptions', () => {
6573
const data = await new QueryClient().fetchQuery(options)
6674
expectTypeOf(data).toEqualTypeOf<number>()
6775
})
76+
it('should work when passed to query with select', async () => {
77+
const options = queryOptions({
78+
queryKey: ['key'],
79+
queryFn: () => Promise.resolve(5),
80+
select: (data) => data.toString(),
81+
})
82+
83+
const data = await new QueryClient().query(options)
84+
expectTypeOf(data).toEqualTypeOf<string>()
85+
})
86+
it('should ignore select when passed to fetchQuery', async () => {
87+
const options = queryOptions({
88+
queryKey: ['key'],
89+
queryFn: () => Promise.resolve(5),
90+
select: (data) => data.toString(),
91+
})
92+
93+
const data = await new QueryClient().fetchQuery(options)
94+
expectTypeOf(data).toEqualTypeOf<number>()
95+
})
6896
it('should work when passed to useQueries', () => {
6997
const options = queryOptions({
7098
queryKey: ['key'],

0 commit comments

Comments
 (0)