-
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 21, 2020
1 parent
7bcd64c
commit 5f01102
Showing
3 changed files
with
57 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,13 @@ | ||
import { watch } from 'vue' | ||
import { useDebounce } from './useDebounce' | ||
|
||
export function useDebounceFn(fn: Function, delay = 200) { | ||
const debounceValue = useDebounce(0, delay) | ||
|
||
watch(debounceValue, () => { | ||
fn() | ||
}) | ||
return () => { | ||
debounceValue.value++ | ||
} | ||
} |
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,43 @@ | ||
import { nextTick } from 'vue' | ||
import { useDebounceFn } from '../src/useDebounceFn' | ||
|
||
let callback: Function | null | ||
|
||
beforeEach(() => { | ||
jest.useFakeTimers() | ||
callback = jest.fn() | ||
}) | ||
|
||
afterEach(() => { | ||
jest.useRealTimers() | ||
jest.clearAllMocks() | ||
}) | ||
|
||
test('callback should not be called after invoking useDebounceFn', () => { | ||
useDebounceFn(callback!) | ||
expect(callback!).not.toBeCalled() | ||
}) | ||
|
||
test('setTimeout should be called after invoking the function returned by useDebounceFn', () => { | ||
const debounceFn = useDebounceFn(callback!) | ||
debounceFn() | ||
expect(setTimeout).toHaveBeenCalledTimes(1) | ||
expect(setTimeout).toHaveBeenCalledWith(expect.any(Function), 200) | ||
}) | ||
|
||
test('timer should be cleared when calling the function returned by useDebounceFn in 200s', () => { | ||
const debounceFn = useDebounceFn(callback!) | ||
debounceFn() | ||
debounceFn() | ||
expect(clearTimeout).toHaveBeenCalledTimes(1) | ||
expect(setTimeout).toHaveBeenCalledTimes(2) | ||
expect(setTimeout).toHaveBeenCalledWith(expect.any(Function), 200) | ||
}) | ||
|
||
test('callback should be called when timeout', async () => { | ||
const debounceFn = useDebounceFn(callback!) | ||
debounceFn() | ||
jest.advanceTimersByTime(200) | ||
await nextTick() | ||
expect(callback!).toHaveBeenCalledTimes(1) | ||
}) |