You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I came across an issue when writing tests with testing library where text typed into an text area becomes completly messed up when directly passing the event into setter function created by useImmer:
exportdefaultfunctionApp(){const[test,setTest]=useImmer({text: ""});return(<divclassName="App"><Form.GroupcontrolId="formDescription"><Form.Label>{"TestInput"}</Form.Label><Form.Controlas="textarea"type="text"placeholder={"Enter input"}value={test.text}onChange={(event)=>{constvalue=event.target.value;setTest((draft)=>{draft.text=event.target.value;// wrong value for event.target.value inside test; gets really messed up//draft.text = value; // works fine});}}/></Form.Group></div>);}
On the other hand, when using useState the following works just fine:
exportdefaultfunctionApp(){const[foo,setFoo]=useState({text: ""});return(<divclassName="App"><Form.GroupcontrolId="formDescription"><Form.Label>{"TestInput"}</Form.Label><Form.Controlas="textarea"type="text"placeholder={"Enter input"}value={test.text}onChange={(event)=>{setFoo((prevState)=>{constnewState={ ...prevState};newState.text=event.target.value;// works finereturnnewState;});}}/></Form.Group></div>);
The test:
import{render}from"@testing-library/react";importuserEventfrom"@testing-library/user-event";importAppfrom"./App";import{screen}from"@testing-library/dom";test("fill in",async()=>{constuser=userEvent.setup({ document });render(<App/>);constdescription="This is a test description.";awaituser.type(screen.getByRole("textbox",{name: "TestInput"}),description);expect(screen.getByText(description)).not.toBeNull();});
Instead of This is a test description. the text area somehow contains Ti sats ecito. when running the test.
Using the text area manually in a browser seems to always work fine though. I am not sure if there can anything be done here and if this only happens in combination with testing library.
I came across an issue when writing tests with testing library where text typed into an text area becomes completly messed up when directly passing the event into setter function created by useImmer:
On the other hand, when using
useState
the following works just fine:The test:
Instead of
This is a test description.
the text area somehow containsTi sats ecito.
when running the test.Using the text area manually in a browser seems to always work fine though. I am not sure if there can anything be done here and if this only happens in combination with testing library.
CSB: https://codesandbox.io/s/billowing-dew-0mdbjx
The text was updated successfully, but these errors were encountered: