Enzyme returns null for prop on shallow rendered react component
Enzyme returns null for prop on shallow rendered react component
问题
我刚刚开始进行酶和反应测试。我正在尝试让酶与 karma 和 webpack 在一个简单的反应组件上一起工作。我的问题是包装 returns 上的 prop()
为空,我不确定为什么。
Greeter.js
import React from 'react';
/*
* A trivial component we added while trying to get the react testing working
* */
export default class Greeter extends React.Component {
constructor(props) {
super(props);
this.state = { name: props.initialName };
this.greeterText = this.greeterText.bind(this);
}
greeterText() {
return (<p>Hello, {this.state.name}!</p>);
}
render() {
return (<div className="greeter">
{this.greeterText()}
</div>);
}
}
Greeter.defaultProps = { initialName: "World" };
Greeter.propTypes = {
initialName: React.PropTypes.string
};
greeter.spec.js
import Greeter from '../../components/test/Greeter';
import React from 'react';
import { shallow } from 'enzyme';
describe("Simple testing with shallow rendering", () => {
beforeEach(function() {
let initialName = "joe";
this.wrapper = shallow(<Greeter initialName={initialName} />);
});
it("renders default Greeter", function() {
console.log(this.wrapper.find(".greeter").text());
console.log(this.wrapper.debug());
console.log(this.wrapper.state('name'));
expect(this.wrapper.state('name')).to.equal("joe");
expect(this.wrapper.prop('initialName')).to.equal("joe");
});
});
结果
Simple testing with shallow rendering
× renders default Greeter
Chrome 50.0.2661 (Windows 10 0.0.0)
AssertionError: expected undefined to equal 'joe'
at Assertion.assertEqual >>(F:/web/forms/node_modules/chai/chai.js:776:12)
at Assertion.ctx.(anonymous function) [as equal] (F:web/forms/node_modules/chai/chai.js:4192:25)
at Context. (F:web/forms/spec/components/greeter.spec.js:9:2932 <- webpack:///spec/components/greeter.spec.js:18:52)
堆栈
- 反应 0.14.8
- 业力
- webpack
- 酵素 2.2.0
如果你想像这样测试传入的道具使用mount
像这样:
it('allows us to set props', () => {
const wrapper = mount(<Foo bar="baz" />);
expect(wrapper.props().bar).to.equal("baz");
wrapper.setProps({ bar: "foo" });
expect(wrapper.props().bar).to.equal("foo");
});
我相信您正在寻找的是wrapper.instance().props.nameOfProp
访问浅包装器的根节点道具。
从概念上讲,假设您有一个在安装时有条件地运行 Function prop 的组件。这当然值得一试,对吧?
问题
我刚刚开始进行酶和反应测试。我正在尝试让酶与 karma 和 webpack 在一个简单的反应组件上一起工作。我的问题是包装 returns 上的 prop()
为空,我不确定为什么。
Greeter.js
import React from 'react';
/*
* A trivial component we added while trying to get the react testing working
* */
export default class Greeter extends React.Component {
constructor(props) {
super(props);
this.state = { name: props.initialName };
this.greeterText = this.greeterText.bind(this);
}
greeterText() {
return (<p>Hello, {this.state.name}!</p>);
}
render() {
return (<div className="greeter">
{this.greeterText()}
</div>);
}
}
Greeter.defaultProps = { initialName: "World" };
Greeter.propTypes = {
initialName: React.PropTypes.string
};
greeter.spec.js
import Greeter from '../../components/test/Greeter';
import React from 'react';
import { shallow } from 'enzyme';
describe("Simple testing with shallow rendering", () => {
beforeEach(function() {
let initialName = "joe";
this.wrapper = shallow(<Greeter initialName={initialName} />);
});
it("renders default Greeter", function() {
console.log(this.wrapper.find(".greeter").text());
console.log(this.wrapper.debug());
console.log(this.wrapper.state('name'));
expect(this.wrapper.state('name')).to.equal("joe");
expect(this.wrapper.prop('initialName')).to.equal("joe");
});
});
结果
Simple testing with shallow rendering × renders default Greeter Chrome 50.0.2661 (Windows 10 0.0.0) AssertionError: expected undefined to equal 'joe' at Assertion.assertEqual >>(F:/web/forms/node_modules/chai/chai.js:776:12) at Assertion.ctx.(anonymous function) [as equal] (F:web/forms/node_modules/chai/chai.js:4192:25) at Context. (F:web/forms/spec/components/greeter.spec.js:9:2932 <- webpack:///spec/components/greeter.spec.js:18:52)
堆栈
- 反应 0.14.8
- 业力
- webpack
- 酵素 2.2.0
如果你想像这样测试传入的道具使用mount
像这样:
it('allows us to set props', () => {
const wrapper = mount(<Foo bar="baz" />);
expect(wrapper.props().bar).to.equal("baz");
wrapper.setProps({ bar: "foo" });
expect(wrapper.props().bar).to.equal("foo");
});
我相信您正在寻找的是wrapper.instance().props.nameOfProp
访问浅包装器的根节点道具。
从概念上讲,假设您有一个在安装时有条件地运行 Function prop 的组件。这当然值得一试,对吧?