Skip to content

Commit

Permalink
refactor: extract job filtering from job list
Browse files Browse the repository at this point in the history
  • Loading branch information
ismay committed May 15, 2023
1 parent c2a42f1 commit 10cfe63
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 10 deletions.
13 changes: 3 additions & 10 deletions src/pages/JobList/JobList.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { JobTable } from '../../components/JobTable'
import { LinkButton } from '../../components/LinkButton'
import { Spinner } from '../../components/Spinner'
import styles from './JobList.module.css'
import filterJobs from './filter-jobs'

const infoLink =
'https://docs.dhis2.org/en/use/user-guides/dhis-core-version-236/maintaining-the-system/scheduling.html'
Expand All @@ -30,16 +31,8 @@ const JobList = () => {
)
}

// 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

const jobs = data.filter(applyJobFilter).filter(applyShowSystemJobs)
// Apply the current filter settings
const jobs = filterJobs({ jobFilter, showSystemJobs, jobs: data })

return (
<React.Fragment>
Expand Down
14 changes: 14 additions & 0 deletions src/pages/JobList/filter-jobs.js
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
58 changes: 58 additions & 0 deletions src/pages/JobList/filter-jobs.test.js
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,
])
})
})

0 comments on commit 10cfe63

Please sign in to comment.