import { describe, it, expect, vi, beforeEach } from 'vitest' import { useWithdrawPhoneCard } from '../use-withdraw-phone-card' import type { PaymentWithdrawPhoneRequestPayload } from '@kira-dancer/nadal' import type { PhoneCardProvider, BaseNadalResponse } from '#lib/types' import { usePaymentStore } from '#lib/stores/payment' const mockPromotion = { isWelcomeType: { value: false }, isUsingPromotion: { value: false }, } const mockDepositData = { fetchPhoneCardProviders: vi.fn(), } const mockNadal = { payment: { withdrawPhone: vi.fn(), }, } vi.mock('#lib/stores/payment', () => ({ usePaymentStore: vi.fn().mockReturnValue({ phoneCardProviders: vi.mocked([]), }), })) vi.mock('#lib/composables', () => ({ usePromotion: () => mockPromotion, useDepositData: () => mockDepositData, useWithdrawError: () => ({ openWithdrawErrorModal: vi.fn(), }), useNadal: () => mockNadal, })) describe('useWithdrawPhoneCard composable', () => { let store: any beforeEach(() => { vi.clearAllMocks() store = usePaymentStore() }) it('should fetch and return phone card providers', async () => { const mockProviders: PhoneCardProvider[] = [ { providerId: 1, providerName: 'Provider A' }, { providerId: 2, providerName: 'Provider B' }, ] store.phoneCardProviders = mockProviders const { phoneCardProviders } = useWithdrawPhoneCard() expect(phoneCardProviders.value).toEqual([ { providerId: 1, providerName: 'Provider A', value: 1, label: 'Provider A', }, { providerId: 2, providerName: 'Provider B', value: 2, label: 'Provider B', }, ]) }) it('should return an empty array if no phone card providers are available', () => { store.phoneCardProviders = [] const { phoneCardProviders } = useWithdrawPhoneCard() expect(phoneCardProviders.value).toEqual([]) }) it('should fetch phone card providers data', async () => { const { fetchData } = useWithdrawPhoneCard() await fetchData() expect(mockDepositData.fetchPhoneCardProviders).toHaveBeenCalled() }) it('should open cancel promotion modal if isUsingPromotion is true on create withdraw phone card', async () => { mockPromotion.isUsingPromotion.value = true const payload = { card_amount_unit: 10000, } as PaymentWithdrawPhoneRequestPayload const { onCreateWithdrawPhoneCard } = useWithdrawPhoneCard() await onCreateWithdrawPhoneCard(payload) expect(mockNadal.payment.withdrawPhone).not.toHaveBeenCalled() }) it('should create withdraw phone card request if isUsingPromotion is false', async () => { mockPromotion.isUsingPromotion.value = false const payload = { card_amount_unit: 10000, } as PaymentWithdrawPhoneRequestPayload const response = { success: true } as BaseNadalResponse mockNadal.payment.withdrawPhone.mockResolvedValue(response) const { onCreateWithdrawPhoneCard } = useWithdrawPhoneCard() const result = await onCreateWithdrawPhoneCard(payload) expect(mockNadal.payment.withdrawPhone).toHaveBeenCalledWith(payload) expect(result).toEqual(response) }) })