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

时间:2018-07-03 06:58:39

标签: javascript testing enzyme

在我的测试中,我有这个:

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

但是,我遇到一个错误:

  

TypeError:无法添加属性值,对象不可扩展。

我正在尝试测试一个按钮,单击该按钮后应清除输入元素的值(id:“起始地址”)。我打算在模拟点击之前断言fromInput.value === 'foo',在点击之后断言fromImput.value === ''

2 个答案:

答案 0 :(得分:1)

这与我合作:

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);
 });

答案 1 :(得分:0)

您需要改用instance()

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

查看this answer,了解更多详细信息。