|
1 | 1 | import { describe, expectTypeOf, it } from 'vitest'
|
2 | 2 | import { QueryClient } from '../queryClient'
|
| 3 | +import type { QueryState } from '../query' |
3 | 4 | import type { DataTag, InfiniteData, QueryKey } from '../types'
|
4 | 5 |
|
5 | 6 | describe('getQueryData', () => {
|
@@ -101,6 +102,35 @@ describe('setQueryData', () => {
|
101 | 102 | })
|
102 | 103 | })
|
103 | 104 |
|
| 105 | +describe('getQueryState', () => { |
| 106 | + it('should be loose typed without tag', () => { |
| 107 | + const queryKey = ['key'] as const |
| 108 | + const queryClient = new QueryClient() |
| 109 | + const data = queryClient.getQueryState(queryKey) |
| 110 | + |
| 111 | + expectTypeOf(data).toEqualTypeOf<QueryState<unknown, Error> | undefined>() |
| 112 | + }) |
| 113 | + |
| 114 | + it('should be typed if key is tagged', () => { |
| 115 | + const queryKey = ['key'] as DataTag<Array<string>, number> |
| 116 | + const queryClient = new QueryClient() |
| 117 | + const data = queryClient.getQueryState(queryKey) |
| 118 | + |
| 119 | + expectTypeOf(data).toEqualTypeOf<QueryState<number, Error> | undefined>() |
| 120 | + }) |
| 121 | + |
| 122 | + it('should be typed including error if key is tagged', () => { |
| 123 | + type CustomError = Error & { customError: string } |
| 124 | + const queryKey = ['key'] as DataTag<Array<string>, number, CustomError> |
| 125 | + const queryClient = new QueryClient() |
| 126 | + const data = queryClient.getQueryState(queryKey) |
| 127 | + |
| 128 | + expectTypeOf(data).toEqualTypeOf< |
| 129 | + QueryState<number, CustomError> | undefined |
| 130 | + >() |
| 131 | + }) |
| 132 | +}) |
| 133 | + |
104 | 134 | describe('fetchInfiniteQuery', () => {
|
105 | 135 | it('should allow passing pages', async () => {
|
106 | 136 | const data = await new QueryClient().fetchInfiniteQuery({
|
|
0 commit comments