Skip to content

Commit

Permalink
✨ 100% dos testes da camada service de cars e motorcycle
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielaMoura25 committed Feb 15, 2023
1 parent 8998a02 commit cf877a7
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 18 deletions.
86 changes: 70 additions & 16 deletions tests/unit/Services/cars.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 });
});
Expand All @@ -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');
Expand All @@ -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 });
});
});
79 changes: 77 additions & 2 deletions tests/unit/Services/motorcycle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });

Expand All @@ -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 }]);

Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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 });
});
});

0 comments on commit cf877a7

Please sign in to comment.