aboutsummaryrefslogtreecommitdiffstats
path: root/ui/app/helpers/utils/fetch.test.js
blob: 12724525a2b74289cf3a7a838d55ea9a8641a93c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import assert from 'assert'
import nock from 'nock'

import http from './fetch'

describe('custom fetch fn', () => {
  it('fetches a url', async () => {
    nock('https://api.infura.io')
      .get('/money')
      .reply(200, '{"hodl": false}')

    const fetch = http()
    const response = await (await fetch('https://api.infura.io/money')).json()
    assert.deepEqual(response, {
      hodl: false,
    })
  })

  it('throws when the request hits a custom timeout', async () => {
    nock('https://api.infura.io')
      .get('/moon')
      .delay(2000)
      .reply(200, '{"moon": "2012-12-21T11:11:11Z"}')

    const fetch = http({
      timeout: 123,
    })

    try {
      await fetch('https://api.infura.io/moon').then(r => r.json())
      assert.fail('Request should throw')
    } catch (e) {
      assert.ok(e)
    }
  })

  it('should abort the request when the custom timeout is hit', async () => {
    nock('https://api.infura.io')
      .get('/moon')
      .delay(2000)
      .reply(200, '{"moon": "2012-12-21T11:11:11Z"}')

    const fetch = http({
      timeout: 123,
    })

    try {
      await fetch('https://api.infura.io/moon').then(r => r.json())
      assert.fail('Request should be aborted')
    } catch (e) {
      assert.deepEqual(e.message, 'Aborted')
    }
  })
})