为什么 mapStateToProps 没有映射到正确的道具?

Why mapStateToProps do not map to the right prop?

我有这个容器

import { connect } from 'react-redux'
import { createType, getTypes } from '../modules/type'

import Type from '../components/Type'

const mapDispatchToProps = {
  createType,
  getTypes
}

const mapStateToProps = state => ({
  types: state.type.types
})

export default connect(mapStateToProps, mapDispatchToProps)(Type)

并且我想使用 enzyme 对其进行测试。为此,我正在使用此测试

import React from 'react'
import { Provider } from 'react-redux'
import { mount } from 'enzyme'

import TypeContainer from 'routes/Type/containers/TypeContainer'

import configureMockStore from 'redux-mock-store';
import thunk from 'redux-thunk';

const mockStore = configureMockStore([ thunk ]);
const mockStoreInitialized = mockStore({
  type: {
    types: [
      {id: 1, name: 'type 1'}
    ]
  }
});

describe.only('(Route) Type', () => {
  it('should get container', () => {
    const wrapper = mount(
      <Provider store={mockStoreInitialized}>
        <TypeContainer />
      </Provider>
    )

    expect(wrapper.find(TypeContainer).prop('types')).to.deep.equal([{id: 1, name: 'type 1'}])
  })
})

测试失败(在断言级别),因为 wrapper.find(TypeContainer).props() 为空。我找不到原因。

奇怪的是,如果我检查覆盖率报告,我的测试通过了 mapStateToProps 函数。

我是不是漏掉了什么?

TypeContainer 不会有名为 types 的道具,它会从商店中拉取 types 并将其传递给 Type 有一个名为types 的道具。所以这并不是说 mapStateToProps 没有做正确的事;而是说 mapStateToProps 没有做对。只是你对错误的对象做出了断言。