diff --git a/tests/unit/Services/cars.test.ts b/tests/unit/Services/cars.test.ts index 743f443..c7410d0 100644 --- a/tests/unit/Services/cars.test.ts +++ b/tests/unit/Services/cars.test.ts @@ -14,13 +14,17 @@ const reqCar: ICar = { seatsQty: 5, }; +const invalid = { message: 'Invalid mongo id' }; +const notFound = { message: 'Car not found' }; +const id = '6348513f34c397abcad040b2'; + describe('Car Service', function () { afterEach(function () { sinon.restore(); }); it('should create a car', async function () { - sinon.stub(Model, 'create').resolves({ id: '6348513f34c397abcad040b2', ...reqCar }); + sinon.stub(Model, 'create').resolves({ id, ...reqCar }); const carService = new CarService(); const car = await carService.create(reqCar); @@ -31,26 +35,34 @@ describe('Car Service', function () { expect(car).to.have.property('year'); expect(car).to.have.property('color'); expect(car).to.have.property('status'); - expect(car).to.be.deep.equal({ id: '6348513f34c397abcad040b2', ...reqCar }); + expect(car).to.be.deep.equal({ id, ...reqCar }); + }); + + it('not exists car', async function () { + sinon.stub(Model, 'create').resolves(null); + + const carService = new CarService(); + const car = await carService.create(reqCar); + + expect(car).to.be.deep.equal(null); }); it('test method GET with function "findAll"', async function () { - sinon.stub(Model, 'find').resolves([{ id: '6348513f34c397abcad040b2', ...reqCar }]); + sinon.stub(Model, 'find').resolves([{ id, ...reqCar }]); const carService = new CarService(); const car = await carService.findAll(); - expect(car).to.be.deep.equal([{ id: '6348513f34c397abcad040b2', ...reqCar }]); + expect(car).to.be.deep.equal([{ id, ...reqCar }]); }); it('test method GET with function "finById" when id exists', async function () { - sinon.stub(Model, 'findOne').resolves({ id: '6348513f34c397abcad040b2', ...reqCar }); + sinon.stub(Model, 'findOne').resolves({ id, ...reqCar }); const carService = new CarService(); - const id = '6348513f34c397abcad040b2'; const car = await carService.findById(id); - const response = { id: '6348513f34c397abcad040b2', ...reqCar }; + const response = { id, ...reqCar }; expect(car).to.be.deep.equal({ status: 200, response }); }); @@ -59,28 +71,22 @@ describe('Car Service', function () { const carService = new CarService(); const car = await carService.findById('1'); - const response = { message: 'Invalid mongo id' }; - - expect(car).to.be.deep.equal({ status: 422, response }); + expect(car).to.be.deep.equal({ status: 422, response: invalid }); }); it('test method GET with function "finById" when car not exists', async function () { sinon.stub(Model, 'findOne').resolves(null); const carService = new CarService(); - const id = '6348513f34c397abcad040b2'; const car = await carService.findById(id); - const response = { message: 'Car not found' }; - - expect(car).to.be.deep.equal({ status: 404, response }); + expect(car).to.be.deep.equal({ status: 404, response: notFound }); }); it('test method PUT with function "updateById" when id exists', async function () { - sinon.stub(Model, 'findOneAndUpdate').resolves({ id: '6348513f34c397abcad040b2', ...reqCar }); + sinon.stub(Model, 'findOneAndUpdate').resolves({ id, ...reqCar }); const carService = new CarService(); - const id = '6348513f34c397abcad040b2'; const car = await carService.updateById(id, reqCar); expect(car).to.be.an('object'); @@ -92,4 +98,52 @@ describe('Car Service', function () { expect(car).to.be.deep.equal({ status: 200, response: { id, ...reqCar } }); }); + + it('test method PUT with function "updateById" 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 () { + sinon.stub(Model, 'findOneAndUpdate').resolves(null); + + const carService = new CarService(); + const car = await carService.updateById(id, reqCar); + + expect(car).to.be.deep.equal({ status: 404, response: notFound }); + }); + + it('test method DELETE with function "deleteById" when id exists', async function () { + sinon.stub(Model, 'findOneAndDelete').resolves({ id, ...reqCar }); + + const carService = new CarService(); + const car = await carService.deleteById(id); + + expect(car).to.be.an('object'); + expect(car.response).to.have.property('id'); + expect(car.response).to.have.property('model'); + expect(car.response).to.have.property('year'); + expect(car.response).to.have.property('color'); + expect(car.response).to.have.property('status'); + expect(car).to.be.deep.equal({ status: 204, + response: { id, ...reqCar } }); + }); + + it('test method DELETE with function "deleteById" 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 () { + sinon.stub(Model, 'findOneAndDelete').resolves(null); + + const carService = new CarService(); + const car = await carService.deleteById(id); + + expect(car).to.be.deep.equal({ status: 404, response: notFound }); + }); }); \ No newline at end of file diff --git a/tests/unit/Services/motorcycle.test.ts b/tests/unit/Services/motorcycle.test.ts index b1ea33d..6faf4dd 100644 --- a/tests/unit/Services/motorcycle.test.ts +++ b/tests/unit/Services/motorcycle.test.ts @@ -14,9 +14,16 @@ const reqMotorcycle: IMotorcycle = { engineCapacity: 1.000, }; +const notFound = { message: 'Motorcycle not found' }; +const invalid = { message: 'Invalid mongo id' }; + const id = '6348513f34c397abcad040b2'; describe('Motorcycle Service', function () { + afterEach(function () { + sinon.restore(); + }); + it('should create a motorcycle', async function () { sinon.stub(Model, 'create').resolves({ id, ...reqMotorcycle }); @@ -32,6 +39,15 @@ describe('Motorcycle Service', function () { expect(motorcycle).to.be.deep.equal({ id, ...reqMotorcycle }); }); + it('should not create a motorcycle', async function () { + sinon.stub(Model, 'create').resolves(null); + + const motorcycleService = new MotorcycleService(); + const motorcycle = await motorcycleService.create(reqMotorcycle); + + expect(motorcycle).to.be.deep.equal(null); + }); + it('should find all motorcycles', async function () { sinon.stub(Model, 'find').resolves([{ id, ...reqMotorcycle }]); @@ -41,7 +57,7 @@ describe('Motorcycle Service', function () { expect(motorcycle).to.be.deep.equal([{ id, ...reqMotorcycle }]); }); - it('should find a motorcycle by id', async function () { + it('should find a motorcycle by id when id exists', async function () { sinon.stub(Model, 'findOne').resolves({ id, ...reqMotorcycle }); const motorcycleService = new MotorcycleService(); @@ -52,7 +68,23 @@ describe('Motorcycle Service', function () { expect(motorcycle).to.be.deep.equal({ status: 200, response }); }); - it('should find a motorcycle by id and update', async function () { + it('should find a motorcycle by id when id is not valid', async function () { + const motorcycleService = new MotorcycleService(); + const motorcycle = await motorcycleService.findById('1'); + + expect(motorcycle).to.be.deep.equal({ status: 422, response: invalid }); + }); + + it('should find a motorcycle by id when id does not exist', async function () { + sinon.stub(Model, 'findOne').resolves(null); + + const motorcycleService = new MotorcycleService(); + const motorcycle = await motorcycleService.findById(id); + + expect(motorcycle).to.be.deep.equal({ status: 404, response: notFound }); + }); + + it('should find a motorcycle by id and update when id exists', async function () { sinon.stub(Model, 'findOneAndUpdate').resolves({ id, ...reqMotorcycle }); const motorcycleService = new MotorcycleService(); @@ -62,4 +94,47 @@ describe('Motorcycle Service', function () { expect(motorcycle).to.be.deep.equal({ status: 200, response }); }); + + it('should find a motorcycle by id and update when id is not valid', async function () { + const motorcycleService = new MotorcycleService(); + const motorcycle = await motorcycleService.updateById('1', reqMotorcycle); + + expect(motorcycle).to.be.deep.equal({ status: 422, response: invalid }); + }); + + it('should find a motorcycle by id and update when id does not exist', async function () { + sinon.stub(Model, 'findOneAndUpdate').resolves(null); + + const motorcycleService = new MotorcycleService(); + const motorcycle = await motorcycleService.updateById(id, reqMotorcycle); + + expect(motorcycle).to.be.deep.equal({ status: 404, response: notFound }); + }); + + it('should find a motorcycle by id and delete when id exists', async function () { + sinon.stub(Model, 'findOneAndDelete').resolves({ id, ...reqMotorcycle }); + + const motorcycleService = new MotorcycleService(); + const motorcycle = await motorcycleService.deleteById(id); + + const response = { id, ...reqMotorcycle }; + + expect(motorcycle).to.be.deep.equal({ status: 204, response }); + }); + + it('should find a motorcycle by id and delete when id is not valid', async function () { + const motorcycleService = new MotorcycleService(); + const motorcycle = await motorcycleService.deleteById('1'); + + expect(motorcycle).to.be.deep.equal({ status: 422, response: invalid }); + }); + + it('should find a motorcycle by id and delete when id does not exist', async function () { + sinon.stub(Model, 'findOneAndDelete').resolves(null); + + const motorcycleService = new MotorcycleService(); + const motorcycle = await motorcycleService.deleteById(id); + + expect(motorcycle).to.be.deep.equal({ status: 404, response: notFound }); + }); }); \ No newline at end of file