File tree Expand file tree Collapse file tree 3 files changed +48
-0
lines changed Expand file tree Collapse file tree 3 files changed +48
-0
lines changed Original file line number Diff line number Diff line change 1+ import { onMounted , onUnmounted } from 'vue-function-api' ;
2+ import { useTimeout } from '..' ;
3+ import renderHook from '../util/renderHook' ;
4+
5+ describe ( 'useTimeout' , ( ) => {
6+ it ( 'should be defined' , ( ) => {
7+ expect ( useTimeout ) . toBeDefined ( ) ;
8+ } ) ;
9+
10+ it ( 'should return true after 3000ms' , ( ) => {
11+ const { vm } = renderHook < unknown > ( ( ) => {
12+ jest . useFakeTimers ( ) ;
13+ const ready = useTimeout ( 3000 ) ;
14+ expect ( ready . value ) . toBe ( false ) ;
15+
16+ onMounted ( ( ) => {
17+ expect ( jest . getTimerCount ( ) ) . toBe ( 1 ) ;
18+ jest . runOnlyPendingTimers ( ) ;
19+ expect ( ready . value ) . toBe ( true ) ;
20+ } ) ;
21+
22+ onUnmounted ( ( ) => {
23+ expect ( jest . getTimerCount ( ) ) . toBe ( 0 ) ;
24+ } ) ;
25+ } ) ;
26+
27+ vm . $destroy ( ) ;
28+ } ) ;
29+ } ) ;
Original file line number Diff line number Diff line change @@ -14,6 +14,7 @@ export { default as useActions } from './useActions';
1414export { default as useRouter } from './useRouter' ;
1515export { default as useRef } from './useRef' ;
1616export { default as useMountedState } from './useMountedState' ;
17+ export { default as useTimeout } from './useTimeout' ;
1718
1819export default function install ( Vue : VueConstructor ) {
1920 Vue . mixin ( { beforeCreate : setRuntimeVM } ) ;
Original file line number Diff line number Diff line change 1+ import { value , onMounted , onUnmounted } from 'vue-function-api' ;
2+
3+ export default function useTimeout ( delay = 0 ) {
4+ const ready = value ( false ) ;
5+ let timerId : number ;
6+
7+ onMounted ( ( ) => {
8+ timerId = window . setTimeout ( ( ) => {
9+ ready . value = true ;
10+ } , delay ) ;
11+ } ) ;
12+
13+ onUnmounted ( ( ) => {
14+ window . clearTimeout ( timerId ) ;
15+ } ) ;
16+
17+ return ready ;
18+ }
You can’t perform that action at this time.
0 commit comments