-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(use-debounce): create useDebounce composable to deprecate Deboun…
…ce decorator EMP-4087
- Loading branch information
1 parent
9485e45
commit 8416b58
Showing
9 changed files
with
120 additions
and
8 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
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
73 changes: 73 additions & 0 deletions
73
packages/x-components/src/composables/__tests__/use-debounce.spec.ts
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,73 @@ | ||
import { mount } from '@vue/test-utils'; | ||
import { DebounceOptions } from '../../utils/types'; | ||
import { useDebounce } from '../use-debounce'; | ||
|
||
const fnMock = jest.fn(); | ||
const fnParamStub = 'It is a parameter'; | ||
|
||
function render(debounceTimeInMs = 200, debounceOptions: DebounceOptions = {}) { | ||
const wrapper = mount({ | ||
setup: () => { | ||
const debouncedFnMock = useDebounce( | ||
(param: string) => fnMock(param), | ||
debounceTimeInMs, | ||
debounceOptions | ||
); | ||
const onClick = () => debouncedFnMock(fnParamStub); | ||
return { onClick }; | ||
}, | ||
template: `<button @click="onClick">Execute fn debounced</button>` | ||
}); | ||
|
||
return { | ||
wrapper, | ||
runDebouncedFnMock: async () => await wrapper.trigger('click') | ||
}; | ||
} | ||
|
||
describe('testing useDebounce composable', () => { | ||
jest.useFakeTimers(); | ||
|
||
beforeEach(() => { | ||
fnMock.mockClear(); | ||
}); | ||
|
||
it('should debounce the fn with the debounced time', async () => { | ||
const { runDebouncedFnMock } = render(); | ||
|
||
await runDebouncedFnMock(); | ||
expect(fnMock).toHaveBeenCalledTimes(0); | ||
jest.advanceTimersByTime(200); | ||
expect(fnMock).toHaveBeenCalledTimes(1); | ||
expect(fnMock).toHaveBeenCalledWith(fnParamStub); | ||
|
||
fnMock.mockClear(); | ||
await runDebouncedFnMock(); | ||
jest.advanceTimersByTime(100); | ||
await runDebouncedFnMock(); | ||
jest.advanceTimersByTime(150); | ||
expect(fnMock).toHaveBeenCalledTimes(0); | ||
jest.advanceTimersByTime(50); | ||
expect(fnMock).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should propagate options to the debounce util', async () => { | ||
const { runDebouncedFnMock } = render(200, { leading: true }); | ||
|
||
await runDebouncedFnMock(); | ||
expect(fnMock).toHaveBeenCalledTimes(1); | ||
await runDebouncedFnMock(); | ||
jest.advanceTimersByTime(200); | ||
expect(fnMock).toHaveBeenCalledTimes(2); | ||
}); | ||
|
||
it('should cancel debounce fn when component is unmounted', async () => { | ||
const { wrapper, runDebouncedFnMock } = render(); | ||
|
||
await runDebouncedFnMock(); | ||
jest.advanceTimersByTime(100); | ||
wrapper.destroy(); | ||
jest.advanceTimersByTime(500); | ||
expect(fnMock).toHaveBeenCalledTimes(0); | ||
}); | ||
}); |
File renamed without changes.
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 |
---|---|---|
@@ -1,9 +1,11 @@ | ||
export * from './create-use-device.composable'; | ||
export * from './use-no-element-render'; | ||
export * from './create-use-device'; | ||
export * from './use-$x'; | ||
export * from './use-register-x-module'; | ||
export * from './use-alias-api'; | ||
export * from './use-debounce'; | ||
export * from './use-getter'; | ||
export * from './use-no-element-render'; | ||
export * from './use-on-display'; | ||
export * from './use-store'; | ||
export * from './use-register-x-module'; | ||
export * from './use-state'; | ||
export * from './use-getter'; | ||
export * from './use-alias-api'; | ||
export * from './use-store'; | ||
export * from './use-x-bus'; |
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,26 @@ | ||
import { onBeforeUnmount } from 'vue'; | ||
import { debounce } from '../utils/debounce'; | ||
import { DebounceOptions } from '../utils/types'; | ||
|
||
/** | ||
* Composable which wraps the function passed as parameter into a debounced function and returns it. | ||
* It also cancels the debounced function when component is unmounted. | ||
* | ||
* @param fn - Function to be debounced. | ||
* @param debounceTimeInMs - Time of debounce in ms. | ||
* @param debounceOptions - The options for the debounce strategy. | ||
* @returns Debounced function obtained from `fn` parameter. | ||
*/ | ||
export function useDebounce<Params extends any[]>( | ||
fn: (...args: Params) => void, | ||
debounceTimeInMs: number, | ||
debounceOptions: DebounceOptions = {} | ||
) { | ||
const debouncedFn = debounce(fn, debounceTimeInMs, debounceOptions); | ||
|
||
onBeforeUnmount(() => { | ||
debouncedFn.cancel(); | ||
}); | ||
|
||
return debouncedFn; | ||
} |
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