Advertisement
Guest User

comments.service.spec -snippet

a guest
May 20th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.   describe('updateComment', () => {
  2.     it('should call commentsRepository.findOne() once with correct query', async () => {
  3.       // Arrange
  4.       const testPostId: string = 'id1';
  5.       const testCommentId: string = 'com';
  6.       let testUserEntity: User;
  7.       const testUpdateCommentDTO: UpdateCommentDTO = {
  8.         message: 'blabla',
  9.       };
  10.       const testShowCommentDTOarray: ShowCommentDTO[] = [{
  11.         id: 'com',
  12.         message: 'blabla',
  13.         user: testUserEntity,
  14.         commentLikes: 0,
  15.         commentDislikes: 1,
  16.         createdOn: new Date(),
  17.         UpdatedDateColumn: new Date(),
  18.       }];
  19.       const testCommentEntity: Comment = {
  20.         includes: null,
  21.         id: 'com', // same as the DTO of the comment
  22.         message: 'blabla',
  23.         user: Promise.resolve(testUserEntity),
  24.         post: null,
  25.         commentsLikesDislikes: null,
  26.         createdOn: new Date(),
  27.         UpdatedDateColumn: new Date(),
  28.         isDeleted: false,
  29.       };
  30.       const testCommentEntityArray: Comment[] = [{
  31.           includes: null,
  32.           id: 'com', // same as the DTO of the comment
  33.           message: 'blabla',
  34.           user: Promise.resolve(testUserEntity),
  35.           post: null,
  36.           commentsLikesDislikes: null,
  37.           createdOn: new Date(),
  38.           UpdatedDateColumn: new Date(),
  39.           isDeleted: false,
  40.       }];
  41.       testUserEntity = {
  42.         id: '1',
  43.         username: 'Кирчо',
  44.         email: 'kircho@abv.bg',
  45.         password: 'kirchoTheBadVibesKiller00!',
  46.         firstName: 'Greatest',
  47.         lastName: 'Ever',
  48.         friended: null,
  49.         beFriendedBy: null,
  50.         posts: null,
  51.         comments: Promise.resolve(testCommentEntityArray),
  52.         postLikesDislikes: null,
  53.         commentsLikesDislikes: null,
  54.         activities: null,
  55.         roles: null,
  56.         createdOn: new Date(),
  57.         UpdatedDateColumn: new Date(),
  58.         isDeleted: false,
  59.       };
  60.  
  61.       const commentsRepoFindOne = jest.spyOn(commentsRepository, 'findOne')
  62.       .mockImplementation(() => Promise.resolve(testCommentEntity));
  63.      
  64.       const commentsRepoSave = jest.spyOn(commentsRepository, 'save')
  65.       .mockImplementation(() => Promise.resolve(testCommentEntity)); // Or Entity ...
  66.  
  67.       const converterServiceConvertToShowCommentDTO = jest.spyOn(converterService, 'convertToShowCommentDTO')
  68.       .mockImplementation(() => Promise.resolve(testShowCommentDTOarray));
  69.  
  70.       const query = {
  71.         id: 'com',
  72.         isDeleted: false,
  73.       };
  74.       // Act
  75.       await service.updateComment(testUpdateCommentDTO, testCommentId, testUserEntity);
  76.       // Assert
  77.       expect(commentsRepoFindOne).toBeCalledTimes(1);
  78.       expect(commentsRepoFindOne).toBeCalledWith(query);
  79.  
  80.       commentsRepoFindOne.mockRestore();
  81.       commentsRepoSave.mockRestore();
  82.       converterServiceConvertToShowCommentDTO.mockRestore();
  83.     });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement