简体   繁体   中英

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.

I'm trying to test a button I have which, on clicking, should clear the value of the input element (id: 'starting-address'). I was planning on asserting that fromInput.value === 'foo' before simulating the click, and fromImput.value === '' after clicking.

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:

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

Check out this answer , for more details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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