-
Notifications
You must be signed in to change notification settings - Fork 258
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add enableAutoUnmount feature (#998)
- port enableAutoDestroy feature from VTU v1
- Loading branch information
Showing
5 changed files
with
98 additions
and
1 deletion.
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
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,32 @@ | ||
import { ComponentPublicInstance } from 'vue' | ||
import type { VueWrapper } from '../vueWrapper' | ||
|
||
let isEnabled = false | ||
const wrapperInstances: VueWrapper<ComponentPublicInstance>[] = [] | ||
|
||
export function disableAutoUnmount() { | ||
isEnabled = false | ||
wrapperInstances.length = 0 | ||
} | ||
|
||
export function enableAutoUnmount(hook: Function) { | ||
if (isEnabled) { | ||
throw new Error('enableAutoUnmount cannot be called more than once') | ||
} | ||
|
||
isEnabled = true | ||
|
||
hook(() => { | ||
wrapperInstances.forEach((wrapper: VueWrapper<ComponentPublicInstance>) => { | ||
wrapper.unmount() | ||
}) | ||
|
||
wrapperInstances.length = 0 | ||
}) | ||
} | ||
|
||
export function trackInstance(wrapper: VueWrapper<ComponentPublicInstance>) { | ||
if (!isEnabled) return | ||
|
||
wrapperInstances.push(wrapper) | ||
} |
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,37 @@ | ||
import { mount, enableAutoUnmount, disableAutoUnmount } from '../src' | ||
|
||
describe('enableAutoUnmount', () => { | ||
beforeEach(() => { | ||
disableAutoUnmount() | ||
}) | ||
|
||
it('calls the hook function', () => { | ||
const hookMock = jest.fn() | ||
|
||
enableAutoUnmount(hookMock) | ||
|
||
expect(hookMock).toHaveBeenCalledWith(expect.any(Function)) | ||
}) | ||
|
||
it('uses the hook function to unmount wrappers', () => { | ||
const hookMock = jest.fn() | ||
|
||
enableAutoUnmount(hookMock) | ||
const [unmountFn] = hookMock.mock.calls[0] | ||
|
||
const wrapper = mount({ template: '<p>test</p>' }) | ||
jest.spyOn(wrapper, 'unmount') | ||
|
||
unmountFn() | ||
|
||
expect(wrapper.unmount).toHaveBeenCalledTimes(1) | ||
}) | ||
|
||
it('cannot be called twice', () => { | ||
const noop = () => {} | ||
|
||
enableAutoUnmount(noop) | ||
|
||
expect(() => enableAutoUnmount(noop)).toThrow() | ||
}) | ||
}) |