Skip to content

Commit a2f2b8a

Browse files
committed
test(query-core): add tests for partial QueryKey matching for QueryFilters
add tests to verify that the new partial QueryKey matching for `QueryFilters` does match partial query keys, and also doesn't allow invalid query keys.
1 parent 7a0f865 commit a2f2b8a

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

packages/query-core/src/__tests__/utils.test-d.tsx

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { describe, expectTypeOf, it } from 'vitest'
1+
import { assertType, describe, expectTypeOf, it } from 'vitest'
22
import { QueryClient } from '../queryClient'
33
import type { QueryFilters } from '../utils'
44
import type { DataTag, QueryKey } from '../types'
@@ -35,11 +35,36 @@ describe('QueryFilters', () => {
3535
}
3636

3737
const queryClient = new QueryClient()
38-
3938
const data = queryClient.getQueryData(a.queryKey!)
4039
expectTypeOf(data).toEqualTypeOf<unknown>()
4140

4241
const error = queryClient.getQueryState(a.queryKey!)?.error
4342
expectTypeOf(error).toEqualTypeOf<Error | null | undefined>()
4443
})
44+
45+
it('should allow a partial query key to be passed', () => {
46+
const filters: QueryFilters<readonly ['key', { a: number; b: string }]> = {
47+
queryKey: ['key'],
48+
}
49+
50+
expectTypeOf(filters.queryKey).toEqualTypeOf<
51+
| undefined
52+
| readonly []
53+
| ['key']
54+
| readonly [
55+
'key',
56+
{
57+
a: number
58+
b: string
59+
},
60+
]
61+
>()
62+
})
63+
64+
it('should error on invalid query keys', () => {
65+
assertType<QueryFilters<readonly ['key', { a: number; b: string }]>>({
66+
// @ts-expect-error cannot pass invalid query key
67+
queryKey: ['invalid', { a: 1, b: '1' }],
68+
})
69+
})
4570
})

0 commit comments

Comments
 (0)