Created
August 9, 2019 21:34
-
-
Save bholmesdev/33c677d2702b150346edf26f578fc594 to your computer and use it in GitHub Desktop.
Good jest test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { range, sortBy, sort, prop } from 'ramda'; | |
import { constructJobsByDept } from './api'; | |
import { DepartmentRaw, JobsByDept } from './models'; | |
describe('convert greenhouse call to valid JobsByDept list', () => { | |
const fakeAxiosResult = departments => ({ | |
data: { | |
departments, | |
}, | |
}); | |
const testDataNoChildren: DepartmentRaw[] = range(1, 4).map( | |
index => | |
({ | |
id: index, | |
name: 'Dept ' + index, | |
jobs: range(1, 4).map(jobIndex => ({ | |
absolute_url: '', | |
title: 'Job ' + jobIndex, | |
location: { | |
name: 'NY', | |
}, | |
})), | |
} as DepartmentRaw), | |
); | |
const expectedNoChildren = range(1, 4).map( | |
index => | |
({ | |
departmentName: 'Dept ' + index, | |
departmentSlug: 'dept-' + index, | |
jobs: range(1, 4).map(jobIndex => ({ | |
title: 'Job ' + jobIndex, | |
location: 'NY', | |
url: '', | |
})), | |
} as JobsByDept), | |
); | |
const testData: DepartmentRaw[] = [ | |
...testDataNoChildren, | |
{ | |
id: 11, | |
parent_id: 10, | |
child_ids: [], | |
name: 'Child 1', | |
jobs: [ | |
{ | |
absolute_url: '', | |
title: 'Child job 1', | |
location: { | |
name: 'Taiwan', | |
}, | |
}, | |
], | |
}, | |
{ | |
id: 12, | |
parent_id: 10, | |
child_ids: [], | |
name: 'Child 2', | |
jobs: [ | |
{ | |
absolute_url: '', | |
title: 'Child job 2', | |
location: { | |
name: 'London', | |
}, | |
}, | |
], | |
}, | |
]; | |
it('does nothing for empty array', () => { | |
expect(constructJobsByDept(fakeAxiosResult([]))).toEqual([]); | |
}); | |
it('converts to JobsByDept list with no merging given no parent nodes', () => { | |
expect(constructJobsByDept(fakeAxiosResult(testDataNoChildren))).toEqual( | |
expectedNoChildren, | |
); | |
}); | |
it('converts to JobsByDept list with merging given parent nodes', () => { | |
const testWithParent = [ | |
...testData, | |
{ | |
id: 10, | |
parent_id: null, | |
child_ids: [11, 12], | |
name: 'Parent', | |
jobs: [], | |
}, | |
]; | |
const expectedResult: JobsByDept[] = [ | |
...expectedNoChildren, | |
{ | |
departmentName: 'Parent', | |
departmentSlug: 'parent', | |
jobs: [ | |
{ | |
title: 'Child job 1', | |
location: 'Taiwan', | |
url: '', | |
}, | |
{ | |
title: 'Child job 2', | |
location: 'London', | |
url: '', | |
}, | |
], | |
}, | |
]; | |
expect( | |
sortBy( | |
prop('departmentName'), | |
constructJobsByDept(fakeAxiosResult(testWithParent)), | |
), | |
).toEqual(expectedResult); | |
}); | |
it('converts to JobsByDept list with merging given parent nodes with jobs', () => { | |
const testWithParent = [ | |
...testData, | |
{ | |
id: 10, | |
parent_id: null, | |
child_ids: [11, 12], | |
name: 'Parent', | |
jobs: [ | |
{ | |
absolute_url: '', | |
title: 'Parent job 1', | |
location: { | |
name: 'New York', | |
}, | |
}, | |
{ | |
absolute_url: '', | |
title: 'Parent job 2', | |
location: { | |
name: 'New York', | |
}, | |
}, | |
], | |
}, | |
]; | |
const expectedResult: JobsByDept[] = [ | |
...expectedNoChildren, | |
{ | |
departmentName: 'Parent', | |
departmentSlug: 'parent', | |
jobs: [ | |
{ | |
title: 'Parent job 1', | |
location: 'New York', | |
url: '', | |
}, | |
{ | |
title: 'Parent job 2', | |
location: 'New York', | |
url: '', | |
}, | |
{ | |
title: 'Child job 1', | |
location: 'Taiwan', | |
url: '', | |
}, | |
{ | |
title: 'Child job 2', | |
location: 'London', | |
url: '', | |
}, | |
], | |
}, | |
]; | |
expect( | |
sortBy( | |
prop('departmentName'), | |
constructJobsByDept(fakeAxiosResult(testWithParent)), | |
), | |
).toEqual(expectedResult); | |
}); | |
it('sorts JobsByDept list by department name', () => { | |
const deptNames = ['Retail', 'Coaching', 'Finance', 'Apparel']; | |
const testDataUnordered: DepartmentRaw[] = deptNames.map( | |
(deptName): DepartmentRaw => ({ | |
id: Math.floor(Math.random() * 100), | |
name: deptName, | |
parent_id: null, | |
child_ids: [], | |
jobs: range(1, 4).map(jobIndex => ({ | |
absolute_url: '', | |
title: 'Job ' + jobIndex, | |
location: { | |
name: 'NY', | |
}, | |
})), | |
}), | |
); | |
const deptNamesSorted = ['Apparel', 'Coaching', 'Finance', 'Retail']; | |
const expectedResult: JobsByDept[] = deptNamesSorted.map( | |
(deptName): JobsByDept => ({ | |
departmentName: deptName, | |
departmentSlug: deptName.toLowerCase(), | |
jobs: range(1, 4).map(jobIndex => ({ | |
title: 'Job ' + jobIndex, | |
location: 'NY', | |
url: '', | |
})), | |
}), | |
); | |
expect(constructJobsByDept(fakeAxiosResult(testDataUnordered))).toEqual( | |
expectedResult, | |
); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment