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-01-19 17:41:57 +01:00
|
|
|
describe('getFetchWithTimeout', function () {
|
2020-02-11 17:51:13 +01:00
|
|
|
it('fetches a url', async function () {
|
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
|
|
|
|
2021-06-10 21:27:03 +02:00
|
|
|
const fetchWithTimeout = getFetchWithTimeout(SECOND * 30);
|
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
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
it('throws when the request hits a custom timeout', async function () {
|
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-06-29 19:50:18 +02:00
|
|
|
const fetchWithTimeoutThrowsError = async () => {
|
2021-02-04 19:15:23 +01:00
|
|
|
await fetchWithTimeout('https://api.infura.io/moon').then((r) =>
|
|
|
|
r.json(),
|
|
|
|
);
|
2021-06-29 19:50:18 +02:00
|
|
|
throw new Error('Request should throw');
|
|
|
|
};
|
|
|
|
|
|
|
|
await expect(fetchWithTimeoutThrowsError()).rejects.toThrow('Aborted');
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2019-06-18 14:17:14 +02:00
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
it('should abort the request when the custom timeout is hit', async function () {
|
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-06-29 19:50:18 +02:00
|
|
|
const fetchWithTimeoutThrowsError = async () => {
|
2021-02-04 19:15:23 +01:00
|
|
|
await fetchWithTimeout('https://api.infura.io/moon').then((r) =>
|
|
|
|
r.json(),
|
|
|
|
);
|
2021-06-29 19:50:18 +02:00
|
|
|
throw new Error('Request should be aborted');
|
|
|
|
};
|
|
|
|
|
|
|
|
await expect(fetchWithTimeoutThrowsError()).rejects.toThrow('Aborted');
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2021-01-19 17:41:57 +01:00
|
|
|
|
|
|
|
it('throws on invalid timeout', async function () {
|
2021-06-29 19:50:18 +02:00
|
|
|
expect(() => getFetchWithTimeout()).toThrow(
|
|
|
|
'Must specify positive integer timeout.',
|
|
|
|
);
|
|
|
|
expect(() => getFetchWithTimeout(-1)).toThrow(
|
|
|
|
'Must specify positive integer timeout.',
|
|
|
|
);
|
|
|
|
expect(() => getFetchWithTimeout({})).toThrow(
|
|
|
|
'Must specify positive integer timeout.',
|
|
|
|
);
|
|
|
|
expect(() => getFetchWithTimeout(true)).toThrow(
|
|
|
|
'Must specify positive integer timeout.',
|
|
|
|
);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
});
|