Skip to content

Commit 7670b40

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

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

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

Lines changed: 68 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<Array<string> | undefined>()
66+
})
5367
it('should work when passed to useSuspenseInfiniteQuery', () => {
5468
const options = infiniteQueryOptions({
5569
queryKey: ['key'],
@@ -62,6 +76,47 @@ describe('infiniteQueryOptions', () => {
6276

6377
expectTypeOf(data).toEqualTypeOf<InfiniteData<string, unknown>>()
6478
})
79+
it('should work when passed to useSuspenseInfiniteQuery 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 } = useSuspenseInfiniteQuery(options)
89+
90+
// known issue: type of pageParams is unknown when returned from useInfiniteQuery
91+
expectTypeOf(data).toEqualTypeOf<Array<string>>()
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+
const data = await new QueryClient().infiniteQuery(options)
102+
103+
expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
104+
})
105+
it('should work when passed to infiniteQuery with select', async () => {
106+
const options = infiniteQueryOptions({
107+
queryKey: ['key'],
108+
queryFn: () => Promise.resolve('string'),
109+
getNextPageParam: () => 1,
110+
initialPageParam: 1,
111+
select: (data) => data.pages,
112+
})
113+
114+
options.select
115+
116+
const data = await new QueryClient().infiniteQuery(options)
117+
118+
expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
119+
})
65120
it('should work when passed to fetchInfiniteQuery', async () => {
66121
const options = infiniteQueryOptions({
67122
queryKey: ['key'],
@@ -74,6 +129,19 @@ describe('infiniteQueryOptions', () => {
74129

75130
expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
76131
})
132+
it('should ignore select when passed to fetchInfiniteQuery', async () => {
133+
const options = infiniteQueryOptions({
134+
queryKey: ['key'],
135+
queryFn: () => Promise.resolve('string'),
136+
getNextPageParam: () => 1,
137+
initialPageParam: 1,
138+
select: (data) => data.pages,
139+
})
140+
141+
const data = await new QueryClient().fetchInfiniteQuery(options)
142+
143+
expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
144+
})
77145
it('should tag the queryKey with the result type of the QueryFn', () => {
78146
const { queryKey } = infiniteQueryOptions({
79147
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)