-
Notifications
You must be signed in to change notification settings - Fork 665
feat: Introduce enableAutoDestroy() helper function #1245
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
6 commits
Select commit
Hold shift + click to select a range
7dd4d1d
feat: Introduce enableAutoDestroy() helper function
winniehell 7064ab0
chore: Move enableAutoDestroy() to separate module
winniehell 2d0a6df
fix: Avoid destroying child wrappers
winniehell 560b6ec
Apply suggestions from code review
winniehell 69dd594
chore: Expose resetAutoDestroyState()
winniehell d17fc1f
docs: add info on resetAutoDestroyState
lmiller1990 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
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,25 @@ | ||
## enableAutoDestroy(hook) | ||
|
||
- **Arguments:** | ||
|
||
- `{Function} hook` | ||
|
||
- **Usage:** | ||
|
||
`enableAutoDestroy` will destroy all created `Wrapper` instances using the passed hook function (for example [`afterEach`](https://jestjs.io/docs/en/api#aftereachfn-timeout)). After calling the method, you can revert to the default behavior by calling the `resetAutoDestroyState` method. | ||
|
||
```js | ||
import { enableAutoDestroy, mount } from '@vue/test-utils' | ||
import Foo from './Foo.vue' | ||
|
||
// calls wrapper.destroy() after each test | ||
enableAutoDestroy(afterEach) | ||
|
||
describe('Foo', () => { | ||
it('renders a div', () => { | ||
const wrapper = mount(Foo) | ||
expect(wrapper.contains('div')).toBe(true) | ||
// no need to call wrapper.destroy() here | ||
}) | ||
}) | ||
``` |
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,37 @@ | ||
// @flow | ||
|
||
import { throwError } from 'shared/util' | ||
import Wrapper from './wrapper' | ||
|
||
let isEnabled = false | ||
const wrapperInstances = [] | ||
|
||
export function resetAutoDestroyState() { | ||
isEnabled = false | ||
wrapperInstances.length = 0 | ||
} | ||
|
||
export function enableAutoDestroy(hook: (() => void) => void) { | ||
if (isEnabled) { | ||
throwError('enableAutoDestroy cannot be called more than once') | ||
} | ||
|
||
isEnabled = true | ||
|
||
hook(() => { | ||
wrapperInstances.forEach((wrapper: Wrapper) => { | ||
// skip child wrappers created by wrapper.find() | ||
if (wrapper.selector) return | ||
|
||
wrapper.destroy() | ||
}) | ||
|
||
wrapperInstances.length = 0 | ||
}) | ||
} | ||
|
||
export function trackInstance(wrapper: Wrapper) { | ||
if (!isEnabled) return | ||
|
||
wrapperInstances.push(wrapper) | ||
} |
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.