All files / app/routes/api/user/invite/get-all get-all-invites-controller.unit.spec.ts

100% Statements 35/35
100% Branches 0/0
100% Functions 5/5
100% Lines 35/35

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 98 99 100 101 102  1x 1x 1x 1x 1x 1x   1x   1x         1x 3x     1x   1x           1x                             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 {getAllInvitesController} from './get-all-invites-controller';
import {NotLoggedInError} from '../../../../../errors';
import {InviteService} from '../../../../../models';
import chai, {expect} from 'chai';
import chaiAsPromised from 'chai-as-promised';
 
chai.use(chaiAsPromised);
 
describe('GetAllInvitesController', function() {
  let req: any;
  let res: any;
  let next: any;
 
  afterEach(function() {
    sinon.restore();
  });
 
  it('calls InviteRepository with correct parameters ' +
  'and sends response', async function() {
    req = {
      user: {
        id: 4,
      },
    };
 
    const invites = [
      {
        userId: req.user.id,
        groupId: 1,
      },
      {
        userId: req.user.id,
        groupId: 2,
      },
      {
        userId: req.user.id,
        groupId: 3,
      },
    ];
 
    const inviteServiceFindAllForUserStub = sinon.stub(InviteService,
        'findAllForUser').resolves(invites as any);
 
    res = {};
    res.send = sinon.stub();
 
    await getAllInvitesController(req, res, next);
 
    assert.calledOnceWithExactly(
        inviteServiceFindAllForUserStub,
        req.user,
        req.user.id,
    );
 
    assert.calledOnceWithExactly(res.send, match({
      invites,
    }));
  });
 
  it('forwards error to next if InviteRepository ' +
  'throws one', function() {
    req = {
      user: {
        id: 4,
      },
    };
 
    const error = new Error('TEST');
 
    const inviteServiceFindAllForUserStub = sinon.stub(InviteService,
        'findAllForUser').rejects(error);
 
    res = {};
    res.send = sinon.stub();
    next = sinon.stub();
 
    expect(getAllInvitesController(req, res, next))
        .to.eventually.be.rejectedWith(error);
 
    assert.calledOnceWithExactly(
        inviteServiceFindAllForUserStub,
        req.user,
        req.user.id,
    );
 
    assert.notCalled(res.send);
  });
 
  it('calls next with NotLoggedInError if user ' +
  'is missing on request', function() {
    req = {};
    res = {};
    res.send = sinon.stub();
    next = sinon.stub();
 
    expect(getAllInvitesController(req, res, next))
        .to.eventually.be.rejectedWith(NotLoggedInError);
  });
});