Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- describe('CommentController', () => {
- let controller: CommentController;
- const commentService = {
- findAll() { },
- add() { },
- findCommentById() { },
- checkAuthor() { },
- updateCommentById() { },
- transformCommentForDisplay() { },
- deleteCommentById() { },
- };
- const postService = {
- findPostById() { },
- };
- const likesService = {
- prepareForVoting() { },
- upvoteComment() { },
- downvoteComment() { }
- };
- beforeEach(async () => {
- const module: TestingModule = await Test.createTestingModule({
- controllers: [CommentController],
- providers: [
- {
- provide: CommentService,
- useValue: commentService,
- },
- {
- provide: PostService,
- useValue: postService,
- },
- {
- provide: LikesService,
- useValue: likesService,
- },
- ],
- }).compile();
- controller = module.get<CommentController>(CommentController);
- });
- it('should be defined', () => {
- expect(controller).toBeDefined();
- });
- describe('all', () => {
- it('should return the correct result from findPostById method', async () => {
- const fakeId = '7';
- const fakePost = new Posst();
- const fakeFindPostById = jest.spyOn(postService, 'findPostById')
- .mockImplementation(() => Promise.resolve(fakePost));
- const fakeShowComments = jest.spyOn(commentService, 'findAll')
- .mockImplementation(() => Promise.resolve(['comment']));
- const result = await controller.all(fakeId);
- expect(result).toEqual(['comment']);
- fakeFindPostById.mockRestore();
- fakeShowComments.mockRestore();
- });
- it('should call findPostById method with correct id', async () => {
- const fakeId = '77';
- const fakePost = new Posst();
- const fakeFindPostById = jest.spyOn(postService, 'findPostById')
- .mockImplementation(() => Promise.resolve(fakePost));
- const fakeShowComments = jest.spyOn(commentService, 'findAll')
- .mockImplementation(() => Promise.resolve(['comment']));
- await controller.all(fakeId);
- expect(fakeFindPostById).toHaveBeenCalled();
- expect(fakeFindPostById).toHaveBeenCalledWith(fakeId);
- fakeFindPostById.mockRestore();
- fakeShowComments.mockRestore();
- });
- it('should call findAll method with correct parameter', async () => {
- const fakeId = '77';
- const fakePost = new Posst();
- const fakeFindPostById = jest.spyOn(postService, 'findPostById')
- .mockImplementation(() => Promise.resolve(fakePost));
- const fakeShowComments = jest.spyOn(commentService, 'findAll')
- .mockImplementation(() => Promise.resolve(['comment']));
- await controller.all(fakeId);
- expect(fakeShowComments).toHaveBeenCalled();
- expect(fakeShowComments).toHaveBeenCalledWith(fakePost);
- fakeFindPostById.mockRestore();
- fakeShowComments.mockRestore();
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement