UNPKG

realm-object-server

Version:

Realm Object Server

250 lines 9.88 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); }; Object.defineProperty(exports, "__esModule", { value: true }); const chai_1 = require("chai"); const decorators_1 = require("./decorators"); describe("BaseRoute decorator", function () { let CarService = class CarService { }; CarService = __decorate([ decorators_1.BaseRoute("/sample") ], CarService); it("should create a baseRoute string metadata", function () { const baseRoute = CarService.baseRoute; chai_1.expect(baseRoute).to.be.not.undefined; chai_1.expect(baseRoute).to.eq("/sample"); }); }); describe("MiddlewaresBefore", function () { const middlewareOnClass = function (req, res, next) { next(); }; const middlewareOnFunction = function (req, res, next) { next(); }; let CarService = class CarService { someFunc() { } }; __decorate([ decorators_1.MiddlewaresBefore(middlewareOnFunction), __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", void 0) ], CarService.prototype, "someFunc", null); CarService = __decorate([ decorators_1.MiddlewaresBefore(middlewareOnClass) ], CarService); it("should have a middleware on class constructor", function () { const carService = new CarService(); const m = carService.constructor.middlewaresBeforeService[0]; chai_1.expect(m).to.be.eq(middlewareOnClass); }); it("should have a middleware on function", function () { const carService = new CarService(); const m = carService.middlewaresBeforeFunction[0]; chai_1.expect(m["functionName"]).to.be.eq("someFunc"); chai_1.expect(m["middlewares"]).to.not.be.empty; chai_1.expect(m["middlewares"][0]).to.be.eq(middlewareOnFunction); }); }); describe("HTTP Decorators", function () { let SampleService = class SampleService { getCar() { } postTruck() { } putCaravan() { } deleteShip() { } headShuttle() { } optionCup() { } traceMonitor() { } patchDog() { } }; __decorate([ decorators_1.Get("/car"), __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", void 0) ], SampleService.prototype, "getCar", null); __decorate([ decorators_1.Post("/truck"), __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", void 0) ], SampleService.prototype, "postTruck", null); __decorate([ decorators_1.Put("/caravan"), __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", void 0) ], SampleService.prototype, "putCaravan", null); __decorate([ decorators_1.Delete("/ship"), __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", void 0) ], SampleService.prototype, "deleteShip", null); __decorate([ decorators_1.Head("/shuttle"), __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", void 0) ], SampleService.prototype, "headShuttle", null); __decorate([ decorators_1.Options("/cup"), __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", void 0) ], SampleService.prototype, "optionCup", null); __decorate([ decorators_1.Trace("/monitor"), __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", void 0) ], SampleService.prototype, "traceMonitor", null); __decorate([ decorators_1.Patch("/dog"), __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", void 0) ], SampleService.prototype, "patchDog", null); SampleService = __decorate([ decorators_1.BaseRoute("/") ], SampleService); it("should create a GET reflected route", function () { const service = new SampleService(); const routes = service.routes; chai_1.expect(routes).to.not.be.undefined; chai_1.expect(routes).to.not.be.empty; chai_1.expect(routes.find(r => r.functionName === "getCar")).to.be.deep.eq({ allowAnonymous: undefined, httpMethod: "get", functionName: "getCar", path: "/car" }); }); it("should create a POST reflected route", function () { const service = new SampleService(); const routes = service.routes; chai_1.expect(routes).to.not.be.undefined; chai_1.expect(routes).to.not.be.empty; chai_1.expect(routes.find(r => r.functionName === "postTruck")).to.be.deep.eq({ allowAnonymous: undefined, httpMethod: "post", functionName: "postTruck", path: "/truck" }); }); it("should create a PUT reflected route", function () { const service = new SampleService(); const routes = service.routes; chai_1.expect(routes).to.not.be.undefined; chai_1.expect(routes).to.not.be.empty; chai_1.expect(routes.find(r => r.functionName === "putCaravan")).to.be.deep.eq({ allowAnonymous: undefined, httpMethod: "put", functionName: "putCaravan", path: "/caravan" }); }); it("should create a DELETE reflected route", function () { const service = new SampleService(); const routes = service.routes; chai_1.expect(routes).to.not.be.undefined; chai_1.expect(routes).to.not.be.empty; chai_1.expect(routes.find(r => r.functionName === "deleteShip")).to.be.deep.eq({ allowAnonymous: undefined, httpMethod: "delete", functionName: "deleteShip", path: "/ship" }); }); it("should create a HEAD reflected route", function () { const service = new SampleService(); const routes = service.routes; chai_1.expect(routes).to.not.be.undefined; chai_1.expect(routes).to.not.be.empty; chai_1.expect(routes.find(r => r.functionName === "headShuttle")).to.be.deep.eq({ allowAnonymous: undefined, httpMethod: "head", functionName: "headShuttle", path: "/shuttle" }); }); it("should create a OPTIONS reflected route", function () { const service = new SampleService(); const routes = service.routes; chai_1.expect(routes).to.not.be.undefined; chai_1.expect(routes).to.not.be.empty; chai_1.expect(routes.find(r => r.functionName === "headShuttle")).to.be.deep.eq({ allowAnonymous: undefined, httpMethod: "head", functionName: "headShuttle", path: "/shuttle" }); }); it("should create a TRACE reflected route", function () { const service = new SampleService(); const routes = service.routes; chai_1.expect(routes).to.not.be.undefined; chai_1.expect(routes).to.not.be.empty; chai_1.expect(routes.find(r => r.functionName === "traceMonitor")).to.be.deep.eq({ allowAnonymous: undefined, httpMethod: "trace", functionName: "traceMonitor", path: "/monitor" }); }); it("should create a PATCH reflected route", function () { const service = new SampleService(); const routes = service.routes; chai_1.expect(routes).to.not.be.undefined; chai_1.expect(routes).to.not.be.empty; chai_1.expect(routes.find(r => r.functionName === "patchDog")).to.be.deep.eq({ allowAnonymous: undefined, httpMethod: "patch", functionName: "patchDog", path: "/dog" }); }); }); describe("Websocket Upgrade and Metadata", function () { let SampleService = class SampleService { upgradeSocket(req, res, head) { } }; __decorate([ decorators_1.Upgrade("/socket"), __metadata("design:type", Function), __metadata("design:paramtypes", [Object, Object, Object]), __metadata("design:returntype", void 0) ], SampleService.prototype, "upgradeSocket", null); SampleService = __decorate([ decorators_1.BaseRoute("/") ], SampleService); it("should create a UpgradeRoutes reflected metadata", function () { const service = new SampleService(); const upgradeRoutes = service.upgradeRoutes; chai_1.expect(upgradeRoutes).to.be.not.undefined; chai_1.expect(upgradeRoutes[0]).to.be.deep.eq({ functionName: "upgradeSocket", path: "/socket" }); }); }); //# sourceMappingURL=decorators.spec.js.map