Skip to content

Commit d9384ac

Browse files
authored
test(solid-query): replace hardcoded query keys with 'queryKey()' utility (#10416)
1 parent e203306 commit d9384ac

File tree

9 files changed

+107
-84
lines changed

9 files changed

+107
-84
lines changed

packages/solid-query/src/__tests__/createQueries.test-d.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { describe, expectTypeOf, it } from 'vitest'
2+
import { queryKey } from '@tanstack/query-test-utils'
23
import { queryOptions, useQueries } from '..'
34
import type { UseQueryResult } from '..'
45

@@ -7,14 +8,14 @@ describe('useQueries', () => {
78
const Queries1 = {
89
get: () =>
910
queryOptions({
10-
queryKey: ['key1'],
11+
queryKey: queryKey(),
1112
queryFn: () => Promise.resolve(1),
1213
}),
1314
}
1415
const Queries2 = {
1516
get: () =>
1617
queryOptions({
17-
queryKey: ['key2'],
18+
queryKey: queryKey(),
1819
queryFn: () => Promise.resolve(true),
1920
}),
2021
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { describe, expectTypeOf, it } from 'vitest'
22
import { dataTagSymbol } from '@tanstack/query-core'
3+
import { queryKey } from '@tanstack/query-test-utils'
34
import { useInfiniteQuery } from '../useInfiniteQuery'
45
import { infiniteQueryOptions } from '../infiniteQueryOptions'
56
import type { InfiniteData } from '@tanstack/query-core'
@@ -12,7 +13,7 @@ describe('infiniteQueryOptions', () => {
1213
it('should infer defined types', () => {
1314
const options = infiniteQueryOptions({
1415
getNextPageParam: () => 10,
15-
queryKey: ['key'],
16+
queryKey: queryKey(),
1617
queryFn: () => ({ wow: true }),
1718
initialData: {
1819
pageParams: [undefined],
@@ -45,7 +46,7 @@ describe('infiniteQueryOptions', () => {
4546
it('should work without defined types', () => {
4647
const options = infiniteQueryOptions({
4748
getNextPageParam: () => undefined,
48-
queryKey: ['key'],
49+
queryKey: queryKey(),
4950
queryFn: () => ({ wow: true }),
5051
initialPageParam: 0,
5152
})

packages/solid-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
)
@@ -183,7 +184,7 @@ describe('mutationOptions', () => {
183184

184185
const isMutating = queryClient.isMutating(
185186
mutationOptions({
186-
mutationKey: ['key'],
187+
mutationKey: queryKey(),
187188
mutationFn: () => Promise.resolve(5),
188189
}),
189190
)
@@ -200,7 +201,7 @@ describe('mutationOptions', () => {
200201
it('should infer types when used with useMutationState', () => {
201202
const mutationState = useMutationState(() => ({
202203
filters: mutationOptions({
203-
mutationKey: ['key'],
204+
mutationKey: queryKey(),
204205
mutationFn: () => Promise.resolve(5),
205206
}),
206207
}))

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

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
22
import { fireEvent, render } from '@solidjs/testing-library'
33
import { createEffect, createRenderEffect } from 'solid-js'
4-
import { sleep } from '@tanstack/query-test-utils'
4+
import { queryKey, sleep } from '@tanstack/query-test-utils'
55
import {
66
QueryClient,
77
QueryClientProvider,
@@ -41,8 +41,9 @@ describe('mutationOptions', () => {
4141
it('should return the number of fetching mutations when used with useIsMutating (with mutationKey in mutationOptions)', async () => {
4242
const isMutatingArray: Array<number> = []
4343
const queryClient = new QueryClient()
44+
const key = queryKey()
4445
const mutationOpts = mutationOptions({
45-
mutationKey: ['key'],
46+
mutationKey: key,
4647
mutationFn: () => sleep(50).then(() => 'data'),
4748
})
4849

@@ -116,8 +117,9 @@ describe('mutationOptions', () => {
116117
it('should return the number of fetching mutations when used with useIsMutating', async () => {
117118
const isMutatingArray: Array<number> = []
118119
const queryClient = new QueryClient()
120+
const key = queryKey()
119121
const mutationOpts1 = mutationOptions({
120-
mutationKey: ['key'],
122+
mutationKey: key,
121123
mutationFn: () => sleep(50).then(() => 'data1'),
122124
})
123125
const mutationOpts2 = mutationOptions({
@@ -159,8 +161,9 @@ describe('mutationOptions', () => {
159161
it('should return the number of fetching mutations when used with useIsMutating (filter mutationOpts1.mutationKey)', async () => {
160162
const isMutatingArray: Array<number> = []
161163
const queryClient = new QueryClient()
164+
const key = queryKey()
162165
const mutationOpts1 = mutationOptions({
163-
mutationKey: ['key'],
166+
mutationKey: key,
164167
mutationFn: () => sleep(50).then(() => 'data1'),
165168
})
166169
const mutationOpts2 = mutationOptions({
@@ -205,8 +208,9 @@ describe('mutationOptions', () => {
205208
it('should return the number of fetching mutations when used with queryClient.isMutating (with mutationKey in mutationOptions)', async () => {
206209
const isMutatingArray: Array<number> = []
207210
const queryClient = new QueryClient()
211+
const key = queryKey()
208212
const mutationOpts = mutationOptions({
209-
mutationKey: ['mutation'],
213+
mutationKey: key,
210214
mutationFn: () => sleep(500).then(() => 'data'),
211215
})
212216

@@ -288,8 +292,9 @@ describe('mutationOptions', () => {
288292
it('should return the number of fetching mutations when used with queryClient.isMutating', async () => {
289293
const isMutatingArray: Array<number> = []
290294
const queryClient = new QueryClient()
295+
const key = queryKey()
291296
const mutationOpts1 = mutationOptions({
292-
mutationKey: ['mutation'],
297+
mutationKey: key,
293298
mutationFn: () => sleep(500).then(() => 'data1'),
294299
})
295300
const mutationOpts2 = mutationOptions({
@@ -336,8 +341,9 @@ describe('mutationOptions', () => {
336341
it('should return the number of fetching mutations when used with queryClient.isMutating (filter mutationOpt1.mutationKey)', async () => {
337342
const isMutatingArray: Array<number> = []
338343
const queryClient = new QueryClient()
344+
const key = queryKey()
339345
const mutationOpts1 = mutationOptions({
340-
mutationKey: ['mutation'],
346+
mutationKey: key,
341347
mutationFn: () => sleep(500).then(() => 'data1'),
342348
})
343349
const mutationOpts2 = mutationOptions({
@@ -392,8 +398,9 @@ describe('mutationOptions', () => {
392398
it('should return the number of fetching mutations when used with useMutationState (with mutationKey in mutationOptions)', async () => {
393399
const mutationStateArray: Array<Array<MutationState>> = []
394400
const queryClient = new QueryClient()
401+
const key = queryKey()
395402
const mutationOpts = mutationOptions({
396-
mutationKey: ['mutation'],
403+
mutationKey: key,
397404
mutationFn: () => sleep(10).then(() => 'data'),
398405
})
399406

@@ -471,8 +478,9 @@ describe('mutationOptions', () => {
471478
it('should return the number of fetching mutations when used with useMutationState', async () => {
472479
const mutationStateArray: Array<Array<MutationState>> = []
473480
const queryClient = new QueryClient()
481+
const key = queryKey()
474482
const mutationOpts1 = mutationOptions({
475-
mutationKey: ['mutation'],
483+
mutationKey: key,
476484
mutationFn: () => sleep(10).then(() => 'data1'),
477485
})
478486
const mutationOpts2 = mutationOptions({
@@ -518,8 +526,9 @@ describe('mutationOptions', () => {
518526
it('should return the number of fetching mutations when used with useMutationState (filter mutationOpt1.mutationKey)', async () => {
519527
const mutationStateArray: Array<Array<MutationState>> = []
520528
const queryClient = new QueryClient()
529+
const key = queryKey()
521530
const mutationOpts1 = mutationOptions({
522-
mutationKey: ['mutation'],
531+
mutationKey: key,
523532
mutationFn: () => sleep(10).then(() => 'data1'),
524533
})
525534
const mutationOpts2 = mutationOptions({

0 commit comments

Comments
 (0)