-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: implement custom useEvent hook
- Loading branch information
gkuzin13
committed
Jan 1, 2024
1 parent
fccfbb3
commit 826f539
Showing
7 changed files
with
180 additions
and
63 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 was deleted.
Oops, something went wrong.
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,109 @@ | ||
import { fireEvent, renderHook } from '@testing-library/react'; | ||
import useEvent from './useEvent'; | ||
|
||
describe('useEvent', () => { | ||
afterEach(() => { | ||
vi.resetAllMocks(); | ||
}); | ||
|
||
it('calls the event listener when the event is triggered', async () => { | ||
const element = document.createElement('button'); | ||
const handler = vi.fn(); | ||
|
||
renderHook(() => useEvent('click', handler, element)); | ||
|
||
fireEvent.click(element); | ||
|
||
expect(handler).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should bind/unbind the event listener to the window object if no element is provided', async () => { | ||
const spyAddEventListener = vi.spyOn(window, 'addEventListener'); | ||
const spyRemoveEventListener = vi.spyOn(window, 'removeEventListener'); | ||
|
||
const handler = vi.fn(); | ||
const options = undefined; | ||
|
||
const { unmount } = renderHook(() => useEvent('click', handler)); | ||
|
||
expect(spyAddEventListener).toHaveBeenCalledTimes(1); | ||
expect(spyAddEventListener).toHaveBeenCalledWith('click', handler, options); | ||
|
||
unmount(); | ||
|
||
expect(spyRemoveEventListener).toHaveBeenCalledTimes(1); | ||
expect(spyRemoveEventListener).toHaveBeenCalledWith( | ||
'click', | ||
handler, | ||
options, | ||
); | ||
}); | ||
|
||
it('should bind/unbind the event listener if element is provided', async () => { | ||
const element = document.createElement('button'); | ||
const spyAddEventListener = vi.spyOn(element, 'addEventListener'); | ||
const spyRemoveEventListener = vi.spyOn(element, 'removeEventListener'); | ||
|
||
const handler = vi.fn(); | ||
const options = undefined; | ||
|
||
const { unmount } = renderHook(() => | ||
useEvent('click', handler, element, options), | ||
); | ||
|
||
expect(spyAddEventListener).toHaveBeenCalledTimes(1); | ||
expect(spyAddEventListener).toHaveBeenCalledWith('click', handler, options); | ||
|
||
unmount(); | ||
|
||
expect(spyRemoveEventListener).toHaveBeenCalledTimes(1); | ||
expect(spyRemoveEventListener).toHaveBeenCalledWith( | ||
'click', | ||
handler, | ||
options, | ||
); | ||
}); | ||
|
||
it('should not bind the event listener if handler is undefined', async () => { | ||
const element = document.createElement('button'); | ||
const spyAddEventListener = vi.spyOn(element, 'addEventListener'); | ||
const spyRemoveEventListener = vi.spyOn(element, 'removeEventListener'); | ||
|
||
const handler = undefined; | ||
|
||
const { unmount } = renderHook(() => useEvent('click', handler, element)); | ||
|
||
expect(spyAddEventListener).toHaveBeenCalledTimes(0); | ||
|
||
unmount(); | ||
|
||
expect(spyRemoveEventListener).toHaveBeenCalledTimes(0); | ||
}); | ||
|
||
it('should pass options to the event listener', async () => { | ||
const element = document.createElement('button'); | ||
const spyAddEventListener = vi.spyOn(element, 'addEventListener'); | ||
const spyRemoveEventListener = vi.spyOn(element, 'removeEventListener'); | ||
|
||
const handler = vi.fn(); | ||
const options = { | ||
capture: true, | ||
passive: true, | ||
once: true, | ||
}; | ||
|
||
const { unmount } = renderHook(() => | ||
useEvent('click', handler, element, { eventOptions: options }), | ||
); | ||
|
||
expect(spyAddEventListener).toHaveBeenCalledWith('click', handler, options); | ||
|
||
unmount(); | ||
|
||
expect(spyRemoveEventListener).toHaveBeenCalledWith( | ||
'click', | ||
handler, | ||
options, | ||
); | ||
}); | ||
}); |
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,48 @@ | ||
import { isBrowser } from '@/utils/is'; | ||
import { useEffect, useRef } from 'react'; | ||
|
||
type EventsMap = HTMLElementEventMap & | ||
WindowEventMap & | ||
DocumentEventMap & | ||
MediaQueryListEventMap & | ||
WebSocketEventMap; | ||
|
||
type HandlerElements = | ||
| Document | ||
| HTMLElement | ||
| MediaQueryList | ||
| Window | ||
| WebSocket; | ||
|
||
const defaultTarget = isBrowser ? window : null; | ||
|
||
function useEvent<K extends keyof EventsMap>( | ||
name: K, | ||
handler: ((event: EventsMap[K]) => void) | undefined, | ||
element: HandlerElements | null = defaultTarget, | ||
options?: { | ||
eventOptions?: AddEventListenerOptions; | ||
deps?: unknown[]; | ||
}, | ||
) { | ||
const savedHandler = useRef(handler); | ||
|
||
useEffect(() => { | ||
savedHandler.current = handler; | ||
}, [handler]); | ||
|
||
useEffect(() => { | ||
if (!element || !savedHandler.current) return; | ||
|
||
const listener = savedHandler.current as EventListenerOrEventListenerObject; | ||
const eventOptions = options?.eventOptions; | ||
|
||
element.addEventListener(name, listener, eventOptions); | ||
|
||
return () => { | ||
element.removeEventListener(name, listener, eventOptions); | ||
}; | ||
}, [name, element, JSON.stringify(options)]); | ||
} | ||
|
||
export default useEvent; |
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