|
| 1 | +/* eslint-disable @typescript-eslint/typedef */ |
| 2 | +import { describe, expect, it } from '@jest/globals'; |
| 3 | + |
| 4 | +import { METADATA_KEY } from '../constants'; |
| 5 | +import { getControllerMethodMetadata } from '../utils'; |
| 6 | + |
| 7 | +describe('Utils', () => { |
| 8 | + describe('getControllerMethodMetadata', () => { |
| 9 | + it('should return an empty array when controller has no methods', () => { |
| 10 | + class EmptyController {} |
| 11 | + |
| 12 | + const result = getControllerMethodMetadata(EmptyController); |
| 13 | + |
| 14 | + expect(result).toEqual([]); |
| 15 | + }); |
| 16 | + |
| 17 | + it('should return metadata from controller own methods', () => { |
| 18 | + class TestController {} |
| 19 | + const methodMetadata = [ |
| 20 | + { |
| 21 | + key: 'get', |
| 22 | + method: 'testMethod', |
| 23 | + middleware: [], |
| 24 | + path: '/test', |
| 25 | + target: TestController, |
| 26 | + }, |
| 27 | + ]; |
| 28 | + |
| 29 | + Reflect.defineMetadata( |
| 30 | + METADATA_KEY.controllerMethod, |
| 31 | + methodMetadata, |
| 32 | + TestController, |
| 33 | + ); |
| 34 | + |
| 35 | + const result = getControllerMethodMetadata(TestController); |
| 36 | + |
| 37 | + expect(result).toEqual(methodMetadata); |
| 38 | + }); |
| 39 | + |
| 40 | + it('should return metadata from inherited methods', () => { |
| 41 | + class BaseController {} |
| 42 | + class ChildController extends BaseController {} |
| 43 | + |
| 44 | + const genericMetadata = [ |
| 45 | + { |
| 46 | + key: 'get', |
| 47 | + method: 'baseMethod', |
| 48 | + middleware: [], |
| 49 | + path: '/base', |
| 50 | + target: BaseController, |
| 51 | + }, |
| 52 | + ]; |
| 53 | + |
| 54 | + Reflect.defineMetadata( |
| 55 | + METADATA_KEY.controllerMethod, |
| 56 | + genericMetadata, |
| 57 | + BaseController, |
| 58 | + ); |
| 59 | + |
| 60 | + const result = getControllerMethodMetadata(ChildController); |
| 61 | + |
| 62 | + expect(result).toEqual(genericMetadata); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should concatenate own and inherited metadata', () => { |
| 66 | + class BaseController {} |
| 67 | + class ChildController extends BaseController {} |
| 68 | + |
| 69 | + const ownMetadata = [ |
| 70 | + { |
| 71 | + key: 'post', |
| 72 | + method: 'childMethod', |
| 73 | + middleware: [], |
| 74 | + path: '/child', |
| 75 | + target: ChildController, |
| 76 | + }, |
| 77 | + ]; |
| 78 | + |
| 79 | + const genericMetadata = [ |
| 80 | + { |
| 81 | + key: 'get', |
| 82 | + method: 'baseMethod', |
| 83 | + middleware: [], |
| 84 | + path: '/base', |
| 85 | + target: BaseController, |
| 86 | + }, |
| 87 | + ]; |
| 88 | + |
| 89 | + Reflect.defineMetadata( |
| 90 | + METADATA_KEY.controllerMethod, |
| 91 | + ownMetadata, |
| 92 | + ChildController, |
| 93 | + ); |
| 94 | + |
| 95 | + Reflect.defineMetadata( |
| 96 | + METADATA_KEY.controllerMethod, |
| 97 | + genericMetadata, |
| 98 | + BaseController, |
| 99 | + ); |
| 100 | + |
| 101 | + const result = getControllerMethodMetadata(ChildController); |
| 102 | + |
| 103 | + expect(result).toEqual([...ownMetadata, ...genericMetadata]); |
| 104 | + }); |
| 105 | + |
| 106 | + it('should handle undefined metadata correctly', () => { |
| 107 | + class TestController {} |
| 108 | + const result = getControllerMethodMetadata(TestController); |
| 109 | + expect(result).toEqual([]); |
| 110 | + }); |
| 111 | + }); |
| 112 | +}); |
0 commit comments