-
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.
Merge pull request #80 from dhis2/move-list-state-to-store
fix(list): move list state to store
- Loading branch information
Showing
11 changed files
with
270 additions
and
177 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
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,48 @@ | ||
import { useContext } from 'react' | ||
import StoreContext from './StoreContext' | ||
|
||
export const useJobs = () => { | ||
const store = useContext(StoreContext) | ||
return store.jobs | ||
} | ||
|
||
/** | ||
* This state is used in the job list, but kept in the | ||
* store since it has to persist after a refetch. | ||
*/ | ||
export const useJobFilter = () => { | ||
const store = useContext(StoreContext) | ||
return store.jobFilter | ||
} | ||
|
||
/** | ||
* This state is used in the job list, but kept in the | ||
* store since it has to persist after a refetch. | ||
*/ | ||
export const useShowSystemJobs = () => { | ||
const store = useContext(StoreContext) | ||
return store.showSystemJobs | ||
} | ||
|
||
/** | ||
* This hook returns the list of jobs that's shown in the | ||
* job list route. The list is filtered by the job filter | ||
* string and the show system jobs toggle from the store | ||
* state. | ||
*/ | ||
export const useListJobs = () => { | ||
const [jobFilter] = useJobFilter() | ||
const [showSystemJobs] = useShowSystemJobs() | ||
const jobs = useJobs() | ||
|
||
// 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) | ||
} |
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,140 @@ | ||
import React from 'react' | ||
import { renderHook } from '@testing-library/react-hooks' | ||
import { useJobFilter, useShowSystemJobs, useJobs, useListJobs } from './hooks' | ||
import StoreContext from './StoreContext' | ||
|
||
describe('useJobs', () => { | ||
it('should return the jobs part of the store', () => { | ||
const jobs = 'jobs' | ||
const store = { | ||
jobs, | ||
} | ||
const wrapper = ({ children }) => ( | ||
<StoreContext.Provider value={store}> | ||
{children} | ||
</StoreContext.Provider> | ||
) | ||
const { result } = renderHook(() => useJobs(), { wrapper }) | ||
|
||
expect(result.current).toBe(jobs) | ||
}) | ||
}) | ||
|
||
describe('useJobFilter', () => { | ||
it('should return the jobFilter part of the store', () => { | ||
const jobFilter = 'jobFilter' | ||
const store = { | ||
jobFilter, | ||
} | ||
const wrapper = ({ children }) => ( | ||
<StoreContext.Provider value={store}> | ||
{children} | ||
</StoreContext.Provider> | ||
) | ||
const { result } = renderHook(() => useJobFilter(), { wrapper }) | ||
|
||
expect(result.current).toBe(jobFilter) | ||
}) | ||
}) | ||
|
||
describe('useShowSystemJobs', () => { | ||
it('should return the showSystemJobs part of the store', () => { | ||
const showSystemJobs = 'showSystemJobs' | ||
const store = { | ||
showSystemJobs, | ||
} | ||
const wrapper = ({ children }) => ( | ||
<StoreContext.Provider value={store}> | ||
{children} | ||
</StoreContext.Provider> | ||
) | ||
const { result } = renderHook(() => useShowSystemJobs(), { wrapper }) | ||
|
||
expect(result.current).toBe(showSystemJobs) | ||
}) | ||
}) | ||
|
||
describe('useListJobs', () => { | ||
const user1 = { | ||
name: 'User One', | ||
configurable: true, | ||
} | ||
const user2 = { | ||
name: 'User Two', | ||
configurable: true, | ||
} | ||
const system1 = { | ||
name: 'System One', | ||
configurable: false, | ||
} | ||
const system2 = { | ||
name: 'System Two', | ||
configurable: false, | ||
} | ||
|
||
it('should return matching jobs when there is no filter and showSystemJobs is false', () => { | ||
const store = { | ||
jobFilter: [''], | ||
showSystemJobs: [false], | ||
jobs: [user1, user2, system1, system2], | ||
} | ||
const wrapper = ({ children }) => ( | ||
<StoreContext.Provider value={store}> | ||
{children} | ||
</StoreContext.Provider> | ||
) | ||
const { result } = renderHook(() => useListJobs(), { wrapper }) | ||
|
||
expect(result.current).toEqual(expect.arrayContaining([user1, user2])) | ||
}) | ||
|
||
it('should return matching jobs when there is no filter and showSystemJobs is true', () => { | ||
const store = { | ||
jobFilter: [''], | ||
showSystemJobs: [true], | ||
jobs: [user1, user2, system1, system2], | ||
} | ||
const wrapper = ({ children }) => ( | ||
<StoreContext.Provider value={store}> | ||
{children} | ||
</StoreContext.Provider> | ||
) | ||
const { result } = renderHook(() => useListJobs(), { wrapper }) | ||
|
||
expect(result.current).toEqual( | ||
expect.arrayContaining([user1, user2, system1, system2]) | ||
) | ||
}) | ||
|
||
it('should return matching jobs when there is a filter and showSystemJobs is false', () => { | ||
const store = { | ||
jobFilter: ['One'], | ||
showSystemJobs: [false], | ||
jobs: [user1, user2, system1, system2], | ||
} | ||
const wrapper = ({ children }) => ( | ||
<StoreContext.Provider value={store}> | ||
{children} | ||
</StoreContext.Provider> | ||
) | ||
const { result } = renderHook(() => useListJobs(), { wrapper }) | ||
|
||
expect(result.current).toEqual(expect.arrayContaining([user1])) | ||
}) | ||
|
||
it('should return matching jobs when there is a filter and showSystemJobs is true', () => { | ||
const store = { | ||
jobFilter: ['One'], | ||
showSystemJobs: [true], | ||
jobs: [user1, user2, system1, system2], | ||
} | ||
const wrapper = ({ children }) => ( | ||
<StoreContext.Provider value={store}> | ||
{children} | ||
</StoreContext.Provider> | ||
) | ||
const { result } = renderHook(() => useListJobs(), { wrapper }) | ||
|
||
expect(result.current).toEqual(expect.arrayContaining([user1, system1])) | ||
}) | ||
}) |
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import Store from './Store' | ||
import StoreContext from './StoreContext' | ||
import * as selectors from './selectors' | ||
import * as hooks from './hooks' | ||
|
||
export { Store, StoreContext, selectors } | ||
export { Store, StoreContext, selectors, hooks } |
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
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.