|
| 1 | +import { renderHook } from '@testing-library/react'; |
| 2 | +import { type Mock, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 3 | +import { optionsResponseDataMock } from '../mocks'; |
| 4 | +import type { OnrampError } from '../types'; |
| 5 | +import { fetchOnrampOptions } from '../utils/fetchOnrampOptions'; |
| 6 | +import { usePaymentMethods } from './usePaymentMethods'; |
| 7 | + |
| 8 | +vi.mock('../utils/fetchOnrampOptions'); |
| 9 | + |
| 10 | +describe('usePaymentMethods', () => { |
| 11 | + const mockSetPaymentMethods = vi.fn(); |
| 12 | + const mockSetIsPaymentMethodsLoading = vi.fn(); |
| 13 | + const mockOnError = vi.fn(); |
| 14 | + |
| 15 | + beforeEach(() => { |
| 16 | + vi.clearAllMocks(); |
| 17 | + (fetchOnrampOptions as Mock).mockResolvedValue(optionsResponseDataMock); |
| 18 | + }); |
| 19 | + |
| 20 | + it('fetches and sets payment methods on mount', async () => { |
| 21 | + renderHook(() => |
| 22 | + usePaymentMethods({ |
| 23 | + country: 'US', |
| 24 | + currency: 'USD', |
| 25 | + setPaymentMethods: mockSetPaymentMethods, |
| 26 | + setIsPaymentMethodsLoading: mockSetIsPaymentMethodsLoading, |
| 27 | + }), |
| 28 | + ); |
| 29 | + |
| 30 | + // Should start loading |
| 31 | + expect(mockSetIsPaymentMethodsLoading).toHaveBeenCalledWith(true); |
| 32 | + |
| 33 | + // Wait for async operations |
| 34 | + await vi.waitFor(() => { |
| 35 | + expect(mockSetPaymentMethods).toHaveBeenCalled(); |
| 36 | + expect(mockSetIsPaymentMethodsLoading).toHaveBeenCalledWith(false); |
| 37 | + }); |
| 38 | + |
| 39 | + // Verify payment methods were set |
| 40 | + const paymentMethods = mockSetPaymentMethods.mock.calls[0][0]; |
| 41 | + expect(paymentMethods).toHaveLength(3); // Coinbase, Apple Pay, Card |
| 42 | + expect(paymentMethods[0].name).toBe('Coinbase'); |
| 43 | + }); |
| 44 | + |
| 45 | + it('handles API errors', async () => { |
| 46 | + const error = new Error('API Error'); |
| 47 | + (fetchOnrampOptions as Mock).mockRejectedValue(error); |
| 48 | + |
| 49 | + renderHook(() => |
| 50 | + usePaymentMethods({ |
| 51 | + country: 'US', |
| 52 | + currency: 'USD', |
| 53 | + setPaymentMethods: mockSetPaymentMethods, |
| 54 | + setIsPaymentMethodsLoading: mockSetIsPaymentMethodsLoading, |
| 55 | + onError: mockOnError, |
| 56 | + }), |
| 57 | + ); |
| 58 | + |
| 59 | + await vi.waitFor(() => { |
| 60 | + // Should call onError with correct error object |
| 61 | + expect(mockOnError).toHaveBeenCalledWith({ |
| 62 | + errorType: 'handled_error', |
| 63 | + code: 'PAYMENT_METHODS_ERROR', |
| 64 | + debugMessage: 'API Error', |
| 65 | + } satisfies OnrampError); |
| 66 | + |
| 67 | + // Should finish loading |
| 68 | + expect(mockSetIsPaymentMethodsLoading).toHaveBeenCalledWith(false); |
| 69 | + }); |
| 70 | + }); |
| 71 | + |
| 72 | + it('handles empty payment methods', async () => { |
| 73 | + // Mock API to return empty payment methods |
| 74 | + (fetchOnrampOptions as Mock).mockResolvedValue({ |
| 75 | + paymentCurrencies: [], |
| 76 | + purchaseCurrencies: [], |
| 77 | + }); |
| 78 | + |
| 79 | + renderHook(() => |
| 80 | + usePaymentMethods({ |
| 81 | + country: 'US', |
| 82 | + currency: 'USD', |
| 83 | + setPaymentMethods: mockSetPaymentMethods, |
| 84 | + setIsPaymentMethodsLoading: mockSetIsPaymentMethodsLoading, |
| 85 | + onError: mockOnError, |
| 86 | + }), |
| 87 | + ); |
| 88 | + |
| 89 | + await vi.waitFor(() => { |
| 90 | + // Should call onError with NO_PAYMENT_METHODS code |
| 91 | + expect(mockOnError).toHaveBeenCalledWith({ |
| 92 | + errorType: 'handled_error', |
| 93 | + code: 'NO_PAYMENT_METHODS', |
| 94 | + debugMessage: |
| 95 | + 'No payment methods found for the selected country and currency. See docs for more information: https://docs.cdp.coinbase.com/onramp/docs/api-configurations', |
| 96 | + } satisfies OnrampError); |
| 97 | + |
| 98 | + // Should set empty payment methods array |
| 99 | + expect(mockSetPaymentMethods).toHaveBeenCalledWith([]); |
| 100 | + }); |
| 101 | + }); |
| 102 | + |
| 103 | + it('includes subdivision in API call when provided', async () => { |
| 104 | + renderHook(() => |
| 105 | + usePaymentMethods({ |
| 106 | + country: 'US', |
| 107 | + subdivision: 'CA', |
| 108 | + currency: 'USD', |
| 109 | + setPaymentMethods: mockSetPaymentMethods, |
| 110 | + setIsPaymentMethodsLoading: mockSetIsPaymentMethodsLoading, |
| 111 | + }), |
| 112 | + ); |
| 113 | + |
| 114 | + expect(fetchOnrampOptions).toHaveBeenCalledWith({ |
| 115 | + country: 'US', |
| 116 | + subdivision: 'CA', |
| 117 | + }); |
| 118 | + }); |
| 119 | +}); |
0 commit comments