0

I am using RTL with Jest to test a Slate editor. I use .type to emulate a user typing into the editor and expect that Slate's onChange will be called. However, this does not happen.

Here's the sandbox where I reproduced the issue.

To reproduce the behavior:

  • Go to 'tests/index.js'
  • Run the tests
  • There is an assertion that the onChange was called but it fails

I expected that the onChange will be called. This behavior only happens when tests are ran. In a normal browser onChange is indeed called.

George Eracleous
  • 4,278
  • 6
  • 41
  • 50
  • My bad, i tried to run the test in CodeSandbox but it was showing passing and it is now not due to previously having the bad data. Removing my answer until i have more info – Taiwei Tuan Sep 01 '23 at 15:48
  • One thing i can tell that there is no contentedible with slate-react, so your `user.type` isn't going to work, and is the cause of why `onChange` is not being called. I'd suggest that you do a assertion of the input field that the input value is really being updated after the `user.type` so you get more context on whats been happening behind the scene. – Taiwei Tuan Sep 01 '23 at 15:50
  • Seems like you have asked this in official repo a while back https://github.com/ianstormtaylor/slate/issues/5425 – Taiwei Tuan Sep 01 '23 at 15:51
  • Yeah indeed I did ask it a while ago but got no reply! Regarding your comment yes the value is indeed changed but my problem is that the onChange is not called. The thing is that my component relies on onChange to be called so that it updates its parent. So even though the actual editor value is changed the parent will never find out. – George Eracleous Sep 01 '23 at 15:53
  • No the value is not changed after the `user.type`, you can assert it by adding `expect(screen.getByTestId("my_editor")).toHaveTextContent("Foo some more text");` after your `user.type` – Taiwei Tuan Sep 01 '23 at 15:54

0 Answers0