All files / app/routes/api/group/create create-group-controller.unit.spec.ts

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

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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133  1x 1x 1x 1x 1x   1x           1x 2x     1x 2x     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           1x         1x         1x           1x       1x       1x     1x 1x 1x 1x      
/* eslint-disable @typescript-eslint/no-explicit-any */
import {GroupService} from '../../../../models';
import sinon, {match} from 'sinon';
import createGroupController from './create-group-controller';
import {expect} from 'chai';
import {BadRequestError} from '../../../../errors';
 
describe('CreateGroupController', function() {
  let req: any;
  let res: any;
  let next: any;
  let createGroupStub: sinon.SinonStub;
 
  beforeEach(function() {
    createGroupStub = sinon.stub(GroupService, 'create');
  });
 
  afterEach(function() {
    sinon.restore();
  });
 
  it('creates group and responses with group data and 201', async function() {
    // Create fake user object
    const user = {
      id: 77,
    };
 
    // Create body object
    const body = {
      name: 'TEST GROUP',
      description: 'TEST DESC',
    };
 
    const group = {
      name: body.name,
      description: body.description,
      ownerId: user.id,
    };
 
    // Stub create group method
    createGroupStub.resolves(group as any);
 
    // Create fake request
    req = {
      body,
      user,
    };
 
    next = sinon.stub();
 
    res = {};
    res.status = sinon.stub().returns(res);
    res.send = sinon.stub();
 
    await createGroupController(req, res, next);
 
    sinon.assert.calledOnceWithExactly(
        createGroupStub,
        user,
        match({
          name: group.name,
          description: group.description,
        }),
    );
    sinon.assert.calledOnceWithExactly(res.status, 201);
    sinon.assert.calledOnce(res.send);
    sinon.assert.notCalled(next);
    sinon.assert.calledOnceWithExactly(res.send, group);
  });
 
  it('throws BadRequestError if request data is wrong', async function() {
    // Create mocks
    res = {};
    res.status = sinon.stub();
    res.send = sinon.stub();
    next = sinon.stub();
 
    const user = {
      id: 77,
    };
 
    /*
     * Test if error when name is missing
     */
    const nameMissing = {
      description: 'TEST DESC',
    };
 
    req = {
      body: nameMissing,
      user,
    };
 
    await expect(createGroupController(req, res, next))
        .to.eventually.be.rejectedWith(BadRequestError);
 
    /*
     * Test if error when description is defined but not a string
     */
    const descriptionWrong = {
      name: 'GROUP_NAME',
      description: 1,
    };
 
    req = {
      body: descriptionWrong,
      user,
    };
 
    await expect(createGroupController(req, res, next))
        .to.eventually.be.rejectedWith(BadRequestError);
 
    /*
     * Test if error when user is missing
     */
    const correctBody = {
      name: 'GROUP_NAME',
      description: 'GORUP_DESC',
    };
    req = {
      body: correctBody,
    };
 
    await expect(createGroupController(req, res, next))
        .to.eventually.be.rejectedWith(BadRequestError);
 
    sinon.assert.notCalled(createGroupStub);
    sinon.assert.notCalled(res.status);
    sinon.assert.notCalled(res.send);
    sinon.assert.notCalled(next);
  });
});