|
1 | 1 | /* eslint-disable @typescript-eslint/no-unused-vars */ |
2 | 2 | /* eslint-disable @typescript-eslint/no-empty-function */ |
3 | 3 | import 'reflect-metadata'; |
4 | | -import { Body, PARAMETER_TOKEN, Header, Query } from './parameter.decorators'; |
| 4 | +import type { NextApiRequest, NextApiResponse } from 'next'; |
| 5 | +import { Body, PARAMETER_TOKEN, Req, Request, Res, Response, Header, Query } from './parameter.decorators'; |
5 | 6 |
|
6 | 7 | describe('Parameter decorators', () => { |
7 | 8 | it('Body should be set.', () => { |
@@ -51,4 +52,42 @@ describe('Parameter decorators', () => { |
51 | 52 | ]) |
52 | 53 | ); |
53 | 54 | }); |
| 55 | + |
| 56 | + it('Req should be set.', () => { |
| 57 | + class Test { |
| 58 | + public index(@Req() req: NextApiRequest) {} |
| 59 | + } |
| 60 | + |
| 61 | + const meta = Reflect.getMetadata(PARAMETER_TOKEN, Test, 'index'); |
| 62 | + expect(Array.isArray(meta)).toBe(true); |
| 63 | + expect(meta).toHaveLength(1); |
| 64 | + expect(meta).toMatchObject(expect.arrayContaining([{ index: 0, location: 'request' }])); |
| 65 | + }); |
| 66 | + |
| 67 | + it('Res should be set.', () => { |
| 68 | + class Test { |
| 69 | + public index(@Res() res: NextApiResponse) {} |
| 70 | + } |
| 71 | + |
| 72 | + const meta = Reflect.getMetadata(PARAMETER_TOKEN, Test, 'index'); |
| 73 | + expect(Array.isArray(meta)).toBe(true); |
| 74 | + expect(meta).toHaveLength(1); |
| 75 | + expect(meta).toMatchObject(expect.arrayContaining([{ index: 0, location: 'response' }])); |
| 76 | + }); |
| 77 | + |
| 78 | + it('Request and Response should be set.', () => { |
| 79 | + class Test { |
| 80 | + public index(@Request() req: NextApiRequest, @Response() res: NextApiResponse) {} |
| 81 | + } |
| 82 | + |
| 83 | + const meta = Reflect.getMetadata(PARAMETER_TOKEN, Test, 'index'); |
| 84 | + expect(Array.isArray(meta)).toBe(true); |
| 85 | + expect(meta).toHaveLength(2); |
| 86 | + expect(meta).toMatchObject( |
| 87 | + expect.arrayContaining([ |
| 88 | + { index: 0, location: 'request' }, |
| 89 | + { index: 1, location: 'response' } |
| 90 | + ]) |
| 91 | + ); |
| 92 | + }); |
54 | 93 | }); |
0 commit comments