-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduces async actions, pseudo-async API, middleware, and types for all of it. See: https://rjzaworski.com/2015/09/typescript-redux-async-actions
- Loading branch information
Showing
12 changed files
with
229 additions
and
22 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,20 @@ | ||
export const api = { | ||
save: (counter: { value: number }): Promise<null> => { | ||
try { | ||
localStorage.setItem('__counterValue', counter.value.toString()) | ||
return Promise.resolve(null) | ||
} | ||
catch (e) { | ||
return Promise.reject(e) | ||
} | ||
}, | ||
load: (): Promise<{ value: number }> => { | ||
try { | ||
const value = parseInt(localStorage.getItem('__counterValue'), 10) | ||
return Promise.resolve({ value }) | ||
} | ||
catch (e) { | ||
return Promise.reject(e) | ||
} | ||
}, | ||
} |
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,67 @@ | ||
import * as redux from 'redux' | ||
|
||
import { apiMiddleware } from '../' | ||
|
||
import { api } from '../../api' | ||
|
||
import { | ||
Action, | ||
loadCount, | ||
saveCount, | ||
} from '../../actions' | ||
|
||
const empty = () => {} | ||
|
||
const mockDispatch = (dispatch: (a: Action) => void): redux.MiddlewareAPI<any> => | ||
({ dispatch, getState: empty }) | ||
|
||
describe('apiMiddleware', () => { | ||
|
||
describe('when SAVE_COUNT_REQUEST succeeds', () => { | ||
|
||
it('includes request { value }', (done) => { | ||
const saveStub = sinon.stub(api, 'save') | ||
.returns(Promise.resolve({})) | ||
|
||
apiMiddleware(mockDispatch((actual: Action) => { | ||
expect(saveStub.firstCall.args[0].value).toEqual(13) | ||
saveStub.restore() | ||
done() | ||
}))(empty)(saveCount.request({ value: 13 })) | ||
}) | ||
|
||
it('fires SAVE_COUNT_SUCCESS', (done) => { | ||
const saveStub = sinon.stub(api, 'save') | ||
.returns(Promise.resolve({})) | ||
|
||
apiMiddleware(mockDispatch((actual: Action) => { | ||
saveStub.restore() | ||
expect(actual.type).toEqual('SAVE_COUNT_SUCCESS') | ||
done() | ||
}))(empty)(saveCount.request()) | ||
}) | ||
|
||
}) | ||
|
||
describe('when LOAD_COUNT_REQUEST succeeds', () => { | ||
|
||
it('fires LOAD_COUNT_SUCCESS', (done) => { | ||
const loadStub = sinon.stub(api, 'load') | ||
.returns(Promise.resolve({ value: 42 })) | ||
|
||
apiMiddleware(mockDispatch((actual: Action) => { | ||
loadStub.restore() | ||
|
||
if (actual.type === 'LOAD_COUNT_SUCCESS') { | ||
expect(42).toEqual(actual.response.value) | ||
done() | ||
} | ||
else { | ||
done.fail('types don\'t match') | ||
} | ||
}))(empty)(loadCount.request()) | ||
}) | ||
}) | ||
|
||
|
||
}) |
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,30 @@ | ||
import * as redux from 'redux' | ||
|
||
import { api } from '../api' | ||
|
||
import { | ||
Action, | ||
saveCount, | ||
loadCount, | ||
} from '../actions' | ||
|
||
export const apiMiddleware = ({ dispatch }: redux.MiddlewareAPI<any>) => | ||
(next: redux.Dispatch<any>) => | ||
(action: Action) => { | ||
switch (action.type) { | ||
|
||
case 'SAVE_COUNT_REQUEST': | ||
api.save(action.request) | ||
.then(() => dispatch(saveCount.success({}, action.request))) | ||
.catch((e) => dispatch(saveCount.error(e, action.request))) | ||
break | ||
|
||
case 'LOAD_COUNT_REQUEST': | ||
api.load() | ||
.then(({ value }) => dispatch(loadCount.success({ value }, action.request))) | ||
.catch((e) => dispatch(loadCount.error(e, action.request))) | ||
break | ||
} | ||
|
||
return next(action) | ||
} |
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