Javascript 酶模拟输入不工作的变化

Javascript 酶模拟输入不工作的变化,javascript,reactjs,typescript,enzyme,Javascript,Reactjs,Typescript,Enzyme,我有一个像这样的反应输入组件 const Input: InputComponent = props => { const { limit, suffix, className, onChange } = props; const [inputLength, setInputLength] = useState(0); const inputRef = useRef<HTMLInputElement>(); const handleChange = (val:

我有一个像这样的反应输入组件

const Input: InputComponent = props => {
  const { limit, suffix, className, onChange } = props;
  const [inputLength, setInputLength] = useState(0);
  const inputRef = useRef<HTMLInputElement>();

  const handleChange = (val: string, e: any): void => {
    setInputLength(val.length);
    onChange?.(val, e);
  };

  useEffect(() => {
    limit && setInputLength((inputRef?.current as any)?.state?.value?.length || 0);
  }, []);

  const { prefixCls: common } = useConfigContext();
  const prefixCls = `${common}-input`;

  return (
    <Component
      {...props}
      className={cx(className, prefixCls)}
      onChange={handleChange}
      ref={inputRef as any}
      suffix={
        limit ? (
          <div className={cx(`${prefixCls}__limit`)}>
            {inputLength}/{limit}
          </div>
        ) : (
          suffix
        )
      }
    />
  );
};
常量输入:InputComponent=props=>{ const{limit,后缀,类名,onChange}=props; 常量[inputLength,setInputLength]=useState(0); const inputRef=useRef(); 常量handleChange=(val:string,e:any):void=>{ 设置输入长度(val.length); onChange?(val,e); }; useffect(()=>{ 限制和设置InputLength((inputRef?.current,如有)?.state?.value?.length | | 0); }, []); const{prefixCls:common}=useConfigContext(); const prefixCls=`${common}-input`; 返回( ); }; 在myInput.spec.tsx中

  it('onChange listener correctly', () => {
    const onChange = jest.fn();
    const component = mount(<Input onChange={onChange} defaultValue={'default'} />);
    const input = component.find('input');
    input.simulate('change', {
      target: {
        value: 'test',
      },
    });
    console.log(input.props()); // input value still show `default`
    expect(onChange.mock.calls.length).toBe(1);
  });
it('onChange listener corrective',()=>{
const onChange=jest.fn();
const component=mount();
const input=component.find('input');
input.simulate('change'{
目标:{
值:“测试”,
},
});
console.log(input.props());//输入值仍显示“默认值”`
expect(onChange.mock.calls.length).toBe(1);
});
输入值仍然显示默认值(“默认”),不更改为“测试”。 但是,将调用onChange事件

谁能给我一些指导,我哪里出了问题?

试试下面的方法(只是一个假设)

it('onChange listener corrective',()=>{
const onChange=jest.fn();
const component=mount();
const input=component.find('input');
input.simulate('change'{
目标:{
值:“测试”,
},
});
component.update();
console.log(input.props());//输入值仍显示“默认值”`
expect(onChange.mock.calls.length).toBe(1);
});
update()-

尝试以下方法(只是一个假设)

it('onChange listener corrective',()=>{
const onChange=jest.fn();
const component=mount();
const input=component.find('input');
input.simulate('change'{
目标:{
值:“测试”,
},
});
component.update();
console.log(input.props());//输入值仍显示“默认值”`
expect(onChange.mock.calls.length).toBe(1);
});

update()-

您好,谢谢,但这在我的情况下不起作用。道具仍然显示默认值。关于为什么会发生这种情况,你还有其他的猜测吗?谢谢,谢谢,但这对我来说不起作用。道具仍然显示默认值。关于为什么会发生这种情况,你还有其他的猜测吗?谢谢
 it('onChange listener correctly', () => {
    const onChange = jest.fn();
    const component = mount(<Input onChange={onChange} defaultValue={'default'} />);
    const input = component.find('input');
    input.simulate('change', {
      target: {
        value: 'test',
      },
    });
    component.update();
    console.log(input.props()); // input value still show `default`
    expect(onChange.mock.calls.length).toBe(1);
  });