-
Notifications
You must be signed in to change notification settings - Fork 17
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
26b5c37
commit 7324143
Showing
5 changed files
with
94 additions
and
4 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
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,24 @@ | ||
import { watch, WatchStopHandle } from 'vue' | ||
import { useTimeout } from './useTimeout' | ||
|
||
export function useTimeoutFn(fn: Function, delay = 1000, immediate = true, clearEffectWhenStop = false) { | ||
const { start, ready, stop } = useTimeout(delay, immediate) | ||
let stopEffect: WatchStopHandle | undefined | ||
const startEffect = () => { | ||
stopEffect = watch(ready, (hasReady) => { | ||
hasReady && fn() | ||
}) | ||
} | ||
const _stop = () => { | ||
clearEffectWhenStop && stopEffect!() | ||
stop() | ||
} | ||
startEffect() | ||
return { | ||
start: () => { | ||
clearEffectWhenStop && startEffect() | ||
start() | ||
}, | ||
stop: _stop | ||
} | ||
} |
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,55 @@ | ||
import { useTimeoutFn } from '../src/useTimeoutFn' | ||
import { nextTick } from 'vue' | ||
|
||
let callback: Function | undefined | ||
|
||
beforeEach(() => { | ||
callback = jest.fn() | ||
jest.useFakeTimers() | ||
}) | ||
|
||
afterEach(() => { | ||
jest.clearAllTimers() | ||
jest.clearAllMocks() | ||
}) | ||
|
||
test('setTimeout should be called with default delay', async () => { | ||
useTimeoutFn(callback!) | ||
expect(setTimeout).toHaveBeenCalledTimes(1) | ||
expect(setTimeout).toHaveBeenCalledWith(expect.any(Function), 1000) | ||
jest.advanceTimersByTime(1000) | ||
await nextTick() | ||
expect(callback!).toHaveBeenCalledTimes(1) | ||
}) | ||
|
||
test('setTimeout should not be called when immediate is set to false', () => { | ||
useTimeoutFn(callback!, 1000, false) | ||
expect(setTimeout).not.toBeCalled() | ||
}) | ||
|
||
test('setTimeout should be called after calling start when immediate is false', async () => { | ||
const { start } = useTimeoutFn(callback!, 1000, false) | ||
expect(setTimeout).not.toBeCalled() | ||
start() | ||
expect(setTimeout).toHaveBeenCalledTimes(1) | ||
expect(setTimeout).toHaveBeenCalledWith(expect.any(Function), 1000) | ||
jest.advanceTimersByTime(1000) | ||
await nextTick() | ||
expect(callback!).toHaveBeenCalledTimes(1) | ||
}) | ||
|
||
test('setTimeout should be clear after calling stop', () => { | ||
const { stop } = useTimeoutFn(callback!, 1000) | ||
stop() | ||
expect(clearTimeout).toHaveBeenCalledTimes(1) | ||
}) | ||
|
||
test('callback should be called 1 time when clearEffectWhenStop is true and calling start after calling stop', async () => { | ||
const { start, stop } = useTimeoutFn(callback!, 1000, true, true) | ||
stop() | ||
expect(clearTimeout).toHaveBeenCalledTimes(1) | ||
start() | ||
jest.advanceTimersByTime(1000) | ||
await nextTick() | ||
expect(callback!).toHaveBeenCalledTimes(1) | ||
}) |