feat: add useMutationWithToast wrapper
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||
import { renderHook, waitFor } from '@testing-library/react'
|
||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
||||
import { useMutationWithToast } from '../use-mutation-with-toast'
|
||||
import { addNotification } from '../../model/notification'
|
||||
|
||||
vi.mock('../../model/notification', () => ({
|
||||
addNotification: vi.fn(),
|
||||
}))
|
||||
|
||||
function createWrapper() {
|
||||
const qc = new QueryClient({ defaultOptions: { queries: { retry: false } } })
|
||||
return ({ children }: { children: React.ReactNode }) => (
|
||||
<QueryClientProvider client={qc}>{children}</QueryClientProvider>
|
||||
)
|
||||
}
|
||||
|
||||
describe('useMutationWithToast', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
})
|
||||
|
||||
it('shows success notification on success with successMessage', async () => {
|
||||
const mutationFn = vi.fn().mockResolvedValue({ ok: true })
|
||||
const { result } = renderHook(() => useMutationWithToast({ mutationFn, successMessage: 'Done!' }), {
|
||||
wrapper: createWrapper(),
|
||||
})
|
||||
result.current.mutate()
|
||||
await waitFor(() => expect(result.current.isSuccess).toBe(true))
|
||||
expect(addNotification).toHaveBeenCalledWith({ type: 'success', message: 'Done!' })
|
||||
})
|
||||
|
||||
it('does NOT show success notification without successMessage', async () => {
|
||||
const mutationFn = vi.fn().mockResolvedValue({ ok: true })
|
||||
const { result } = renderHook(() => useMutationWithToast({ mutationFn }), { wrapper: createWrapper() })
|
||||
result.current.mutate()
|
||||
await waitFor(() => expect(result.current.isSuccess).toBe(true))
|
||||
expect(addNotification).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('shows error notification on mutation error', async () => {
|
||||
const mutationFn = vi.fn().mockRejectedValue(new Error('Boom'))
|
||||
const { result } = renderHook(() => useMutationWithToast({ mutationFn }), { wrapper: createWrapper() })
|
||||
result.current.mutate()
|
||||
await waitFor(() => expect(result.current.isError).toBe(true))
|
||||
expect(addNotification).toHaveBeenCalledWith({ type: 'error', message: 'Boom' })
|
||||
})
|
||||
|
||||
it('calls user-provided onSuccess callback', async () => {
|
||||
const onSuccess = vi.fn()
|
||||
const mutationFn = vi.fn().mockResolvedValue({ ok: true })
|
||||
const { result } = renderHook(() => useMutationWithToast({ mutationFn, onSuccess, successMessage: 'OK' }), {
|
||||
wrapper: createWrapper(),
|
||||
})
|
||||
result.current.mutate()
|
||||
await waitFor(() => expect(result.current.isSuccess).toBe(true))
|
||||
expect(onSuccess).toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('calls user-provided onError callback', async () => {
|
||||
const onError = vi.fn()
|
||||
const mutationFn = vi.fn().mockRejectedValue(new Error('fail'))
|
||||
const { result } = renderHook(() => useMutationWithToast({ mutationFn, onError }), { wrapper: createWrapper() })
|
||||
result.current.mutate()
|
||||
await waitFor(() => expect(result.current.isError).toBe(true))
|
||||
expect(onError).toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user