Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions packages/vue-query/src/__tests__/mutationOptions.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<number>(() => ({
// @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<number>()
},
})),
).toEqualTypeOf<
() => WithRequired<
MutationOptions<number, DefaultError, string, unknown>,
'mutationKey'
>
>()
expectTypeOf(
mutationOptions(() => ({
mutationFn: (id: string) => Promise.resolve(id.length),
onSuccess: (data) => {
expectTypeOf(data).toEqualTypeOf<number>()
},
})),
).toEqualTypeOf<
() => Omit<
MutationOptions<number, DefaultError, string, unknown>,
'mutationKey'
>
>()
})

it('should work when used with useMutation (getter)', () => {
const mutation = useMutation(
mutationOptions(() => ({
mutationKey: ['key'],
mutationFn: () => Promise.resolve('data'),
onSuccess: (data) => {
expectTypeOf(data).toEqualTypeOf<string>()
},
})),
)
expectTypeOf(mutation.data.value).toEqualTypeOf<string | undefined>()
})
})
19 changes: 19 additions & 0 deletions packages/vue-query/src/__tests__/mutationOptions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<number> = []
const mutationOpts = mutationOptions({
Expand Down
Loading