Search code examples
reactjsreact-testing-libraryreact-test-renderer

React test: after update input the props value still not change


I have this code in test

let formatField = subject.find("input").at(0);
(formatField.props() as any).onChange({
     currentTarget: { value: "Potato" }
});

The problem is after this action when I get formatField.props().value it still undefined

Do I missing something?

I tried with

formatField .simulate('change', { target: { value: 'Potato' } });

and also

formatField .simulate('keydown', { keyCode: 13 }); 

the value still not update

Thank you very much!


Solution

  • I found it by my self: need to wait for it update

    const waitForAsync = () => new Promise(resolve => setImmediate(resolve));
    await waitForAsync();
    subject.update(); 
    
    

    also the simulate change event should like this

     formatField.simulate("change", {
            target: { value: "my value" }
          });