-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: extract job filtering from job list
- Loading branch information
ismay
committed
May 15, 2023
1 parent
c2a42f1
commit 10cfe63
Showing
3 changed files
with
75 additions
and
10 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
const filterJobs = ({ jobFilter, showSystemJobs, jobs }) => { | ||
// Filter jobs by the current jobFilter | ||
const applyJobFilter = (job) => | ||
job.name.toLowerCase().includes(jobFilter.toLowerCase()) | ||
|
||
// Filter jobs depending on the current showSystemJobs | ||
const applyShowSystemJobs = (job) => | ||
// Jobs that are configurable are user jobs | ||
showSystemJobs ? true : job.configurable | ||
|
||
return jobs.filter(applyJobFilter).filter(applyShowSystemJobs) | ||
} | ||
|
||
export default filterJobs |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import filterJobs from './filter-jobs' | ||
|
||
describe('filterJobs', () => { | ||
it('should filter jobs by the current jobFilter', () => { | ||
const jobFilter = 'One' | ||
const showSystemJobs = true | ||
const expected = { name: 'One', configurable: true } | ||
const jobs = [expected, { name: 'Two', configurable: true }] | ||
|
||
expect(filterJobs({ jobFilter, showSystemJobs, jobs })).toEqual([ | ||
expected, | ||
]) | ||
}) | ||
|
||
it('should ignore job name capitalization', () => { | ||
const jobFilter = 'one' | ||
const showSystemJobs = true | ||
const expected = { name: 'One', configurable: true } | ||
const jobs = [expected, { name: 'Two', configurable: true }] | ||
|
||
expect(filterJobs({ jobFilter, showSystemJobs, jobs })).toEqual([ | ||
expected, | ||
]) | ||
}) | ||
|
||
it('should ignore jobFilter capitalization', () => { | ||
const jobFilter = 'One' | ||
const showSystemJobs = true | ||
const expected = { name: 'one', configurable: true } | ||
const jobs = [expected, { name: 'Two', configurable: true }] | ||
|
||
expect(filterJobs({ jobFilter, showSystemJobs, jobs })).toEqual([ | ||
expected, | ||
]) | ||
}) | ||
|
||
it('should show system jobs and user jobs if showSystemJobs is true', () => { | ||
const jobFilter = '' | ||
const showSystemJobs = true | ||
const jobs = [ | ||
{ name: 'One', configurable: false }, | ||
{ name: 'Two', configurable: true }, | ||
] | ||
|
||
expect(filterJobs({ jobFilter, showSystemJobs, jobs })).toEqual(jobs) | ||
}) | ||
|
||
it('should hide system jobs and show user jobs if showSystemJobs is false', () => { | ||
const jobFilter = '' | ||
const showSystemJobs = false | ||
const expected = { name: 'Two', configurable: true } | ||
const jobs = [{ name: 'One', configurable: false }, expected] | ||
|
||
expect(filterJobs({ jobFilter, showSystemJobs, jobs })).toEqual([ | ||
expected, | ||
]) | ||
}) | ||
}) |