【发布时间】:2018-11-06 17:57:50
【问题描述】:
我正在尝试使用 Jest 和 Enzyme 测试我的智能组件,但它没有要呈现的数据,因为它应该是通过操作获取的。错误是:getTasks is not a function
Enzyme.configure({ adapter: new Adapter() });
describe('Main', () => {
describe('when loading is true', () => {
it('should render loading div', () => {
const wrapper = mount(<Main.WrappedComponent loading={true} />);
expect(wrapper.html()).toEqual('<div>Loading</div>');
wrapper.unmount();
});
});
});
这是我正在尝试测试的组件,它通过操作获取数据,然后用它们做一些事情,但如果没有数据(loading === true)它只会渲染带有“正在加载”文本的<div> . getTasks() 只是import 数据:
class Main extends React.Component {
constructor(props) {
super();
this.handleData = this.handleData.bind(this);
this.handleHigh = this.handleHigh.bind(this);
}
componentDidMount() {
const { getTasks } = this.props;
getTasks();
}
render() {
const { data, loading } = this.props;
if (!loading) {
this.handleData(data);
return (
{data.map(task => {
if (task.obj_status === 'active')
return (
// Doing some stuff with data here
);
} else {
return <div>Loading</div>;
}
}
}
const mapStateToProps = state => ({
data: state.main.data,
loading: state.main.loading
});
const mapDispatchToProps = dispatch => ({
...bindActionCreators(
{
getTasks: loadTasks,
dispatch
},
dispatch
)
});
export default withRouter(
connect(
mapStateToProps,
mapDispatchToProps
)(Main)
);
【问题讨论】:
-
你能展示 WrappedComponent 吗?
-
您需要模拟来自服务器的响应。您可以使用 nock 模块
-
@AdrianPażucha 完成
-
@IuriiBudnikov 它只是从本地存储导入,没有服务器甚至
fetchfunc,但我明白你的意思。你可以给我一个例子的链接吗? -
@IuriiBudnikov 我检查了
nock,但它仅适用于 HTTP 请求,我那里没有 HTTP
标签: javascript reactjs react-redux jestjs enzyme