【发布时间】:2019-01-20 00:49:02
【问题描述】:
我有一个带有 Redux @connect 装饰器的 react 组件,例如:
import React, { Component } from 'react'
import { connect } from 'react-redux'
@connect(mapStateToProps,
{
onPress: () => {...code}) // Component receives this func, not passed from the test
}
export class Component extends Component {
render () {
return <button onclick={this.props.onPress>....</button>
}
}
我遇到了一个问题,在测试文件中传递给组件的模拟函数没有传递给组件:
const store = createStore(
combineReducers({name: reducer})
);
const ComponentConnected = connect(..., {
onPress: {jest.fn()} // Component doesn't receive this mock
})(() =>(<Component />));
describe('Component testing', () => {
it('should render component', () => {
const wrapper = mount(
<Provider store={store}>
<ComponentConnected />
</Provider>
);
expect(wrapper.find(Component)).toHaveLength(1);
});
});
另外,我尝试将模拟函数作为经过测试的组件道具传递,它也没有帮助。不重写组件@connect装饰器是否可以解决这个问题?
【问题讨论】:
标签: javascript reactjs unit-testing react-redux jestjs