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

100% Statements 47/47
100% Branches 0/0
100% Functions 6/6
100% Lines 46/46

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  1x 1x 1x 1x   1x   1x             1x 2x 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 sinon, {createSandbox} from 'sinon';
import {UserService} from '../../../models';
import {expect} from 'chai';
import {userProfilePicController} from './user-controller';
 
const sandbox = createSandbox();
 
describe('GetUserProfilePicture', function() {
  let req: any;
  let res: any;
  let next: any;
  let user: any;
  let getProfilePic: sinon.SinonStub;
 
  beforeEach(function() {
    getProfilePic = sandbox.stub(UserService, 'getProfilePicture');
    user = {
      id: 42,
    };
  });
 
  afterEach(() => {
    sandbox.restore();
  });
 
  it('returns the profile picture data', async function() {
    const userId = 42;
 
    // Create fake data
    const data = 'TEST';
    const fakePic: any = sandbox.stub();
    fakePic.data = data;
    getProfilePic.resolves(fakePic as any);
 
    // Stub express function
    req = sandbox.stub();
    res = sandbox.stub();
    next = sandbox.stub();
    req.params = {
      userId,
    };
    req.user = user;
    res.type = sandbox.stub().returnsThis();
    res.send = sandbox.stub();
 
 
    await userProfilePicController(req, res, next);
 
    sandbox.assert.calledOnce(getProfilePic);
    sandbox.assert.calledWith(getProfilePic, user, userId);
    sandbox.assert.calledOnce(res.type);
    sandbox.assert.calledWith(res.type, 'image/jpeg');
    sandbox.assert.calledOnce(res.send);
    sandbox.assert.calledWith(res.send, data);
    sandbox.assert.notCalled(next);
    expect(data).to.equal(data);
  });
 
  it('calls next with error thrown by ProfilePic.findOne', async function() {
    const userId = 44;
 
    const err = new Error('TEST ERROR');
    getProfilePic.callsFake(() => Promise.reject(err));
 
    // Stub express function
    req = sandbox.stub();
    res = sandbox.stub();
    req.params = {
      userId,
    };
    req.user = user;
    next = sandbox.stub();
 
    await expect(userProfilePicController(req, res, next))
        .to.eventually.be.rejectedWith(err);
 
    sandbox.assert.calledOnce(getProfilePic);
    sandbox.assert.calledWith(getProfilePic, user, userId);
    sandbox.assert.notCalled(next);
  });
});