-
Notifications
You must be signed in to change notification settings - Fork 149
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Mock Store State is not being updated #71
Comments
As you can see from codebase - it doesn't execute any reducers. |
OK, that's what I was thinking. Is there a reason for |
I'm not sure, but I think the main reason is backward compatibility with redux API. |
Ah, OK. So I was thinking that I'd be able to test the state along with testing what actions were triggered, but |
The one place this kind of falls down is when you're testing something that uses an async action creator (thunks), that change the state before triggering other actions. |
Yep, |
For testing, complex flows with async actions you can check https://github.com/redux-things/redux-actions-assertions, it was designed for that kind of testing. |
That is exactly what I needed. Thanks, @dmitry-zaets! |
Always welcome! |
God damnit, I just found this after pulling my hair for two hours. I think it should be added to the docs very explicitly that the mock stores' state is never changed, as one would assume it does when it's in the API. Reading the discussion here, it does make sense, but it's potentially a timewaster |
This is definitely something that should have documented in README.md. @dmitry-zaets Can I document it and make a pull request not to confuse the newbie here? |
@ohtangza Sure, would be happy to merge the PR |
Testing your reducer alongside your actions with redux-mock-store is as simple as running your reducer function on the state that you pass to the mock store and the action you expect: import actions from './actions'
import reducer from './reducer'
import initialState from './initialState'
const state = { ...initialState, numbers: '23' }
const expectedAction = { type: 'DELETE_LAST_NUMBER', payload: '*' }
return store.dispatch(actions.pressKey('*')).then(() => {
expect(store.getActions()[0]).toEqual(expectedAction)
expect(reducer(state, expectedAction)).toEqual({ ...state, numbers: '2' })
}) |
I had the same issue, and found I kept writing the same lines over and over to test store, so wrote a tiny lib on top of redux-mock-store to capture snapshots of reducer states here |
Incidentally, this can be achieved via the following:
This is particularly useful if you're dispatching thunks which rely on |
Modified version of pzhine's answer that supports Immutable and calling the same action creator multiple times in a different action creator (we have an async action creator that we pass a synchronous action creator to). import { fromJS } from "immutable"
import mockStore from "__tests__helpers/mockStore"
import { setTranscriptsValues } from "shared/profiles/event/event-profile-action-creators"
import { eventReducer } from "shared/profiles/event/event-profile-reducer"
let store = mockStore({})
const setTranscriptsValuesHelper = ({
values,
profileId,
}) => {
store = mockStore(
eventReducer(
fromJS(store.getState()),
setTranscriptsValues({
values,
profileId,
})
)
)
}
beforeEach(() => {
store = mockStore({})
})
it("loadInlines", () => {
loadInlines({
...stuff
setTranscriptsValues: setTranscriptsValuesHelper,
}).then(() => {
console.log(store.getState())
})
}) |
@mbellman thanks man! It just works fine. |
I finally get to test the final store state like this:
The reducer is a pure function, so in order to get the final state we just has to call every action on top of an initial state. This way every action receive the previous state and return a new one until the end of the array. |
I'm having the same issue as #45. Maybe I'm not understanding this correctly, but does mock-store actually run the actions through the reducers anywhere? I'm not seeing how they're ever connected with a setup like this:
The text was updated successfully, but these errors were encountered: