UNPKG

@on-time/core

Version:

core business logic for OT API

100 lines (97 loc) 4.29 kB
const { expect } = require('chai'); const sinon = require('sinon'); const Controller = require('../src/Controller'); describe('Core Controller Tests', () => { let fakeService; beforeEach(() => { fakeService = { create: sinon.spy(), getAll: sinon.spy(), delete: sinon.spy(), update: sinon.spy(), }; }); it('should return a controller object', () => { const controller = new Controller(fakeService); expect(controller).to.have.property('get'); expect(controller).to.have.property('post'); expect(controller).to.have.property('delete'); expect(controller).to.have.property('put'); }); describe('get Method Tests', () => { let controller; let fakeHttpRequestObject; beforeEach(() => { controller = new Controller(fakeService); fakeHttpRequestObject = { body: {} } }); it('should return data received from the service', async () => { const response = await controller.get(fakeHttpRequestObject); expect(response.body).to.eql(fakeService.getAll()); expect(response.status).to.eql(200) }); it('should return error if service layer throws error', async () => { fakeService.getAll = sinon.stub().throws(Error('Service Error')); const response = await controller.get(fakeHttpRequestObject); expect(response.error.message).to.equal('Service Error'); expect(response.status).to.equal(500) }); }); describe('Create Method Tests', () => { let controller; let fakeHttpRequestObject; beforeEach(() => { controller = new Controller(fakeService); fakeHttpRequestObject = { body: {} } }); it('should return data received from the service', async () => { const response = await controller.post(fakeHttpRequestObject); expect(response.body).to.eql(fakeService.create()); expect(response.status).to.eql(201) }); it('should return error if service throws an error', async () => { fakeService.create = sinon.stub().throws(Error('Service Error')); const response = await controller.post(fakeHttpRequestObject); expect(response.error.message).to.equal('Service Error'); expect(response.status).to.equal(500) }); }); describe('put Method Tests', () => { let controller; let fakeHttpRequestObject; beforeEach(() => { controller = new Controller(fakeService); fakeHttpRequestObject = { body: {}, params: { id: '' } } }); it('should return data received from the service', async () => { const response = await controller.put(fakeHttpRequestObject); expect(response.body).to.eql(fakeService.update()); expect(response.status).to.eql(200) }); it('should return error if service throws an error', async () => { fakeService.update = sinon.stub().throws(Error('Service Error')); const response = await controller.put(fakeHttpRequestObject); expect(response.error.message).to.equal('Service Error'); expect(response.status).to.equal(500) }); }); describe('Delete Method Tests', () => { let controller; let fakeHttpRequestObject; beforeEach(() => { controller = new Controller(fakeService); fakeHttpRequestObject = { body: {}, params: { id: '' } } }); it('should return data received from the service', async () => { const response = await controller.delete(fakeHttpRequestObject); expect(response.body).to.eql(fakeService.delete()); expect(response.status).to.eql(200) }); it('should return error if service throws an error', async () => { fakeService.delete = sinon.stub().throws(Error('Service Error')); const response = await controller.delete(fakeHttpRequestObject); expect(response.error.message).to.equal('Service Error'); expect(response.status).to.equal(500) }); }); });