|
1 | | -import fetch from 'unfetch' |
| 1 | +import { fetchData } from '../src/utils' |
2 | 2 | import { callEndpoint } from '../src/endpoint' |
3 | 3 |
|
4 | | -jest.mock('unfetch', () => jest.fn(() => { |
5 | | - return Promise.resolve({ |
6 | | - ok: true, |
7 | | - json: () => Promise.resolve({ success: true }) |
8 | | - }) |
9 | | -})) |
| 4 | +jest.mock('../src/utils', () => { |
| 5 | + const originalModule = jest.requireActual('../src/utils') |
| 6 | + |
| 7 | + return { |
| 8 | + __esModule: true, |
| 9 | + ...originalModule, |
| 10 | + fetchData: jest.fn(() => Promise.resolve({ success: true })), |
| 11 | + } |
| 12 | +}) |
10 | 13 |
|
11 | 14 | describe('callEndpoint', () => { |
12 | | - it('should accept just a path', () => { |
13 | | - expect( |
14 | | - callEndpoint('https://safe-client.xdai.staging.gnosisdev.com/v1', '/balances/supported-fiat-codes') |
| 15 | + it('should accept just a path', async () => { |
| 16 | + await expect( |
| 17 | + callEndpoint('https://safe-client.xdai.staging.gnosisdev.com/v1', '/balances/supported-fiat-codes'), |
15 | 18 | ).resolves.toEqual({ success: true }) |
16 | 19 |
|
17 | | - expect(fetch).toHaveBeenCalledWith('https://safe-client.xdai.staging.gnosisdev.com/v1/balances/supported-fiat-codes', undefined) |
| 20 | + expect(fetchData).toHaveBeenCalledWith( |
| 21 | + 'https://safe-client.xdai.staging.gnosisdev.com/v1/balances/supported-fiat-codes', |
| 22 | + undefined, |
| 23 | + ) |
18 | 24 | }) |
19 | 25 |
|
20 | | - it('should accept a path param', () => { |
21 | | - expect( |
22 | | - callEndpoint('https://safe-client.rinkeby.staging.gnosisdev.com/v1', '/safe/{address}', { path: { address: '0x123' } }) |
| 26 | + it('should accept a path param', async () => { |
| 27 | + await expect( |
| 28 | + callEndpoint('https://safe-client.rinkeby.staging.gnosisdev.com/v1', '/safe/{address}', { |
| 29 | + path: { address: '0x123' }, |
| 30 | + }), |
23 | 31 | ).resolves.toEqual({ success: true }) |
24 | 32 |
|
25 | | - expect(fetch).toHaveBeenCalledWith('https://safe-client.rinkeby.staging.gnosisdev.com/v1/safe/0x123', undefined) |
| 33 | + expect(fetchData).toHaveBeenCalledWith('https://safe-client.rinkeby.staging.gnosisdev.com/v1/safe/0x123', undefined) |
26 | 34 | }) |
27 | 35 |
|
28 | | - it('should accept several path params', () => { |
29 | | - expect( |
30 | | - callEndpoint('https://safe-client.rinkeby.staging.gnosisdev.com/v1', '/balances/{address}/{currency}', { path: { address: '0x123', currency: 'usd' } }) |
| 36 | + it('should accept several path params', async () => { |
| 37 | + await expect( |
| 38 | + callEndpoint('https://safe-client.rinkeby.staging.gnosisdev.com/v1', '/balances/{address}/{currency}', { |
| 39 | + path: { address: '0x123', currency: 'usd' }, |
| 40 | + }), |
31 | 41 | ).resolves.toEqual({ success: true }) |
32 | 42 |
|
33 | | - expect(fetch).toHaveBeenCalledWith('https://safe-client.rinkeby.staging.gnosisdev.com/v1/balances/0x123/usd', undefined) |
| 43 | + expect(fetchData).toHaveBeenCalledWith( |
| 44 | + 'https://safe-client.rinkeby.staging.gnosisdev.com/v1/balances/0x123/usd', |
| 45 | + undefined, |
| 46 | + ) |
34 | 47 | }) |
35 | 48 |
|
36 | | - it('should accept query params', () => { |
37 | | - expect( |
38 | | - callEndpoint('https://safe-client.rinkeby.staging.gnosisdev.com/v1', '/balances/{address}/{currency}', { path: { address: '0x123', currency: 'usd' }, query: { exclude_spam: true } }) |
| 49 | + it('should accept query params', async () => { |
| 50 | + await expect( |
| 51 | + callEndpoint('https://safe-client.rinkeby.staging.gnosisdev.com/v1', '/balances/{address}/{currency}', { |
| 52 | + path: { address: '0x123', currency: 'usd' }, |
| 53 | + query: { exclude_spam: true }, |
| 54 | + }), |
39 | 55 | ).resolves.toEqual({ success: true }) |
40 | 56 |
|
41 | | - expect(fetch).toHaveBeenCalledWith('https://safe-client.rinkeby.staging.gnosisdev.com/v1/balances/0x123/usd?exclude_spam=true', undefined) |
| 57 | + expect(fetchData).toHaveBeenCalledWith( |
| 58 | + 'https://safe-client.rinkeby.staging.gnosisdev.com/v1/balances/0x123/usd?exclude_spam=true', |
| 59 | + undefined, |
| 60 | + ) |
42 | 61 | }) |
43 | 62 |
|
44 | | - it('should accept body', () => { |
45 | | - expect( |
46 | | - callEndpoint( |
47 | | - 'https://safe-client.rinkeby.staging.gnosisdev.com/v1', |
48 | | - '/transactions/{safe_address}/propose', |
49 | | - { |
50 | | - path: { safe_address: '0x123' }, |
51 | | - body: { test: 'test' } |
52 | | - } |
53 | | - ) |
| 63 | + it('should accept body', async () => { |
| 64 | + await expect( |
| 65 | + callEndpoint('https://safe-client.rinkeby.staging.gnosisdev.com/v1', '/transactions/{safe_address}/propose', { |
| 66 | + path: { safe_address: '0x123' }, |
| 67 | + body: { test: 'test' }, |
| 68 | + }), |
54 | 69 | ).resolves.toEqual({ success: true }) |
55 | 70 |
|
56 | | - expect(fetch).toHaveBeenCalledWith( |
| 71 | + expect(fetchData).toHaveBeenCalledWith( |
57 | 72 | 'https://safe-client.rinkeby.staging.gnosisdev.com/v1/transactions/0x123/propose', |
58 | | - { |
59 | | - method: 'POST', |
60 | | - body: '{"test":"test"}', |
61 | | - headers: { |
62 | | - 'Content-Type': 'application/json' |
63 | | - } |
64 | | - } |
| 73 | + { test: 'test' }, |
65 | 74 | ) |
66 | 75 | }) |
67 | 76 |
|
68 | | - it('should accept a raw URL', () => { |
69 | | - expect( |
70 | | - callEndpoint('https://safe-client.rinkeby.staging.gnosisdev.com/v1', '/balances/{address}/{currency}', { path: { address: '0x123', currency: 'usd' }, query: { exclude_spam: true } }, '/test-url?raw=true') |
| 77 | + it('should accept a raw URL', async () => { |
| 78 | + await expect( |
| 79 | + callEndpoint( |
| 80 | + 'https://safe-client.rinkeby.staging.gnosisdev.com/v1', |
| 81 | + '/balances/{address}/{currency}', |
| 82 | + { path: { address: '0x123', currency: 'usd' }, query: { exclude_spam: true } }, |
| 83 | + '/test-url?raw=true', |
| 84 | + ), |
71 | 85 | ).resolves.toEqual({ success: true }) |
72 | 86 |
|
73 | | - expect(fetch).toHaveBeenCalledWith('/test-url?raw=true', undefined) |
| 87 | + expect(fetchData).toHaveBeenCalledWith('/test-url?raw=true', undefined) |
74 | 88 | }) |
75 | 89 | }) |
0 commit comments