freshyo/apps/admin-ui/tests/lib/toaster.test.tsx
2026-10-01 00:33:03 +05:30

51 lines
1.4 KiB
TypeScript

/**
* toaster (services/toaster.tsx) — Info/Error/Success helpers funnel into
* react-native-toast-message's Toast.show with the right type/position and an
* onPress that hides the toast.
*/
jest.mock('expo-router', () => require('../mocks/router').expoRouterMock())
import Toast from 'react-native-toast-message'
import { ErrorToast, InfoToast, SuccessToast } from '@/services/toaster'
const showMock = Toast.show as jest.Mock
const hideMock = Toast.hide as jest.Mock
const showArg = (index: number) => showMock.mock.calls[index][0]
describe('toaster', () => {
it('InfoToast shows an info toast at the top', () => {
InfoToast('heads up')
expect(showMock).toHaveBeenCalledWith(
expect.objectContaining({ type: 'info', text1: 'heads up', position: 'top' })
)
expect(showArg(0).visibilityTime).toBe(10000)
})
it('ErrorToast shows an error toast at the top', () => {
ErrorToast('boom')
expect(showMock).toHaveBeenCalledWith(
expect.objectContaining({ type: 'error', text1: 'boom', position: 'top' })
)
})
it('SuccessToast shows a success toast at the top', () => {
SuccessToast('done')
expect(showMock).toHaveBeenCalledWith(
expect.objectContaining({ type: 'success', text1: 'done', position: 'top' })
)
})
it('hides the toast when pressed', () => {
SuccessToast('done')
showArg(0).onPress()
expect(hideMock).toHaveBeenCalledTimes(1)
})
})