1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-29 23:58:06 +01:00
metamask-extension/shared/modules/fetch-with-timeout.test.js
Alex Donesky e8ab578ed0
bump @metamask/controllers to v15.0.2 and remove AbortController workaround in e2e tests (#11988)
* bump @metamask/controllers to v15.0.1 and remove AbortController workaround in e2e tests

* remove old abortcontroller polyfill

* bump @metamask/controllers to v15.0.2
2021-09-03 09:34:21 -05:00

73 lines
2.2 KiB
JavaScript

import nock from 'nock';
import { MILLISECOND, SECOND } from '../constants/time';
import getFetchWithTimeout from './fetch-with-timeout';
describe('getFetchWithTimeout', function () {
it('fetches a url', async function () {
nock('https://api.infura.io').get('/money').reply(200, '{"hodl": false}');
const fetchWithTimeout = getFetchWithTimeout(SECOND * 30);
const response = await (
await fetchWithTimeout('https://api.infura.io/money')
).json();
expect(response).toStrictEqual({
hodl: false,
});
});
it('throws when the request hits a custom timeout', async function () {
nock('https://api.infura.io')
.get('/moon')
.delay(SECOND * 2)
.reply(200, '{"moon": "2012-12-21T11:11:11Z"}');
const fetchWithTimeout = getFetchWithTimeout(MILLISECOND * 123);
const fetchWithTimeoutThrowsError = async () => {
await fetchWithTimeout('https://api.infura.io/moon').then((r) =>
r.json(),
);
throw new Error('Request should throw');
};
await expect(fetchWithTimeoutThrowsError()).rejects.toThrow(
'The user aborted a request.',
);
});
it('should abort the request when the custom timeout is hit', async function () {
nock('https://api.infura.io')
.get('/moon')
.delay(SECOND * 2)
.reply(200, '{"moon": "2012-12-21T11:11:11Z"}');
const fetchWithTimeout = getFetchWithTimeout(MILLISECOND * 123);
const fetchWithTimeoutThrowsError = async () => {
await fetchWithTimeout('https://api.infura.io/moon').then((r) =>
r.json(),
);
throw new Error('Request should be aborted');
};
await expect(fetchWithTimeoutThrowsError()).rejects.toThrow(
'The user aborted a request.',
);
});
it('throws on invalid timeout', async function () {
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.',
);
});
});