| Total Complexity | 1 |
| Complexity/F | 1 |
| Lines of Code | 59 |
| Function Count | 1 |
| Duplicated Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 1 | import { flushPromises } from '@test/utils/testUtils'; |
||
| 2 | import { Request, Response } from 'express'; |
||
| 3 | import { Route } from '@/decorators/Route'; |
||
| 4 | import { registeredBeans, registeredMethods } from '@/decorators'; |
||
| 5 | |||
| 6 | jest.mock('@/decorators', () => ({ |
||
| 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 | class Class { |
||
| 37 | @Route(method, '/num') |
||
| 38 | getNum(_req: Request, res: Response) { |
||
| 39 | res.send('OK'); |
||
| 40 | } |
||
| 41 | } |
||
| 42 | const bean: any = new Class(); |
||
| 43 | registeredMethods.set(bean.getNum, bean); |
||
| 44 | bean.routerConfig = { |
||
| 45 | path: '/router', |
||
| 46 | router: { |
||
| 47 | [method.toLowerCase()]: mock, |
||
| 48 | }, |
||
| 49 | }; |
||
| 50 | registeredBeans.set('Class', bean); |
||
| 51 | await flushPromises(); |
||
| 52 | |||
| 53 | // THEN |
||
| 54 | expect(mock).toBeCalledWith('/num', expect.any(Function)); |
||
| 55 | expect(mock.mock.calls[0][1].name) |
||
| 56 | .toBe('bound getNum'); |
||
| 57 | }); |
||
| 58 | }); |
||
| 59 |