Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.09 KB | None | 0 0
  1. import { range, sortBy, sort, prop } from 'ramda';
  2. import { constructJobsByDept } from './api';
  3. import { DepartmentRaw, JobsByDept } from './models';
  4.  
  5. describe('convert greenhouse call to valid JobsByDept list', () => {
  6. const fakeAxiosResult = departments => ({
  7. data: {
  8. departments,
  9. },
  10. });
  11.  
  12. const testDataNoChildren: DepartmentRaw[] = range(1, 4).map(
  13. index =>
  14. ({
  15. id: index,
  16. name: 'Dept ' + index,
  17. jobs: range(1, 4).map(jobIndex => ({
  18. absolute_url: '',
  19. title: 'Job ' + jobIndex,
  20. location: {
  21. name: 'NY',
  22. },
  23. })),
  24. } as DepartmentRaw),
  25. );
  26.  
  27. const expectedNoChildren = range(1, 4).map(
  28. index =>
  29. ({
  30. departmentName: 'Dept ' + index,
  31. departmentSlug: 'dept-' + index,
  32. jobs: range(1, 4).map(jobIndex => ({
  33. title: 'Job ' + jobIndex,
  34. location: 'NY',
  35. url: '',
  36. })),
  37. } as JobsByDept),
  38. );
  39.  
  40. const testData: DepartmentRaw[] = [
  41. ...testDataNoChildren,
  42. {
  43. id: 11,
  44. parent_id: 10,
  45. child_ids: [],
  46. name: 'Child 1',
  47. jobs: [
  48. {
  49. absolute_url: '',
  50. title: 'Child job 1',
  51. location: {
  52. name: 'Taiwan',
  53. },
  54. },
  55. ],
  56. },
  57. {
  58. id: 12,
  59. parent_id: 10,
  60. child_ids: [],
  61. name: 'Child 2',
  62. jobs: [
  63. {
  64. absolute_url: '',
  65. title: 'Child job 2',
  66. location: {
  67. name: 'London',
  68. },
  69. },
  70. ],
  71. },
  72. ];
  73.  
  74. it('does nothing for empty array', () => {
  75. expect(constructJobsByDept(fakeAxiosResult([]))).toEqual([]);
  76. });
  77.  
  78. it('converts to JobsByDept list with no merging given no parent nodes', () => {
  79. expect(constructJobsByDept(fakeAxiosResult(testDataNoChildren))).toEqual(
  80. expectedNoChildren,
  81. );
  82. });
  83.  
  84. it('converts to JobsByDept list with merging given parent nodes', () => {
  85. const testWithParent = [
  86. ...testData,
  87. {
  88. id: 10,
  89. parent_id: null,
  90. child_ids: [11, 12],
  91. name: 'Parent',
  92. jobs: [],
  93. },
  94. ];
  95. const expectedResult: JobsByDept[] = [
  96. ...expectedNoChildren,
  97. {
  98. departmentName: 'Parent',
  99. departmentSlug: 'parent',
  100. jobs: [
  101. {
  102. title: 'Child job 1',
  103. location: 'Taiwan',
  104. url: '',
  105. },
  106. {
  107. title: 'Child job 2',
  108. location: 'London',
  109. url: '',
  110. },
  111. ],
  112. },
  113. ];
  114.  
  115. expect(
  116. sortBy(
  117. prop('departmentName'),
  118. constructJobsByDept(fakeAxiosResult(testWithParent)),
  119. ),
  120. ).toEqual(expectedResult);
  121. });
  122.  
  123. it('converts to JobsByDept list with merging given parent nodes with jobs', () => {
  124. const testWithParent = [
  125. ...testData,
  126. {
  127. id: 10,
  128. parent_id: null,
  129. child_ids: [11, 12],
  130. name: 'Parent',
  131. jobs: [
  132. {
  133. absolute_url: '',
  134. title: 'Parent job 1',
  135. location: {
  136. name: 'New York',
  137. },
  138. },
  139. {
  140. absolute_url: '',
  141. title: 'Parent job 2',
  142. location: {
  143. name: 'New York',
  144. },
  145. },
  146. ],
  147. },
  148. ];
  149.  
  150. const expectedResult: JobsByDept[] = [
  151. ...expectedNoChildren,
  152. {
  153. departmentName: 'Parent',
  154. departmentSlug: 'parent',
  155. jobs: [
  156. {
  157. title: 'Parent job 1',
  158. location: 'New York',
  159. url: '',
  160. },
  161. {
  162. title: 'Parent job 2',
  163. location: 'New York',
  164. url: '',
  165. },
  166. {
  167. title: 'Child job 1',
  168. location: 'Taiwan',
  169. url: '',
  170. },
  171. {
  172. title: 'Child job 2',
  173. location: 'London',
  174. url: '',
  175. },
  176. ],
  177. },
  178. ];
  179.  
  180. expect(
  181. sortBy(
  182. prop('departmentName'),
  183. constructJobsByDept(fakeAxiosResult(testWithParent)),
  184. ),
  185. ).toEqual(expectedResult);
  186. });
  187.  
  188. it('sorts JobsByDept list by department name', () => {
  189. const deptNames = ['Retail', 'Coaching', 'Finance', 'Apparel'];
  190. const testDataUnordered: DepartmentRaw[] = deptNames.map(
  191. (deptName): DepartmentRaw => ({
  192. id: Math.floor(Math.random() * 100),
  193. name: deptName,
  194. parent_id: null,
  195. child_ids: [],
  196. jobs: range(1, 4).map(jobIndex => ({
  197. absolute_url: '',
  198. title: 'Job ' + jobIndex,
  199. location: {
  200. name: 'NY',
  201. },
  202. })),
  203. }),
  204. );
  205.  
  206. const deptNamesSorted = ['Apparel', 'Coaching', 'Finance', 'Retail'];
  207. const expectedResult: JobsByDept[] = deptNamesSorted.map(
  208. (deptName): JobsByDept => ({
  209. departmentName: deptName,
  210. departmentSlug: deptName.toLowerCase(),
  211. jobs: range(1, 4).map(jobIndex => ({
  212. title: 'Job ' + jobIndex,
  213. location: 'NY',
  214. url: '',
  215. })),
  216. }),
  217. );
  218.  
  219. expect(constructJobsByDept(fakeAxiosResult(testDataUnordered))).toEqual(
  220. expectedResult,
  221. );
  222. });
  223. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement