diff --git a/src/Controllers/Car.controller.ts b/src/Controllers/Car.controller.ts index b3860c5..8b72431 100644 --- a/src/Controllers/Car.controller.ts +++ b/src/Controllers/Car.controller.ts @@ -6,7 +6,7 @@ export default class CarController { private _req: Request; private _res: Response; private _next: NextFunction; - private _carService: CarService; + public _carService: CarService; constructor(req: Request, res: Response, next: NextFunction) { this._req = req; diff --git a/src/Controllers/Motorcycle.controller.ts b/src/Controllers/Motorcycle.controller.ts index a65ab04..6c0c36e 100644 --- a/src/Controllers/Motorcycle.controller.ts +++ b/src/Controllers/Motorcycle.controller.ts @@ -6,7 +6,7 @@ export default class MotorcycleController { private req: Request; private res: Response; private next: NextFunction; - private motorcycleService: MotorcycleService; + public motorcycleService: MotorcycleService; constructor(req: Request, res: Response, next: NextFunction) { this.req = req; diff --git a/tests/unit/Controllers/cars.test.ts b/tests/unit/Controllers/cars.test.ts new file mode 100644 index 0000000..303c81d --- /dev/null +++ b/tests/unit/Controllers/cars.test.ts @@ -0,0 +1,180 @@ +import sinon from 'sinon'; +import { Request, NextFunction } from 'express'; +import CarController from '../../../src/Controllers/Car.controller'; + +const reqCar = { + model: 'Gol', + year: 2002, + color: 'Write', + status: true, + buyValue: 10.000, + doorsQty: 4, + seatsQty: 5, +}; + +const res = { + status: sinon.stub().returnsThis(), + json: sinon.stub().returnsThis(), +}; +const next = sinon.stub(); + +describe('CarController', function () { + it('should create a car', async function () { + const req = { body: reqCar }; + + const carController = new CarController(req as Request, res as any, next as NextFunction); + + const car = reqCar; + + const carServiceMock = sinon.mock(carController._carService); + carServiceMock.expects('create').once().withArgs(req.body).resolves(car); + + await carController.create(); + + carServiceMock.verify(); + carServiceMock.restore(); + }); + + it('should find all cars', async function () { + const req = {}; + + const carController = new CarController(req as Request, res as any, next as NextFunction); + + const cars = [reqCar, + { + model: 'Uno', + year: 2002, + color: 'Write', + status: true, + buyValue: 10.000, + doorsQty: 4, + seatsQty: 5, + }, + ]; + + const carServiceMock = sinon.mock(carController._carService); + carServiceMock.expects('findAll').resolves(cars); + + await carController.findAll(); + + carServiceMock.verify(); + carServiceMock.restore(); + }); + + it('should find a car by id', async function () { + const req = { params: { id: '5f5e9c9b6b8c1f0d1c0d3f3c' } }; + + const carController = new CarController(req as any, res as any, next as NextFunction); + + const car = reqCar; + + const carServiceMock = sinon.mock(carController._carService); + carServiceMock.expects('findById').withArgs(req.params.id).resolves(car); + + await carController.findById(); + + carServiceMock.verify(); + carServiceMock.restore(); + }); + + it('should update a car', async function () { + const req = { params: { id: '5f5e9c9b6b8c1f0d1c0d3f3c' }, + body: reqCar }; + + const carController = new CarController(req as any, res as any, next as NextFunction); + + const car = reqCar; + + const carServiceMock = sinon.mock(carController._carService); + carServiceMock.expects('updateById').withArgs(req.params.id, req.body).resolves(car); + + await carController.updateById(); + + carServiceMock.verify(); + carServiceMock.restore(); + }); + + it('should delete a car', async function () { + const req = { params: { id: '5' } }; + + const carController = new CarController(req as any, res as any, next as NextFunction); + + const carServiceMock = sinon.mock(carController._carService); + + await carController.deleteById(); + + carServiceMock.verify(); + carServiceMock.restore(); + }); + + it('should return a error when create a car', async function () { + const req = { body: reqCar }; + + const carController = new CarController(req as Request, res as any, next as NextFunction); + + const carServiceMock = sinon.mock(carController._carService); + carServiceMock.expects('create').once().withArgs(req.body).rejects(); + + await carController.create(); + + carServiceMock.verify(); + carServiceMock.restore(); + }); + + it('should return a error when find all cars', async function () { + const req = {}; + + const carController = new CarController(req as Request, res as any, next as NextFunction); + + const carServiceMock = sinon.mock(carController._carService); + carServiceMock.expects('findAll').rejects(); + + await carController.findAll(); + + carServiceMock.verify(); + carServiceMock.restore(); + }); + + it('should return a error when find a car by id', async function () { + const req = { params: { id: '5f5e9c9b6b8c1f0d1c0d3f3c' } }; + + const carController = new CarController(req as any, res as any, next as NextFunction); + + const carServiceMock = sinon.mock(carController._carService); + carServiceMock.expects('findById').withArgs(req.params.id).rejects(); + + await carController.findById(); + + carServiceMock.verify(); + carServiceMock.restore(); + }); + + it('should return a error when update a car', async function () { + const req = { params: { id: '5f5e9c9b6b8c1f0d1c0d3f3c' }, + body: reqCar }; + + const carController = new CarController(req as any, res as any, next as NextFunction); + + const carServiceMock = sinon.mock(carController._carService); + carServiceMock.expects('updateById').withArgs(req.params.id, req.body).rejects(); + + await carController.updateById(); + + carServiceMock.verify(); + carServiceMock.restore(); + }); + + it('should return a error when delete a car', async function () { + const req = { params: { id: '5f5e9c9b6b8c1f0d1c0d3f3c' } }; + + const carController = new CarController(req as any, res as any, next as NextFunction); + + const carServiceMock = sinon.mock(carController._carService); + carServiceMock.expects('deleteById').withArgs(req.params.id).rejects(); + + await carController.deleteById(); + + carServiceMock.verify(); + carServiceMock.restore(); + }); +}); \ No newline at end of file diff --git a/tests/unit/Controllers/motorcycle.test.ts b/tests/unit/Controllers/motorcycle.test.ts new file mode 100644 index 0000000..9065ac1 --- /dev/null +++ b/tests/unit/Controllers/motorcycle.test.ts @@ -0,0 +1,222 @@ +import sinon from 'sinon'; +import { Request, NextFunction } from 'express'; +import MotorcycleController from '../../../src/Controllers/Motorcycle.controller'; + +const reqMotorcycle = { + model: 'Gol', + year: 2002, + color: 'Write', + status: true, + buyValue: 10.000, + category: 'Sport', + engineCapacity: 1.000, +}; + +const res = { + status: sinon.stub().returnsThis(), + json: sinon.stub().returnsThis(), +}; + +const next = sinon.stub(); + +describe('MotorcycleController', function () { + it('should create a motorcycle', async function () { + const req = { body: reqMotorcycle }; + + const motorcycleController = new MotorcycleController( + req as Request, + res as any, + next as NextFunction, + ); + + const motorcycle = { reqMotorcycle }; + + const motorcycleServiceMock = sinon.mock(motorcycleController.motorcycleService); + motorcycleServiceMock.expects('create').once().withArgs(req.body).resolves(motorcycle); + + await motorcycleController.create(); + + motorcycleServiceMock.verify(); + motorcycleServiceMock.restore(); + }); + + it('should find all motorcycles', async function () { + const req = {}; + + const motorcycleController = new MotorcycleController( + req as Request, + res as any, + next as NextFunction, + ); + + const motorcycles = [ + { reqMotorcycle }, + { + model: 'Uno', + year: 2002, + color: 'Write', + status: true, + buyValue: 10.000, + capacity: 'Sport', + engineCapacity: 800, + }, + ]; + + const motorcycleMock = sinon.mock(motorcycleController.motorcycleService); + motorcycleMock.expects('findAll').once().resolves(motorcycles); + + await motorcycleController.findAll(); + + motorcycleMock.verify(); + motorcycleMock.restore(); + }); + + it('should find a motorcycle by id', async function () { + const req = { params: { id: '1' } }; + + const motorcycleController = new MotorcycleController( + req as any, + res as any, + next as NextFunction, + ); + + const motorcycle = { reqMotorcycle }; + + const motorcycleMock = sinon.mock(motorcycleController.motorcycleService); + motorcycleMock.expects('findById').once().withArgs(req.params.id).resolves(motorcycle); + + await motorcycleController.findById(); + + motorcycleMock.verify(); + motorcycleMock.restore(); + }); + + it('should update a motorcycle', async function () { + const req = { params: { id: '1' }, body: { reqMotorcycle } }; + + const motorcycleController = new MotorcycleController( + req as any, + res as any, + next as NextFunction, + ); + + const motorcycle = { reqMotorcycle }; + + const motorcycleMock = sinon.mock(motorcycleController.motorcycleService); + motorcycleMock.expects('updateById') + .once().withArgs(req.params.id, req.body).resolves(motorcycle); + + await motorcycleController.updateById(); + + motorcycleMock.verify(); + motorcycleMock.restore(); + }); + + it('should delete a motorcycle', async function () { + const req = { params: { id: '1' } }; + + const motorcycleController = new MotorcycleController( + req as any, + res as any, + next as NextFunction, + ); + + const motorcycleMock = sinon.mock(motorcycleController.motorcycleService); + + await motorcycleController.deleteById(); + + motorcycleMock.verify(); + motorcycleMock.restore(); + }); + + it('should return a error when create a motorcycle', async function () { + const req = { body: { reqMotorcycle } }; + + const motorcycleController = new MotorcycleController( + req as any, + res as any, + next as NextFunction, + ); + + const motorcycleMock = sinon.mock(motorcycleController.motorcycleService); + motorcycleMock.expects('create').once().withArgs(req.body).rejects(); + + await motorcycleController.create(); + + motorcycleMock.verify(); + motorcycleMock.restore(); + }); + + it('should return a error when find all motorcycles', async function () { + const req = {}; + + const motorcycleController = new MotorcycleController( + req as any, + res as any, + next as NextFunction, + ); + + const motorcycleMock = sinon.mock(motorcycleController.motorcycleService); + motorcycleMock.expects('findAll').once().rejects(); + + await motorcycleController.findAll(); + + motorcycleMock.verify(); + motorcycleMock.restore(); + }); + + it('should return a error when find a motorcycle by id', async function () { + const req = { params: { id: '1' } }; + + const motorcycleController = new MotorcycleController( + req as any, + res as any, + next as NextFunction, + ); + + const motorcycleMock = sinon.mock(motorcycleController.motorcycleService); + motorcycleMock.expects('findById').once().withArgs(req.params.id).rejects(); + + await motorcycleController.findById(); + + motorcycleMock.verify(); + motorcycleMock.restore(); + }); + + it('should return a error when update a motorcycle', async function () { + const req = { params: { id: '1' }, body: { reqMotorcycle } }; + + const motorcycleController = new MotorcycleController( + req as any, + res as any, + next as NextFunction, + ); + + const motorcycleMock = sinon.mock(motorcycleController.motorcycleService); + motorcycleMock.expects('updateById') + .once().withArgs(req.params.id, req.body).rejects(); + + await motorcycleController.updateById(); + + motorcycleMock.verify(); + motorcycleMock.restore(); + }); + + it('should return a error when delete a motorcycle', async function () { + const req = { params: { id: '1' } }; + + const motorcycleController = new MotorcycleController( + req as any, + res as any, + next as NextFunction, + ); + + const motorcycleMock = sinon.mock(motorcycleController.motorcycleService); + motorcycleMock.expects('deleteById').once().withArgs(req.params.id).rejects(); + + await motorcycleController.deleteById(); + + motorcycleMock.verify(); + motorcycleMock.restore(); + }); +}); \ No newline at end of file diff --git a/tests/unit/Services/cars.test.ts b/tests/unit/Services/cars.test.ts index c7410d0..645db05 100644 --- a/tests/unit/Services/cars.test.ts +++ b/tests/unit/Services/cars.test.ts @@ -38,7 +38,7 @@ describe('Car Service', function () { expect(car).to.be.deep.equal({ id, ...reqCar }); }); - it('not exists car', async function () { + it('should not create a car', async function () { sinon.stub(Model, 'create').resolves(null); const carService = new CarService(); @@ -47,7 +47,7 @@ describe('Car Service', function () { expect(car).to.be.deep.equal(null); }); - it('test method GET with function "findAll"', async function () { + it('should find all cars', async function () { sinon.stub(Model, 'find').resolves([{ id, ...reqCar }]); const carService = new CarService(); @@ -56,7 +56,7 @@ describe('Car Service', function () { expect(car).to.be.deep.equal([{ id, ...reqCar }]); }); - it('test method GET with function "finById" when id exists', async function () { + it('should find a car by id when id exists', async function () { sinon.stub(Model, 'findOne').resolves({ id, ...reqCar }); const carService = new CarService(); @@ -67,14 +67,14 @@ describe('Car Service', function () { expect(car).to.be.deep.equal({ status: 200, response }); }); - it('test method GET with function "finById" when id is not valid', async function () { + it('should find a car by id when id is not valid', async function () { const carService = new CarService(); const car = await carService.findById('1'); expect(car).to.be.deep.equal({ status: 422, response: invalid }); }); - it('test method GET with function "finById" when car not exists', async function () { + it('should find a car by id when id does not exist', async function () { sinon.stub(Model, 'findOne').resolves(null); const carService = new CarService(); @@ -83,7 +83,7 @@ describe('Car Service', function () { expect(car).to.be.deep.equal({ status: 404, response: notFound }); }); - it('test method PUT with function "updateById" when id exists', async function () { + it('should find a car by id and update when id exists', async function () { sinon.stub(Model, 'findOneAndUpdate').resolves({ id, ...reqCar }); const carService = new CarService(); @@ -99,14 +99,14 @@ describe('Car Service', function () { response: { id, ...reqCar } }); }); - it('test method PUT with function "updateById" when id is not valid', async function () { + it('should find a car by id and update when id is not valid', async function () { const carService = new CarService(); const car = await carService.updateById('1', reqCar); expect(car).to.be.deep.equal({ status: 422, response: invalid }); }); - it('test method PUT with function "updateById" when car not exists', async function () { + it('should find a car by id and update when id does not exist', async function () { sinon.stub(Model, 'findOneAndUpdate').resolves(null); const carService = new CarService(); @@ -115,7 +115,7 @@ describe('Car Service', function () { expect(car).to.be.deep.equal({ status: 404, response: notFound }); }); - it('test method DELETE with function "deleteById" when id exists', async function () { + it('should find a car by id and delete when id exists', async function () { sinon.stub(Model, 'findOneAndDelete').resolves({ id, ...reqCar }); const carService = new CarService(); @@ -131,14 +131,14 @@ describe('Car Service', function () { response: { id, ...reqCar } }); }); - it('test method DELETE with function "deleteById" when id is not valid', async function () { + it('should find a car by id and delete when id is not valid', async function () { const carService = new CarService(); const car = await carService.deleteById('1'); expect(car).to.be.deep.equal({ status: 422, response: invalid }); }); - it('test method DELETE with function "deleteById" when car not exists', async function () { + it('should find a car by id and delete when id does not exist', async function () { sinon.stub(Model, 'findOneAndDelete').resolves(null); const carService = new CarService();