From 10cfe63c45af276c9557df8c5586807d9837e656 Mon Sep 17 00:00:00 2001 From: ismay Date: Tue, 9 May 2023 15:15:16 +0200 Subject: [PATCH] refactor: extract job filtering from job list --- src/pages/JobList/JobList.js | 13 ++---- src/pages/JobList/filter-jobs.js | 14 +++++++ src/pages/JobList/filter-jobs.test.js | 58 +++++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 10 deletions(-) create mode 100644 src/pages/JobList/filter-jobs.js create mode 100644 src/pages/JobList/filter-jobs.test.js diff --git a/src/pages/JobList/JobList.js b/src/pages/JobList/JobList.js index 49029f897..22bc814b4 100644 --- a/src/pages/JobList/JobList.js +++ b/src/pages/JobList/JobList.js @@ -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' @@ -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 ( diff --git a/src/pages/JobList/filter-jobs.js b/src/pages/JobList/filter-jobs.js new file mode 100644 index 000000000..d43ac18cb --- /dev/null +++ b/src/pages/JobList/filter-jobs.js @@ -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 diff --git a/src/pages/JobList/filter-jobs.test.js b/src/pages/JobList/filter-jobs.test.js new file mode 100644 index 000000000..12c7c7a98 --- /dev/null +++ b/src/pages/JobList/filter-jobs.test.js @@ -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, + ]) + }) +})