feat: add "cleanup" function (#237)

* feat: add "cleanup" function

* docs: add "cleanup" section

* test: "cleanup" function

* don't expose cleanup queue


Co-authored-by: Alec Larson <alec.stanford.larson@gmail.com>
Co-authored-by: Michał Pierzchała <thymikee@gmail.com>
This commit is contained in:
Alec Larson
2019-12-09 05:41:48 -05:00
committed by Kacper Wiszczuk
parent 69d148023f
commit 4c77f4a7dc
6 changed files with 92 additions and 7 deletions
+39
View File
@@ -115,6 +115,45 @@ toJSON(): ReactTestRendererJSON | null
Get the rendered component JSON representation, e.g. for snapshot testing.
## `cleanup`
```ts
const cleanup: () => void
```
Unmounts React trees that were mounted with `render`.
For example, if you're using the `jest` testing framework, then you would need to use the `afterEach` hook like so:
```jsx
import { cleanup, render } from 'react-native-testing-library'
import { View } from 'react-native'
afterEach(cleanup)
it('renders a view', () => {
render(<View />)
// ...
})
```
The `afterEach(cleanup)` call also works in `describe` blocks:
```jsx
describe('when logged in', () => {
afterEach(cleanup)
it('renders the user', () => {
render(<SiteHeader />)
// ...
});
})
```
Failing to call `cleanup` when you've called `render` could result in a memory leak and tests which are not "idempotent" (which can lead to difficult to debug errors in your tests).
The alternative to `cleanup` is balancing every `render` with an `unmount` method call.
## `fireEvent`
```ts