-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Filter out eventlistener on window from useLocalStorage hook to separ…
…ate useReactiveLocalStorage hook
- Loading branch information
Showing
5 changed files
with
78 additions
and
17 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
frontend/packages/shared/src/hooks/useEventListener.test.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,39 @@ | ||
import { renderHook } from '@testing-library/react'; | ||
import userEvent from '@testing-library/user-event'; | ||
import { act } from 'react-dom/test-utils'; | ||
|
||
import { useEventListener } from './useEventListener'; | ||
|
||
const user = userEvent.setup(); | ||
|
||
const renderUseEventListener = (eventType: string, action: () => void) => | ||
renderHook(() => useEventListener(eventType, action), { | ||
initialProps: { eventType, action }, | ||
}); | ||
|
||
describe('useEventListener', () => { | ||
it('Calls action when given event happens', async () => { | ||
const action = jest.fn(); | ||
renderUseEventListener('click', action); | ||
await act(() => user.click(document.body)); | ||
expect(action).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('Does not call action when another event is given', async () => { | ||
const action = jest.fn(); | ||
renderUseEventListener('click', action); | ||
await act(() => user.keyboard('{Enter}')); | ||
expect(action).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('Removes event listener on unmount', () => { | ||
const removeEventListenerSpy = jest.spyOn( | ||
window, | ||
'removeEventListener', | ||
); | ||
const { unmount } = renderUseEventListener('click', jest.fn()); | ||
expect(removeEventListenerSpy).not.toHaveBeenCalled(); | ||
unmount(); | ||
expect(removeEventListenerSpy).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |
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,16 @@ | ||
import { useEffect } from 'react'; | ||
|
||
/** | ||
* Adds an event listener to the given element or the document body if no element is given. The listener is removed on unmount. | ||
* @param eventType The event type to listen for. | ||
* @param action The action to perform when the event is triggered. | ||
*/ | ||
export function useEventListener( | ||
eventType: string, | ||
action: () => void, | ||
) { | ||
useEffect(() => { | ||
window.addEventListener(eventType, action); | ||
return () => window.removeEventListener(eventType, action); | ||
}, [eventType, action]); | ||
} |
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
20 changes: 20 additions & 0 deletions
20
frontend/packages/shared/src/hooks/useReactiveLocalStorage.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,20 @@ | ||
import { useCallback } from 'react'; | ||
import { typedLocalStorage } from 'app-shared/utils/webStorage'; | ||
import { useLocalStorage } from './useLocalStorage'; | ||
import { useEventListener } from './useEventListener'; | ||
|
||
export const useReactiveLocalStorage = <T>( | ||
key: string, | ||
initialValue?: T, | ||
): [T, (newValue: T) => void, () => void] => { | ||
const [value, setValue, removeValue] = useLocalStorage(key, initialValue); | ||
|
||
const handleStorageChange = useCallback(() => { | ||
const item = typedLocalStorage.getItem<T>(key); | ||
setValue(item); | ||
}, [key, setValue]); | ||
|
||
useEventListener('storage', handleStorageChange); | ||
|
||
return [value, setValue, removeValue]; | ||
}; |
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