|
1 | 1 | import { getOnrampBuyUrl } from '@/fund/utils/getOnrampBuyUrl';
|
2 | 2 | import { SwapUnit } from '@/swap/types';
|
3 | 3 | import { Token } from '@/token';
|
4 |
| -import type { Chain } from 'viem'; |
5 |
| -import { base } from 'viem/chains'; |
6 |
| -import { describe, it, expect, vi } from 'vitest'; |
| 4 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
7 | 5 | import { getBuyFundingUrl } from './getBuyFundingUrl';
|
8 | 6 |
|
9 | 7 | vi.mock('@/fund/utils/getOnrampBuyUrl', () => ({
|
10 | 8 | getOnrampBuyUrl: vi.fn(),
|
11 | 9 | }));
|
12 | 10 |
|
13 | 11 | describe('getBuyFundingUrl', () => {
|
14 |
| - const baseParams = { |
15 |
| - projectId: 'abc123', |
16 |
| - address: '0x123', |
17 |
| - paymentMethodId: 'pm_001', |
18 |
| - chain: base as Chain, |
19 |
| - }; |
| 12 | + beforeEach(() => { |
| 13 | + vi.clearAllMocks(); |
| 14 | + }); |
20 | 15 |
|
21 |
| - it('returns undefined if projectId is null', () => { |
| 16 | + it('returns undefined if sessionToken is not provided', () => { |
22 | 17 | const result = getBuyFundingUrl({
|
23 |
| - ...baseParams, |
24 |
| - projectId: null, |
25 | 18 | to: {
|
26 | 19 | amount: '1',
|
27 | 20 | token: { symbol: 'ETH' } as Token,
|
28 | 21 | } as SwapUnit,
|
| 22 | + paymentMethodId: 'pm_001', |
29 | 23 | });
|
30 | 24 |
|
31 | 25 | expect(result).toBeUndefined();
|
| 26 | + expect(getOnrampBuyUrl).not.toHaveBeenCalled(); |
32 | 27 | });
|
33 | 28 |
|
34 |
| - it('returns undefined if address is undefined', () => { |
| 29 | + it('returns undefined if fundAmount is missing', () => { |
35 | 30 | const result = getBuyFundingUrl({
|
36 |
| - ...baseParams, |
37 |
| - address: undefined, |
38 |
| - to: { |
39 |
| - amount: '1', |
40 |
| - token: { symbol: 'ETH' }, |
41 |
| - } as SwapUnit, |
| 31 | + to: undefined, |
| 32 | + sessionToken: 'test-session-token', |
| 33 | + paymentMethodId: 'pm_001', |
42 | 34 | });
|
43 | 35 |
|
44 | 36 | expect(result).toBeUndefined();
|
| 37 | + expect(getOnrampBuyUrl).toHaveBeenCalled(); |
45 | 38 | });
|
46 | 39 |
|
47 |
| - it('returns undefined if assetSymbol is missing', () => { |
| 40 | + it('returns undefined if amount is missing from to', () => { |
48 | 41 | const result = getBuyFundingUrl({
|
49 |
| - ...baseParams, |
50 | 42 | to: {
|
51 |
| - amount: '1', |
52 |
| - token: undefined, |
| 43 | + token: { symbol: 'ETH' } as Token, |
53 | 44 | } as SwapUnit,
|
| 45 | + sessionToken: 'test-session-token', |
| 46 | + paymentMethodId: 'pm_001', |
54 | 47 | });
|
55 | 48 |
|
56 | 49 | expect(result).toBeUndefined();
|
| 50 | + expect(getOnrampBuyUrl).toHaveBeenCalled(); |
57 | 51 | });
|
58 | 52 |
|
59 |
| - it('calls getOnrampBuyUrl with correct parameters', () => { |
| 53 | + it('calls getOnrampBuyUrl with correct parameters when sessionToken is provided', () => { |
60 | 54 | const mockUrl = 'https://onramp.com/buy';
|
61 | 55 | (getOnrampBuyUrl as ReturnType<typeof vi.fn>).mockReturnValue(mockUrl);
|
62 | 56 |
|
63 | 57 | const result = getBuyFundingUrl({
|
64 |
| - ...baseParams, |
65 | 58 | to: {
|
66 | 59 | amount: '1.5',
|
67 | 60 | token: { symbol: 'ETH' },
|
68 | 61 | } as SwapUnit,
|
| 62 | + sessionToken: 'test-session-token', |
| 63 | + paymentMethodId: 'pm_001', |
69 | 64 | });
|
70 | 65 |
|
71 | 66 | expect(getOnrampBuyUrl).toHaveBeenCalledWith({
|
72 |
| - projectId: 'abc123', |
73 |
| - assets: ['ETH'], |
| 67 | + sessionToken: 'test-session-token', |
74 | 68 | presetCryptoAmount: 1.5,
|
75 | 69 | defaultPaymentMethod: 'pm_001',
|
76 |
| - addresses: { |
77 |
| - '0x123': ['base'], |
78 |
| - }, |
79 | 70 | originComponentName: 'FundCard',
|
80 | 71 | });
|
81 | 72 |
|
82 | 73 | expect(result).toBe(mockUrl);
|
83 | 74 | });
|
| 75 | + |
| 76 | + it('uses correct originComponentName', () => { |
| 77 | + const mockUrl = 'https://onramp.com/buy'; |
| 78 | + (getOnrampBuyUrl as ReturnType<typeof vi.fn>).mockReturnValue(mockUrl); |
| 79 | + |
| 80 | + getBuyFundingUrl({ |
| 81 | + to: { |
| 82 | + amount: '2.0', |
| 83 | + token: { symbol: 'USDC' }, |
| 84 | + } as SwapUnit, |
| 85 | + sessionToken: 'another-session-token', |
| 86 | + paymentMethodId: 'CARD', |
| 87 | + }); |
| 88 | + |
| 89 | + expect(getOnrampBuyUrl).toHaveBeenCalledWith({ |
| 90 | + sessionToken: 'another-session-token', |
| 91 | + presetCryptoAmount: 2.0, |
| 92 | + defaultPaymentMethod: 'CARD', |
| 93 | + originComponentName: 'FundCard', |
| 94 | + }); |
| 95 | + }); |
84 | 96 | });
|
0 commit comments