|
1 | 1 | import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
2 | 2 |
|
| 3 | +import { ChainController } from '../../exports' |
3 | 4 | import { AccountController } from '../../src/controllers/AccountController' |
4 | 5 | import { EventsController } from '../../src/controllers/EventsController' |
5 | 6 | import { ExchangeController } from '../../src/controllers/ExchangeController' |
| 7 | +import { OptionsController } from '../../src/controllers/OptionsController' |
6 | 8 | import { SnackController } from '../../src/controllers/SnackController' |
7 | 9 | import { CoreHelperUtil } from '../../src/utils/CoreHelperUtil' |
8 | 10 | import * as ExchangeUtil from '../../src/utils/ExchangeUtil' |
@@ -43,6 +45,30 @@ describe('ExchangeController', () => { |
43 | 45 | }) |
44 | 46 |
|
45 | 47 | describe('fetchExchanges', () => { |
| 48 | + beforeEach(() => { |
| 49 | + vi.restoreAllMocks() |
| 50 | + vi.spyOn(OptionsController, 'state', 'get').mockReturnValue({ |
| 51 | + remoteFeatures: { payWithExchange: true }, |
| 52 | + features: { pay: true } |
| 53 | + } as any) |
| 54 | + ChainController.state.activeCaipNetwork = { |
| 55 | + caipNetworkId: 'eip155:1', |
| 56 | + chainNamespace: 'eip155', |
| 57 | + id: 1, |
| 58 | + name: 'Ethereum', |
| 59 | + nativeCurrency: { name: 'Ethereum', symbol: 'ETH', decimals: 18 }, |
| 60 | + rpcUrls: { |
| 61 | + default: { |
| 62 | + http: ['https://rpc.ankr.com/eth'] |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + vi.spyOn(ExchangeUtil, 'getExchanges').mockResolvedValue({ |
| 67 | + exchanges: [], |
| 68 | + total: 0 |
| 69 | + }) |
| 70 | + }) |
| 71 | + |
46 | 72 | it('loads exchanges and truncates to two', async () => { |
47 | 73 | const mockResponse = { |
48 | 74 | exchanges: [ |
@@ -95,6 +121,62 @@ describe('ExchangeController', () => { |
95 | 121 | expect(SnackController.showError).toHaveBeenCalledWith('Unable to get exchanges') |
96 | 122 | expect(ExchangeController.state.isLoading).toBe(false) |
97 | 123 | }) |
| 124 | + |
| 125 | + it('does not fetch exchanges when pay with exchange is not enabled', async () => { |
| 126 | + vi.spyOn(OptionsController, 'state', 'get').mockReturnValue({ |
| 127 | + remoteFeatures: { payWithExchange: false }, |
| 128 | + features: { pay: false } |
| 129 | + } as any) |
| 130 | + vi.spyOn(ExchangeUtil, 'getExchanges') |
| 131 | + await ExchangeController.fetchExchanges() |
| 132 | + expect(ExchangeUtil.getExchanges).not.toHaveBeenCalled() |
| 133 | + }) |
| 134 | + |
| 135 | + it('does not fetch exchanges when pay with exchange is not supported', async () => { |
| 136 | + vi.spyOn(OptionsController, 'state', 'get').mockReturnValue({ |
| 137 | + remoteFeatures: { payWithExchange: true }, |
| 138 | + features: { pay: true } |
| 139 | + } as any) |
| 140 | + vi.spyOn(ChainController, 'state', 'get').mockReturnValue({ |
| 141 | + activeCaipNetwork: { chainNamespace: 'bip122' } |
| 142 | + } as any) |
| 143 | + vi.spyOn(ExchangeUtil, 'getExchanges') |
| 144 | + await ExchangeController.fetchExchanges() |
| 145 | + expect(ExchangeUtil.getExchanges).not.toHaveBeenCalled() |
| 146 | + }) |
| 147 | + it('fetches exchanges when pay is enabled but pay with exchange is not', async () => { |
| 148 | + vi.spyOn(OptionsController, 'state', 'get').mockReturnValue({ |
| 149 | + remoteFeatures: { payWithExchange: false }, |
| 150 | + features: { pay: true } |
| 151 | + } as any) |
| 152 | + vi.spyOn(ChainController, 'state', 'get').mockReturnValue({ |
| 153 | + activeCaipNetwork: { chainNamespace: 'eip155' } |
| 154 | + } as any) |
| 155 | + vi.spyOn(ExchangeUtil, 'getExchanges') |
| 156 | + await ExchangeController.fetchExchanges() |
| 157 | + expect(ExchangeUtil.getExchanges).toHaveBeenCalled() |
| 158 | + }) |
| 159 | + it('fetches exchanges when pay with exchange is enabled but pay is not', async () => { |
| 160 | + vi.spyOn(OptionsController, 'state', 'get').mockReturnValue({ |
| 161 | + remoteFeatures: { payWithExchange: true }, |
| 162 | + features: { pay: false } |
| 163 | + } as any) |
| 164 | + vi.spyOn(ChainController, 'state', 'get').mockReturnValue({ |
| 165 | + activeCaipNetwork: { chainNamespace: 'eip155' } |
| 166 | + } as any) |
| 167 | + vi.spyOn(ExchangeUtil, 'getExchanges') |
| 168 | + await ExchangeController.fetchExchanges() |
| 169 | + expect(ExchangeUtil.getExchanges).toHaveBeenCalled() |
| 170 | + }) |
| 171 | + it('fetches exchanges when payments is enabled and pay with exchange is not', async () => { |
| 172 | + vi.spyOn(OptionsController, 'state', 'get').mockReturnValue({ |
| 173 | + remoteFeatures: { payWithExchange: false, payments: true }, |
| 174 | + features: { pay: false } |
| 175 | + } as any) |
| 176 | + vi.spyOn(ExchangeUtil, 'getExchanges') |
| 177 | + await ExchangeController.fetchExchanges() |
| 178 | + expect(ExchangeUtil.getExchanges).toHaveBeenCalled() |
| 179 | + }) |
98 | 180 | }) |
99 | 181 |
|
100 | 182 | describe('getPayUrl', () => { |
|
0 commit comments