Skip to content

Commit c8de088

Browse files
authored
test(vue-query/mutationOptions): add 'useMutationState' tests for getter overloads (#10472)
1 parent 8fc02cc commit c8de088

1 file changed

Lines changed: 68 additions & 0 deletions

File tree

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

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,74 @@ describe('mutationOptions', () => {
564564
expect(states.value).toEqual(['data1'])
565565
})
566566

567+
it('should return mutation states when used with useMutationState (getter without mutationKey in mutationOptions)', async () => {
568+
const mutationOpts = mutationOptions(() => ({
569+
mutationFn: () => sleep(10).then(() => 'data'),
570+
}))
571+
572+
const { mutate } = useMutation(mutationOpts)
573+
const states = useMutationState({
574+
filters: { status: 'success' },
575+
select: (mutation) => mutation.state.data,
576+
})
577+
578+
expect(states.value).toEqual([])
579+
580+
mutate()
581+
await vi.advanceTimersByTimeAsync(10)
582+
expect(states.value).toEqual(['data'])
583+
})
584+
585+
it('should return mutation states when used with useMutationState (getter)', async () => {
586+
const mutationOpts1 = mutationOptions(() => ({
587+
mutationKey: ['mutation'],
588+
mutationFn: () => sleep(10).then(() => 'data1'),
589+
}))
590+
const mutationOpts2 = mutationOptions(() => ({
591+
mutationFn: () => sleep(10).then(() => 'data2'),
592+
}))
593+
594+
const { mutate: mutate1 } = useMutation(mutationOpts1)
595+
const { mutate: mutate2 } = useMutation(mutationOpts2)
596+
const states = useMutationState({
597+
filters: { status: 'success' },
598+
select: (mutation) => mutation.state.data,
599+
})
600+
601+
expect(states.value).toEqual([])
602+
603+
mutate1()
604+
mutate2()
605+
await vi.advanceTimersByTimeAsync(10)
606+
expect(states.value).toEqual(['data1', 'data2'])
607+
})
608+
609+
it('should return mutation states when used with useMutationState (getter, filter mutationOpts1.mutationKey)', async () => {
610+
const mutationOpts1 = mutationOptions(() => ({
611+
mutationKey: ['mutation'],
612+
mutationFn: () => sleep(10).then(() => 'data1'),
613+
}))
614+
const mutationOpts2 = mutationOptions(() => ({
615+
mutationFn: () => sleep(10).then(() => 'data2'),
616+
}))
617+
618+
const resolvedOpts1 = mutationOpts1()
619+
620+
const { mutate: mutate1 } = useMutation(mutationOpts1)
621+
const { mutate: mutate2 } = useMutation(mutationOpts2)
622+
const states = useMutationState({
623+
filters: { mutationKey: resolvedOpts1.mutationKey, status: 'success' },
624+
select: (mutation) => mutation.state.data,
625+
})
626+
627+
expect(states.value).toEqual([])
628+
629+
mutate1()
630+
mutate2()
631+
await vi.advanceTimersByTimeAsync(10)
632+
expect(states.value).toEqual(['data1'])
633+
})
634+
567635
it('should work with getter passed to mutationOptions when used with useIsMutating', async () => {
568636
const keyRef = ref('isMutatingGetter')
569637
const mutationOpts = mutationOptions(() => ({

0 commit comments

Comments
 (0)