1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-23 02:10:12 +01:00
metamask-extension/shared/modules/fetch-with-timeout.test.ts
Elliot Winkler bbfc455342
Convert shared/modules/fetch-with-timeout to TS (#18351)
We want to convert NetworkController to TypeScript in order to be able
to compare differences in the controller between in this repo and the
core repo. To do this, however, we need to convert the dependencies of
the controller to TypeScript.

As a part of this effort, this commit converts
`shared/modules/fetch-with-timeout.js` to TypeScript.
2023-03-31 10:52:33 -06:00

57 lines
1.7 KiB
TypeScript

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