import { describe, it, expect, vi, beforeEach } from 'vitest' import { useWithdrawBank } from '../use-withdraw-bank' import { valueToNumber } from '#lib/utils' import type { PaymentWithdrawBankRequestPayload } from '@kira-dancer/nadal' import type { BaseNadalResponse } from '#lib/types' // Mock dependencies const mockPaymentStore = { withdrawBanks: [], } const mockPromotion = { isUsingPromotion: { value: false }, } const mockUserBank = { userBanks: [], } const mockNadal = { payment: { withdrawBank: vi.fn(), }, } const mockDepositData = { fetchWithdrawBanks: vi.fn(), } const mockWithdrawError = { openWithdrawErrorModal: vi.fn(), } const mockNuxtApp = { $payment: { withdrawBank: vi.fn(), }, } vi.mock('#lib/stores/payment', () => ({ usePaymentStore: () => mockPaymentStore, })) vi.mock('#lib/composables', () => ({ useNadal: () => mockNadal, usePromotion: () => mockPromotion, useUserBank: () => mockUserBank, useDepositData: () => mockDepositData, useWithdrawError: () => mockWithdrawError, })) vi.mock('nuxt/app', () => ({ useNuxtApp: () => mockNuxtApp, })) vi.mock('#lib/utils', () => ({ valueToNumber: vi.fn(), })) describe('useWithdrawBank composable', () => { beforeEach(() => { // Reset the mocks before each test mockDepositData.fetchWithdrawBanks.mockReset() vi.clearAllMocks() }) it('should fetch withdraw banks data', async () => { const { fetchData } = useWithdrawBank() await fetchData() expect(mockDepositData.fetchWithdrawBanks).toHaveBeenCalled() }) it('should return withdraw banks from the store', () => { mockPaymentStore.withdrawBanks = [{ id: 1, name: 'Bank A' }] const { withdrawBanks } = useWithdrawBank() expect(withdrawBanks.value).toEqual([{ id: 1, name: 'Bank A' }]) }) it('should return user banks', () => { mockUserBank.userBanks = [{ id: 1, name: 'User Bank A' }] const { userBanks } = useWithdrawBank() expect(userBanks).toEqual([{ id: 1, name: 'User Bank A' }]) }) it('should open cancel promotion modal if isUsingPromotion is true', async () => { mockPromotion.isUsingPromotion.value = true const payload = { amount: 10000 } as PaymentWithdrawBankRequestPayload const { onCreateWithdrawBanking } = useWithdrawBank() await onCreateWithdrawBanking(payload) expect(mockWithdrawError.openWithdrawErrorModal).toHaveBeenCalled() expect(mockNadal.payment.withdrawBank).not.toHaveBeenCalled() }) it('should create withdraw banking request if isUsingPromotion is false', async () => { mockPromotion.isUsingPromotion.value = false const payload = { amount: 10000 } as PaymentWithdrawBankRequestPayload const response = { success: true } as BaseNadalResponse mockNadal.payment.withdrawBank.mockResolvedValue(response) vi.mocked(valueToNumber).mockReturnValue(10) const { onCreateWithdrawBanking } = useWithdrawBank() const result = await onCreateWithdrawBanking(payload) expect(valueToNumber).toHaveBeenCalledWith(10000, 1000) expect(mockNadal.payment.withdrawBank).toHaveBeenCalledWith(payload) expect(result).toEqual(response) }) })