redux-mock-store
redux-mock-store copied to clipboard
Issue with the getState mock
Hi, so I am having an issue that's a variant of #71 , and I understand that getState returns the same state based on @dmitry-zaets explanation that the library is for testing the actions, but I have the two following files:
HttpClient-test.js
it('Should place cookie in header to be sent if there is one in local storage', () => {
const opts = {
headers: {}
}
const cookie = 'cookie'
const store = mockStore({
global: {
cookie: cookie
}
})
const client = new HttpClient(null, store.dispatch)
const expectedOpts = {
headers: {
'X-RSA-COOKIE': cookie
}
}
expect(client.sendRSACookie(opts)).toEqual(expectedOpts)
})
HttpClient.js
import {store} '../configureStore'
sendRSACookie (opts) {
if(_.isUndefined(opts.headers['X-RSA-COOKIE'])) {
this.dispatch(retrieveCookie)
const cookie = store.getState().global.cookie
if (!_.isUndefined(cookie) && cookie !== null) {
opts.headers['X-RSA-COOKIE'] = store.getState().global.cookie
}
}
return opts
}
and the test is failing because the store.getState().global.cookie is null. How would I be able to test this without attempting to override the getState function?
You can initialize the state to whatever you want, so there is no need to rewrite or override the getState function. Aside from this, I'm wondering how your code is working with a real Redux store. AFAIK, you should subscribe to state changes and only then you read the state.