UNPKG

inversify-express-utils

Version:

Some utilities for the development of express applications with Inversify

134 lines 6.3 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; Object.defineProperty(exports, "__esModule", { value: true }); const globals_1 = require("@jest/globals"); const constants_1 = require("../constants"); const decorators_1 = require("../decorators"); (0, globals_1.describe)('Unit Test: Controller Decorators', () => { (0, globals_1.it)('should add controller metadata to a class when decorated with @controller', () => { const middleware = [ () => undefined, 'foo', Symbol.for('bar'), ]; const path = 'foo'; let TestController = class TestController { }; TestController = __decorate([ (0, decorators_1.controller)(path, ...middleware) ], TestController); const controllerMetadata = Reflect.getMetadata(constants_1.METADATA_KEY.controller, TestController); (0, globals_1.expect)(controllerMetadata.middleware).toEqual(middleware); (0, globals_1.expect)(controllerMetadata.path).toEqual(path); (0, globals_1.expect)(controllerMetadata.target).toEqual(TestController); }); (0, globals_1.it)('should add method metadata to a class when decorated with @httpMethod', () => { const middleware = [ () => undefined, 'bar', Symbol.for('baz'), ]; const path = 'foo'; const method = constants_1.HTTP_VERBS_ENUM.get; class TestController { test() { return undefined; } test2() { return undefined; } test3() { return undefined; } } __decorate([ (0, decorators_1.httpMethod)(method, path, ...middleware), __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", void 0) ], TestController.prototype, "test", null); __decorate([ (0, decorators_1.httpMethod)('foo', 'bar'), __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", void 0) ], TestController.prototype, "test2", null); __decorate([ (0, decorators_1.httpMethod)('bar', 'foo'), __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", void 0) ], TestController.prototype, "test3", null); const methodMetadata = Reflect.getMetadata(constants_1.METADATA_KEY.controllerMethod, TestController); (0, globals_1.expect)(methodMetadata.length).toEqual(3); const metadata = methodMetadata[0]; (0, globals_1.expect)(metadata?.middleware).toEqual(middleware); (0, globals_1.expect)(metadata?.path).toEqual(path); (0, globals_1.expect)(metadata?.target.constructor).toEqual(TestController); (0, globals_1.expect)(metadata?.key).toEqual('test'); (0, globals_1.expect)(metadata?.method).toEqual(method); }); (0, globals_1.it)('should add parameter metadata to a class when decorated with @params', () => { const middleware = [ () => { // }, 'bar', Symbol.for('baz'), ]; const path = 'foo'; const method = constants_1.HTTP_VERBS_ENUM.get; const methodName = 'test'; class TestController { test(_id, _cat) { // } test2(_dog) { // } test3() { // } } __decorate([ (0, decorators_1.httpMethod)(method, path, ...middleware), __param(0, (0, decorators_1.params)(constants_1.PARAMETER_TYPE.PARAMS, 'id')), __param(1, (0, decorators_1.params)(constants_1.PARAMETER_TYPE.PARAMS, 'cat')), __metadata("design:type", Function), __metadata("design:paramtypes", [Object, Object]), __metadata("design:returntype", void 0) ], TestController.prototype, "test", null); __decorate([ (0, decorators_1.httpMethod)('foo', 'bar'), __param(0, (0, decorators_1.params)(constants_1.PARAMETER_TYPE.PARAMS, 'dog')), __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", void 0) ], TestController.prototype, "test2", null); __decorate([ (0, decorators_1.httpMethod)('bar', 'foo'), __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", void 0) ], TestController.prototype, "test3", null); const methodMetadataList = Reflect.getMetadata(constants_1.METADATA_KEY.controllerParameter, TestController); (0, globals_1.expect)(methodMetadataList['test'] && true).toEqual(true); const paramaterMetadataList = methodMetadataList[methodName]; (0, globals_1.expect)(paramaterMetadataList?.length).toEqual(2); const paramaterMetadata = paramaterMetadataList?.[0]; (0, globals_1.expect)(paramaterMetadata?.index).toEqual(0); (0, globals_1.expect)(paramaterMetadata?.parameterName).toEqual('id'); }); }); //# sourceMappingURL=decorators.test.js.map