|
| 1 | +import { describe, expectTypeOf, it } from 'vitest' |
| 2 | + |
| 3 | +import type { DefaultError, MutateFunction, MutateOptions } from '../types' |
| 4 | + |
| 5 | +describe('MutateFunction', () => { |
| 6 | + it('void variables', () => { |
| 7 | + const mutate = {} as MutateFunction |
| 8 | + |
| 9 | + expectTypeOf<Parameters<typeof mutate>[0]>().toEqualTypeOf< |
| 10 | + undefined | void |
| 11 | + >() |
| 12 | + |
| 13 | + expectTypeOf<Parameters<typeof mutate>[1]>().toEqualTypeOf< |
| 14 | + undefined | MutateOptions<unknown, DefaultError, void, unknown> |
| 15 | + >() |
| 16 | + |
| 17 | + mutate() // can be called with no arguments |
| 18 | + mutate(undefined, { |
| 19 | + onError: (e) => { |
| 20 | + expectTypeOf(e).toEqualTypeOf<DefaultError>() |
| 21 | + }, |
| 22 | + }) |
| 23 | + }) |
| 24 | + |
| 25 | + it('optional undefinable variables', () => { |
| 26 | + const mutate = {} as MutateFunction< |
| 27 | + unknown, |
| 28 | + DefaultError, |
| 29 | + number | undefined, |
| 30 | + unknown |
| 31 | + > |
| 32 | + |
| 33 | + expectTypeOf<Parameters<typeof mutate>[0]>().toEqualTypeOf< |
| 34 | + number | undefined |
| 35 | + >() |
| 36 | + |
| 37 | + expectTypeOf<Parameters<typeof mutate>[1]>().toEqualTypeOf< |
| 38 | + | undefined |
| 39 | + | MutateOptions<unknown, DefaultError, number | undefined, unknown> |
| 40 | + >() |
| 41 | + |
| 42 | + mutate() // can be called with no arguments |
| 43 | + mutate(undefined, { |
| 44 | + onError: (e) => { |
| 45 | + expectTypeOf(e).toEqualTypeOf<DefaultError>() |
| 46 | + }, |
| 47 | + }) |
| 48 | + }) |
| 49 | + |
| 50 | + it('unknown variables', () => { |
| 51 | + const mutate = {} as MutateFunction<unknown, DefaultError, unknown, unknown> |
| 52 | + |
| 53 | + expectTypeOf<Parameters<typeof mutate>[0]>().toEqualTypeOf<unknown>() |
| 54 | + |
| 55 | + expectTypeOf<Parameters<typeof mutate>[1]>().toEqualTypeOf< |
| 56 | + undefined | MutateOptions<unknown, DefaultError, unknown, unknown> |
| 57 | + >() |
| 58 | + |
| 59 | + mutate() // can be called with no arguments |
| 60 | + }) |
| 61 | + |
| 62 | + it('any variables', () => { |
| 63 | + const mutate = {} as MutateFunction<unknown, DefaultError, any, unknown> |
| 64 | + |
| 65 | + expectTypeOf<Parameters<typeof mutate>[0]>().toEqualTypeOf<any>() |
| 66 | + |
| 67 | + expectTypeOf<Parameters<typeof mutate>[1]>().toEqualTypeOf< |
| 68 | + undefined | MutateOptions<unknown, DefaultError, any, unknown> |
| 69 | + >() |
| 70 | + |
| 71 | + mutate() // can be called with no arguments |
| 72 | + }) |
| 73 | + |
| 74 | + it('void union variables', () => { |
| 75 | + const mutate = {} as MutateFunction< |
| 76 | + unknown, |
| 77 | + DefaultError, |
| 78 | + void | string, |
| 79 | + unknown |
| 80 | + > |
| 81 | + |
| 82 | + expectTypeOf<Parameters<typeof mutate>[0]>().toEqualTypeOf< |
| 83 | + void | string | undefined |
| 84 | + >() |
| 85 | + |
| 86 | + expectTypeOf<Parameters<typeof mutate>[1]>().toEqualTypeOf< |
| 87 | + undefined | MutateOptions<unknown, DefaultError, void | string, unknown> |
| 88 | + >() |
| 89 | + |
| 90 | + mutate() // can be called with no arguments |
| 91 | + }) |
| 92 | + |
| 93 | + it('required non-undefinable variables', () => { |
| 94 | + const mutate = {} as MutateFunction<unknown, DefaultError, number, unknown> |
| 95 | + |
| 96 | + expectTypeOf<Parameters<typeof mutate>[0]>().toEqualTypeOf<number>() |
| 97 | + |
| 98 | + expectTypeOf<Parameters<typeof mutate>[1]>().toEqualTypeOf< |
| 99 | + undefined | MutateOptions<unknown, DefaultError, number, unknown> |
| 100 | + >() |
| 101 | + |
| 102 | + // @ts-expect-error --- required variables |
| 103 | + mutate() |
| 104 | + mutate(123, { |
| 105 | + onError: (e) => { |
| 106 | + expectTypeOf(e).toEqualTypeOf<DefaultError>() |
| 107 | + }, |
| 108 | + }) |
| 109 | + }) |
| 110 | + |
| 111 | + describe('compatible with spread arguments pattern', () => { |
| 112 | + // this is common pattern used internal so we need make sure it still works |
| 113 | + |
| 114 | + it('void variables', () => { |
| 115 | + const mutate = {} as (...options: Parameters<MutateFunction>) => void |
| 116 | + |
| 117 | + expectTypeOf<Parameters<typeof mutate>[0]>().toEqualTypeOf< |
| 118 | + undefined | void |
| 119 | + >() |
| 120 | + |
| 121 | + expectTypeOf<Parameters<typeof mutate>[1]>().toEqualTypeOf< |
| 122 | + undefined | MutateOptions<unknown, DefaultError, void, unknown> |
| 123 | + >() |
| 124 | + |
| 125 | + mutate() // can be called with no arguments |
| 126 | + mutate(undefined, { |
| 127 | + onError: (e) => { |
| 128 | + expectTypeOf(e).toEqualTypeOf<DefaultError>() |
| 129 | + }, |
| 130 | + }) |
| 131 | + }) |
| 132 | + |
| 133 | + it('optional undefinable variables', () => { |
| 134 | + const mutate = {} as ( |
| 135 | + ...options: Parameters< |
| 136 | + MutateFunction<unknown, DefaultError, number | undefined, unknown> |
| 137 | + > |
| 138 | + ) => void |
| 139 | + |
| 140 | + expectTypeOf<Parameters<typeof mutate>[0]>().toEqualTypeOf< |
| 141 | + number | undefined |
| 142 | + >() |
| 143 | + |
| 144 | + expectTypeOf<Parameters<typeof mutate>[1]>().toEqualTypeOf< |
| 145 | + | undefined |
| 146 | + | MutateOptions<unknown, DefaultError, number | undefined, unknown> |
| 147 | + >() |
| 148 | + |
| 149 | + mutate() // can be called with no arguments |
| 150 | + mutate(undefined, { |
| 151 | + onError: (e) => { |
| 152 | + expectTypeOf(e).toEqualTypeOf<DefaultError>() |
| 153 | + }, |
| 154 | + }) |
| 155 | + }) |
| 156 | + |
| 157 | + it('unknown variables', () => { |
| 158 | + const mutate = {} as ( |
| 159 | + ...options: Parameters< |
| 160 | + MutateFunction<unknown, DefaultError, unknown, unknown> |
| 161 | + > |
| 162 | + ) => void |
| 163 | + |
| 164 | + expectTypeOf<Parameters<typeof mutate>[0]>().toEqualTypeOf<unknown>() |
| 165 | + |
| 166 | + expectTypeOf<Parameters<typeof mutate>[1]>().toEqualTypeOf< |
| 167 | + undefined | MutateOptions<unknown, DefaultError, unknown, unknown> |
| 168 | + >() |
| 169 | + |
| 170 | + mutate() // can be called with no arguments |
| 171 | + }) |
| 172 | + |
| 173 | + it('any variables', () => { |
| 174 | + const mutate = {} as ( |
| 175 | + ...options: Parameters< |
| 176 | + MutateFunction<unknown, DefaultError, any, unknown> |
| 177 | + > |
| 178 | + ) => void |
| 179 | + |
| 180 | + expectTypeOf<Parameters<typeof mutate>[0]>().toEqualTypeOf<any>() |
| 181 | + |
| 182 | + expectTypeOf<Parameters<typeof mutate>[1]>().toEqualTypeOf< |
| 183 | + undefined | MutateOptions<unknown, DefaultError, any, unknown> |
| 184 | + >() |
| 185 | + |
| 186 | + mutate() // can be called with no arguments |
| 187 | + }) |
| 188 | + |
| 189 | + it('void union variables', () => { |
| 190 | + const mutate = {} as ( |
| 191 | + ...options: Parameters< |
| 192 | + MutateFunction<unknown, DefaultError, void | string, unknown> |
| 193 | + > |
| 194 | + ) => void |
| 195 | + |
| 196 | + expectTypeOf<Parameters<typeof mutate>[0]>().toEqualTypeOf< |
| 197 | + void | string | undefined |
| 198 | + >() |
| 199 | + |
| 200 | + expectTypeOf<Parameters<typeof mutate>[1]>().toEqualTypeOf< |
| 201 | + undefined | MutateOptions<unknown, DefaultError, void | string, unknown> |
| 202 | + >() |
| 203 | + |
| 204 | + mutate() // can be called with no arguments |
| 205 | + }) |
| 206 | + |
| 207 | + it('required non-undefinable variables', () => { |
| 208 | + const mutate = {} as ( |
| 209 | + ...options: Parameters< |
| 210 | + MutateFunction<unknown, DefaultError, number, unknown> |
| 211 | + > |
| 212 | + ) => void |
| 213 | + |
| 214 | + expectTypeOf<Parameters<typeof mutate>[0]>().toEqualTypeOf<number>() |
| 215 | + |
| 216 | + expectTypeOf<Parameters<typeof mutate>[1]>().toEqualTypeOf< |
| 217 | + undefined | MutateOptions<unknown, DefaultError, number, unknown> |
| 218 | + >() |
| 219 | + |
| 220 | + // @ts-expect-error --- required variables |
| 221 | + mutate() |
| 222 | + mutate(123, { |
| 223 | + onError: (e) => { |
| 224 | + expectTypeOf(e).toEqualTypeOf<DefaultError>() |
| 225 | + }, |
| 226 | + }) |
| 227 | + }) |
| 228 | + }) |
| 229 | +}) |
0 commit comments