|  | 
|  | 1 | +import { QueryClient } from '../queryClient' | 
|  | 2 | +import { doNotExecute } from './utils' | 
|  | 3 | +import type { Equal, Expect } from './utils' | 
|  | 4 | +import type { DataTag, InfiniteData } from '../types' | 
|  | 5 | + | 
|  | 6 | +describe('getQueryData', () => { | 
|  | 7 | +  it('should be typed if key is tagged', () => { | 
|  | 8 | +    doNotExecute(() => { | 
|  | 9 | +      const queryKey = ['key'] as DataTag<Array<string>, number> | 
|  | 10 | +      const queryClient = new QueryClient() | 
|  | 11 | +      const data = queryClient.getQueryData(queryKey) | 
|  | 12 | + | 
|  | 13 | +      const result: Expect<Equal<typeof data, number | undefined>> = true | 
|  | 14 | +      return result | 
|  | 15 | +    }) | 
|  | 16 | +  }) | 
|  | 17 | + | 
|  | 18 | +  it('should infer unknown if key is not tagged', () => { | 
|  | 19 | +    doNotExecute(() => { | 
|  | 20 | +      const queryKey = ['key'] as const | 
|  | 21 | +      const queryClient = new QueryClient() | 
|  | 22 | +      const data = queryClient.getQueryData(queryKey) | 
|  | 23 | + | 
|  | 24 | +      const result: Expect<Equal<typeof data, unknown>> = true | 
|  | 25 | +      return result | 
|  | 26 | +    }) | 
|  | 27 | +  }) | 
|  | 28 | + | 
|  | 29 | +  it('should infer passed generic if passed', () => { | 
|  | 30 | +    doNotExecute(() => { | 
|  | 31 | +      const queryKey = ['key'] as const | 
|  | 32 | +      const queryClient = new QueryClient() | 
|  | 33 | +      const data = queryClient.getQueryData<number>(queryKey) | 
|  | 34 | + | 
|  | 35 | +      const result: Expect<Equal<typeof data, number | undefined>> = true | 
|  | 36 | +      return result | 
|  | 37 | +    }) | 
|  | 38 | +  }) | 
|  | 39 | + | 
|  | 40 | +  it('should only allow Arrays to be passed', () => { | 
|  | 41 | +    doNotExecute(() => { | 
|  | 42 | +      const queryKey = 'key' as const | 
|  | 43 | +      const queryClient = new QueryClient() | 
|  | 44 | +      // @ts-expect-error TS2345: Argument of type 'string' is not assignable to parameter of type 'QueryKey' | 
|  | 45 | +      return queryClient.getQueryData(queryKey) | 
|  | 46 | +    }) | 
|  | 47 | +  }) | 
|  | 48 | +}) | 
|  | 49 | + | 
|  | 50 | +describe('setQueryData', () => { | 
|  | 51 | +  it('updater should be typed if key is tagged', () => { | 
|  | 52 | +    doNotExecute(() => { | 
|  | 53 | +      const queryKey = ['key'] as DataTag<Array<string>, number> | 
|  | 54 | +      const queryClient = new QueryClient() | 
|  | 55 | +      const data = queryClient.setQueryData(queryKey, (prev) => { | 
|  | 56 | +        const result: Expect<Equal<typeof prev, number | undefined>> = true | 
|  | 57 | +        return result ? prev : 1 | 
|  | 58 | +      }) | 
|  | 59 | + | 
|  | 60 | +      const result: Expect<Equal<typeof data, number | undefined>> = true | 
|  | 61 | +      return result | 
|  | 62 | +    }) | 
|  | 63 | +  }) | 
|  | 64 | + | 
|  | 65 | +  it('value should be typed if key is tagged', () => { | 
|  | 66 | +    doNotExecute(() => { | 
|  | 67 | +      const queryKey = ['key'] as DataTag<Array<string>, number> | 
|  | 68 | +      const queryClient = new QueryClient() | 
|  | 69 | + | 
|  | 70 | +      // @ts-expect-error value should be a number | 
|  | 71 | +      queryClient.setQueryData(queryKey, '1') | 
|  | 72 | + | 
|  | 73 | +      // @ts-expect-error value should be a number | 
|  | 74 | +      queryClient.setQueryData(queryKey, () => '1') | 
|  | 75 | + | 
|  | 76 | +      const data = queryClient.setQueryData(queryKey, 1) | 
|  | 77 | + | 
|  | 78 | +      const result: Expect<Equal<typeof data, number | undefined>> = true | 
|  | 79 | +      return result | 
|  | 80 | +    }) | 
|  | 81 | +  }) | 
|  | 82 | + | 
|  | 83 | +  it('should infer unknown for updater if key is not tagged', () => { | 
|  | 84 | +    doNotExecute(() => { | 
|  | 85 | +      const queryKey = ['key'] as const | 
|  | 86 | +      const queryClient = new QueryClient() | 
|  | 87 | +      const data = queryClient.setQueryData(queryKey, (prev) => { | 
|  | 88 | +        const result: Expect<Equal<typeof prev, unknown>> = true | 
|  | 89 | +        return result ? prev : 1 | 
|  | 90 | +      }) | 
|  | 91 | + | 
|  | 92 | +      const result: Expect<Equal<typeof data, unknown>> = true | 
|  | 93 | +      return result | 
|  | 94 | +    }) | 
|  | 95 | +  }) | 
|  | 96 | + | 
|  | 97 | +  it('should infer unknown for value if key is not tagged', () => { | 
|  | 98 | +    doNotExecute(() => { | 
|  | 99 | +      const queryKey = ['key'] as const | 
|  | 100 | +      const queryClient = new QueryClient() | 
|  | 101 | +      const data = queryClient.setQueryData(queryKey, 'foo') | 
|  | 102 | + | 
|  | 103 | +      const result: Expect<Equal<typeof data, unknown>> = true | 
|  | 104 | +      return result | 
|  | 105 | +    }) | 
|  | 106 | +  }) | 
|  | 107 | + | 
|  | 108 | +  it('should infer passed generic if passed', () => { | 
|  | 109 | +    doNotExecute(() => { | 
|  | 110 | +      const queryKey = ['key'] as const | 
|  | 111 | +      const queryClient = new QueryClient() | 
|  | 112 | +      const data = queryClient.setQueryData<string>(queryKey, (prev) => { | 
|  | 113 | +        const result: Expect<Equal<typeof prev, string | undefined>> = true | 
|  | 114 | +        return result ? prev : '1' | 
|  | 115 | +      }) | 
|  | 116 | + | 
|  | 117 | +      const result: Expect<Equal<typeof data, string | undefined>> = true | 
|  | 118 | +      return result | 
|  | 119 | +    }) | 
|  | 120 | +  }) | 
|  | 121 | + | 
|  | 122 | +  it('should infer passed generic for value', () => { | 
|  | 123 | +    doNotExecute(() => { | 
|  | 124 | +      const queryKey = ['key'] as const | 
|  | 125 | +      const queryClient = new QueryClient() | 
|  | 126 | +      const data = queryClient.setQueryData<string>(queryKey, 'foo') | 
|  | 127 | + | 
|  | 128 | +      const result: Expect<Equal<typeof data, string | undefined>> = true | 
|  | 129 | +      return result | 
|  | 130 | +    }) | 
|  | 131 | +  }) | 
|  | 132 | +}) | 
|  | 133 | + | 
|  | 134 | +describe('fetchInfiniteQuery', () => { | 
|  | 135 | +  it('should allow passing pages', () => { | 
|  | 136 | +    doNotExecute(async () => { | 
|  | 137 | +      const data = await new QueryClient().fetchInfiniteQuery({ | 
|  | 138 | +        queryKey: ['key'], | 
|  | 139 | +        queryFn: () => Promise.resolve('string'), | 
|  | 140 | +        getNextPageParam: () => 1, | 
|  | 141 | +        initialPageParam: 1, | 
|  | 142 | +        pages: 5, | 
|  | 143 | +      }) | 
|  | 144 | + | 
|  | 145 | +      const result: Expect<Equal<typeof data, InfiniteData<string, number>>> = | 
|  | 146 | +        true | 
|  | 147 | +      return result | 
|  | 148 | +    }) | 
|  | 149 | +  }) | 
|  | 150 | + | 
|  | 151 | +  it('should not allow passing getNextPageParam without pages', () => { | 
|  | 152 | +    doNotExecute(async () => { | 
|  | 153 | +      return new QueryClient().fetchInfiniteQuery({ | 
|  | 154 | +        queryKey: ['key'], | 
|  | 155 | +        queryFn: () => Promise.resolve('string'), | 
|  | 156 | +        initialPageParam: 1, | 
|  | 157 | +        getNextPageParam: () => 1, | 
|  | 158 | +      }) | 
|  | 159 | +    }) | 
|  | 160 | +  }) | 
|  | 161 | + | 
|  | 162 | +  it('should not allow passing pages without getNextPageParam', () => { | 
|  | 163 | +    doNotExecute(async () => { | 
|  | 164 | +      // @ts-expect-error Property 'getNextPageParam' is missing | 
|  | 165 | +      return new QueryClient().fetchInfiniteQuery({ | 
|  | 166 | +        queryKey: ['key'], | 
|  | 167 | +        queryFn: () => Promise.resolve('string'), | 
|  | 168 | +        initialPageParam: 1, | 
|  | 169 | +        pages: 5, | 
|  | 170 | +      }) | 
|  | 171 | +    }) | 
|  | 172 | +  }) | 
|  | 173 | +}) | 
0 commit comments