Skip to content

Commit

Permalink
fix: options at removeEventListener
Browse files Browse the repository at this point in the history
  • Loading branch information
JimGitFE committed Nov 21, 2024
1 parent 5026898 commit c77ba5f
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ describe('useEventListener()', () => {
expect(windowRemoveEventListenerSpy).toHaveBeenCalledWith(
eventName,
expect.any(Function),
options,
)
})

Expand All @@ -90,16 +91,17 @@ describe('useEventListener()', () => {
expect(refRemoveEventListenerSpy).toHaveBeenCalledWith(
eventName,
expect.any(Function),
options,
)
})

it('should bind/unbind the event listener to the document when document is provided', () => {
const eventName = 'test-event'
const listener = vitest.fn()
const handler = vitest.fn()
const options = undefined

const { unmount } = renderHook(() => {
useEventListener(eventName, listener, { element: docRef, options })
useEventListener(eventName, handler, { element: docRef, options })
})

expect(docAddEventListenerSpy).toHaveBeenCalledTimes(1)
Expand All @@ -115,6 +117,7 @@ describe('useEventListener()', () => {
expect(docRemoveEventListenerSpy).toHaveBeenCalledWith(
eventName,
expect.any(Function),
options,
)
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,7 @@ function useEventListener<

useEffect(() => {
// Define the listening target
const targetElement: E | Window = config.element
? 'current' in config.element
? config.element.current ?? window
: config.element
: window
const targetElement: E | Window = config.element ? 'current' in config.element ? config.element.current ?? window : config.element : window

if (!targetElement) return

Expand Down

0 comments on commit c77ba5f

Please sign in to comment.