import { describe, it, expect, vi } from 'vitest' import { useWithdrawService } from '../use-withdraw-service' import { useAPI } from '#lib/composables' import { DEPOSIT_WITHDRAW_API } from '#lib/configs/api' import type { PaymentWithdrawBankRequestPayload } from '@kira-dancer/nadal' import type { BaseNadalResponse } from '#lib/types/api' // Mock `useAPI` composable vi.mock('#lib/composables', () => ({ useAPI: vi.fn(), })) describe('useWithdrawService', () => { it('should call useAPI with correct parameters for withdrawal request', async () => { const mockResponse: BaseNadalResponse = { success: true, data: { transactionId: '12345', }, } // Mock the return value of useAPI vi.mocked(useAPI).mockResolvedValue(mockResponse) const { createWithdrawCrypto } = useWithdrawService() const mockRequestData: PaymentWithdrawBankRequestPayload = { amount: 1000, currency: 'USD', bankAccount: { accountNumber: '1234567890', bankCode: 'ABC123', }, recipientName: 'John Doe', } const response = await createWithdrawCrypto(mockRequestData) // Assert that useAPI was called with correct parameters expect(useAPI).toHaveBeenCalledWith(DEPOSIT_WITHDRAW_API.WITHDRAW_CRYPTO, { body: mockRequestData, method: 'post', }) // Assert that the response is as expected expect(response).toEqual(mockResponse) }) it('should handle API errors gracefully', async () => { const mockError = new Error('API Error') // Mock the return value of useAPI to reject with an error vi.mocked(useAPI).mockRejectedValue(mockError) const { createWithdrawCrypto } = useWithdrawService() const mockRequestData: PaymentWithdrawBankRequestPayload = { amount: 1000, currency: 'USD', bankAccount: { accountNumber: '1234567890', bankCode: 'ABC123', }, recipientName: 'John Doe', } await expect(createWithdrawCrypto(mockRequestData)).rejects.toThrow( 'API Error', ) }) })