Skip to content

Commit

Permalink
Add getIsDone to request and requestById
Browse files Browse the repository at this point in the history
  • Loading branch information
alvaro-cuesta committed Feb 5, 2021
1 parent ce8937f commit a19b7c2
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 2 deletions.
3 changes: 3 additions & 0 deletions lib/request/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ makeRequestReducer.getError = state =>
makeRequestReducer.getIsLoading = state =>
state.state === STATES.EMPTY || state.state === STATES.REQUESTING

makeRequestReducer.getIsDone = state =>
!makeRequestReducer.getIsLoading(state)

makeRequestReducer.STATES = STATES

makeRequestReducer.INITIAL_STATE = INITIAL_STATE
Expand Down
5 changes: 5 additions & 0 deletions lib/request/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ describe('request', () => {
expect(request.getIsError(nextState)).toEqual(false)
expect(request.getError(nextState)).toEqual(undefined)
expect(request.getIsLoading(nextState)).toEqual(true)
expect(request.getIsDone(nextState)).toEqual(false)
})
})

Expand All @@ -26,6 +27,7 @@ describe('request', () => {
expect(request.getIsError(nextState)).toEqual(false)
expect(request.getError(nextState)).toEqual(undefined)
expect(request.getIsLoading(nextState)).toEqual(true)
expect(request.getIsDone(nextState)).toEqual(false)
})

it('success', () => {
Expand All @@ -38,6 +40,7 @@ describe('request', () => {
expect(request.getIsError(nextState)).toEqual(false)
expect(request.getError(nextState)).toEqual(undefined)
expect(request.getIsLoading(nextState)).toEqual(false)
expect(request.getIsDone(nextState)).toEqual(true)
})

it('error', () => {
Expand All @@ -50,6 +53,7 @@ describe('request', () => {
expect(request.getIsError(nextState)).toEqual(true)
expect(request.getError(nextState)).toEqual('foobar')
expect(request.getIsLoading(nextState)).toEqual(false)
expect(request.getIsDone(nextState)).toEqual(true)
})

it('clear', () => {
Expand All @@ -62,6 +66,7 @@ describe('request', () => {
expect(request.getIsError(nextState)).toEqual(false)
expect(request.getError(nextState)).toEqual(undefined)
expect(request.getIsLoading(nextState)).toEqual(true)
expect(request.getIsDone(nextState)).toEqual(false)
})
})
})
3 changes: 3 additions & 0 deletions lib/requestById/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ makeRequestByIdReducer.getErrorById = id =>
makeRequestByIdReducer.getIsLoadingById = id =>
F.pipe(makeRequestByIdReducer.getById(id), request.getIsLoading)

makeRequestByIdReducer.getIsDoneById = id =>
F.pipe(makeRequestByIdReducer.getById(id), request.getIsDone)

makeRequestByIdReducer.STATES = request.STATES

export default makeRequestByIdReducer
9 changes: 7 additions & 2 deletions lib/requestById/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ describe('requestById', () => {
expect(requestById.getIsErrorById('foo')(nextState)).toEqual(false)
expect(requestById.getErrorById('foo')(nextState)).toEqual(undefined)
expect(requestById.getIsLoadingById('foo')(nextState)).toEqual(true)
expect(requestById.getIsDoneById('foo')(nextState)).toEqual(false)
})
})

Expand All @@ -26,6 +27,7 @@ describe('requestById', () => {
expect(requestById.getIsErrorById('foo')(nextState)).toEqual(false)
expect(requestById.getErrorById('foo')(nextState)).toEqual(undefined)
expect(requestById.getIsLoadingById('foo')(nextState)).toEqual(true)
expect(requestById.getIsDoneById('foo')(nextState)).toEqual(false)
})

it('success', () => {
Expand All @@ -38,13 +40,14 @@ describe('requestById', () => {
expect(requestById.getIsErrorById('foo')(nextState)).toEqual(false)
expect(requestById.getErrorById('foo')(nextState)).toEqual(undefined)
expect(requestById.getIsLoadingById('foo')(nextState)).toEqual(false)
expect(requestById.getIsDoneById('foo')(nextState)).toEqual(true)
})

it('error', () => {
const reducer = requestById({ error: 'ERROR' })
const nextState = reducer(undefined, {
type: 'ERROR',
payload: { id: 'foo', error: 'error' }
type: 'ERROR',
payload: { id: 'foo', error: 'error' }
})

expect(requestById.getIsEmptyById('foo')(nextState)).toEqual(false)
Expand All @@ -53,6 +56,7 @@ describe('requestById', () => {
expect(requestById.getIsErrorById('foo')(nextState)).toEqual(true)
expect(requestById.getErrorById('foo')(nextState)).toEqual('error')
expect(requestById.getIsLoadingById('foo')(nextState)).toEqual(false)
expect(requestById.getIsDoneById('foo')(nextState)).toEqual(true)
})

it('clear', () => {
Expand All @@ -65,6 +69,7 @@ describe('requestById', () => {
expect(requestById.getIsErrorById('foo')(nextState)).toEqual(false)
expect(requestById.getErrorById('foo')(nextState)).toEqual(undefined)
expect(requestById.getIsLoadingById('foo')(nextState)).toEqual(true)
expect(requestById.getIsDoneById('foo')(nextState)).toEqual(false)
})
})
})

0 comments on commit a19b7c2

Please sign in to comment.