Skip to content

Commit ce18db8

Browse files
committed
feat(vue-query/mutationOptions): add getter support and fix test descriptions to match actual behavior
1 parent 5ca23d0 commit ce18db8

3 files changed

Lines changed: 67 additions & 27 deletions

File tree

packages/vue-query/src/__tests__/mutationOptions.test-d.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import type { VueMutationOptions } from '../types'
1414

1515
describe('mutationOptions', () => {
1616
it('should not allow excess properties', () => {
17-
// @ts-expect-error this is a good error, because onMutates does not exist!
1817
mutationOptions({
18+
// @ts-expect-error this is a good error, because onMutates does not exist!
1919
mutationFn: () => Promise.resolve(5),
2020
mutationKey: ['key'],
2121
onMutates: 1000,
@@ -217,4 +217,30 @@ describe('mutationOptions', () => {
217217
}),
218218
})
219219
})
220+
221+
it('should allow getter and infer types correctly', () => {
222+
const options = mutationOptions(() => ({
223+
mutationKey: ['key'] as const,
224+
mutationFn: () => Promise.resolve('data'),
225+
onSuccess: (data) => {
226+
expectTypeOf(data).toEqualTypeOf<string>()
227+
},
228+
}))
229+
230+
const resolved = options()
231+
expectTypeOf(resolved.mutationFn).not.toBeUndefined()
232+
expectTypeOf(resolved.mutationKey).not.toBeUndefined()
233+
})
234+
235+
it('should allow getter without mutationKey', () => {
236+
const options = mutationOptions(() => ({
237+
mutationFn: () => Promise.resolve(5),
238+
onSuccess: (data) => {
239+
expectTypeOf(data).toEqualTypeOf<number>()
240+
},
241+
}))
242+
243+
const resolved = options()
244+
expectTypeOf(resolved.mutationFn).not.toBeUndefined()
245+
})
220246
})

packages/vue-query/src/__tests__/mutationOptions.test.ts

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -340,50 +340,43 @@ describe('mutationOptions', () => {
340340
expect(states.value).toEqual(['data1'])
341341
})
342342

343-
it('should work with options getter and be reactive when used with useIsMutating', async () => {
344-
const keyRef = ref('isMutatingGetter2')
345-
const mutationOpts = mutationOptions({
346-
mutationKey: ['isMutatingGetter'],
343+
it('should work with getter passed to mutationOptions when used with useIsMutating', async () => {
344+
const keyRef = ref('isMutatingGetter')
345+
const mutationOpts = mutationOptions(() => ({
346+
mutationKey: [keyRef.value],
347347
mutationFn: () => sleep(10).then(() => 'data'),
348-
})
348+
}))
349349

350350
const { mutate } = useMutation(mutationOpts)
351351
mutate()
352352

353-
const isMutating = useIsMutating(() => ({
353+
const isMutating = useIsMutating({
354354
mutationKey: [keyRef.value],
355-
}))
356-
357-
expect(isMutating.value).toEqual(0)
358-
359-
keyRef.value = 'isMutatingGetter'
355+
})
360356

361357
await vi.advanceTimersByTimeAsync(0)
362-
363358
expect(isMutating.value).toEqual(1)
359+
360+
await vi.advanceTimersByTimeAsync(10)
361+
expect(isMutating.value).toEqual(0)
364362
})
365363

366-
it('should work with options getter and be reactive when used with useMutationState', async () => {
367-
const keyRef = ref('useMutationStateGetter2')
368-
const mutationOpts = mutationOptions({
369-
mutationKey: ['useMutationStateGetter'],
364+
it('should work with getter passed to mutationOptions when used with useMutationState', async () => {
365+
const keyRef = ref('useMutationStateGetter')
366+
const mutationOpts = mutationOptions(() => ({
367+
mutationKey: [keyRef.value],
370368
mutationFn: (params: string) => sleep(10).then(() => params),
371-
})
369+
}))
372370

373371
const { mutate } = useMutation(mutationOpts)
374372
mutate('foo')
375373

376-
const states = useMutationState(() => ({
374+
const states = useMutationState({
377375
filters: { mutationKey: [keyRef.value], status: 'pending' },
378376
select: (mutation) => mutation.state.variables,
379-
}))
380-
381-
expect(states.value).toEqual([])
382-
383-
keyRef.value = 'useMutationStateGetter'
377+
})
384378

385379
await vi.advanceTimersByTimeAsync(0)
386-
387380
expect(states.value).toEqual(['foo'])
388381
})
389382

packages/vue-query/src/mutationOptions.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,20 @@ export function mutationOptions<
1515
VueMutationOptions<TData, TError, TVariables, TOnMutateResult>,
1616
'mutationKey'
1717
>
18+
export function mutationOptions<
19+
TData = unknown,
20+
TError = DefaultError,
21+
TVariables = void,
22+
TOnMutateResult = unknown,
23+
>(
24+
options: () => WithRequired<
25+
VueMutationOptions<TData, TError, TVariables, TOnMutateResult>,
26+
'mutationKey'
27+
>,
28+
): () => WithRequired<
29+
VueMutationOptions<TData, TError, TVariables, TOnMutateResult>,
30+
'mutationKey'
31+
>
1832
export function mutationOptions<
1933
TData = unknown,
2034
TError = DefaultError,
@@ -35,7 +49,14 @@ export function mutationOptions<
3549
TVariables = void,
3650
TOnMutateResult = unknown,
3751
>(
38-
options: VueMutationOptions<TData, TError, TVariables, TOnMutateResult>,
39-
): VueMutationOptions<TData, TError, TVariables, TOnMutateResult> {
52+
options: () => Omit<
53+
VueMutationOptions<TData, TError, TVariables, TOnMutateResult>,
54+
'mutationKey'
55+
>,
56+
): () => Omit<
57+
VueMutationOptions<TData, TError, TVariables, TOnMutateResult>,
58+
'mutationKey'
59+
>
60+
export function mutationOptions(options: unknown) {
4061
return options
4162
}

0 commit comments

Comments
 (0)