简体   繁体   English

无法在酶中设置输入元素的值

[英]Can't set input element value in Enzyme

In my test, I have this: 在我的测试中,我有这个:

let fromInput = wrapper.find('#starting-address').getElement();
fromInput.value = 'Foo';

However, I'm getting an error: 但是,出现错误:

TypeError: Cannot add property value, object is not extensible. TypeError:无法添加属性值,对象不可扩展。

I'm trying to test a button I have which, on clicking, should clear the value of the input element (id: 'starting-address'). 我正在尝试测试一个按钮,单击该按钮后应清除输入元素的值(id:'starting-address')。 I was planning on asserting that fromInput.value === 'foo' before simulating the click, and fromImput.value === '' after clicking. 我打算在模拟单击之前断言fromInput.value === 'foo' ,在单击之后fromImput.value === ''

This worked with me: 这与我一起工作:

it("Successfully add an employee to the employees' list when the form submitted",function(){
   const wrapper = mount(<App/>);
   const addEmpWapper = wrapper.find('AddEmployee');
   addEmpWapper.find("#txtName").getDOMNode().value = "Youki";
   addEmpWapper.find("#txtImgUrl").getDOMNode().value = "ImageUrl1";
   addEmpWapper.find("#txtDesc").getDOMNode().value = "Cute baby.";

   const form = addEmpWapper.find('form');
   form.simulate("submit");
   // I already had 3 employees in the app's states bag.
   expect(wrapper.state().employees).to.have.lengthOf(4);
 });

You need to use instance() instead: 您需要使用instance()代替:

const formInput = wrapper.find('#starting-address').instance();
formInput.value = 'Foo';

Check out this answer , for more details. 查看此答案 ,以获取更多详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM