-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fallback to a text response if JSON fails (#5)
* Fallback to a text response if JSON fails * If instead of try-catch * fetchJSON -> fetchData * [CI] Add the target branch * Separate test commands * Break a test on purpose * Restore test * Bump the version
- Loading branch information
Showing
9 changed files
with
134 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,4 @@ yalc.lock | |
tsconfig.tsbuildinfo | ||
/openapi | ||
.#* | ||
jest.results.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,75 +1,89 @@ | ||
import fetch from 'unfetch' | ||
import { fetchData } from '../src/utils' | ||
import { callEndpoint } from '../src/endpoint' | ||
|
||
jest.mock('unfetch', () => jest.fn(() => { | ||
return Promise.resolve({ | ||
ok: true, | ||
json: () => Promise.resolve({ success: true }) | ||
}) | ||
})) | ||
jest.mock('../src/utils', () => { | ||
const originalModule = jest.requireActual('../src/utils') | ||
|
||
return { | ||
__esModule: true, | ||
...originalModule, | ||
fetchData: jest.fn(() => Promise.resolve({ success: true })), | ||
} | ||
}) | ||
|
||
describe('callEndpoint', () => { | ||
it('should accept just a path', () => { | ||
expect( | ||
callEndpoint('https://safe-client.xdai.staging.gnosisdev.com/v1', '/balances/supported-fiat-codes') | ||
it('should accept just a path', async () => { | ||
await expect( | ||
callEndpoint('https://safe-client.xdai.staging.gnosisdev.com/v1', '/balances/supported-fiat-codes'), | ||
).resolves.toEqual({ success: true }) | ||
|
||
expect(fetch).toHaveBeenCalledWith('https://safe-client.xdai.staging.gnosisdev.com/v1/balances/supported-fiat-codes', undefined) | ||
expect(fetchData).toHaveBeenCalledWith( | ||
'https://safe-client.xdai.staging.gnosisdev.com/v1/balances/supported-fiat-codes', | ||
undefined, | ||
) | ||
}) | ||
|
||
it('should accept a path param', () => { | ||
expect( | ||
callEndpoint('https://safe-client.rinkeby.staging.gnosisdev.com/v1', '/safe/{address}', { path: { address: '0x123' } }) | ||
it('should accept a path param', async () => { | ||
await expect( | ||
callEndpoint('https://safe-client.rinkeby.staging.gnosisdev.com/v1', '/safe/{address}', { | ||
path: { address: '0x123' }, | ||
}), | ||
).resolves.toEqual({ success: true }) | ||
|
||
expect(fetch).toHaveBeenCalledWith('https://safe-client.rinkeby.staging.gnosisdev.com/v1/safe/0x123', undefined) | ||
expect(fetchData).toHaveBeenCalledWith('https://safe-client.rinkeby.staging.gnosisdev.com/v1/safe/0x123', undefined) | ||
}) | ||
|
||
it('should accept several path params', () => { | ||
expect( | ||
callEndpoint('https://safe-client.rinkeby.staging.gnosisdev.com/v1', '/balances/{address}/{currency}', { path: { address: '0x123', currency: 'usd' } }) | ||
it('should accept several path params', async () => { | ||
await expect( | ||
callEndpoint('https://safe-client.rinkeby.staging.gnosisdev.com/v1', '/balances/{address}/{currency}', { | ||
path: { address: '0x123', currency: 'usd' }, | ||
}), | ||
).resolves.toEqual({ success: true }) | ||
|
||
expect(fetch).toHaveBeenCalledWith('https://safe-client.rinkeby.staging.gnosisdev.com/v1/balances/0x123/usd', undefined) | ||
expect(fetchData).toHaveBeenCalledWith( | ||
'https://safe-client.rinkeby.staging.gnosisdev.com/v1/balances/0x123/usd', | ||
undefined, | ||
) | ||
}) | ||
|
||
it('should accept query params', () => { | ||
expect( | ||
callEndpoint('https://safe-client.rinkeby.staging.gnosisdev.com/v1', '/balances/{address}/{currency}', { path: { address: '0x123', currency: 'usd' }, query: { exclude_spam: true } }) | ||
it('should accept query params', async () => { | ||
await expect( | ||
callEndpoint('https://safe-client.rinkeby.staging.gnosisdev.com/v1', '/balances/{address}/{currency}', { | ||
path: { address: '0x123', currency: 'usd' }, | ||
query: { exclude_spam: true }, | ||
}), | ||
).resolves.toEqual({ success: true }) | ||
|
||
expect(fetch).toHaveBeenCalledWith('https://safe-client.rinkeby.staging.gnosisdev.com/v1/balances/0x123/usd?exclude_spam=true', undefined) | ||
expect(fetchData).toHaveBeenCalledWith( | ||
'https://safe-client.rinkeby.staging.gnosisdev.com/v1/balances/0x123/usd?exclude_spam=true', | ||
undefined, | ||
) | ||
}) | ||
|
||
it('should accept body', () => { | ||
expect( | ||
callEndpoint( | ||
'https://safe-client.rinkeby.staging.gnosisdev.com/v1', | ||
'/transactions/{safe_address}/propose', | ||
{ | ||
path: { safe_address: '0x123' }, | ||
body: { test: 'test' } | ||
} | ||
) | ||
it('should accept body', async () => { | ||
await expect( | ||
callEndpoint('https://safe-client.rinkeby.staging.gnosisdev.com/v1', '/transactions/{safe_address}/propose', { | ||
path: { safe_address: '0x123' }, | ||
body: { test: 'test' }, | ||
}), | ||
).resolves.toEqual({ success: true }) | ||
|
||
expect(fetch).toHaveBeenCalledWith( | ||
expect(fetchData).toHaveBeenCalledWith( | ||
'https://safe-client.rinkeby.staging.gnosisdev.com/v1/transactions/0x123/propose', | ||
{ | ||
method: 'POST', | ||
body: '{"test":"test"}', | ||
headers: { | ||
'Content-Type': 'application/json' | ||
} | ||
} | ||
{ test: 'test' }, | ||
) | ||
}) | ||
|
||
it('should accept a raw URL', () => { | ||
expect( | ||
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') | ||
it('should accept a raw URL', async () => { | ||
await expect( | ||
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', | ||
), | ||
).resolves.toEqual({ success: true }) | ||
|
||
expect(fetch).toHaveBeenCalledWith('/test-url?raw=true', undefined) | ||
expect(fetchData).toHaveBeenCalledWith('/test-url?raw=true', undefined) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters