callstack / react-native-testing-library

🦉 Simple and complete React Native testing utilities that encourage good testing practices.
https://callstack.github.io/react-native-testing-library/
MIT License
3.01k stars 264 forks source link

waitForElementToBeRemoved doesn't work as expected with `get` type queries #1580

Closed TMaszko closed 2 months ago

TMaszko commented 3 months ago

Describe the bug

Docs says here: https://callstack.github.io/react-native-testing-library/docs/api#waitforelementtoberemoved that we could use any of getBy, getAllBy, queryBy and queryAllBy queries for expectation parameter but during writing some tests I discovered that get type queries are not fully compatible with waitForElementToBeRemoved because when the error is thrown it will be caught by the catch block and initialElements will be returned. Due to the fact that waitFor will stop once expectation doesn't throw an error it will call onDone callback with the result which will "work" only because the element was removed in the first cycle (first checkExpectation call).

Expected behaviour

Docs shouldn't mention get type queries or it should throw an error instead of returning initialElements in the catch block.

Steps to Reproduce

  1. Create a component with a simple text and a button
  2. After clicking the button hide the text using setTimeout with 1000ms timeout.
  3. Write test that will use waitForElementToBeRemoved with a get type query(getByText) for example :)
  4. Test will fail because element won't be removed in the first cycle.

Screenshots

Versions

npmPackages: @testing-library/react-native: 12.4.5 => 12.4.5 react: 18.2.0 => 18.2.0 react-native: 0.73.6 => 0.73.6 react-test-renderer: 18.2.0 => 18.2.0

pierrezimmermannbam commented 3 months ago

Hello @TMaszko, thanks for reporting this issue. I don't understand the problem here though, if I follow the steps to reproduce then the test will fail because the element gets removed after the waitFor has timed out, which is the expected behavior. In the waitFor if we use a getBy query and get an error it means the element was removed so it will be successful but that is the expected behavior. Could you provide the full code for a test example that would not work as expected?

TMaszko commented 2 months ago

Hi @pierrezimmermannbam ! My fault, Sorry I think I've missed really important detail at that point. Everything is clear now! Thanks a lot :)