[英]Can't understand why similiar test make 1 pass and not the other, can't spot it
這是一個模擬文本區域中存在的注釋的簡單測試,而第一個測試通過時,下一個模擬從文本區域提交文本,運行handleSubmit(目前已連接到我的表單),該操作清除了文本區域,但是測試指出了該注釋是預期的(下注)到=> .toEqual(“”);
beforeEach(() => {
wrapper.find("textarea").simulate("change", {
target: { value: "new comment" }
});
wrapper.update();
});
it("has a text area that user can type into", () => {
expect(wrapper.find("textarea").prop("value")).toEqual("new comment");
});
it("simulate button click (in real form submit) and clear textarea", () => {
wrapper.update();
wrapper.find("form").simulate("submit");
wrapper.update();
expect(wrapper.find("textarea").prop("value")).toEqual("");
});
});
edit: here's the component I wanted to test...
import React, { Component } from "react";
import { connect } from "react-redux";
import * as actions from "../actions";
class CommentBox extends Component {
state = {
comment: ""
};
handleChange = e => {
e.preventDefault();
this.setState({ comment: e.target.value });
};
handleSubmit = e => {
e.preventDefault();
this.props.saveComment(this.state.comment);
this.setState({ comment: "" });
};
render() {
return (
<form onSubmit={this.handleSubumit}> <-------------- TYPO :(
<h2>Add a comment:</h2>
<textarea onChange={this.handleChange} value={this.state.comment} />
<div>
<button>Submit comment</button>
</div>
</form>
);
}
}
export default connect(
null,
actions
)(CommentBox);
beforeEach(() => { wrapper.find("textarea").simulate("change", { target: { value: "new comment" } }); wrapper.update(); }); it("has a text area that user can type into", () => { expect(wrapper.find("textarea").prop("value")).toEqual("new comment"); }); it("simulate button click (in real form submit) and clear textarea", () => { wrapper.find("form").simulate("submit"); wrapper.update(); expect(wrapped.find("textarea").prop("value").length).toEqual(0); }); });
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.