Javascript 在酶中用参数模拟handler方法
我已经编写了一个绑定到Javascript 在酶中用参数模拟handler方法,javascript,jestjs,enzyme,Javascript,Jestjs,Enzyme,我已经编写了一个绑定到input元素的onChange事件的处理程序方法,它需要两个参数 <div className="p-lg-12"> <InputText value={this.state.blog.title} id="title" placeholder="Blog Title" onChange={e => this.handleInput("title", e
input
元素的onChange
事件的处理程序方法,它需要两个参数
<div className="p-lg-12">
<InputText
value={this.state.blog.title}
id="title"
placeholder="Blog Title"
onChange={e =>
this.handleInput("title", e.target.value)
}
/>
</div>
我无法在酶中模拟这种方法
it("updates blog data with user input", () => {
const blogEditor = shallow(<BlogEditor />);
blogEditor
.find({ placeholder: "Blog Title" })
.simulate("onChange", ("title", { target: { value: "x" } }));
expect(blogEditor.state().blog.title).toEqual("x");
});
it(“使用用户输入更新博客数据”,()=>{
const blogEditor=shallow();
博客编辑
.find({占位符:“博客标题”})
.simulate(“onChange”,(“title”,{target:{value:{x}}]);
expect(blogeeditor.state().blog.title).toEqual(“x”);
});
由此产生的错误是
expect(received).toEqual(expected) // deep equality
Expected: "x"
Received: ""
45 | .find({ placeholder: "Blog Title" })
46 | .simulate("onChange", ("title", { target: { value: "x" } }));
> 47 | expect(blogEditor.state().blog.title).toEqual("x");
| ^
48 | });
49 | });
50 |
at Object.<anonymous> (resources/js/tests/BlogEditor/BlogEditor.test.js:47:47)
expect(已接收).toEqual(预期)//深度相等
预期:“x”
收到:“”
45 |.查找({占位符:“博客标题”})
46 |。模拟(“onChange”(“title”),{target:{value:{x}});
>47 | expect(blogeeditor.state().blog.title).toEqual(“x”);
| ^
48 | });
49 | });
50 |
反对。(参考资料/js/tests/BlogEditor/BlogEditor.test.js:47:47)
那么,我是否正确地将参数传递给处理程序方法
另外,可能是find
方法没有返回我尝试过的正确元素
asfind(“#title”)
是HTML idtitle
作为这个组件作为5个输入 每个组件都具有唯一的id属性和占位符 属性值find({占位符:“Blog Title”})
blogeeditor
相关,而不是Input
,因此我建议使用mount()
而不是shall()
编辑
从以下文件:
常见问题
- 目前,浅层渲染器的事件模拟并不像人们在真实环境中通常期望的那样传播。因此,必须在设置了事件处理程序的实际节点上调用.simulate()
更多:所以有两个变化,比如@shaun-e-tobias正确地提到我们应该在这种情况下使用
mount
;并且必须添加一个at
方法,因为find
似乎返回了一个数组,更新了测试
it("updates blog data with user input", () => {
const blogEditor = mount(<BlogEditor />);
blogEditor
.find({ placeholder: "Blog Title" }).at(0).
.simulate("onChange", ("title", { target: { value: "x" } }));
expect(blogEditor.state().blog.title).toEqual("x");
});
我在change
事件中使用了错误的参数,正如enzyme
文档中所注意到的那样,因此有效的方法变成了
it("updates blog data with user input", () => {
const blogEditor = mount(<BlogEditor />);
blogEditor.find("#title").at(0).simulate("change",("title", { target: { value: "x" } }));
expect(blogEditor.state('blog').title).toEqual("x");
});
这有助于克服这个错误,谢谢!我现在有一个新的要处理:-)将添加详细信息我得到错误
TypeError:ReactWrapper::simulate()事件“onChange”不存在。模拟(“onChange”(“title”,{target:{value:{x”}}))
在blogeEditor.find(#title”).at(0.props().onChange(((“title”,{target:{value:'x}))时给出上述错误代码>工作
TypeError: ReactWrapper::simulate() event 'onChange' does not exist
it("updates blog data with user input", () => {
const blogEditor = mount(<BlogEditor />);
blogEditor.find("#title").at(0).simulate("change",("title", { target: { value: "x" } }));
expect(blogEditor.state('blog').title).toEqual("x");
});
blogEditor.find("#title").at(0).props().onChange(("title", { target: { value: "x" } }));