Skip to content

Commit 4335fce

Browse files
authored
test(angular-query-experimental): replace hardcoded query keys with 'queryKey()' utility (#10418)
1 parent dbe3f95 commit 4335fce

15 files changed

+299
-169
lines changed

packages/angular-query-experimental/src/__tests__/infinite-query-options.test-d.ts

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { assertType, describe, expectTypeOf, it, test } from 'vitest'
2+
import { queryKey } from '@tanstack/query-test-utils'
23
import { QueryClient, dataTagSymbol } from '@tanstack/query-core'
34
import { infiniteQueryOptions } from '../infinite-query-options'
45
import { injectInfiniteQuery } from '../inject-infinite-query'
@@ -11,9 +12,10 @@ import type {
1112

1213
describe('infiniteQueryOptions', () => {
1314
it('should not allow excess properties', () => {
15+
const key = queryKey()
1416
assertType(
1517
infiniteQueryOptions({
16-
queryKey: ['key'],
18+
queryKey: key,
1719
queryFn: () => Promise.resolve('data'),
1820
getNextPageParam: () => 1,
1921
initialPageParam: 1,
@@ -23,8 +25,9 @@ describe('infiniteQueryOptions', () => {
2325
)
2426
})
2527
it('should infer types for callbacks', () => {
28+
const key = queryKey()
2629
infiniteQueryOptions({
27-
queryKey: ['key'],
30+
queryKey: key,
2831
queryFn: () => Promise.resolve('data'),
2932
staleTime: 1000,
3033
getNextPageParam: () => 1,
@@ -35,8 +38,9 @@ describe('infiniteQueryOptions', () => {
3538
})
3639
})
3740
it('should work when passed to useInfiniteQuery', () => {
41+
const key = queryKey()
3842
const options = infiniteQueryOptions({
39-
queryKey: ['key'],
43+
queryKey: key,
4044
queryFn: () => Promise.resolve('string'),
4145
getNextPageParam: () => 1,
4246
initialPageParam: 1,
@@ -51,8 +55,9 @@ describe('infiniteQueryOptions', () => {
5155
})
5256

5357
it('should work when passed to fetchInfiniteQuery', async () => {
58+
const key = queryKey()
5459
const options = infiniteQueryOptions({
55-
queryKey: ['key'],
60+
queryKey: key,
5661
queryFn: () => Promise.resolve('string'),
5762
getNextPageParam: () => 1,
5863
initialPageParam: 1,
@@ -63,61 +68,66 @@ describe('infiniteQueryOptions', () => {
6368
expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
6469
})
6570
it('should tag the queryKey with the result type of the QueryFn', () => {
66-
const { queryKey } = infiniteQueryOptions({
67-
queryKey: ['key'],
71+
const key = queryKey()
72+
const { queryKey: tagged } = infiniteQueryOptions({
73+
queryKey: key,
6874
queryFn: () => Promise.resolve('string'),
6975
getNextPageParam: () => 1,
7076
initialPageParam: 1,
7177
})
7278

73-
expectTypeOf(queryKey[dataTagSymbol]).toEqualTypeOf<InfiniteData<string>>()
79+
expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<InfiniteData<string>>()
7480
})
7581
it('should tag the queryKey even if no promise is returned', () => {
76-
const { queryKey } = infiniteQueryOptions({
77-
queryKey: ['key'],
82+
const key = queryKey()
83+
const { queryKey: tagged } = infiniteQueryOptions({
84+
queryKey: key,
7885
queryFn: () => 'string',
7986
getNextPageParam: () => 1,
8087
initialPageParam: 1,
8188
})
8289

83-
expectTypeOf(queryKey[dataTagSymbol]).toEqualTypeOf<InfiniteData<string>>()
90+
expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<InfiniteData<string>>()
8491
})
8592
it('should tag the queryKey with the result type of the QueryFn if select is used', () => {
86-
const { queryKey } = infiniteQueryOptions({
87-
queryKey: ['key'],
93+
const key = queryKey()
94+
const { queryKey: tagged } = infiniteQueryOptions({
95+
queryKey: key,
8896
queryFn: () => Promise.resolve('string'),
8997
select: (data) => data.pages,
9098
getNextPageParam: () => 1,
9199
initialPageParam: 1,
92100
})
93101

94-
expectTypeOf(queryKey[dataTagSymbol]).toEqualTypeOf<InfiniteData<string>>()
102+
expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<InfiniteData<string>>()
95103
})
96104
it('should return the proper type when passed to getQueryData', () => {
97-
const { queryKey } = infiniteQueryOptions({
98-
queryKey: ['key'],
105+
const key = queryKey()
106+
const { queryKey: tagged } = infiniteQueryOptions({
107+
queryKey: key,
99108
queryFn: () => Promise.resolve('string'),
100109
getNextPageParam: () => 1,
101110
initialPageParam: 1,
102111
})
103112

104113
const queryClient = new QueryClient()
105-
const data = queryClient.getQueryData(queryKey)
114+
const data = queryClient.getQueryData(tagged)
106115

107116
expectTypeOf(data).toEqualTypeOf<
108117
InfiniteData<string, unknown> | undefined
109118
>()
110119
})
111120
it('should properly type when passed to setQueryData', () => {
112-
const { queryKey } = infiniteQueryOptions({
113-
queryKey: ['key'],
121+
const key = queryKey()
122+
const { queryKey: tagged } = infiniteQueryOptions({
123+
queryKey: key,
114124
queryFn: () => Promise.resolve('string'),
115125
getNextPageParam: () => 1,
116126
initialPageParam: 1,
117127
})
118128

119129
const queryClient = new QueryClient()
120-
const data = queryClient.setQueryData(queryKey, (prev) => {
130+
const data = queryClient.setQueryData(tagged, (prev) => {
121131
expectTypeOf(prev).toEqualTypeOf<
122132
InfiniteData<string, unknown> | undefined
123133
>()
@@ -130,9 +140,10 @@ describe('infiniteQueryOptions', () => {
130140
})
131141

132142
test('should not be allowed to be passed to non-infinite query functions', () => {
143+
const key = queryKey()
133144
const queryClient = new QueryClient()
134145
const options = infiniteQueryOptions({
135-
queryKey: ['key'],
146+
queryKey: key,
136147
queryFn: () => Promise.resolve('string'),
137148
getNextPageParam: () => 1,
138149
initialPageParam: 1,
@@ -156,9 +167,10 @@ describe('infiniteQueryOptions', () => {
156167
})
157168

158169
test('allow optional initialData function', () => {
170+
const key = queryKey()
159171
const initialData: { example: boolean } | undefined = { example: true }
160172
const queryOptions = infiniteQueryOptions({
161-
queryKey: ['example'],
173+
queryKey: key,
162174
queryFn: () => initialData,
163175
initialData: initialData
164176
? () => ({ pages: [initialData], pageParams: [] })
@@ -174,9 +186,10 @@ describe('infiniteQueryOptions', () => {
174186
})
175187

176188
test('allow optional initialData object', () => {
189+
const key = queryKey()
177190
const initialData: { example: boolean } | undefined = { example: true }
178191
const queryOptions = infiniteQueryOptions({
179-
queryKey: ['example'],
192+
queryKey: key,
180193
queryFn: () => initialData,
181194
initialData: initialData
182195
? { pages: [initialData], pageParams: [] }

packages/angular-query-experimental/src/__tests__/infinite-query-options.test.ts

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

34
import { infiniteQueryOptions } from '../infinite-query-options'
45
import type { CreateInfiniteQueryOptions } from '../types'
56

67
describe('infiniteQueryOptions', () => {
78
it('should return the object received as a parameter without any modification.', () => {
9+
const key = queryKey()
810
const object: CreateInfiniteQueryOptions = {
9-
queryKey: ['key'],
11+
queryKey: key,
1012
queryFn: () => Promise.resolve(5),
1113
getNextPageParam: () => null,
1214
initialPageParam: null,

packages/angular-query-experimental/src/__tests__/inject-infinite-query.test-d.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { TestBed } from '@angular/core/testing'
22
import { afterEach, beforeEach, describe, expectTypeOf, test, vi } from 'vitest'
33
import { provideZonelessChangeDetection } from '@angular/core'
4-
import { sleep } from '@tanstack/query-test-utils'
4+
import { queryKey, sleep } from '@tanstack/query-test-utils'
55
import { QueryClient, injectInfiniteQuery, provideTanStackQuery } from '..'
66
import type { InfiniteData } from '@tanstack/query-core'
77

@@ -24,9 +24,10 @@ describe('injectInfiniteQuery', () => {
2424
})
2525

2626
test('should narrow type after isSuccess', () => {
27+
const key = queryKey()
2728
const query = TestBed.runInInjectionContext(() => {
2829
return injectInfiniteQuery(() => ({
29-
queryKey: ['infiniteQuery'],
30+
queryKey: key,
3031
queryFn: ({ pageParam }) =>
3132
sleep(0).then(() => 'data on page ' + pageParam),
3233
initialPageParam: 0,

packages/angular-query-experimental/src/__tests__/inject-infinite-query.test.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { TestBed } from '@angular/core/testing'
22
import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'
33
import { Injector, provideZonelessChangeDetection } from '@angular/core'
4-
import { sleep } from '@tanstack/query-test-utils'
4+
import { queryKey, sleep } from '@tanstack/query-test-utils'
55
import { QueryClient, injectInfiniteQuery, provideTanStackQuery } from '..'
66
import { expectSignals } from './test-utils'
77

@@ -24,9 +24,10 @@ describe('injectInfiniteQuery', () => {
2424
})
2525

2626
test('should properly execute infinite query', async () => {
27+
const key = queryKey()
2728
const query = TestBed.runInInjectionContext(() => {
2829
return injectInfiniteQuery(() => ({
29-
queryKey: ['infiniteQuery'],
30+
queryKey: key,
3031
queryFn: ({ pageParam }) =>
3132
sleep(10).then(() => 'data on page ' + pageParam),
3233
initialPageParam: 0,
@@ -64,9 +65,10 @@ describe('injectInfiniteQuery', () => {
6465

6566
describe('injection context', () => {
6667
test('throws NG0203 with descriptive error outside injection context', () => {
68+
const key = queryKey()
6769
expect(() => {
6870
injectInfiniteQuery(() => ({
69-
queryKey: ['injectionContextError'],
71+
queryKey: key,
7072
queryFn: ({ pageParam }) =>
7173
sleep(0).then(() => 'data on page ' + pageParam),
7274
initialPageParam: 0,
@@ -76,9 +78,10 @@ describe('injectInfiniteQuery', () => {
7678
})
7779

7880
test('can be used outside injection context when passing an injector', () => {
81+
const key = queryKey()
7982
const query = injectInfiniteQuery(
8083
() => ({
81-
queryKey: ['manualInjector'],
84+
queryKey: key,
8285
queryFn: ({ pageParam }) =>
8386
sleep(0).then(() => 'data on page ' + pageParam),
8487
initialPageParam: 0,

packages/angular-query-experimental/src/__tests__/inject-is-fetching.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { TestBed } from '@angular/core/testing'
22
import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'
33
import { Injector, provideZonelessChangeDetection } from '@angular/core'
4-
import { sleep } from '@tanstack/query-test-utils'
4+
import { queryKey, sleep } from '@tanstack/query-test-utils'
55
import {
66
QueryClient,
77
injectIsFetching,
@@ -29,9 +29,10 @@ describe('injectIsFetching', () => {
2929
})
3030

3131
test('Returns number of fetching queries', async () => {
32+
const key = queryKey()
3233
const isFetching = TestBed.runInInjectionContext(() => {
3334
injectQuery(() => ({
34-
queryKey: ['isFetching1'],
35+
queryKey: key,
3536
queryFn: () => sleep(100).then(() => 'Some data'),
3637
}))
3738
return injectIsFetching()

packages/angular-query-experimental/src/__tests__/inject-is-mutating.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'
22
import { TestBed } from '@angular/core/testing'
33
import { Injector, provideZonelessChangeDetection } from '@angular/core'
4-
import { sleep } from '@tanstack/query-test-utils'
4+
import { queryKey, sleep } from '@tanstack/query-test-utils'
55
import {
66
QueryClient,
77
injectIsMutating,
@@ -29,9 +29,10 @@ describe('injectIsMutating', () => {
2929
})
3030

3131
test('should properly return isMutating state', async () => {
32+
const key = queryKey()
3233
const [mutation, isMutating] = TestBed.runInInjectionContext(() => [
3334
injectMutation(() => ({
34-
mutationKey: ['isMutating1'],
35+
mutationKey: key,
3536
mutationFn: (params: { par1: string }) => sleep(10).then(() => params),
3637
})),
3738
injectIsMutating(),

packages/angular-query-experimental/src/__tests__/inject-mutation-state.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
import { TestBed } from '@angular/core/testing'
99
import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'
1010
import { By } from '@angular/platform-browser'
11-
import { sleep } from '@tanstack/query-test-utils'
11+
import { queryKey, sleep } from '@tanstack/query-test-utils'
1212
import {
1313
QueryClient,
1414
injectMutation,
@@ -37,7 +37,7 @@ describe('injectMutationState', () => {
3737

3838
describe('injectMutationState', () => {
3939
test('should return variables after calling mutate 1', () => {
40-
const mutationKey = ['mutation']
40+
const mutationKey = queryKey()
4141
const variables = 'foo123'
4242

4343
const mutation = TestBed.runInInjectionContext(() => {
@@ -60,8 +60,8 @@ describe('injectMutationState', () => {
6060
})
6161

6262
test('reactive options should update injectMutationState', () => {
63-
const mutationKey1 = ['mutation1']
64-
const mutationKey2 = ['mutation2']
63+
const mutationKey1 = queryKey()
64+
const mutationKey2 = queryKey()
6565
const variables1 = 'foo123'
6666
const variables2 = 'bar234'
6767

@@ -98,7 +98,7 @@ describe('injectMutationState', () => {
9898

9999
test('should return variables after calling mutate 2', () => {
100100
queryClient.clear()
101-
const mutationKey = ['mutation']
101+
const mutationKey = queryKey()
102102
const variables = 'bar234'
103103

104104
const mutation = TestBed.runInInjectionContext(() => {

0 commit comments

Comments
 (0)