From 9c94c0808977ac1047073f5fc5a50426be94d0e3 Mon Sep 17 00:00:00 2001 From: Sergii Mykyteiek Date: Wed, 6 Oct 2021 14:53:12 +0300 Subject: [PATCH] LT-9: Cover stack services with tests --- src/modules/stack/stack.controller.spec.ts | 2 +- src/modules/stack/stack.repository.spec.ts | 70 +++++++++++++++++++ .../stack.validation-service.spec.ts | 60 ++++++++++++++++ 3 files changed, 131 insertions(+), 1 deletion(-) create mode 100644 src/modules/stack/stack.repository.spec.ts create mode 100644 src/modules/stack/validation/stack.validation-service.spec.ts diff --git a/src/modules/stack/stack.controller.spec.ts b/src/modules/stack/stack.controller.spec.ts index 1119c40..b322130 100644 --- a/src/modules/stack/stack.controller.spec.ts +++ b/src/modules/stack/stack.controller.spec.ts @@ -95,4 +95,4 @@ describe('StackController', () => { expect(mockResponse.json).toBeCalledWith(response); }); }); -}) \ No newline at end of file +}); diff --git a/src/modules/stack/stack.repository.spec.ts b/src/modules/stack/stack.repository.spec.ts new file mode 100644 index 0000000..b54f897 --- /dev/null +++ b/src/modules/stack/stack.repository.spec.ts @@ -0,0 +1,70 @@ +import 'reflect-metadata'; +import { Container } from 'typedi'; +import { StackModel } from './stack.model'; +import { StackRepository } from './stack.repository'; + +describe('StackRepository', () => { + let stackModel; + let stackRepository: StackRepository; + + beforeEach(() => { + stackModel = Container.get(StackModel); + stackRepository = Container.get(StackRepository); + }); + + afterEach(() => { + jest.clearAllMocks(); + }); + + describe('add', () => { + it('Should return created item', () => { + const data = 'some data' + const dto = { + data, + }; + jest.spyOn(stackModel, 'push').mockImplementation(() => dto); + + const result = stackRepository.add(dto); + + expect(stackModel.push).toBeCalledWith(dto.data); + expect(result).toHaveProperty('data'); + }); + }); + + describe('get', () => { + it('Should call pop method', () => { + const data = 'some data' + const dto = { + data, + }; + jest.spyOn(stackModel, 'pop').mockImplementation(() => dto); + + const result = stackRepository.get(); + + expect(stackModel.pop).toBeCalled(); + expect(result).toHaveProperty('data'); + }); + }); + + describe('isEmpty', () => { + it('When stack is empty -> should return true', () => { + const length = 0; + jest.spyOn(stackModel, 'getLength').mockImplementation(() => length); + + const result = stackRepository.isEmpty(); + + expect(stackModel.getLength).toBeCalled(); + expect(result).toBe(true); + }); + + it('When stack is not empty -> should return false', () => { + const length = 2; + jest.spyOn(stackModel, 'getLength').mockImplementation(() => length); + + const result = stackRepository.isEmpty(); + + expect(stackModel.getLength).toBeCalled(); + expect(result).toBe(false); + }); + }); +}); diff --git a/src/modules/stack/validation/stack.validation-service.spec.ts b/src/modules/stack/validation/stack.validation-service.spec.ts new file mode 100644 index 0000000..6f8ac79 --- /dev/null +++ b/src/modules/stack/validation/stack.validation-service.spec.ts @@ -0,0 +1,60 @@ +import 'reflect-metadata'; +import { Container } from 'typedi'; +import { StackValidationService } from './stack.validation-service'; + +describe('StackValidationService', () => { + let validateService: StackValidationService; + + beforeEach(() => { + validateService = Container.get(StackValidationService); + }); + + afterEach(() => { + jest.clearAllMocks(); + }); + + describe('validateBody', () => { + describe('When body does not pass validation', () => { + it('When body has extra properties -> should return errors', async () => { + const body = { + data: 'some data', + extra: 100, + }; + + 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 wrong fields -> should return errors', async () => { + const body: any = { + dara: 6, + }; + + 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 = { + data: 'some data', + }; + + const errors = await validateService.validateBody(body); + + expect(errors.length).toBe(0); + }); + }) + }); +}); -- GitLab