-
Notifications
You must be signed in to change notification settings - Fork 274
feat: call "cleanup" automatically #238
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
eb8d275
feat: add "cleanup" function
aleclarson 42cd20e
docs: add "cleanup" section
aleclarson 20969cb
feat: automatically call "cleanup" after each test
aleclarson 74d63bf
docs: mention automatic cleanup
aleclarson 70fbaff
add within
thymikee eac99b2
fix lint
thymikee 4a83e8e
Merge branch 'next' into cleanup-v2
mdjastrzebski 561022f
Updated tests, added auto-cleanup test
mdjastrzebski b8f96a9
Added ways to prevent auto cleanup
mdjastrzebski b0b7b58
Small fix
mdjastrzebski c358d72
Code review changes
mdjastrzebski 57a6f6c
Update website/docs/API.md
mdjastrzebski 8dec704
More changes
mdjastrzebski 0812780
Removed afterEach(cleanup) calls in examples
mdjastrzebski 349c1f5
Code cleanup
mdjastrzebski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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'; | ||
thymikee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.