Skip to content

Commit f699190

Browse files
test(react-query): replace hardcoded query keys with 'queryKey()' utility (#10420)
* test(react-query): replace hardcoded query keys with 'queryKey()' utility * ci: apply automated fixes --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent 389bbca commit f699190

18 files changed

+209
-184
lines changed

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

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { assertType, describe, expectTypeOf, it, test } from 'vitest'
22
import { QueryClient, dataTagSymbol, skipToken } from '@tanstack/query-core'
3+
import { queryKey } from '@tanstack/query-test-utils'
34
import { infiniteQueryOptions } from '../infiniteQueryOptions'
45
import { useInfiniteQuery } from '../useInfiniteQuery'
56
import { useSuspenseInfiniteQuery } from '../useSuspenseInfiniteQuery'
@@ -14,7 +15,7 @@ describe('infiniteQueryOptions', () => {
1415
it('should not allow excess properties', () => {
1516
assertType(
1617
infiniteQueryOptions({
17-
queryKey: ['key'],
18+
queryKey: queryKey(),
1819
queryFn: () => Promise.resolve('data'),
1920
getNextPageParam: () => 1,
2021
initialPageParam: 1,
@@ -25,7 +26,7 @@ describe('infiniteQueryOptions', () => {
2526
})
2627
it('should infer types for callbacks', () => {
2728
infiniteQueryOptions({
28-
queryKey: ['key'],
29+
queryKey: queryKey(),
2930
queryFn: () => Promise.resolve('data'),
3031
staleTime: 1000,
3132
getNextPageParam: () => 1,
@@ -37,7 +38,7 @@ describe('infiniteQueryOptions', () => {
3738
})
3839
it('should work when passed to useInfiniteQuery', () => {
3940
const options = infiniteQueryOptions({
40-
queryKey: ['key'],
41+
queryKey: queryKey(),
4142
queryFn: () => Promise.resolve('string'),
4243
getNextPageParam: () => 1,
4344
initialPageParam: 1,
@@ -52,7 +53,7 @@ describe('infiniteQueryOptions', () => {
5253
})
5354
it('should work when passed to useSuspenseInfiniteQuery', () => {
5455
const options = infiniteQueryOptions({
55-
queryKey: ['key'],
56+
queryKey: queryKey(),
5657
queryFn: () => Promise.resolve('string'),
5758
getNextPageParam: () => 1,
5859
initialPageParam: 1,
@@ -64,7 +65,7 @@ describe('infiniteQueryOptions', () => {
6465
})
6566
it('should work when passed to fetchInfiniteQuery', async () => {
6667
const options = infiniteQueryOptions({
67-
queryKey: ['key'],
68+
queryKey: queryKey(),
6869
queryFn: () => Promise.resolve('string'),
6970
getNextPageParam: () => 1,
7071
initialPageParam: 1,
@@ -75,61 +76,61 @@ describe('infiniteQueryOptions', () => {
7576
expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
7677
})
7778
it('should tag the queryKey with the result type of the QueryFn', () => {
78-
const { queryKey } = infiniteQueryOptions({
79-
queryKey: ['key'],
79+
const { queryKey: tagged } = infiniteQueryOptions({
80+
queryKey: queryKey(),
8081
queryFn: () => Promise.resolve('string'),
8182
getNextPageParam: () => 1,
8283
initialPageParam: 1,
8384
})
8485

85-
expectTypeOf(queryKey[dataTagSymbol]).toEqualTypeOf<InfiniteData<string>>()
86+
expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<InfiniteData<string>>()
8687
})
8788
it('should tag the queryKey even if no promise is returned', () => {
88-
const { queryKey } = infiniteQueryOptions({
89-
queryKey: ['key'],
89+
const { queryKey: tagged } = infiniteQueryOptions({
90+
queryKey: queryKey(),
9091
queryFn: () => 'string',
9192
getNextPageParam: () => 1,
9293
initialPageParam: 1,
9394
})
9495

95-
expectTypeOf(queryKey[dataTagSymbol]).toEqualTypeOf<InfiniteData<string>>()
96+
expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<InfiniteData<string>>()
9697
})
9798
it('should tag the queryKey with the result type of the QueryFn if select is used', () => {
98-
const { queryKey } = infiniteQueryOptions({
99-
queryKey: ['key'],
99+
const { queryKey: tagged } = infiniteQueryOptions({
100+
queryKey: queryKey(),
100101
queryFn: () => Promise.resolve('string'),
101102
select: (data) => data.pages,
102103
getNextPageParam: () => 1,
103104
initialPageParam: 1,
104105
})
105106

106-
expectTypeOf(queryKey[dataTagSymbol]).toEqualTypeOf<InfiniteData<string>>()
107+
expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<InfiniteData<string>>()
107108
})
108109
it('should return the proper type when passed to getQueryData', () => {
109-
const { queryKey } = infiniteQueryOptions({
110-
queryKey: ['key'],
110+
const { queryKey: tagged } = infiniteQueryOptions({
111+
queryKey: queryKey(),
111112
queryFn: () => Promise.resolve('string'),
112113
getNextPageParam: () => 1,
113114
initialPageParam: 1,
114115
})
115116

116117
const queryClient = new QueryClient()
117-
const data = queryClient.getQueryData(queryKey)
118+
const data = queryClient.getQueryData(tagged)
118119

119120
expectTypeOf(data).toEqualTypeOf<
120121
InfiniteData<string, unknown> | undefined
121122
>()
122123
})
123124
it('should properly type when passed to setQueryData', () => {
124-
const { queryKey } = infiniteQueryOptions({
125-
queryKey: ['key'],
125+
const { queryKey: tagged } = infiniteQueryOptions({
126+
queryKey: queryKey(),
126127
queryFn: () => Promise.resolve('string'),
127128
getNextPageParam: () => 1,
128129
initialPageParam: 1,
129130
})
130131

131132
const queryClient = new QueryClient()
132-
const data = queryClient.setQueryData(queryKey, (prev) => {
133+
const data = queryClient.setQueryData(tagged, (prev) => {
133134
expectTypeOf(prev).toEqualTypeOf<
134135
InfiniteData<string, unknown> | undefined
135136
>()
@@ -142,7 +143,7 @@ describe('infiniteQueryOptions', () => {
142143
})
143144
it('should throw a type error when using queryFn with skipToken in a suspense query', () => {
144145
const options = infiniteQueryOptions({
145-
queryKey: ['key'],
146+
queryKey: queryKey(),
146147
queryFn:
147148
Math.random() > 0.5 ? skipToken : () => Promise.resolve('string'),
148149
getNextPageParam: () => 1,
@@ -156,7 +157,7 @@ describe('infiniteQueryOptions', () => {
156157
test('should not be allowed to be passed to non-infinite query functions', () => {
157158
const queryClient = new QueryClient()
158159
const options = infiniteQueryOptions({
159-
queryKey: ['key'],
160+
queryKey: queryKey(),
160161
queryFn: () => Promise.resolve('string'),
161162
getNextPageParam: () => 1,
162163
initialPageParam: 1,
@@ -182,7 +183,7 @@ describe('infiniteQueryOptions', () => {
182183
test('allow optional initialData function', () => {
183184
const initialData: { example: boolean } | undefined = { example: true }
184185
const queryOptions = infiniteQueryOptions({
185-
queryKey: ['example'],
186+
queryKey: queryKey(),
186187
queryFn: () => initialData,
187188
initialData: initialData
188189
? () => ({ pages: [initialData], pageParams: [] })
@@ -200,7 +201,7 @@ describe('infiniteQueryOptions', () => {
200201
test('allow optional initialData object', () => {
201202
const initialData: { example: boolean } | undefined = { example: true }
202203
const queryOptions = infiniteQueryOptions({
203-
queryKey: ['example'],
204+
queryKey: queryKey(),
204205
queryFn: () => initialData,
205206
initialData: initialData
206207
? { pages: [initialData], pageParams: [] }

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { describe, expect, it } from 'vitest'
2+
import { queryKey } from '@tanstack/query-test-utils'
23

34
import { infiniteQueryOptions } from '../infiniteQueryOptions'
45
import type { UseInfiniteQueryOptions } from '../types'
56

67
describe('infiniteQueryOptions', () => {
78
it('should return the object received as a parameter without any modification.', () => {
89
const object: UseInfiniteQueryOptions = {
9-
queryKey: ['key'],
10+
queryKey: queryKey(),
1011
queryFn: () => Promise.resolve(5),
1112
getNextPageParam: () => null,
1213
initialPageParam: null,

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

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { assertType, describe, expectTypeOf, it } from 'vitest'
22
import { QueryClient } from '@tanstack/query-core'
3+
import { queryKey } from '@tanstack/query-test-utils'
34
import { useIsMutating, useMutation, useMutationState } from '..'
45
import { mutationOptions } from '../mutationOptions'
56
import type {
@@ -15,7 +16,7 @@ describe('mutationOptions', () => {
1516
// @ts-expect-error this is a good error, because onMutates does not exist!
1617
mutationOptions({
1718
mutationFn: () => Promise.resolve(5),
18-
mutationKey: ['key'],
19+
mutationKey: queryKey(),
1920
onMutates: 1000,
2021
onSuccess: (data) => {
2122
expectTypeOf(data).toEqualTypeOf<number>()
@@ -26,7 +27,7 @@ describe('mutationOptions', () => {
2627
it('should infer types for callbacks', () => {
2728
mutationOptions({
2829
mutationFn: () => Promise.resolve(5),
29-
mutationKey: ['key'],
30+
mutationKey: queryKey(),
3031
onSuccess: (data) => {
3132
expectTypeOf(data).toEqualTypeOf<number>()
3233
},
@@ -38,7 +39,7 @@ describe('mutationOptions', () => {
3839
mutationFn: () => {
3940
throw new Error('fail')
4041
},
41-
mutationKey: ['key'],
42+
mutationKey: queryKey(),
4243
onError: (error) => {
4344
expectTypeOf(error).toEqualTypeOf<DefaultError>()
4445
},
@@ -51,14 +52,14 @@ describe('mutationOptions', () => {
5152
expectTypeOf(vars).toEqualTypeOf<{ id: string }>()
5253
return Promise.resolve(5)
5354
},
54-
mutationKey: ['with-vars'],
55+
mutationKey: queryKey(),
5556
})
5657
})
5758

5859
it('should infer result type correctly', () => {
5960
mutationOptions<number, DefaultError, void, { name: string }>({
6061
mutationFn: () => Promise.resolve(5),
61-
mutationKey: ['key'],
62+
mutationKey: queryKey(),
6263
onMutate: () => {
6364
return { name: 'onMutateResult' }
6465
},
@@ -74,7 +75,7 @@ describe('mutationOptions', () => {
7475
expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
7576
return Promise.resolve(5)
7677
},
77-
mutationKey: ['key'],
78+
mutationKey: queryKey(),
7879
onMutate: (_variables, context) => {
7980
expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
8081
},
@@ -112,7 +113,7 @@ describe('mutationOptions', () => {
112113
expectTypeOf(
113114
mutationOptions({
114115
mutationFn: (id: string) => Promise.resolve(id.length),
115-
mutationKey: ['key'],
116+
mutationKey: queryKey(),
116117
onSuccess: (data) => {
117118
expectTypeOf(data).toEqualTypeOf<number>()
118119
},
@@ -138,7 +139,7 @@ describe('mutationOptions', () => {
138139
it('should infer types when used with useMutation', () => {
139140
const mutation = useMutation(
140141
mutationOptions({
141-
mutationKey: ['key'],
142+
mutationKey: queryKey(),
142143
mutationFn: () => Promise.resolve('data'),
143144
onSuccess: (data) => {
144145
expectTypeOf(data).toEqualTypeOf<string>()
@@ -163,7 +164,7 @@ describe('mutationOptions', () => {
163164
it('should infer types when used with useIsMutating', () => {
164165
const isMutating = useIsMutating(
165166
mutationOptions({
166-
mutationKey: ['key'],
167+
mutationKey: queryKey(),
167168
mutationFn: () => Promise.resolve(5),
168169
}),
169170
)
@@ -182,7 +183,7 @@ describe('mutationOptions', () => {
182183

183184
const isMutating = queryClient.isMutating(
184185
mutationOptions({
185-
mutationKey: ['key'],
186+
mutationKey: queryKey(),
186187
mutationFn: () => Promise.resolve(5),
187188
}),
188189
)
@@ -199,7 +200,7 @@ describe('mutationOptions', () => {
199200
it('should infer types when used with useMutationState', () => {
200201
const mutationState = useMutationState({
201202
filters: mutationOptions({
202-
mutationKey: ['key'],
203+
mutationKey: queryKey(),
203204
mutationFn: () => Promise.resolve(5),
204205
}),
205206
})

packages/react-query/src/__tests__/mutationOptions.test.tsx

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
22
import { QueryClient } from '@tanstack/query-core'
3-
import { sleep } from '@tanstack/query-test-utils'
3+
import { queryKey, sleep } from '@tanstack/query-test-utils'
44
import { fireEvent } from '@testing-library/react'
55
import { mutationOptions } from '../mutationOptions'
66
import { useIsMutating, useMutation, useMutationState } from '..'
@@ -38,7 +38,7 @@ describe('mutationOptions', () => {
3838
const isMutatingArray: Array<number> = []
3939
const queryClient = new QueryClient()
4040
const mutationOpts = mutationOptions({
41-
mutationKey: ['key'],
41+
mutationKey: queryKey(),
4242
mutationFn: () => sleep(50).then(() => 'data'),
4343
})
4444

@@ -129,7 +129,7 @@ describe('mutationOptions', () => {
129129
const isMutatingArray: Array<number> = []
130130
const queryClient = new QueryClient()
131131
const mutationOpts1 = mutationOptions({
132-
mutationKey: ['key'],
132+
mutationKey: queryKey(),
133133
mutationFn: () => sleep(50).then(() => 'data1'),
134134
})
135135
const mutationOpts2 = mutationOptions({
@@ -181,7 +181,7 @@ describe('mutationOptions', () => {
181181
const isMutatingArray: Array<number> = []
182182
const queryClient = new QueryClient()
183183
const mutationOpts1 = mutationOptions({
184-
mutationKey: ['key'],
184+
mutationKey: queryKey(),
185185
mutationFn: () => sleep(50).then(() => 'data1'),
186186
})
187187
const mutationOpts2 = mutationOptions({
@@ -235,7 +235,7 @@ describe('mutationOptions', () => {
235235
const isMutatingArray: Array<number> = []
236236
const queryClient = new QueryClient()
237237
const mutationOpts = mutationOptions({
238-
mutationKey: ['mutation'],
238+
mutationKey: queryKey(),
239239
mutationFn: () => sleep(500).then(() => 'data'),
240240
})
241241

@@ -298,7 +298,7 @@ describe('mutationOptions', () => {
298298
const isMutatingArray: Array<number> = []
299299
const queryClient = new QueryClient()
300300
const mutationOpts1 = mutationOptions({
301-
mutationKey: ['mutation'],
301+
mutationKey: queryKey(),
302302
mutationFn: () => sleep(500).then(() => 'data1'),
303303
})
304304
const mutationOpts2 = mutationOptions({
@@ -336,7 +336,7 @@ describe('mutationOptions', () => {
336336
const isMutatingArray: Array<number> = []
337337
const queryClient = new QueryClient()
338338
const mutationOpts1 = mutationOptions({
339-
mutationKey: ['mutation'],
339+
mutationKey: queryKey(),
340340
mutationFn: () => sleep(500).then(() => 'data1'),
341341
})
342342
const mutationOpts2 = mutationOptions({
@@ -378,7 +378,7 @@ describe('mutationOptions', () => {
378378
> = []
379379
const queryClient = new QueryClient()
380380
const mutationOpts = mutationOptions({
381-
mutationKey: ['mutation'],
381+
mutationKey: queryKey(),
382382
mutationFn: () => sleep(10).then(() => 'data'),
383383
})
384384

@@ -447,7 +447,7 @@ describe('mutationOptions', () => {
447447
> = []
448448
const queryClient = new QueryClient()
449449
const mutationOpts1 = mutationOptions({
450-
mutationKey: ['mutation'],
450+
mutationKey: queryKey(),
451451
mutationFn: () => sleep(10).then(() => 'data1'),
452452
})
453453
const mutationOpts2 = mutationOptions({
@@ -489,7 +489,7 @@ describe('mutationOptions', () => {
489489
> = []
490490
const queryClient = new QueryClient()
491491
const mutationOpts1 = mutationOptions({
492-
mutationKey: ['mutation'],
492+
mutationKey: queryKey(),
493493
mutationFn: () => sleep(10).then(() => 'data1'),
494494
})
495495
const mutationOpts2 = mutationOptions({

0 commit comments

Comments
 (0)