[英]Can't set input element value in Enzyme
在我的測試中,我有這個:
let fromInput = wrapper.find('#starting-address').getElement();
fromInput.value = 'Foo';
但是,出現錯誤:
TypeError:無法添加屬性值,對象不可擴展。
我正在嘗試測試一個按鈕,單擊該按鈕后應清除輸入元素的值(id:'starting-address')。 我打算在模擬單擊之前斷言fromInput.value === 'foo'
,在單擊之后fromImput.value === ''
。
這與我一起工作:
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);
});
您需要使用instance()代替:
const formInput = wrapper.find('#starting-address').instance();
formInput.value = 'Foo';
查看此答案 ,以獲取更多詳細信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.