summaryrefslogtreecommitdiffstats
path: root/test/types/mock-agent.test-d.ts
blob: 5f7f9686494f6039ecd12c835caf11919b8f49d2 (plain)
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import {expectAssignable, expectType} from 'tsd'
import {Agent, Dispatcher, MockAgent, MockClient, MockPool, setGlobalDispatcher} from '../..'
import {MockInterceptor} from '../../types/mock-interceptor'
import MockDispatch = MockInterceptor.MockDispatch;

expectAssignable<MockAgent>(new MockAgent())
expectAssignable<MockAgent>(new MockAgent({}))

{
  const mockAgent = new MockAgent()
  expectAssignable<void>(setGlobalDispatcher(mockAgent))

  // get
  expectAssignable<MockPool>(mockAgent.get(''))
  expectAssignable<MockPool>(mockAgent.get(new RegExp('')))
  expectAssignable<MockPool>(mockAgent.get((origin) => {
    expectAssignable<string>(origin)
    return true
  }))
  expectAssignable<Dispatcher>(mockAgent.get(''))

  // close
  expectAssignable<Promise<void>>(mockAgent.close())

  // deactivate
  expectAssignable<void>(mockAgent.deactivate())

  // activate
  expectAssignable<void>(mockAgent.activate())

  // enableNetConnect
  expectAssignable<void>(mockAgent.enableNetConnect())
  expectAssignable<void>(mockAgent.enableNetConnect(''))
  expectAssignable<void>(mockAgent.enableNetConnect(new RegExp('')))
  expectAssignable<void>(mockAgent.enableNetConnect((host) => {
    expectAssignable<string>(host)
    return true
  }))

  // disableNetConnect
  expectAssignable<void>(mockAgent.disableNetConnect())

  // dispatch
  expectAssignable<boolean>(mockAgent.dispatch({origin: '', path: '', method: 'GET'}, {}))

  // intercept
  expectAssignable<MockInterceptor>((mockAgent.get('foo')).intercept({path: '', method: 'GET'}))
}

{
  const mockAgent = new MockAgent({connections: 1})
  expectAssignable<void>(setGlobalDispatcher(mockAgent))
  expectAssignable<MockClient>(mockAgent.get(''))
}

{
  const agent = new Agent()
  const mockAgent = new MockAgent({agent})
  expectAssignable<void>(setGlobalDispatcher(mockAgent))
  expectAssignable<MockPool>(mockAgent.get(''))
}

{
  interface PendingInterceptor extends MockDispatch {
    origin: string;
  }

  const agent = new MockAgent({agent: new Agent()})
  expectType<() => PendingInterceptor[]>(agent.pendingInterceptors)
  expectType<(options?: {
    pendingInterceptorsFormatter?: {
      format(pendingInterceptors: readonly PendingInterceptor[]): string;
    }
  }) => void>(agent.assertNoPendingInterceptors)
}