Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: call "cleanup" automatically (#238)
* feat: add "cleanup" function * docs: add "cleanup" section * feat: automatically call "cleanup" after each test ...if an "afterEach" global function is defined, and process.env.RTL_SKIP_AUTO_CLEANUP is falsy. Taken from: https://github.com/testing-library/react-testing-library/blob/14670debd45236d2c5d0a61a83dadc72af1bef7c/src/index.js * docs: mention automatic cleanup * add within * fix lint * Updated tests, added auto-cleanup test * Added ways to prevent auto cleanup * Small fix * Code review changes * Update website/docs/API.md Co-authored-by: Micha艂 Pierzcha艂a <thymikee@gmail.com> * More changes * Removed afterEach(cleanup) calls in examples * Code cleanup Co-authored-by: Micha艂 Pierzcha艂a <thymikee@gmail.com> Co-authored-by: Maciej Jastrzebski <mdjastrzebski@gmail.com>
- Loading branch information
1 parent
40ed268
commit ad66a54
Showing
15 changed files
with
151 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
process.env.RNTL_SKIP_AUTO_CLEANUP = true; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// makes it so people can import from 'react-native-testing-library/pure' | ||
module.exports = require('./build/pure'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import React from 'react'; | ||
import { View } from 'react-native'; | ||
|
||
let render; | ||
beforeAll(() => { | ||
process.env.RNTL_SKIP_AUTO_CLEANUP = 'true'; | ||
const rtl = require('../'); | ||
render = rtl.render; | ||
}); | ||
|
||
let isMounted = false; | ||
|
||
class Test extends React.Component<*> { | ||
componentDidMount() { | ||
isMounted = true; | ||
} | ||
|
||
componentWillUnmount() { | ||
isMounted = false; | ||
if (this.props.onUnmount) { | ||
this.props.onUnmount(); | ||
} | ||
} | ||
render() { | ||
return <View />; | ||
} | ||
} | ||
|
||
// This just verifies that by importing RNTL in pure mode in an environment which supports | ||
// afterEach (like jest) we won't get automatic cleanup between tests. | ||
test('component is mounted, but not umounted before test ends', () => { | ||
const fn = jest.fn(); | ||
render(<Test onUnmount={fn} />); | ||
expect(fn).not.toHaveBeenCalled(); | ||
}); | ||
|
||
test('component is NOT automatically umounted after first test ends', () => { | ||
expect(isMounted).toEqual(true); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import React from 'react'; | ||
import { View } from 'react-native'; | ||
import { render } from '..'; | ||
|
||
let isMounted = false; | ||
|
||
class Test extends React.Component<*> { | ||
componentDidMount() { | ||
isMounted = true; | ||
} | ||
|
||
componentWillUnmount() { | ||
isMounted = false; | ||
if (this.props.onUnmount) { | ||
this.props.onUnmount(); | ||
} | ||
} | ||
render() { | ||
return <View />; | ||
} | ||
} | ||
|
||
// This just verifies that by importing RNTL in an environment which supports afterEach (like jest) | ||
// we'll get automatic cleanup between tests. | ||
test('component is mounted, but not umounted before test ends', () => { | ||
const fn = jest.fn(); | ||
render(<Test onUnmount={fn} />); | ||
expect(fn).not.toHaveBeenCalled(); | ||
}); | ||
|
||
test('component is automatically umounted after first test ends', () => { | ||
expect(isMounted).toEqual(false); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,18 @@ | ||
// @flow | ||
import act from './act'; | ||
import cleanup from './cleanup'; | ||
import fireEvent from './fireEvent'; | ||
import flushMicrotasksQueue from './flushMicrotasksQueue'; | ||
import render from './render'; | ||
import shallow from './shallow'; | ||
import waitFor, { waitForElement } from './waitFor'; | ||
import within from './within'; | ||
import { cleanup } from './pure'; | ||
|
||
export { act }; | ||
export { cleanup }; | ||
export { fireEvent }; | ||
export { flushMicrotasksQueue }; | ||
export { render }; | ||
export { shallow }; | ||
export { waitFor, waitForElement }; | ||
export { within }; | ||
// If we're running in a test runner that supports afterEach | ||
// then we'll automatically run cleanup afterEach test | ||
// this ensures that tests run in isolation from each other | ||
// if you don't like this then either import the `pure` module | ||
// or set the RNTL_SKIP_AUTO_CLEANUP env variable to 'true'. | ||
if (typeof afterEach === 'function' && !process.env.RNTL_SKIP_AUTO_CLEANUP) { | ||
// eslint-disable-next-line no-undef | ||
afterEach(async () => { | ||
await flushMicrotasksQueue(); | ||
cleanup(); | ||
}); | ||
} | ||
|
||
export * from './pure'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// @flow | ||
import act from './act'; | ||
import cleanup from './cleanup'; | ||
import fireEvent from './fireEvent'; | ||
import flushMicrotasksQueue from './flushMicrotasksQueue'; | ||
import render from './render'; | ||
import shallow from './shallow'; | ||
import waitFor, { waitForElement } from './waitFor'; | ||
import within from './within'; | ||
|
||
export { act }; | ||
export { cleanup }; | ||
export { fireEvent }; | ||
export { flushMicrotasksQueue }; | ||
export { render }; | ||
export { shallow }; | ||
export { waitFor, waitForElement }; | ||
export { within }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters