-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Update event listener hooks - Remove useEventListener function overloads - Remove useDocument hook - Remove useWindow hook - Add useDocument hook - Add useWindow hook - Add RefObjectOption to useEventListener for ref support #101 #102 * Remove invalid parameters in docs * Create isRefObject function See: #118 (comment) * Rename getRefObjectOption to unref * Remove unnecessary useDocument and useWindow hooks * Update src/utils/unref/unref.mdx Co-authored-by: Arjan van Wijk <[email protected]> --------- Co-authored-by: Arjan van Wijk <[email protected]>
- Loading branch information
1 parent
eae00dc
commit 09fe1e8
Showing
24 changed files
with
267 additions
and
1,901 deletions.
There are no files selected for viewing
26 changes: 0 additions & 26 deletions
26
src/hooks/useDocumentEventListener/useDocumentEventListener.stories.mdx
This file was deleted.
Oops, something went wrong.
38 changes: 0 additions & 38 deletions
38
src/hooks/useDocumentEventListener/useDocumentEventListener.stories.tsx
This file was deleted.
Oops, something went wrong.
18 changes: 0 additions & 18 deletions
18
src/hooks/useDocumentEventListener/useDocumentEventListener.test.tsx
This file was deleted.
Oops, something went wrong.
12 changes: 0 additions & 12 deletions
12
src/hooks/useDocumentEventListener/useDocumentEventListener.ts
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
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 |
---|---|---|
@@ -1,18 +1,48 @@ | ||
import { renderHook } from '@testing-library/react'; | ||
/* eslint-disable react/no-multi-comp */ | ||
import { jest } from '@jest/globals'; | ||
import { render } from '@testing-library/react'; | ||
import { createRef, useEffect, useState, type ReactElement } from 'react'; | ||
import { useEventListener } from './useEventListener.js'; | ||
|
||
describe('useEventListener', () => { | ||
it('should not crash', () => { | ||
renderHook( | ||
() => { | ||
useEventListener(typeof document === 'undefined' ? undefined : document, 'focusin', () => { | ||
// eslint-disable-next-line no-console | ||
console.log(document.activeElement); | ||
}); | ||
}, | ||
{ | ||
initialProps: undefined, | ||
}, | ||
); | ||
it('Should listen to event attached to element from RefObject', () => { | ||
const spy = jest.fn(); | ||
const ref = createRef<HTMLDivElement>(); | ||
|
||
function Test(): ReactElement { | ||
useEventListener(ref, 'click', spy); | ||
|
||
return <div ref={ref} />; | ||
} | ||
|
||
render(<Test />); | ||
|
||
ref.current?.click(); | ||
|
||
expect(spy).toBeCalledTimes(1); | ||
}); | ||
|
||
it('Should listen to event attached to element from state', async () => { | ||
const spy = jest.fn(); | ||
let exposedRef: HTMLDivElement | null = null; | ||
|
||
function Test(): ReactElement { | ||
const [ref, setRef] = useState<HTMLDivElement | null>(null); | ||
|
||
useEffect(() => { | ||
exposedRef = ref; | ||
}, [ref]); | ||
|
||
useEventListener(ref, 'click', spy); | ||
|
||
return <div ref={setRef} />; | ||
} | ||
|
||
render(<Test />); | ||
|
||
// @ts-expect-error typescript doesn't infer type for exposedRef correctly | ||
exposedRef?.click(); | ||
|
||
expect(spy).toBeCalledTimes(1); | ||
}); | ||
}); |
Oops, something went wrong.