All files / app/models/user user-repository.unit.spec.ts

100% Statements 74/74
100% Branches 0/0
100% Functions 16/16
100% Lines 74/74

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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211  1x 1x 1x 1x 1x 1x   1x   1x     1x 8x     1x 8x     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 3x     1x 1x 1x   1x       1x     1x 1x 1x       1x   1x       1x     1x 1x 1x       1x 1x       1x       1x       1x     1x 2x     1x 1x 1x 1x       1x   1x         1x   1x             1x   1x 1x   1x   1x     1x                
/* eslint-disable @typescript-eslint/no-explicit-any */
import sinon, {assert} from 'sinon';
import {ProfilePic, User} from '../../models';
import {expect} from 'chai';
import {UserRepository} from './user-repository';
import sequelize, {Transaction} from 'sequelize';
import {ProfilePictureNotFoundError, UserNotFoundError} from '../../errors';
import {RepositoryQueryOptions} from '../../../typings';
const Op = sequelize.Op;
 
describe('UserRepository', function() {
  let userFindAllStub: sinon.SinonStub<any, any>;
 
  beforeEach(function() {
    userFindAllStub = sinon.stub(User, 'findAll');
  });
 
  afterEach(function() {
    sinon.restore();
  });
 
  describe('findLimitedWithFilter', function() {
    it('throws TypeError if startsWith is not a string', async function() {
      const startsWith = 123;
      const limit = 10;
 
      await expect(UserRepository.findLimitedWithFilter(
        startsWith as any,
        limit,
      ))
          .to.be.eventually.rejectedWith(TypeError);
 
      assert.notCalled(userFindAllStub);
    });
 
    it('throws TypeError if limit is not a number', async function() {
      const startsWith = 'test';
      const limit = '123';
 
      await expect(UserRepository.findLimitedWithFilter(
          startsWith,
         limit as any,
      ))
          .to.be.eventually.rejectedWith(TypeError);
 
      assert.notCalled(userFindAllStub);
    });
 
    it('calls User.findAll with correct parameters', async function() {
      const startsWith = 'test';
      const limit = 10;
 
      const users = [
        {
          id: 1,
          username: 'test1',
        },
        {
          id: 2,
          username: 'test2',
        },
        {
          id: 3,
          username: 'test3',
        },
      ];
 
      userFindAllStub.resolves(users as any);
 
      const options = {
        transaction: {},
      };
 
      const actual = await expect(UserRepository.findLimitedWithFilter(
          startsWith,
         limit as any,
         options as any,
      ))
          .to.be.fulfilled;
 
      expect(actual).to.be.equal(users);
 
      assert.calledOnce(userFindAllStub);
 
      const parameter = userFindAllStub.firstCall.args[0];
      expect(parameter).to.exist;
 
      const expectedParameter: any = {
        where: {
          [Op.and]: [
            {
              deletedAt: {
                [Op.is]: null,
              },
              username: {
                [Op.startsWith]: startsWith,
              },
            },
          ],
        },
        order: [['username', 'ASC']],
        attributes: User.simpleAttributes,
        limit,
        transaction: options.transaction,
      };
 
      expect(parameter).to.be.eql(expectedParameter);
    });
  });
 
  describe('findById', function() {
    let userStub: sinon.SinonStub;
 
    beforeEach(function() {
      userStub = sinon.stub(User, 'findByPk');
    });
 
    it('throws UserNotFoundError if not user is found', async function() {
      userStub.resolves(null);
      const id = 2;
 
      await expect(
          UserRepository.findById(id),
      ).to.eventually.be.rejectedWith(UserNotFoundError);
 
      assert.calledOnceWithExactly(userStub, id, undefined);
    });
 
    it('returns the user', async function() {
      const id = 2;
      const user = {
        id,
        username: 'TEST',
      };
      userStub.resolves(user);
 
      await expect(
          UserRepository.findById(id),
      ).to.eventually.eql(user);
 
      assert.calledOnceWithExactly(userStub, id, undefined);
    });
 
    it('forwards the given options to the database query', async function() {
      const id = 2;
      const user = {
        id,
        username: 'TEST',
      };
      userStub.resolves(user);
      const options: Partial<RepositoryQueryOptions> = {
        transaction: 5 as unknown as Transaction,
      };
 
      await expect(
          UserRepository.findById(id, options),
      ).to.eventually.eql(user);
 
      assert.calledOnceWithExactly(userStub, id, options);
    });
  });
 
  describe('findProfilePictureById', function() {
    let pbFindByPkStub: sinon.SinonStub;
 
    beforeEach(function() {
      pbFindByPkStub = sinon.stub(ProfilePic, 'findByPk');
    });
 
    it('calls ProfilePic.findByPk with correct arguments', async function() {
      const fakePB = 'TEST';
      const userId = 10;
      const options = {
        transaction: 'T',
      };
 
      pbFindByPkStub.resolves(fakePB);
 
      const actual = await UserRepository.findProfilePictureById(
          userId,
          options as any,
      );
 
      expect(actual).to.eq(fakePB);
 
      assert.calledOnceWithExactly(
          pbFindByPkStub,
          userId,
          sinon.match({transaction: options.transaction}),
      );
    });
 
    it('throws ProfilePictureNotFoundError if no profile ' +
      'picture found', async function() {
      const fakePB = null;
      const userId = 10;
 
      pbFindByPkStub.resolves(fakePB);
 
      await expect(UserRepository.findProfilePictureById(userId))
          .to.be.eventually.be.rejectedWith(ProfilePictureNotFoundError);
 
      assert.calledOnceWithExactly(
          pbFindByPkStub,
          userId,
          sinon.match.any,
      );
    });
  });
});