From 16b016ceefd4e2d9171613d8c73812f8e33eddab Mon Sep 17 00:00:00 2001 From: Sergii Mykyteiek Date: Wed, 6 Oct 2021 16:15:15 +0300 Subject: [PATCH] LT-11: Cover ttl services with tests, lint project --- src/middleware/error.middleware.ts | 2 +- src/modules/stack/stack.controller.ts | 3 +- src/modules/stack/stack.router.ts | 10 +-- src/modules/ttl/ttl.router.ts | 15 +---- .../validation/ttl.validation-service.spec.ts | 63 +++++++++++++++++++ src/shared/logger/logger.interfaces.ts | 14 +++-- 6 files changed, 78 insertions(+), 29 deletions(-) create mode 100644 src/modules/ttl/validation/ttl.validation-service.spec.ts diff --git a/src/middleware/error.middleware.ts b/src/middleware/error.middleware.ts index 4609de1..33eca2c 100644 --- a/src/middleware/error.middleware.ts +++ b/src/middleware/error.middleware.ts @@ -1,4 +1,4 @@ -import { Response, Request, NextFunction} from 'express'; +import { Response, Request, NextFunction } from 'express'; export const errorMiddleware = (error: Error, req: Request, res: Response, next: NextFunction) => { delete error.stack; diff --git a/src/modules/stack/stack.controller.ts b/src/modules/stack/stack.controller.ts index 2e6a91b..caa0304 100644 --- a/src/modules/stack/stack.controller.ts +++ b/src/modules/stack/stack.controller.ts @@ -28,7 +28,7 @@ export class StackController { this.logger.error({ placement: `[${this.constructor.name}].add`, error: e, - arguments: Array.from(arguments), + arguments: Array.from([req.body]), }); next(e); @@ -48,7 +48,6 @@ export class StackController { this.logger.error({ placement: `[${this.constructor.name}].get`, error: e, - arguments: Array.from(arguments), }); next(e); diff --git a/src/modules/stack/stack.router.ts b/src/modules/stack/stack.router.ts index b0e1ff3..894d626 100644 --- a/src/modules/stack/stack.router.ts +++ b/src/modules/stack/stack.router.ts @@ -5,14 +5,8 @@ import { StackController } from './stack.controller'; const stackRouter = Router(); const stackController = Container.get(StackController); -stackRouter.post( - '/', - stackController.add.bind(stackController), -); +stackRouter.post('/', stackController.add.bind(stackController)); -stackRouter.get( - '/', - stackController.get.bind(stackController), -); +stackRouter.get('/', stackController.get.bind(stackController)); export { stackRouter }; diff --git a/src/modules/ttl/ttl.router.ts b/src/modules/ttl/ttl.router.ts index 091f9d5..0d28f63 100644 --- a/src/modules/ttl/ttl.router.ts +++ b/src/modules/ttl/ttl.router.ts @@ -5,19 +5,10 @@ import { TtlController } from './ttl.controller'; const ttlRouter = Router(); const ttlController = Container.get(TtlController); -ttlRouter.get( - '/:key', - ttlController.get.bind(ttlController), -); +ttlRouter.get('/:key', ttlController.get.bind(ttlController)); -ttlRouter.delete( - '/:key', - ttlController.remove.bind(ttlController), -); +ttlRouter.delete('/:key', ttlController.remove.bind(ttlController)); -ttlRouter.post( - '/', - ttlController.add.bind(ttlController), -); +ttlRouter.post('/', ttlController.add.bind(ttlController)); export { ttlRouter }; diff --git a/src/modules/ttl/validation/ttl.validation-service.spec.ts b/src/modules/ttl/validation/ttl.validation-service.spec.ts new file mode 100644 index 0000000..d1588bf --- /dev/null +++ b/src/modules/ttl/validation/ttl.validation-service.spec.ts @@ -0,0 +1,63 @@ +import 'reflect-metadata'; +import { Container } from 'typedi'; +import { TtlValidationService } from './ttl.validation-service'; + +describe('TtlValidationService', () => { + let validateService: TtlValidationService; + + beforeEach(() => { + validateService = Container.get(TtlValidationService); + }); + + afterEach(() => { + jest.clearAllMocks(); + }); + + describe('validateBody', () => { + describe('When body does not pass validation', () => { + it('When body has extra properties -> should return errors', async () => { + const body = { + key: 'name', + value: 100, + extra: 7, + }; + + const errors = await validateService.validateBody(body); + + expect(errors.length).not.toBe(0); + }); + + it('When body is empty -> should return errors', async () => { + const body: any = {}; + + const errors = await validateService.validateBody(body); + + expect(errors.length).not.toBe(0); + }); + + it('When body fields have wrong format -> should return errors', async () => { + const body: any = { + key: 6, + value: 100, + }; + + const errors = await validateService.validateBody(body); + + expect(errors.length).not.toBe(0); + }); + }); + + describe('When body pass validation', () => { + it('Should return empty array of errors', async () => { + const body = { + key: 'name', + value: 'John', + }; + + const errors = await validateService.validateBody(body); + + expect(errors.length).toBe(0); + }); + }) + }); +}); diff --git a/src/shared/logger/logger.interfaces.ts b/src/shared/logger/logger.interfaces.ts index 6c663f2..a850417 100644 --- a/src/shared/logger/logger.interfaces.ts +++ b/src/shared/logger/logger.interfaces.ts @@ -1,9 +1,11 @@ -export type LoggerMessage = string | { - placement: string; - arguments?: any; - error?: Error | string; -}; +export type LoggerMessage = + | string + | { + placement: string; + arguments?: any; + error?: Error | string; + }; export interface ILoggerError { error(message: LoggerMessage): void; -} \ No newline at end of file +} -- GitLab