-
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.
M2-5096: eliminate 3rd-party dep useWindowFocus
Implement custom version instead.
- Loading branch information
1 parent
932918e
commit b92ef06
Showing
8 changed files
with
83 additions
and
27 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { act, renderHook } from '@testing-library/react'; | ||
|
||
import { useWindowFocus } from './useWindowFocus'; | ||
|
||
const fireFocusEvent = () => { | ||
global.window.dispatchEvent(new Event('focus')); | ||
}; | ||
|
||
const fireBlurEvent = () => { | ||
global.window.dispatchEvent(new Event('blur')); | ||
}; | ||
|
||
describe('useWindowFocus)', () => { | ||
test('should default to false', () => { | ||
const { result } = renderHook(() => useWindowFocus()); | ||
|
||
expect(result.current).toBe(false); | ||
}); | ||
|
||
test('should be true after focus event', () => { | ||
const { result } = renderHook(() => useWindowFocus()); | ||
|
||
act(() => { | ||
fireFocusEvent(); | ||
}); | ||
|
||
expect(result.current).toBe(true); | ||
}); | ||
|
||
test('should be false after blur event', () => { | ||
const { result } = renderHook(() => useWindowFocus()); | ||
|
||
act(() => { | ||
fireFocusEvent(); | ||
}); | ||
|
||
expect(result.current).toBe(true); | ||
|
||
act(() => { | ||
fireBlurEvent(); | ||
}); | ||
|
||
expect(result.current).toBe(false); | ||
}); | ||
}); |
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,27 @@ | ||
import { useState, useEffect } from 'react'; | ||
|
||
const hasFocus = () => typeof document !== 'undefined' && document.hasFocus(); | ||
|
||
/** | ||
* Monitors and returns boolean window focus state reactively. | ||
*/ | ||
export const useWindowFocus = () => { | ||
const [focused, setFocused] = useState(hasFocus); | ||
|
||
useEffect(() => { | ||
setFocused(hasFocus()); | ||
|
||
const onFocus = () => setFocused(true); | ||
const onBlur = () => setFocused(false); | ||
|
||
window.addEventListener('focus', onFocus); | ||
window.addEventListener('blur', onBlur); | ||
|
||
return () => { | ||
window.removeEventListener('focus', onFocus); | ||
window.removeEventListener('blur', onBlur); | ||
}; | ||
}, []); | ||
|
||
return focused; | ||
}; |