问题
我刚刚开始使用酶和反应测试。我试图让酶在一个微不足道的反应组件上与业力和 webpack 一起工作。我的问题是prop()
包装器返回 null 我不知道为什么。
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");
});
});
结果
使用浅渲染 × 渲染默认 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) 在 Assertion.ctx.(anonymous function) [as equal] (F:web/forms/node_modules/chai/chai.js:4192:25) 在 Context。(F:web/forms/spec/components/greeter.spec.js:9:2932 <- webpack:///spec/components/greeter.spec.js:18:52)
堆
- 反应 0.14.8
- 业力
- 网页包
- 酶2.2.0