This repository was archived by the owner on Nov 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
Use redux-thunk for side effects #176
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
b8ab3ce
Try out redux-thunk
kumar303 fdb9c24
Add ThunkActionCreator
kumar303 772aa66
Fix ThunkMiddleware
kumar303 a591bad
Add alias for ThunkDispatch
kumar303 cc84fea
Lint cleanup
kumar303 c41bea7
Prettier
kumar303 d665ec9
Maybe some better comments
kumar303 364efbe
Moved helper to test-helpers
kumar303 8dc2f62
Fixed type of dispatch in ConnectedReduxProps. Duh.
kumar303 52fa64a
Better names for things, maybe
kumar303 9f40d78
A better thunk tester
kumar303 95beacd
Simplified use of this helper
kumar303 6293dcc
Fix lint
kumar303 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -11,6 +11,7 @@ describe(__filename, () => { | |
}; | ||
|
||
const wrapper = () => { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess this resolves #174 :D |
||
return (WrappedComponent: any) => { | ||
return (props: object) => { | ||
return <WrappedComponent {...props} />; | ||
|
This file contains hidden or 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,7 +1,12 @@ | ||
import PropTypes from 'prop-types'; | ||
import { History, Location } from 'history'; | ||
import { shallow } from 'enzyme'; | ||
import { Store } from 'redux'; | ||
|
||
import configureStore, { | ||
ApplicationState, | ||
ThunkActionCreator, | ||
} from './configureStore'; | ||
import { ExternalUser } from './reducers/users'; | ||
import { | ||
ExternalVersion, | ||
|
@@ -167,7 +172,9 @@ type ShallowUntilTargetOptions = { | |
}; | ||
|
||
export const shallowUntilTarget = ( | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
componentInstance: React.ReactElement<any>, | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
targetComponent: React.JSXElementConstructor<any>, | ||
{ | ||
maxTries = 10, | ||
|
@@ -197,3 +204,79 @@ export const shallowUntilTarget = ( | |
gave up after ${maxTries} tries`, | ||
); | ||
}; | ||
|
||
/* | ||
* Creates a fake thunk for testing. | ||
* | ||
* Let's say you had a real thunk like this: | ||
* | ||
* const doLogout = () => { | ||
* return (dispatch, getState) => { | ||
* // Make a request to the API... | ||
* dispatch({ type: 'LOG_OUT' }); | ||
* }; | ||
* }; | ||
* | ||
* You can replace this thunk for testing as: | ||
* | ||
* const fakeThunk = createFakeThunk(); | ||
* render({ _doLogout: fakeThunk.createThunk }); | ||
* | ||
* You can make an assertion that it was called like: | ||
* | ||
* expect(dispatch).toHaveBeenCalledWith(fakeThunk.thunk); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ❤️ |
||
*/ | ||
export const createFakeThunk = () => { | ||
// This is a placeholder for the dispatch callback function, | ||
// the thunk itself. | ||
// In reality it would look like (dispatch, getState) => {} | ||
// but here it gets set to a string for easy test assertions. | ||
const dispatchCallback = '__thunkDispatchCallback__'; | ||
|
||
return { | ||
// This is a function that creates the dispatch callback. | ||
createThunk: jest.fn().mockReturnValue(dispatchCallback), | ||
thunk: dispatchCallback, | ||
}; | ||
}; | ||
|
||
/* | ||
* Sets up a thunk for testing. | ||
* | ||
* Let's say you had a real thunk like this: | ||
* | ||
* const doLogout = () => { | ||
* return (dispatch, getState) => { | ||
* // Make a request to the API... | ||
* dispatch({ type: 'LOG_OUT' }); | ||
* }; | ||
* }; | ||
* | ||
* You can set it up for testing like this: | ||
* | ||
* const { dispatch, thunk, store } = thunkTester({ | ||
* createThunk: () => doLogout(), | ||
* }); | ||
* | ||
* await thunk(); | ||
* | ||
* expect(dispatch).toHaveBeenCalledWith({ type: 'LOG_OUT' }); | ||
* | ||
*/ | ||
export const thunkTester = ({ | ||
store = configureStore(), | ||
createThunk, | ||
}: { | ||
store?: Store<ApplicationState>; | ||
createThunk: () => ThunkActionCreator; | ||
}) => { | ||
const thunk = createThunk(); | ||
const dispatch = jest.fn(); | ||
|
||
return { | ||
dispatch, | ||
// This simulates how the middleware will run the thunk. | ||
thunk: () => thunk(dispatch, () => store.getState(), undefined), | ||
store, | ||
}; | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.