Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
lmhcoding
committed
Sep 20, 2020
1 parent
2f3c6db
commit b19dd8d
Showing
3 changed files
with
31 additions
and
0 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
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,12 @@ | ||
import { getCurrentInstance, onMounted, onUnmounted } from 'vue' | ||
|
||
interface Callback { | ||
(): any | ||
} | ||
|
||
export function useLifecycles(mountedCb: Callback, unmountCb: Callback): void { | ||
if (getCurrentInstance()) { | ||
mountedCb && onMounted(mountedCb) | ||
unmountCb && onUnmounted(unmountCb) | ||
} | ||
} |
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 @@ | ||
import { mount } from '@vue/test-utils' | ||
import { useLifecycles } from '../src/useLifecycles' | ||
|
||
describe('test useLifecycles', () => { | ||
test('callback should be called when mounted or unmounted', () => { | ||
const onMounted = jest.fn() | ||
const onUnmounted = jest.fn() | ||
const wrapper = mount({ | ||
template: '<div>test</div>', | ||
setup () { | ||
useLifecycles(onMounted, onUnmounted) | ||
} | ||
}) | ||
expect(onMounted).toHaveBeenCalledTimes(1) | ||
wrapper.unmount() | ||
expect(onUnmounted).toHaveBeenCalledTimes(1) | ||
}) | ||
}) |