Enzyme - How to access and set <input> value?

后端 未结 16 2029
面向向阳花
面向向阳花 2020-12-07 22:06

I\'m confused about how to access value when using mount. Here\'s what I\'ve got as my test:

  it(\'cancels changes w         


        
相关标签:
16条回答
  • 2020-12-07 22:07

    In my case i was using ref callbacks,

      <input id="usuario" className="form-control" placeholder="Usuario"
                                                           name="usuario" type="usuario"
                                                           onKeyUp={this._validateMail.bind(this)}
                                                           onChange={()=> this._validateMail()}
                                                           ref={(val) =>{ this._username = val}}
                                                        >
    

    To obtain the value. So enzyme will not change the value of this._username.

    So i had to:

    login.node._username.value = "mario@com.com";
        user.simulate('change');
        expect(login.state('mailValid')).toBe(true);
    

    To be able to set the value then call change . And then assert.

    0 讨论(0)
  • 2020-12-07 22:09

    None of the above worked for me. This is what worked for me on Enzyme ^3.1.1:

    input.instance().props.onChange(({ target: { value: '19:00' } }));
    

    Here is the rest of the code for context:

    const fakeHandleChangeValues = jest.fn();
      const fakeErrors = {
        errors: [{
          timePeriod: opHoursData[0].timePeriod,
          values: [{
            errorIndex: 2,
            errorTime: '19:00',
          }],
        }],
        state: true,
      };
    const wrapper = mount(<AccessibleUI
        handleChangeValues={fakeHandleChangeValues}
        opHoursData={opHoursData}
        translations={translationsForRendering}
      />);
    const input = wrapper.find('#input-2').at(0);
    input.instance().props.onChange(({ target: { value: '19:00' } }));
    expect(wrapper.state().error).toEqual(fakeErrors);
    
    0 讨论(0)
  • 2020-12-07 22:10

    here is my code..

    const input = MobileNumberComponent.find('input')
    // when
    input.props().onChange({target: {
       id: 'mobile-no',
       value: '1234567900'
    }});
    MobileNumberComponent.update()
    const Footer = (loginComponent.find('Footer'))
    expect(Footer.find('Buttons').props().disabled).equals(false)
    

    I have update my DOM with componentname.update() And then checking submit button validation(disable/enable) with length 10 digit.

    0 讨论(0)
  • 2020-12-07 22:14

    .simulate() doesn't work for me somehow, I got it working with just accessing the node.value without needing to call .simulate(); in your case:

    const wrapper = mount(<EditableText defaultValue="Hello" />);
    const input = wrapper.find('input').at(0);
    
    // Get the value
    console.log(input.node.value); // Hello
    
    // Set the value
    input.node.value = 'new value';
    
    // Get the value
    console.log(input.node.value); // new value
    

    Hope this helps for others!

    0 讨论(0)
  • 2020-12-07 22:16

    None of the solutions above worked for me because I was using Formik and I needed to mark the field "touched" along with changing the field value. Following code worked for me.

    const emailField = orderPageWrapper.find('input[name="email"]')
    
    emailField.simulate('focus')
    emailField.simulate('change', { target: { value: 'test@example.com', name: 'email' } })
    emailField.simulate('blur')
    
    0 讨论(0)
  • 2020-12-07 22:18

    This works for me using enzyme 2.4.1:

    const wrapper = mount(<EditableText defaultValue="Hello" />);
    const input = wrapper.find('input');
    
    console.log(input.node.value);
    
    0 讨论(0)
提交回复
热议问题