方法“props”仅用于在单个节点上运行。 0找到了。酶 - 错误
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了方法“props”仅用于在单个节点上运行。 0找到了。酶 - 错误相关的知识,希望对你有一定的参考价值。
在我的反应应用程序中,我正在使用jest和酶测试我的容器。我正在测试我的容器的createAgent功能。但是我的测试失败了,我遇到了下面的错误。
Container.js
import React, { Component } from "react";
import { connect } from "react-redux";
import PropTypes from "prop-types";
import { push } from "react-router-redux";
import { getIds, getAgents, getAgent } from "../../reducers/agents";
import {
readAgents,
confirmDeleteAgent,
cancelDeleteAgent,
deleteAgent
} from "../../actions";
import List from "../../components/List";
class Agents extends Component {
static propTypes = {
//props
};
createAgent = () => {
this.props.push("/agents/add");
};
render() {
//render code
return (
<button className="ui primary button"
onClick={this.createAgent}>
Add
</button>
//render code
)
}
};
containerTest.js
import React from "react";
import ReactDOM from "react-dom";
import { Provider } from "react-redux";
import configureStore from "../../store/configureStore";
import Agents from "./index";
import { mount, shallow } from "enzyme";
it("should call instance methods on agents", () => {
const store = configureStore();
let mockCreateAgent = jest.fn();
Agents.prototype.createAgent = mockCreateAgent;
const wrapper = shallow(
<Provider store={store}>
<Agents />
</Provider>
);
wrapper.find('.ui primary button').simulate('click');
expect(mockCreateAgent).toHaveBeenCalled();
});
错误:
Method “props” is only meant to be run on a single node. 0 found
instead.
at ShallowWrapper.single
(node_modules/enzyme/build/ShallowWrapper.js:1516:17)
at ShallowWrapper.props
(node_modules/enzyme/build/ShallowWrapper.js:867:21)
at ShallowWrapper.prop
(node_modules/enzyme/build/ShallowWrapper.js:1075:21)
at ShallowWrapper.simulate
(node_modules/enzyme/build/ShallowWrapper.js:838:28)
at Object.<anonymous>.it
(src/containers/Agents/Agents.test.js:31:38)
at Promise (<anonymous>)
at Promise.resolve.theI.el (node_modules/p-map/index.js:42:16)
at <anonymous>
我该如何解决这个问题?我是使用jest和酶进行测试的新手。任何帮助都会非常明显。
答案
实际上你是以错误的方式找到元素。您已为按钮声明了类,但您将其视为元素。检查以下更正代码。
wrapper.find('button.ui.primary.button').simulate('click');
测试它,如果您遇到任何问题,请告诉我。
另一答案
使用.first()
示例const wrapper = shallow()
wrapper.find('button h1或p或.ClassName或#id')。first()。simulate('click');;
import React from 'react'
import { shallow } from 'enzyme'
import renderer from 'react-test-renderer'
import About from '../pages/about.js'
describe('With Enzyme', () => {
it('App shows "About"', () => {
const wrapper = shallow(
<About />
)
wrapper.find('button.ui.primary.button').first().simulate('click');
})
})
以上是关于方法“props”仅用于在单个节点上运行。 0找到了。酶 - 错误的主要内容,如果未能解决你的问题,请参考以下文章
st_network_paths 仅生成带有单个节点的 node_path