1
- import HTTP , { HTTPComponent , HTTPMiddleware } from '..' ;
2
- import * as express from '../__mocks__/express' ;
1
+ import HTTP , { HTTPComponent , HTTPMiddleware , HTTPComponentInstance } from '..' ;
2
+ import * as httpMock from '../__mocks__/http' ;
3
+ import * as expressMock from '../__mocks__/express' ;
3
4
5
+ jest . mock ( 'http' ) ;
4
6
jest . mock ( 'express' ) ;
5
7
6
- const { instance } = express ;
8
+ const { instance : httpInstance } = httpMock ;
9
+ const { instance : expressInstance } = expressMock ;
7
10
8
11
describe ( 'HTTP' , ( ) => {
9
12
beforeEach ( ( ) => {
10
- instance . get . mockReset ( ) ;
11
- instance . put . mockReset ( ) ;
12
- instance . post . mockReset ( ) ;
13
- instance . delete . mockReset ( ) ;
13
+ expressInstance . get . mockReset ( ) ;
14
+ expressInstance . put . mockReset ( ) ;
15
+ expressInstance . post . mockReset ( ) ;
16
+ expressInstance . delete . mockReset ( ) ;
14
17
} ) ;
15
18
16
19
describe ( '#constructor' , ( ) => {
@@ -27,7 +30,7 @@ describe('HTTP', () => {
27
30
28
31
expect ( http . app ) . not . toBeUndefined ( ) ;
29
32
30
- expect ( http . app ) . toBe ( instance ) ;
33
+ expect ( http . app ) . toBe ( expressInstance ) ;
31
34
} ) . not . toThrow ( ) ;
32
35
} ) ;
33
36
} ) ;
@@ -36,7 +39,7 @@ describe('HTTP', () => {
36
39
test ( 'installs http components where path is a string' , ( ) => {
37
40
const http = new HTTP ( ) ;
38
41
39
- const component : HTTPComponent = {
42
+ const component : HTTPComponentInstance = {
40
43
is : 'component' ,
41
44
type : 'http' ,
42
45
config : {
@@ -62,17 +65,19 @@ describe('HTTP', () => {
62
65
http . http ( component ) ;
63
66
} ) . not . toThrow ( ) ;
64
67
65
- expect ( instance . get . mock . calls . length ) . toBe ( 1 ) ;
66
- expect ( instance . get . mock . calls [ 0 ] [ 0 ] ) . toBe (
68
+ expect ( expressInstance . get . mock . calls . length ) . toBe ( 1 ) ;
69
+ expect ( expressInstance . get . mock . calls [ 0 ] [ 0 ] ) . toBe (
67
70
component . config . http . path ,
68
71
) ;
69
- expect ( typeof instance . get . mock . calls [ 0 ] [ 1 ] ) . toBe ( 'function' ) ;
72
+ expect ( typeof expressInstance . get . mock . calls [ 0 ] [ 1 ] ) . toBe (
73
+ 'function' ,
74
+ ) ;
70
75
} ) ;
71
76
72
77
test ( 'installs http components where path is a regex' , ( ) => {
73
78
const http = new HTTP ( ) ;
74
79
75
- const component : HTTPComponent = {
80
+ const component : HTTPComponentInstance = {
76
81
is : 'component' ,
77
82
type : 'http' ,
78
83
config : {
@@ -98,17 +103,19 @@ describe('HTTP', () => {
98
103
http . http ( component ) ;
99
104
} ) . not . toThrow ( ) ;
100
105
101
- expect ( instance . get . mock . calls . length ) . toBe ( 1 ) ;
102
- expect ( instance . get . mock . calls [ 0 ] [ 0 ] ) . toBe (
106
+ expect ( expressInstance . get . mock . calls . length ) . toBe ( 1 ) ;
107
+ expect ( expressInstance . get . mock . calls [ 0 ] [ 0 ] ) . toBe (
103
108
component . config . http . path ,
104
109
) ;
105
- expect ( typeof instance . get . mock . calls [ 0 ] [ 1 ] ) . toBe ( 'function' ) ;
110
+ expect ( typeof expressInstance . get . mock . calls [ 0 ] [ 1 ] ) . toBe (
111
+ 'function' ,
112
+ ) ;
106
113
} ) ;
107
114
108
115
test ( 'installs http components where path is an array' , ( ) => {
109
116
const http = new HTTP ( ) ;
110
117
111
- const component : HTTPComponent = {
118
+ const component : HTTPComponentInstance = {
112
119
is : 'component' ,
113
120
type : 'http' ,
114
121
config : {
@@ -137,11 +144,13 @@ describe('HTTP', () => {
137
144
http . http ( component ) ;
138
145
} ) . not . toThrow ( ) ;
139
146
140
- expect ( instance . get . mock . calls . length ) . toBe ( 1 ) ;
141
- expect ( instance . get . mock . calls [ 0 ] [ 0 ] ) . toBe (
147
+ expect ( expressInstance . get . mock . calls . length ) . toBe ( 1 ) ;
148
+ expect ( expressInstance . get . mock . calls [ 0 ] [ 0 ] ) . toBe (
142
149
component . config . http . path ,
143
150
) ;
144
- expect ( typeof instance . get . mock . calls [ 0 ] [ 1 ] ) . toBe ( 'function' ) ;
151
+ expect ( typeof expressInstance . get . mock . calls [ 0 ] [ 1 ] ) . toBe (
152
+ 'function' ,
153
+ ) ;
145
154
} ) ;
146
155
147
156
test ( 'does not accept invalid components' , ( ) => {
@@ -353,9 +362,12 @@ describe('HTTP', () => {
353
362
expect ( ( middleware . http as jest . Mock < { } > ) . mock . calls . length ) . toBe (
354
363
1 ,
355
364
) ;
356
- expect ( ( middleware . http as jest . Mock < { } > ) . mock . calls [ 0 ] [ 0 ] ) . toBe (
357
- instance ,
358
- ) ;
365
+ expect (
366
+ ( middleware . http as jest . Mock < { } > ) . mock . calls [ 0 ] [ 0 ] . app ,
367
+ ) . toEqual ( expressInstance ) ;
368
+ expect (
369
+ ( middleware . http as jest . Mock < { } > ) . mock . calls [ 0 ] [ 0 ] . server ,
370
+ ) . toBeTruthy ( ) ;
359
371
} ) ;
360
372
361
373
test ( 'does not accept invalid http middleware' , ( ) => {
0 commit comments