diff --git a/packages/vue-query/src/__tests__/mutationOptions.test-d.ts b/packages/vue-query/src/__tests__/mutationOptions.test-d.ts index 9f0d1fe4a74..14d19a9f43e 100644 --- a/packages/vue-query/src/__tests__/mutationOptions.test-d.ts +++ b/packages/vue-query/src/__tests__/mutationOptions.test-d.ts @@ -243,4 +243,56 @@ describe('mutationOptions', () => { const resolved = options() expectTypeOf(resolved.mutationFn).not.toBeUndefined() }) + + it('should error if mutationFn return type mismatches TData (getter)', () => { + assertType( + mutationOptions(() => ({ + // @ts-expect-error this is a good error, because return type is string, not number + mutationFn: async () => Promise.resolve('wrong return'), + })), + ) + }) + + it('should infer all types when not explicitly provided (getter)', () => { + expectTypeOf( + mutationOptions(() => ({ + mutationFn: (id: string) => Promise.resolve(id.length), + mutationKey: ['key'], + onSuccess: (data) => { + expectTypeOf(data).toEqualTypeOf() + }, + })), + ).toEqualTypeOf< + () => WithRequired< + MutationOptions, + 'mutationKey' + > + >() + expectTypeOf( + mutationOptions(() => ({ + mutationFn: (id: string) => Promise.resolve(id.length), + onSuccess: (data) => { + expectTypeOf(data).toEqualTypeOf() + }, + })), + ).toEqualTypeOf< + () => Omit< + MutationOptions, + 'mutationKey' + > + >() + }) + + it('should work when used with useMutation (getter)', () => { + const mutation = useMutation( + mutationOptions(() => ({ + mutationKey: ['key'], + mutationFn: () => Promise.resolve('data'), + onSuccess: (data) => { + expectTypeOf(data).toEqualTypeOf() + }, + })), + ) + expectTypeOf(mutation.data.value).toEqualTypeOf() + }) }) diff --git a/packages/vue-query/src/__tests__/mutationOptions.test.ts b/packages/vue-query/src/__tests__/mutationOptions.test.ts index df7678c6438..3c0a1ba52c3 100644 --- a/packages/vue-query/src/__tests__/mutationOptions.test.ts +++ b/packages/vue-query/src/__tests__/mutationOptions.test.ts @@ -35,6 +35,25 @@ describe('mutationOptions', () => { expect(mutationOptions(object)).toStrictEqual(object) }) + it('should return the getter received as a parameter without any modification (with mutationKey in mutationOptions)', () => { + const getter = () => + ({ + mutationKey: ['key'], + mutationFn: () => sleep(10).then(() => 5), + }) as const + + expect(mutationOptions(getter)).toBe(getter) + }) + + it('should return the getter received as a parameter without any modification (without mutationKey in mutationOptions)', () => { + const getter = () => + ({ + mutationFn: () => sleep(10).then(() => 5), + }) as const + + expect(mutationOptions(getter)).toBe(getter) + }) + it('should return the number of fetching mutations when used with useIsMutating (with mutationKey in mutationOptions)', async () => { const isMutatingArray: Array = [] const mutationOpts = mutationOptions({