简体   繁体   中英

Check if onChange works correctly with setState

Trying to test if ParameterLabel value changed when onChange event was tested. Something is missing on this line:

expect(wrapper.state('ParameterLabel'))).toEqual("ParameterLabel value"); //??

I need to add context somewhere after wrapper.state, so it can check the value inside of ParamenterLabel and perform the change Using Shallow - Jest / enzyme

it('Testing ParameterLabel onBlur event ', () => {
 baseProps.onBlur.mockClear();
 wrapper.setState({
    context:{
      ParameterLabel:'test',
      PARAMETER_TYPE: {
          ParameterTypeId:'ParameterLabel-Test'
        },
      PARAMETER_DATATYPE:{
          DataTypeId:'test'
        },
     },
  });
  wrapper.update() 
  wrapper.find('input[id="ParameterLabel"]').simulate('blur',{
    target: { 
       value:'ParameterLabel value', 
       id: 'ParameterLabel'
     }
 });
   expect(wrapper.state('ParameterLabel'))).toEqual("ParameterLabel value"); //??
});


<input id='ParameterLabel' className='user-value-field' onBlur={this.updateContext} defaultValue={this.state.context.ParameterLabel}

From your setState call, it looks like the state property 'ParameterLabel' is nested inside of 'context'.

Try searching for the 'context' object inside your assertion then pulling out that property. Something like:

expect(wrapper.state('context').ParameterLabel)).toEqual("ParameterLabel value");

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