Guest User

Untitled

a guest
Mar 22nd, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.97 KB | None | 0 0
  1. import { Post, IPost } from './Post';
  2.  
  3. describe('Test Post entity', () => {
  4. /* tslint:disable-next-line:max-line-length */
  5. const bigString = 'est rerum tempore vitae sequi sint nihil reprehenderit dolor beatae ea dolores neque fugiat blanditiis voluptate porro vel nihil molestiae ut reiciendis qui aperiam non debitis possimus qui neque nisi nulla est rerum tempore vitae sequi sint nihil reprehenderit dolor beatae ea dolores neque fugiat blanditiis voluptate porro vel nihil molestiae ut reiciendis qui aperiam non debitis possimus qui neque nisi nulla';
  6. let post: IPost;
  7.  
  8. beforeEach(() => {
  9. post = new Post();
  10. });
  11.  
  12. it('should copy an object data into a Post instance', () => {
  13. const data = {
  14. id: 1,
  15. userId: 3,
  16. title: 'Copy',
  17. body: 'Copied',
  18. };
  19. post.copyData(data);
  20.  
  21. expect(post.id).toBe(1);
  22. expect(post.userId).toBe(3);
  23. expect(post.title).toBe('Copy');
  24. expect(post.body).toBe('Copied');
  25. });
  26.  
  27. it('should return title is invalid for empty string', () => {
  28. expect(post.isValidTitle()).toBeFalsy();
  29. });
  30.  
  31. it('should return title is invalid using additional validator', () => {
  32. post.title = 'New';
  33. expect(post.isValidTitle((title: string): boolean => {
  34. return title.length > 3;
  35. })).toBeFalsy();
  36. });
  37.  
  38. it('should return title is invalid for long titles', () => {
  39. post.title = bigString;
  40. expect(post.isValidTitle()).toBeFalsy();
  41. });
  42.  
  43. it('should return title is valid', () => {
  44. post.title = 'New post';
  45. expect(post.isValidTitle()).toBeTruthy();
  46. });
  47.  
  48. it('should return title is valid using additional validation', () => {
  49. post.title = 'Lorem ipsum';
  50. expect(post.isValidTitle((title: string) => {
  51. return title.indexOf('dolor') < 0;
  52. })).toBeTruthy();
  53. });
  54.  
  55. it('should return body is invalid for strings with less than 10 characters', () => {
  56. post.body = 'Lorem ip'
  57. expect(post.isValidBody()).toBeFalsy();
  58. });
  59.  
  60. it('should return body is invalid using additional validation', () => {
  61. post.body = 'Lorem ipsum dolor sit amet';
  62. expect(post.isValidBody((body: string): boolean => {
  63. return body.length > 30;
  64. })).toBeFalsy();
  65. });
  66.  
  67. it('should return body is valid', () => {
  68. post.body = 'Lorem ipsum dolor sit amet';
  69. expect(post.isValidBody()).toBeTruthy();
  70. });
  71.  
  72. it('should return body is valid using additional validation', () => {
  73. post.body = 'Lorem ipsum sit amet';
  74. expect(post.isValidBody((body: string): boolean => {
  75. return body.indexOf('dolor') < 0;
  76. })).toBeTruthy();
  77. });
  78.  
  79. it('should return post is invalid without previous validation', () => {
  80. expect(post.isValid()).toBeFalsy();
  81. });
  82.  
  83. it('should return post is valid without previous validation', () => {
  84. post.title = 'Lorem ipsum dolor sit amet';
  85. post.body = bigString;
  86.  
  87. expect(post.isValid()).toBeTruthy();
  88. });
  89.  
  90. it('should return post is invalid with previous title validation', () => {
  91. post.title = 'Lorem ipsum dolor';
  92. post.body = bigString;
  93.  
  94. expect(post.isValidTitle((title: string): boolean => {
  95. return title.indexOf('dolor') < 0;
  96. })).toBeFalsy();
  97.  
  98. expect(post.isValid()).toBeFalsy();
  99. });
  100.  
  101. it('should return post is invalid with previous body validation', () => {
  102. post.title = 'Lorem ipsum dolor';
  103. post.body = 'Invalid body';
  104.  
  105. expect(post.isValidBody((body: string): boolean => {
  106. return body.length > 20;
  107. })).toBeFalsy();
  108.  
  109. expect(post.isValid()).toBeFalsy();
  110. });
  111.  
  112. it('should return post is invalid with previous title and body validation, title is valid', () => {
  113. post.title = 'Lorem ipsum dolor';
  114. post.body = bigString;
  115.  
  116. expect(post.isValidTitle()).toBeTruthy();
  117. expect(post.isValidBody((body: string): boolean => {
  118. return body.length < 300;
  119. })).toBeFalsy();
  120.  
  121. expect(post.isValid()).toBeFalsy();
  122. });
  123.  
  124. it('should return post is invalid with previous title and body validation, body is valid', () => {
  125. post.title = 'Lorem ipsum dolor';
  126. post.body = bigString;
  127.  
  128. expect(post.isValidTitle((title: string): boolean => {
  129. return title.indexOf('dolor') < 0;
  130. })).toBeFalsy();
  131. expect(post.isValidBody()).toBeTruthy();
  132.  
  133. expect(post.isValid()).toBeFalsy();
  134. });
  135.  
  136. it('should return post is valid with previous title validation', () => {
  137. post.title = 'Lorem ipsum dolor';
  138. post.body = bigString;
  139.  
  140. expect(post.isValidTitle()).toBeTruthy();
  141. expect(post.isValid()).toBeTruthy();
  142. });
  143.  
  144. it('should return post is valid with previous body validation', () => {
  145. post.title = 'Lorem ipsum dolor';
  146. post.body = bigString;
  147.  
  148. expect(post.isValidBody()).toBeTruthy();
  149. expect(post.isValid()).toBeTruthy();
  150. });
  151.  
  152. it('should return post is valid with previous title and body validation', () => {
  153. post.title = 'Lorem ipsum';
  154. post.body = bigString;
  155.  
  156. expect(post.isValidTitle((title: string): boolean => {
  157. return title.indexOf('dolor') < 0;
  158. })).toBeTruthy();
  159. expect(post.isValidBody()).toBeTruthy();
  160. expect(post.isValid()).toBeTruthy();
  161. });
  162. });
Add Comment
Please, Sign In to add comment