All files / app/routes/api/group/group-id/car/car-id/delete delete-car-controller.unit.spec.ts

100% Statements 36/36
100% Branches 0/0
100% Functions 8/8
100% Lines 36/36

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97  1x 1x   1x 1x 1x   1x           1x 4x 4x       4x   4x                           1x   4x   4x     1x 1x   1x   1x       1x   1x     1x     1x   1x   1x       1x   1x     1x   1x   1x   1x     1x       1x   1x     1x 1x 1x      
/* eslint-disable @typescript-eslint/no-explicit-any */
import sinon, {assert, match} from 'sinon';
import {CarService} from '../../../../../../../models';
import {Response} from 'express';
import {expect} from 'chai';
import {deleteCarController} from './delete-car-controller';
import {BadRequestError} from '../../../../../../../errors';
 
describe('deleteCarController', function() {
  let deleteStub: sinon.SinonStub;
  let resStub: Partial<Response>;
  let nextStub: sinon.SinonStub;
  let req: any;
 
  beforeEach(function() {
    deleteStub = sinon.stub(CarService, 'delete');
    resStub = {
      status: sinon.stub().returnsThis(),
      send: sinon.stub().returnsThis(),
    };
    nextStub = sinon.stub();
 
    req = {
      params: {
        groupId: '21',
        carId: '42',
      },
      user: {
        username: 'TEST_USER',
        email: 'TEST@MAIL.COM',
        id: 11,
        isBetaUser: false,
      } as Express.User,
    };
  });
 
  afterEach(function() {
    // Next should never be called
    assert.notCalled(nextStub);
 
    sinon.restore();
  });
 
  describe('throws BadRequestError if ', function() {
    it('groupId can\'t be parsed to int', async function() {
      // Remove groupId
      req.params.groupId = undefined;
 
      await expect(deleteCarController(req, resStub as any, nextStub))
          .to.be.rejectedWith(BadRequestError);
 
      // Change groupId to be not parsable
      req.params.groupId = 'NONONO';
 
      await expect(deleteCarController(req, resStub as any, nextStub))
          .to.be.rejectedWith(BadRequestError);
 
      assert.notCalled(deleteStub);
    });
 
    it('carId can\'t be parsed to int', async function() {
      // Remove carId
      req.params.carId = undefined;
 
      await expect(deleteCarController(req, resStub as any, nextStub))
          .to.be.rejectedWith(BadRequestError);
 
      // Change carId to be not parsable
      req.params.carId = 'NONONO';
 
      await expect(deleteCarController(req, resStub as any, nextStub))
          .to.be.rejectedWith(BadRequestError);
 
      assert.notCalled(deleteStub);
    });
    it('user missing on request', async function() {
      // Remove carId
      req.user = undefined;
 
      await expect(deleteCarController(req, resStub as any, nextStub))
          .to.be.rejectedWith(BadRequestError);
 
      assert.notCalled(deleteStub);
    });
  });
 
  it('calls CarService.delete with correct arguments and responses with ' +
    'status code 204', async function() {
    await expect(deleteCarController(req, resStub as any, nextStub))
        .to.be.eventually.fulfilled;
 
    assert.calledOnceWithExactly(deleteStub, match(req.user), 21, 42);
    assert.calledOnceWithExactly(resStub.status as any, 204);
    assert.calledOnce(resStub.send as any);
  });
});