import { describe, it, expect, vi, beforeEach } from 'vitest' import { useWithdrawCrypto } from '../use-withdraw-crypto' import { valueToNumber, formatNumberWithCommas, roundNumber } from '#lib/utils' import { ResponseStatus } from '#lib/enums/app' import type { PaymentWithdrawCryptoRequestPayload } from '#lib/types/withdraw' import type { UpdateProfileRequestPayload } from '@kira-dancer/nadal' // Mock dependencies const mockWithdrawService = { createWithdrawCrypto: vi.fn(), } const mockDepositService = { fetchCrypto: vi.fn(), } const mockAccount = { onUpdateProfile: vi.fn(), } const mockPromotion = { isUsingPromotion: { value: false }, } const mockCancelPromotion = { openModalCancelPromotion: vi.fn(), } const mockWithdrawError = { openWithdrawErrorModal: vi.fn(), } vi.mock('#lib/composables/service/use-withdraw-service', () => ({ useWithdrawService: () => mockWithdrawService, })) vi.mock('#lib/composables/service/use-deposit-service', () => ({ useDepositService: () => mockDepositService, })) vi.mock('#lib/composables/account', () => ({ useAccount: () => mockAccount, })) vi.mock('#lib/composables', () => ({ usePromotion: () => mockPromotion, useCancelPromotion: () => mockCancelPromotion, useWithdrawError: () => mockWithdrawError, })) vi.mock('#lib/utils', () => ({ valueToNumber: vi.fn(), formatNumberWithCommas: vi.fn(), roundNumber: vi.fn(), })) describe('useWithdrawCrypto composable', () => { beforeEach(() => { vi.clearAllMocks() }) it('should open cancel promotion modal if isUsingPromotion is true on create withdraw crypto', async () => { mockPromotion.isUsingPromotion.value = true const payload = { amount: '10000' } as PaymentWithdrawCryptoRequestPayload const { onCreateWithdrawCrypto } = useWithdrawCrypto() await onCreateWithdrawCrypto(payload) expect(mockWithdrawError.openWithdrawErrorModal).toHaveBeenCalled() expect(mockWithdrawService.createWithdrawCrypto).not.toHaveBeenCalled() }) it('should create withdraw crypto request if isUsingPromotion is false', async () => { mockPromotion.isUsingPromotion.value = false const payload = { amount: '10000' } as PaymentWithdrawCryptoRequestPayload const response = { data: { value: { status: ResponseStatus.OK } } } mockWithdrawService.createWithdrawCrypto.mockResolvedValue(response) vi.mocked(valueToNumber).mockReturnValue(10) const { onCreateWithdrawCrypto } = useWithdrawCrypto() const result = await onCreateWithdrawCrypto(payload) expect(valueToNumber).toHaveBeenCalledWith('10000', 1000) expect(mockWithdrawService.createWithdrawCrypto).toHaveBeenCalledWith( payload, ) expect(result).toEqual(response.data.value) }) it('should request new crypto data if crypto exchange rate error occurs', async () => { mockPromotion.isUsingPromotion.value = false const payload = { amount: '10000' } as PaymentWithdrawCryptoRequestPayload const error = { value: { statusMessage: ResponseStatus.CRYPTO_EXCHANGE_RATE_CHANGED_ERROR, }, } mockWithdrawService.createWithdrawCrypto.mockResolvedValue({ error }) mockDepositService.fetchCrypto.mockResolvedValue({}) const { onCreateWithdrawCrypto } = useWithdrawCrypto() await expect(onCreateWithdrawCrypto(payload)).rejects.toThrow() expect(mockDepositService.fetchCrypto).toHaveBeenCalled() }) it('should update wallet address correctly', async () => { const payload = { address: 'new-address' } as UpdateProfileRequestPayload mockAccount.onUpdateProfile.mockResolvedValue() const { updateWalletAddress } = useWithdrawCrypto() await updateWalletAddress(payload) expect(mockAccount.onUpdateProfile).toHaveBeenCalledWith(payload) }) it('should correctly convert deposit amount to USDT', () => { const depositAmount = '100000' const sellPrice = 0.95 const mockRoundedAmount = '105.26' vi.mocked(valueToNumber).mockReturnValue(100) vi.mocked(roundNumber).mockReturnValue(105.26) vi.mocked(formatNumberWithCommas).mockReturnValue(mockRoundedAmount) const { convertAmountToUSDT } = useWithdrawCrypto() const result = convertAmountToUSDT(depositAmount, sellPrice) expect(valueToNumber).toHaveBeenCalledWith(depositAmount, 1000) expect(roundNumber).toHaveBeenCalledWith(100 / sellPrice, 2) expect(formatNumberWithCommas).toHaveBeenCalledWith(105.26) expect(result).toBe(mockRoundedAmount) }) })