使用jest
和enzyme
进行测试,我认为我所做的一切都正确,但是以某种方式得到警告:
警告:道具类型失败:道具
project
在ProjectPageUI
中标记为必需,但其值为undefined
。在ProjectPageUI中
这里是测试的样子:
test('shallow-render without crashing', () => {
/*
project: PropTypes.object.isRequired,
*/
const props = {
match: { params: {} },
}
const store = configureStore()(STATE_WITH_2_FAMILIES)
shallow(<ProjectPageUIComponent store={store} {...props} />)
})
在ProjectPageUI.jsx
内部,我使用选择器获得了project
属性:
const mapStateToProps = (state, ownProps) => ({
project: getCurrentProject(state),
})
getCurrentProject
如下:
export const getProjectsByGuid = state => state.projectsByGuid
export const getProjectGuid = state => state.currentProjectGuid
export const getCurrentProject = createSelector(
getProjectsByGuid, getProjectGuid, (projectsByGuid, currentProjectGuid) => projectsByGuid[currentProjectGuid],
)
在STATE_WITH_2_FAMILIES
中,我同时定义了projectsByGuid
和currentProjectGuid
,并且projectsByGuid
分别具有currentProjectGuid
和定义的对象的键。因此,现在我想知道为什么会发生此警告,以及应该如何更改测试,因为我希望它完全没有问题:选择器从project
中获取state
,应该为defined
。
尝试使用redux Provider组件包装您的组件
shallow(
<Provider store={store}>
<ProjectPageUIComponent {...props} />
</Provider>)