0

我正在尝试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 容器 › 渲染不崩溃

类型错误:state.get 不是函数

4

1 回答 1

0

你应该挂载你的组件包裹在一个Provider

import { Provider } from 'react-redux'

// ...store configuration

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

// ... assertions

shallow还有一个用于传递上下文的第二个参数,但我不确定它是否正在使用 redux 存储。

于 2020-06-15T12:46:03.890 回答