无法初始化Redux模拟存储

时间:2018-07-04 14:56:34

标签: javascript unit-testing redux mocking redux-mock-store

我正在尝试根据文档使用redux-mock-store

import React from 'react'
import Enzyme from 'enzyme'
import Adapter from 'enzyme-adapter-react-16'
import configureStore from 'redux-mock-store'

import App from '.'
import rootReducer from '../../store/reducers'

Enzyme.configure({ adapter: new Adapter() })

describe('The App container', () => {
    let store
    const mockStore = configureStore()

    beforeEach(() => store = mockStore(rootReducer))

    it('renders without crashing', () => {
        const tree = Enzyme.shallow(<App store={store} />)
        expect(tree).toMatchSnapshot()
    })
})

问题是,出现以下错误:

  

Reducer收到的先前状态是意外类型。预期参数为具有以下属性的Immutable.Collection或Immutable.Record的实例:

以及:

  

TypeError:inputState.withMutations不是函数

我是否应该不包括rootReducer

是否有更好的方法来初始化模拟存储?


编辑:我也尝试过使用initialState来启动商店,但没有成功:

import React from 'react'
import Enzyme from 'enzyme'
import Adapter from 'enzyme-adapter-react-16'
import configureStore from 'redux-mock-store'

import App from '.'

Enzyme.configure({ adapter: new Adapter() })

describe('The App container', () => {
    let store
    const mockStore = configureStore()
    const initialState = {}

    beforeEach(() => store = mockStore(initialState))

    it('renders without crashing', () => {
        const tree = Enzyme.shallow(<App store={store} />)
        expect(tree).toMatchSnapshot()
    })
})
  

●App容器›呈现而不会崩溃

     

TypeError:state.get不是函数

1 个答案:

答案 0 :(得分:0)

您应该安装包裹在Provider中的组件:

import { Provider } from 'react-redux'

// ...store configuration

const tree = Enzyme.mount(
  <Provider store={store}>
    <App />
  <Provider>
)

// ... assertions

shallow还具有用于传递上下文的第二个参数,但是我不确定它是否可以与redux store一起使用。