1
|
|
|
import { flushPromises } from '@test/utils/testUtils'; |
2
|
|
|
import { Request, Response } from 'express'; |
3
|
|
|
import { Route } from '@/core/decorators/Route'; |
4
|
|
|
import { registeredBeans, registeredMethods } from '@/core'; |
5
|
|
|
|
6
|
|
|
jest.mock('@/core', () => ({ |
7
|
|
|
registeredBeans: new Map(), |
8
|
|
|
registeredMethods: new Map(), |
9
|
|
|
logger: { |
10
|
|
|
info: jest.fn(), |
11
|
|
|
debug: jest.fn(), |
12
|
|
|
error: jest.fn(), |
13
|
|
|
}, |
14
|
|
|
})); |
15
|
|
|
|
16
|
|
|
describe('Route.ts', () => { |
17
|
|
|
beforeEach(() => { |
18
|
|
|
jest.resetAllMocks(); |
19
|
|
|
registeredMethods.clear(); |
20
|
|
|
registeredBeans.clear(); |
21
|
|
|
}); |
22
|
|
|
|
23
|
|
|
it.each([ |
24
|
|
|
'GET', |
25
|
|
|
'HEAD', |
26
|
|
|
'POST', |
27
|
|
|
'PUT', |
28
|
|
|
'DELETE', |
29
|
|
|
'CONNECT', |
30
|
|
|
'OPTIONS', |
31
|
|
|
'TRACE', |
32
|
|
|
'PATCH', |
33
|
|
|
])('registers a %s route', async (method: any) => { |
34
|
|
|
// GIVEN |
35
|
|
|
const mock = jest.fn(); |
36
|
|
|
const resMock = { send: jest.fn() }; |
37
|
|
|
class Class { |
38
|
|
|
num = 42; |
39
|
|
|
|
40
|
|
|
@Route(method, '/num') |
41
|
|
|
getNum(_req: Request, res: Response) { |
42
|
|
|
res.send('OK'); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
const bean: any = new Class(); |
46
|
|
|
registeredMethods.set(bean.getNum, bean); |
47
|
|
|
bean.routerConfig = { |
48
|
|
|
path: '/router', |
49
|
|
|
router: { |
50
|
|
|
[method.toLowerCase()]: mock, |
51
|
|
|
}, |
52
|
|
|
}; |
53
|
|
|
registeredBeans.set('Class', bean); |
54
|
|
|
await flushPromises(); |
55
|
|
|
mock.mock.calls[0][1](null, resMock); |
56
|
|
|
|
57
|
|
|
// THEN |
58
|
|
|
expect(mock).toHaveBeenCalledWith('/num', expect.any(Function)); |
59
|
|
|
expect(resMock.send).toHaveBeenCalledWith('OK'); |
60
|
|
|
}); |
61
|
|
|
|
62
|
|
|
it.each([ |
63
|
|
|
'GET', |
64
|
|
|
'HEAD', |
65
|
|
|
'POST', |
66
|
|
|
'PUT', |
67
|
|
|
'DELETE', |
68
|
|
|
'CONNECT', |
69
|
|
|
'OPTIONS', |
70
|
|
|
'TRACE', |
71
|
|
|
'PATCH', |
72
|
|
|
])('registers a %s route with async function', async (method: any) => { |
73
|
|
|
// GIVEN |
74
|
|
|
const mock = jest.fn(); |
75
|
|
|
const resMock = { send: jest.fn() }; |
76
|
|
|
class Class { |
77
|
|
|
num = 42; |
78
|
|
|
|
79
|
|
|
@Route(method, '/num') |
80
|
|
|
async getNum(_req: Request, res: Response) { |
81
|
|
|
res.send('OK'); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
const bean: any = new Class(); |
85
|
|
|
registeredMethods.set(bean.getNum, bean); |
86
|
|
|
bean.routerConfig = { |
87
|
|
|
path: '/router', |
88
|
|
|
router: { |
89
|
|
|
[method.toLowerCase()]: mock, |
90
|
|
|
}, |
91
|
|
|
}; |
92
|
|
|
registeredBeans.set('Class', bean); |
93
|
|
|
await flushPromises(); |
94
|
|
|
mock.mock.calls[0][1](null, resMock); |
95
|
|
|
|
96
|
|
|
// THEN |
97
|
|
|
expect(mock).toHaveBeenCalledWith('/num', expect.any(Function)); |
98
|
|
|
expect(resMock.send).toHaveBeenCalledWith('OK'); |
99
|
|
|
}); |
100
|
|
|
|
101
|
|
|
it('does not registers a route on a generic Bean', async () => { |
102
|
|
|
// GIVEN |
103
|
|
|
const mock = jest.fn(); |
104
|
|
|
class Class { |
105
|
|
|
@Route('GET', '/num') |
106
|
|
|
getNum(_req: Request, res: Response) { |
107
|
|
|
res.send('OK'); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
const bean: any = new Class(); |
111
|
|
|
registeredMethods.set(bean.getNum, bean); |
112
|
|
|
registeredBeans.set('Class', bean); |
113
|
|
|
await flushPromises(); |
114
|
|
|
|
115
|
|
|
// THEN |
116
|
|
|
expect(mock).not.toHaveBeenCalled(); |
117
|
|
|
}); |
118
|
|
|
}); |
119
|
|
|
|