Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- describe('updateComment', () => {
- it('should call commentsRepository.findOne() once with correct query', async () => {
- // Arrange
- const testPostId: string = 'id1';
- const testCommentId: string = 'com';
- let testUserEntity: User;
- const testUpdateCommentDTO: UpdateCommentDTO = {
- message: 'blabla',
- };
- const testShowCommentDTOarray: ShowCommentDTO[] = [{
- id: 'com',
- message: 'blabla',
- user: testUserEntity,
- commentLikes: 0,
- commentDislikes: 1,
- createdOn: new Date(),
- UpdatedDateColumn: new Date(),
- }];
- const testCommentEntity: Comment = {
- includes: null,
- id: 'com', // same as the DTO of the comment
- message: 'blabla',
- user: Promise.resolve(testUserEntity),
- post: null,
- commentsLikesDislikes: null,
- createdOn: new Date(),
- UpdatedDateColumn: new Date(),
- isDeleted: false,
- };
- const testCommentEntityArray: Comment[] = [{
- includes: null,
- id: 'com', // same as the DTO of the comment
- message: 'blabla',
- user: Promise.resolve(testUserEntity),
- post: null,
- commentsLikesDislikes: null,
- createdOn: new Date(),
- UpdatedDateColumn: new Date(),
- isDeleted: false,
- }];
- testUserEntity = {
- id: '1',
- username: 'Кирчо',
- email: 'kircho@abv.bg',
- password: 'kirchoTheBadVibesKiller00!',
- firstName: 'Greatest',
- lastName: 'Ever',
- friended: null,
- beFriendedBy: null,
- posts: null,
- comments: Promise.resolve(testCommentEntityArray),
- postLikesDislikes: null,
- commentsLikesDislikes: null,
- activities: null,
- roles: null,
- createdOn: new Date(),
- UpdatedDateColumn: new Date(),
- isDeleted: false,
- };
- const commentsRepoFindOne = jest.spyOn(commentsRepository, 'findOne')
- .mockImplementation(() => Promise.resolve(testCommentEntity));
- const commentsRepoSave = jest.spyOn(commentsRepository, 'save')
- .mockImplementation(() => Promise.resolve(testCommentEntity)); // Or Entity ...
- const converterServiceConvertToShowCommentDTO = jest.spyOn(converterService, 'convertToShowCommentDTO')
- .mockImplementation(() => Promise.resolve(testShowCommentDTOarray));
- const query = {
- id: 'com',
- isDeleted: false,
- };
- // Act
- await service.updateComment(testUpdateCommentDTO, testCommentId, testUserEntity);
- // Assert
- expect(commentsRepoFindOne).toBeCalledTimes(1);
- expect(commentsRepoFindOne).toBeCalledWith(query);
- commentsRepoFindOne.mockRestore();
- commentsRepoSave.mockRestore();
- converterServiceConvertToShowCommentDTO.mockRestore();
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement