2021-02-04 19:15:23 +01:00
|
|
|
import nock from 'nock';
|
2021-06-10 21:27:03 +02:00
|
|
|
import { MILLISECOND, SECOND } from '../constants/time';
|
2021-03-16 22:00:08 +01:00
|
|
|
import getFetchWithTimeout from './fetch-with-timeout';
|
2019-06-18 14:17:14 +02:00
|
|
|
|
2021-11-30 19:09:46 +01:00
|
|
|
describe('getFetchWithTimeout', () => {
|
|
|
|
it('fetches a url', async () => {
|
2021-02-04 19:15:23 +01:00
|
|
|
nock('https://api.infura.io').get('/money').reply(200, '{"hodl": false}');
|
2019-06-18 14:17:14 +02:00
|
|
|
|
2022-07-19 18:13:45 +02:00
|
|
|
const fetchWithTimeout = getFetchWithTimeout();
|
2021-01-19 17:41:57 +01:00
|
|
|
const response = await (
|
|
|
|
await fetchWithTimeout('https://api.infura.io/money')
|
2021-02-04 19:15:23 +01:00
|
|
|
).json();
|
2021-06-29 19:50:18 +02:00
|
|
|
expect(response).toStrictEqual({
|
2019-06-18 14:17:14 +02:00
|
|
|
hodl: false,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
});
|
2019-06-18 14:17:14 +02:00
|
|
|
|
2021-11-30 19:09:46 +01:00
|
|
|
it('throws when the request hits a custom timeout', async () => {
|
2019-06-18 14:17:14 +02:00
|
|
|
nock('https://api.infura.io')
|
|
|
|
.get('/moon')
|
2021-06-10 21:27:03 +02:00
|
|
|
.delay(SECOND * 2)
|
2021-02-04 19:15:23 +01:00
|
|
|
.reply(200, '{"moon": "2012-12-21T11:11:11Z"}');
|
2019-06-18 14:17:14 +02:00
|
|
|
|
2021-06-10 21:27:03 +02:00
|
|
|
const fetchWithTimeout = getFetchWithTimeout(MILLISECOND * 123);
|
2019-06-18 14:17:14 +02:00
|
|
|
|
2021-11-30 19:09:46 +01:00
|
|
|
await expect(async () => {
|
2021-02-04 19:15:23 +01:00
|
|
|
await fetchWithTimeout('https://api.infura.io/moon').then((r) =>
|
|
|
|
r.json(),
|
|
|
|
);
|
2021-11-30 19:09:46 +01:00
|
|
|
}).rejects.toThrow('The user aborted a request.');
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2019-06-18 14:17:14 +02:00
|
|
|
|
2021-11-30 19:09:46 +01:00
|
|
|
it('should abort the request when the custom timeout is hit', async () => {
|
2019-06-18 14:17:14 +02:00
|
|
|
nock('https://api.infura.io')
|
|
|
|
.get('/moon')
|
2021-06-10 21:27:03 +02:00
|
|
|
.delay(SECOND * 2)
|
2021-02-04 19:15:23 +01:00
|
|
|
.reply(200, '{"moon": "2012-12-21T11:11:11Z"}');
|
2019-06-18 14:17:14 +02:00
|
|
|
|
2021-06-10 21:27:03 +02:00
|
|
|
const fetchWithTimeout = getFetchWithTimeout(MILLISECOND * 123);
|
2019-06-18 14:17:14 +02:00
|
|
|
|
2021-11-30 19:09:46 +01:00
|
|
|
await expect(async () => {
|
2021-02-04 19:15:23 +01:00
|
|
|
await fetchWithTimeout('https://api.infura.io/moon').then((r) =>
|
|
|
|
r.json(),
|
|
|
|
);
|
2021-11-30 19:09:46 +01:00
|
|
|
}).rejects.toThrow('The user aborted a request.');
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2021-01-19 17:41:57 +01:00
|
|
|
|
2021-11-30 19:09:46 +01:00
|
|
|
it('throws on invalid timeout', async () => {
|
|
|
|
await expect(() => getFetchWithTimeout(-1)).toThrow(
|
2021-06-29 19:50:18 +02:00
|
|
|
'Must specify positive integer timeout.',
|
|
|
|
);
|
2021-11-30 19:09:46 +01:00
|
|
|
await expect(() => getFetchWithTimeout(true)).toThrow(
|
2021-06-29 19:50:18 +02:00
|
|
|
'Must specify positive integer timeout.',
|
|
|
|
);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
});
|