我在使用模拟商店测试 thunk 时遇到问题。
在我的 thunk 中,我调用 getState() 来获取 redux 存储的状态,然后根据状态调度操作。
如何让我的 thunk getState() 调用来检查模拟存储状态而不是 redux 存储状态?
import {initialState} from '../configureStore'
import {saveAndSendTask} from '../thunks/queueThunk'
import configureMockStore from 'redux-mock-store'
import thunk from 'redux-thunk'
import * as actions from '../actions/index'
const middlewares = [thunk]
const mockStore = configureMockStore(middlewares)
describe('Testing TaskQueue Thunk', () => {
const store = mockStore(initialState)
test('Test TaskQueue thunk sends to sever and removes from queue.', () => {
// removed variables test data for the sake of brevity.
// Add actions to Mock Store and set up Base state needed to test.
store.dispatch(actions.addToTaskQueue(task1))
store.dispatch(actions.addToTaskQueue(task2))
store.dispatch(actions.setTasks([task1, task2, task3]))
store.dispatch(actions.selectTask(task3.id))
store.dispatch(actions.isConnected(true))
// This is the thunk I want to test.
store.dispatch(saveAndSendTask())
expect('something').toEqual('something')
})
)}
这是我要测试的thunk。
export const saveAndSendTask = (): any => {
return (dispatch: Dispatch, getState) => {
// This state is the same as initial state of redux store. Ignores Mock store state.
console.log(getState())
// Selected task is undefined.
dispatch(addToTaskQueue(getState().tasks.selectedTask))
// ....rest of logic.....
}
}