Advertisement
Guest User

Untitled

a guest
May 20th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.94 KB | None | 0 0
  1. import { Test, TestingModule } from '@nestjs/testing';
  2. import { PostsService } from './posts.service';
  3. import { getRepositoryToken } from '@nestjs/typeorm';
  4. import { User } from '../data/entities/user.entity';
  5. import { Post } from '../data/entities/post.entity';
  6. import { FlagPostEntity } from '../data/entities/flag.entity';
  7. import { CommentsService } from '../comments/comments.service';
  8. import { PostShowWithoutCommentsDTO } from '../models/posts/post-show-without-comments.dto';
  9. import { UserShowDTO } from 'src/models/user';
  10. import { PostShowWithCommentsDTO } from 'src/models/posts/post-show-with-comments.dto';
  11. import { ShowCommentDTO } from 'src/models/comments/show-comment.dto';
  12.  
  13. describe('PostsService', () => {
  14. let service: PostsService;
  15.  
  16. const like: UserShowDTO = {
  17. banstatus: undefined,
  18. id: undefined,
  19. roles: undefined,
  20. username: undefined,
  21. };
  22.  
  23. const author: UserShowDTO = {
  24. banstatus: undefined,
  25. id: undefined,
  26. roles: undefined,
  27. username: undefined,
  28. };
  29.  
  30. const post: any = {
  31. author: {
  32. username: undefined,
  33. },
  34. like: [like],
  35. dislike: [like],
  36. comments: [{}],
  37. };
  38.  
  39. const plainToClassPost: PostShowWithoutCommentsDTO = {
  40. author,
  41. body: undefined,
  42. dateModified: undefined,
  43. datePosted: undefined,
  44. description: undefined,
  45. dislike: [like],
  46. flag: undefined,
  47. id: undefined,
  48. isLocked: undefined,
  49. like: [like],
  50. title: undefined,
  51. };
  52.  
  53. const comment: ShowCommentDTO = {
  54. author: undefined,
  55. body: undefined,
  56. dateCreated: undefined,
  57. dateModified: undefined,
  58. id: undefined,
  59. };
  60.  
  61. const plainToClassPostWithComments: PostShowWithCommentsDTO = {
  62. author,
  63. body: undefined,
  64. dateModified: undefined,
  65. datePosted: undefined,
  66. description: undefined,
  67. dislike: [like],
  68. flag: undefined,
  69. id: undefined,
  70. isLocked: undefined,
  71. like: [like],
  72. title: undefined,
  73. comments: [comment],
  74. };
  75.  
  76. const mockUsersRepository = {
  77. findOne: (options: any) => author,
  78. };
  79.  
  80. const mockPostsRepository = {
  81. findOne: (options: any) => post,
  82. create: (options: any) => post,
  83. save: (options: any) => post,
  84. find: (options: any) => [post],
  85. };
  86.  
  87. const mockFlagPostRepository = {};
  88.  
  89. const mockCommentsService = {
  90. createCommentDTO: (comments: any) => comments,
  91. };
  92.  
  93. beforeEach(async () => {
  94. const module: TestingModule = await Test.createTestingModule({
  95. providers: [PostsService, {
  96. provide: getRepositoryToken(User),
  97. useValue: mockUsersRepository,
  98. }, {
  99. provide: getRepositoryToken(Post),
  100. useValue: mockPostsRepository,
  101. }, {
  102. provide: getRepositoryToken(FlagPostEntity),
  103. useValue: mockFlagPostRepository,
  104. }, {
  105. provide: CommentsService,
  106. useValue: mockCommentsService,
  107. }],
  108. }).compile();
  109.  
  110. service = module.get<PostsService>(PostsService);
  111. });
  112.  
  113. it('should be defined', () => {
  114. expect(service).toBeDefined();
  115. });
  116.  
  117. describe('isUserCreator', () => {
  118. it('isUserCreator should return true if the user is creator', async () => {
  119. const user: any = { username: undefined };
  120. const result = await service.isUserCreator(user, 'postId');
  121. expect(result).toBe(true);
  122. });
  123.  
  124. it('isUserCreator should return false if the user is not creator', async () => {
  125. const user: any = { username: 'NotUser' };
  126. const result = await service.isUserCreator(user, 'postId');
  127. expect(result).toBe(false);
  128. });
  129.  
  130. it('isUserCreator should call postRepository.findOne', async () => {
  131. const findOne = jest.spyOn(mockPostsRepository, 'findOne');
  132. const user: any = { username: 'NotUser' };
  133. await service.isUserCreator(user, 'postId');
  134. expect(findOne).toBeCalledTimes(1);
  135. });
  136. });
  137.  
  138. describe('getAllPosts', () => {
  139. it('getAllPosts should return correct value', async () => {
  140. const createPostShowDTO = jest.spyOn(service, 'createPostShowDTO').mockImplementation((mockPost: any) => Promise.resolve(post));
  141. const result = await service.getAllPosts(0);
  142. expect(result).toEqual([plainToClassPost]);
  143. createPostShowDTO.mockRestore();
  144. });
  145.  
  146. it('getAllPosts should return correct value', async () => {
  147. const createPostShowDTO = jest.spyOn(service, 'createPostShowDTO').mockImplementation((mockPost: any) => Promise.resolve(post));
  148. const result = await service.getAllPosts('0');
  149. expect(result).toEqual([]);
  150. createPostShowDTO.mockRestore();
  151. });
  152.  
  153. it('getAllPosts should call postRepository.find', async () => {
  154. const createPostShowDTO = jest.spyOn(service, 'createPostShowDTO').mockImplementation((mockPost: any) => Promise.resolve(post));
  155. const find = jest.spyOn(mockPostsRepository, 'find');
  156. await service.getAllPosts('0');
  157. expect(find).toBeCalledTimes(1);
  158. createPostShowDTO.mockRestore();
  159. find.mockRestore();
  160. });
  161.  
  162. it('getAllPosts should call createPostShowDTO', async () => {
  163. const createPostShowDTO = jest.spyOn(service, 'createPostShowDTO').mockImplementation((mockPost: any) => Promise.resolve(post));
  164. await service.getAllPosts('0');
  165. expect(createPostShowDTO).toBeCalledTimes(1);
  166. createPostShowDTO.mockRestore();
  167. });
  168. });
  169.  
  170. describe('createPostShowDTO', () => {
  171. it('createPostShowDTO should return correct values', async () => {
  172. const result = await service.createPostShowDTO(post);
  173. expect(result).toEqual(plainToClassPostWithComments);
  174. });
  175. });
  176.  
  177. describe('getPostById', () => {
  178. it('getPostById should return correct values', async () => {
  179. const result = await service.getPostById('postId');
  180. expect(result).toEqual(plainToClassPostWithComments);
  181. });
  182.  
  183. it('getPostById should call postRepository.findOne', async () => {
  184. const findOne = jest.spyOn(mockPostsRepository, 'findOne');
  185. await service.getPostById('postId');
  186. expect(findOne).toBeCalledTimes(3);
  187. findOne.mockRestore();
  188. });
  189. });
  190.  
  191. describe('create', () => {
  192. it('create should return correct values', async () => {
  193. const createPostShowDTO = jest.spyOn(service, 'createPostShowDTO').mockImplementation((mockPost: any) => Promise.resolve(post));
  194. const result = await service.create(post, author);
  195. expect(result).toEqual(plainToClassPost);
  196. createPostShowDTO.mockRestore();
  197. });
  198. });
  199.  
  200. describe('updatePost', () => {
  201. it('updatePost should return correct values', async () => {
  202. const createPostShowDTO = jest.spyOn(service, 'createPostShowDTO').mockImplementation((mockPost: any) => Promise.resolve(post));
  203. const result = await service.updatePost('postId', post);
  204. expect(result).toEqual(plainToClassPost);
  205. createPostShowDTO.mockRestore();
  206. });
  207. });
  208.  
  209. describe('deletePost', () => {
  210. it('deletePost should return correct values', async () => {
  211. const createPostShowDTO = jest.spyOn(service, 'createPostShowDTO').mockImplementation((mockPost: any) => Promise.resolve(post));
  212. const result = await service.deletePost('postId', author);
  213. expect(result).toEqual(plainToClassPost);
  214. createPostShowDTO.mockRestore();
  215. });
  216.  
  217. it('deletePost should return correct values', async () => {
  218. const user: any = { username: 'NotUser' };
  219. const createPostShowDTO = jest.spyOn(service, 'createPostShowDTO').mockImplementation((mockPost: any) => Promise.resolve(post));
  220. const result = await service.deletePost('postId', user);
  221. expect(result).toEqual(undefined);
  222. createPostShowDTO.mockRestore();
  223. });
  224. });
  225.  
  226. describe('filterByUser', () => {
  227. it('updatePost should return correct values', async () => {
  228. const createPostShowDTO = jest.spyOn(service, 'createPostShowDTO').mockImplementation((mockPost: any) => Promise.resolve(post));
  229. const authorAny: any = {
  230. posts: [{}],
  231. };
  232. const findOne = jest.spyOn(mockUsersRepository, 'findOne').mockImplementation((userId: string) => authorAny);
  233. const result = await service.filterByUser(undefined);
  234. expect(result).toEqual([plainToClassPost]);
  235. createPostShowDTO.mockRestore();
  236. findOne.mockRestore();
  237. });
  238. });
  239.  
  240. describe('likePost', () => {
  241. it('likePost should return correct values', async () => {
  242. const createPostShowDTO = jest.spyOn(service, 'createPostShowDTO').mockImplementation((mockPost: any) => Promise.resolve(post));
  243. const mockPost = {
  244. like: [],
  245. dislike: [],
  246. };
  247. const mockUser: any = {
  248. username: 'username',
  249. };
  250. const foundOne = jest.spyOn(mockPostsRepository, 'findOne').mockImplementation(() => mockPost);
  251. const foundUser = jest.spyOn(mockUsersRepository, 'findOne').mockImplementation(() => mockUser);
  252. const expectedResult = plainToClassPost;
  253. expectedResult.like = [like];
  254. const result = await service.likePost(mockUser, 'postId');
  255. expect(result).toEqual(expectedResult);
  256. createPostShowDTO.mockRestore();
  257. foundOne.mockRestore();
  258. foundUser.mockRestore();
  259. });
  260.  
  261. it('likePost should return correct values', async () => {
  262. const createPostShowDTO = jest.spyOn(service, 'createPostShowDTO').mockImplementation((mockPost: any) => Promise.resolve(post));
  263. const mockUser: any = {
  264. username: 'username',
  265. };
  266. const mockPost = {
  267. like: [mockUser],
  268. dislike: [],
  269. };
  270. const foundOne = jest.spyOn(mockPostsRepository, 'findOne').mockImplementation(() => mockPost);
  271. const foundUser = jest.spyOn(mockUsersRepository, 'findOne').mockImplementation(() => mockUser);
  272. const expectedResult = plainToClassPost;
  273. expectedResult.like = [like];
  274. const result = await service.likePost(mockUser, 'postId');
  275. expect(result).toEqual(expectedResult);
  276. createPostShowDTO.mockRestore();
  277. foundOne.mockRestore();
  278. foundUser.mockRestore();
  279. });
  280.  
  281. it('likePost should return correct values', async () => {
  282. const createPostShowDTO = jest.spyOn(service, 'createPostShowDTO').mockImplementation((mockPost: any) => Promise.resolve(post));
  283. const mockUser: any = {
  284. username: 'username',
  285. };
  286. const mockPost = {
  287. like: [],
  288. dislike: [mockUser],
  289. };
  290. const foundOne = jest.spyOn(mockPostsRepository, 'findOne').mockImplementation(() => mockPost);
  291. const foundUser = jest.spyOn(mockUsersRepository, 'findOne').mockImplementation(() => mockUser);
  292. const expectedResult = plainToClassPost;
  293. expectedResult.like = [like];
  294. const result = await service.likePost(mockUser, 'postId');
  295. expect(result).toEqual(expectedResult);
  296. createPostShowDTO.mockRestore();
  297. foundOne.mockRestore();
  298. foundUser.mockRestore();
  299. });
  300. });
  301.  
  302. describe('dislikePost', () => {
  303. it('dislikePost should return correct values', async () => {
  304. const createPostShowDTO = jest.spyOn(service, 'createPostShowDTO').mockImplementation((mockPost: any) => Promise.resolve(post));
  305. const mockPost = {
  306. like: [],
  307. dislike: [],
  308. };
  309. const mockUser: any = {
  310. username: 'username',
  311. };
  312. const foundOne = jest.spyOn(mockPostsRepository, 'findOne').mockImplementation(() => mockPost);
  313. const foundUser = jest.spyOn(mockUsersRepository, 'findOne').mockImplementation(() => mockUser);
  314. const expectedResult = plainToClassPost;
  315. expectedResult.like = [like];
  316. const result = await service.dislikePost(mockUser, 'postId');
  317. expect(result).toEqual(expectedResult);
  318. createPostShowDTO.mockRestore();
  319. foundOne.mockRestore();
  320. foundUser.mockRestore();
  321. });
  322.  
  323. it('dislikePost should return correct values', async () => {
  324. const createPostShowDTO = jest.spyOn(service, 'createPostShowDTO').mockImplementation((mockPost: any) => Promise.resolve(post));
  325. const mockUser: any = {
  326. username: 'username',
  327. };
  328. const mockPost = {
  329. like: [mockUser],
  330. dislike: [],
  331. };
  332. const foundOne = jest.spyOn(mockPostsRepository, 'findOne').mockImplementation(() => mockPost);
  333. const foundUser = jest.spyOn(mockUsersRepository, 'findOne').mockImplementation(() => mockUser);
  334. const expectedResult = plainToClassPost;
  335. expectedResult.like = [like];
  336. const result = await service.dislikePost(mockUser, 'postId');
  337. expect(result).toEqual(expectedResult);
  338. createPostShowDTO.mockRestore();
  339. foundOne.mockRestore();
  340. foundUser.mockRestore();
  341. });
  342.  
  343. it('dislikePost should return correct values', async () => {
  344. const createPostShowDTO = jest.spyOn(service, 'createPostShowDTO').mockImplementation((mockPost: any) => Promise.resolve(post));
  345. const mockUser: any = {
  346. username: 'username',
  347. };
  348. const mockPost = {
  349. like: [],
  350. dislike: [mockUser],
  351. };
  352. const foundOne = jest.spyOn(mockPostsRepository, 'findOne').mockImplementation(() => mockPost);
  353. const foundUser = jest.spyOn(mockUsersRepository, 'findOne').mockImplementation(() => mockUser);
  354. const expectedResult = plainToClassPost;
  355. expectedResult.like = [like];
  356. const result = await service.dislikePost(mockUser, 'postId');
  357. expect(result).toEqual(expectedResult);
  358. createPostShowDTO.mockRestore();
  359. foundOne.mockRestore();
  360. foundUser.mockRestore();
  361. });
  362. });
  363.  
  364. describe('lockPost', () => {
  365. it('lockPost should return correct values', async () => {
  366. const createPostShowDTO = jest.spyOn(service, 'createPostShowDTO').mockImplementation((mockPost: any) => Promise.resolve(post));
  367. const result = await service.lockPost('postId');
  368. const expectedResult = plainToClassPost;
  369. expectedResult.isLocked = true;
  370. expect(result).toEqual(expectedResult);
  371. createPostShowDTO.mockRestore();
  372. });
  373. });
  374. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement