-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(react): useBlockDoubleClick 추가 (#82)
* feat(react): useBlockDoubleClick 추가 * imp: useBlockPromiseMultipleClick 훅 테스트코드 및 네이밍 수정 완료 * fix: 누락된 useBlockPromiseMultipleClick의 메서드 네이밍 변경 완료
- Loading branch information
Showing
3 changed files
with
73 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
25 changes: 25 additions & 0 deletions
25
packages/react/src/hooks/useBlockPromiseMultipleClick/index.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,25 @@ | ||
import { useRef, useState } from 'react'; | ||
|
||
export const useBlockMultipleClick = () => { | ||
const [isLoading, setIsLoading] = useState(false); | ||
const isClicked = useRef(false); | ||
|
||
const blockMultipleClick = async (callback: () => Promise<unknown>) => { | ||
if (isClicked.current) { | ||
return; | ||
} | ||
|
||
isClicked.current = true; | ||
setIsLoading(true); | ||
|
||
await callback(); | ||
|
||
isClicked.current = false; | ||
setIsLoading(false); | ||
}; | ||
|
||
return { | ||
isLoading, | ||
blockMultipleClick, | ||
}; | ||
}; |
47 changes: 47 additions & 0 deletions
47
packages/react/src/hooks/useBlockPromiseMultipleClick/useBlockPromiseMultipleClick.spec.tsx
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,47 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { screen, renderHook } from '@testing-library/react'; | ||
import { renderSetup } from '../../utils/test/renderSetup'; | ||
import { useBlockMultipleClick } from '.'; | ||
|
||
const delay = (time: number) => { | ||
return new Promise<void>((resolve) => { | ||
setTimeout(() => resolve(), time); | ||
}); | ||
}; | ||
|
||
beforeEach(() => { | ||
vi.useFakeTimers(); | ||
}); | ||
|
||
afterEach(() => { | ||
vi.useRealTimers(); | ||
}); | ||
|
||
describe('useBlockMultipleClick', () => { | ||
it('should block double click', async () => { | ||
const mockFn = vi.fn(async () => await delay(1000)); | ||
const { result } = renderHook(useBlockMultipleClick); | ||
|
||
const { blockMultipleClick } = result.current; | ||
expect(result.current.isLoading).toBe(false); | ||
|
||
const onClick = () => blockMultipleClick(mockFn); | ||
|
||
const { user } = renderSetup( | ||
<button onClick={onClick}>TestButton</button>, | ||
{ delay: null }, | ||
); | ||
|
||
const button = screen.getByRole('button'); | ||
|
||
await user.click(button); | ||
await user.click(button); | ||
|
||
expect(result.current.isLoading).toBe(true); | ||
expect(mockFn).toHaveBeenCalledTimes(1); | ||
|
||
await vi.advanceTimersByTimeAsync(1000); | ||
|
||
expect(result.current.isLoading).toBe(false); | ||
}); | ||
}); |