【发布时间】:2019-02-27 19:36:30
【问题描述】:
我在测试一个 prop 在我的 HOC 上被触发时遇到了一些麻烦。
import { connect } from 'react-redux';
import { compose, lifecycle } from 'recompose';
import { fetchCurrentUser } from '../../actions/users';
import { getUser } from '../../reducers/users';
import User from '../../models/User';
export default Component => compose(
connect(state => ({
user: getUser(state),
}),
{ fetchCurrentUser }),
lifecycle({
componentDidMount() {
if (this.props.user instanceof User) return;
this.props.fetchCurrentUser();
},
}),
)(Component);
我想知道fetchCurrentUser 是否在user 不是用户实例时触发。
到目前为止,我的测试中有这个:
it.only('fetches user if user is not a User instance', () => {
const setup = () => {
const props = {
user: 'string',
fetchCurrentUser: jest.fn(),
};
const enzymeWrapper = mounting(props);
return {
props,
enzymeWrapper,
};
};
// That returns 0 so false
expect(setup().props.fetchCurrentUser.mock.calls.length).toEqual(1);
});
似乎我无法以这种方式替换道具。如果我在生命周期方法中记录this.props,我永远不会看到user: 'string'
提前致谢
【问题讨论】:
标签: reactjs testing higher-order-components recompose