Advertisement
Guest User

Untitled

a guest
May 20th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. describe('CommentController', () => {
  2.     let controller: CommentController;
  3.  
  4.     const commentService = {
  5.         findAll() { },
  6.         add() { },
  7.         findCommentById() { },
  8.         checkAuthor() { },
  9.         updateCommentById() { },
  10.         transformCommentForDisplay() { },
  11.         deleteCommentById() { },
  12.  
  13.     };
  14.  
  15.     const postService = {
  16.         findPostById() { },
  17.     };
  18.  
  19.     const likesService = {
  20.         prepareForVoting() { },
  21.  
  22.         upvoteComment() { },
  23.  
  24.         downvoteComment() { }
  25.     };
  26.  
  27.     beforeEach(async () => {
  28.         const module: TestingModule = await Test.createTestingModule({
  29.             controllers: [CommentController],
  30.             providers: [
  31.                 {
  32.                     provide: CommentService,
  33.                     useValue: commentService,
  34.                 },
  35.                 {
  36.                     provide: PostService,
  37.                     useValue: postService,
  38.                 },
  39.                 {
  40.                     provide: LikesService,
  41.                     useValue: likesService,
  42.                 },
  43.             ],
  44.         }).compile();
  45.  
  46.         controller = module.get<CommentController>(CommentController);
  47.     });
  48.  
  49.     it('should be defined', () => {
  50.         expect(controller).toBeDefined();
  51.     });
  52.     describe('all', () => {
  53.         it('should return the correct result from findPostById method', async () => {
  54.             const fakeId = '7';
  55.             const fakePost = new Posst();
  56.  
  57.             const fakeFindPostById = jest.spyOn(postService, 'findPostById')
  58.                 .mockImplementation(() => Promise.resolve(fakePost));
  59.             const fakeShowComments = jest.spyOn(commentService, 'findAll')
  60.                 .mockImplementation(() => Promise.resolve(['comment']));
  61.  
  62.             const result = await controller.all(fakeId);
  63.  
  64.             expect(result).toEqual(['comment']);
  65.  
  66.             fakeFindPostById.mockRestore();
  67.             fakeShowComments.mockRestore();
  68.         });
  69.         it('should call findPostById method with correct id', async () => {
  70.             const fakeId = '77';
  71.             const fakePost = new Posst();
  72.  
  73.             const fakeFindPostById = jest.spyOn(postService, 'findPostById')
  74.                 .mockImplementation(() => Promise.resolve(fakePost));
  75.             const fakeShowComments = jest.spyOn(commentService, 'findAll')
  76.                 .mockImplementation(() => Promise.resolve(['comment']));
  77.  
  78.             await controller.all(fakeId);
  79.  
  80.             expect(fakeFindPostById).toHaveBeenCalled();
  81.             expect(fakeFindPostById).toHaveBeenCalledWith(fakeId);
  82.  
  83.             fakeFindPostById.mockRestore();
  84.             fakeShowComments.mockRestore();
  85.         });
  86.         it('should call findAll method with correct parameter', async () => {
  87.             const fakeId = '77';
  88.             const fakePost = new Posst();
  89.  
  90.             const fakeFindPostById = jest.spyOn(postService, 'findPostById')
  91.                 .mockImplementation(() => Promise.resolve(fakePost));
  92.             const fakeShowComments = jest.spyOn(commentService, 'findAll')
  93.                 .mockImplementation(() => Promise.resolve(['comment']));
  94.  
  95.             await controller.all(fakeId);
  96.  
  97.             expect(fakeShowComments).toHaveBeenCalled();
  98.             expect(fakeShowComments).toHaveBeenCalledWith(fakePost);
  99.  
  100.             fakeFindPostById.mockRestore();
  101.             fakeShowComments.mockRestore();
  102.         });
  103.     });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement