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
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { sleep } from '@tanstack/query-test-utils'
import { createMutation } from '../../src/index.js'
import { withEffectRoot } from '../utils.svelte.js'
import ResetExample from './ResetExample.svelte'
import OnSuccessExample from './OnSuccessExample.svelte'
import SuccessExample from './SuccessExample.svelte'
import FailureExample from './FailureExample.svelte'

describe('createMutation', () => {
Expand Down Expand Up @@ -42,7 +42,7 @@ describe('createMutation', () => {
const onSuccessMock = vi.fn()
const onSettledMock = vi.fn()

const rendered = render(OnSuccessExample, {
const rendered = render(SuccessExample, {
props: {
queryClient,
onSuccessMock,
Expand Down
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>()
})
})
Loading
Loading