Skip to content

Commit 2bff9b7

Browse files
Niloofar/ useFullScreen test cases (#144)
* test: added test cases for usefullscreen * fix: review comments * fix: review comments
1 parent d000c5a commit 2bff9b7

File tree

2 files changed

+58
-4
lines changed

2 files changed

+58
-4
lines changed
+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { act, renderHook } from '@testing-library/react';
2+
import { useFullScreen } from '../custom-hooks';
3+
4+
describe('useFullScreen', () => {
5+
afterEach(() => {
6+
jest.restoreAllMocks();
7+
});
8+
9+
it('should add and remove fullscreen event listeners', () => {
10+
const addEventListenerSpy = jest.spyOn(document, 'addEventListener');
11+
const removeEventListenerSpy = jest.spyOn(document, 'removeEventListener');
12+
13+
const { unmount } = renderHook(() => useFullScreen());
14+
15+
expect(addEventListenerSpy).toHaveBeenCalledWith('fullscreenchange', expect.any(Function), false);
16+
expect(addEventListenerSpy).toHaveBeenCalledWith('webkitfullscreenchange', expect.any(Function), false);
17+
expect(addEventListenerSpy).toHaveBeenCalledWith('mozfullscreenchange', expect.any(Function), false);
18+
expect(addEventListenerSpy).toHaveBeenCalledWith('MSFullscreenChange', expect.any(Function), false);
19+
20+
unmount();
21+
22+
expect(removeEventListenerSpy).toHaveBeenCalledWith('fullscreenchange', expect.any(Function), false);
23+
expect(removeEventListenerSpy).toHaveBeenCalledWith('webkitfullscreenchange', expect.any(Function), false);
24+
expect(removeEventListenerSpy).toHaveBeenCalledWith('mozfullscreenchange', expect.any(Function), false);
25+
expect(removeEventListenerSpy).toHaveBeenCalledWith('MSFullscreenChange', expect.any(Function), false);
26+
});
27+
28+
it('should call requestFullscreen when trying to enter fullscreen', () => {
29+
const requestFullscreenMock = jest.fn();
30+
document.documentElement.requestFullscreen = requestFullscreenMock;
31+
const { result } = renderHook(() => useFullScreen());
32+
33+
act(() => {
34+
result.current.toggleFullScreenMode();
35+
});
36+
37+
expect(requestFullscreenMock).toHaveBeenCalled();
38+
});
39+
40+
it('should clean up event listeners on unmount', () => {
41+
const removeEventListenerSpy = jest.spyOn(document, 'removeEventListener');
42+
const { unmount } = renderHook(() => useFullScreen());
43+
44+
unmount();
45+
46+
expect(removeEventListenerSpy).toHaveBeenCalledTimes(4);
47+
});
48+
});

src/hooks/custom-hooks/useFullScreen.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,19 @@ const useFullScreen = () => {
2020
screenChange.forEach(event => {
2121
document.addEventListener(event, onFullScreen, false);
2222
});
23+
24+
return () => {
25+
screenChange.forEach(event => {
26+
document.removeEventListener(event, onFullScreen, false);
27+
});
28+
};
2329
}, [onFullScreen, screenChange]);
2430

25-
const toggleFullScreenMode = (event: MouseEvent<HTMLButtonElement>) => {
26-
event.stopPropagation();
31+
const toggleFullScreenMode = (event?: MouseEvent<HTMLButtonElement>) => {
32+
event?.stopPropagation();
2733

28-
const exitFullScreen = exit.find(element => document[element as keyof Document]);
29-
const requestFullScreen = request.find(element => document.documentElement[element as keyof HTMLElement]);
34+
const exitFullScreen = exit.find(method => document[method as keyof Document]);
35+
const requestFullScreen = request.find(method => document.documentElement[method as keyof HTMLElement]);
3036

3137
if (isInFullScreenMode && exitFullScreen) {
3238
(document[exitFullScreen as keyof Document] as Document['exitFullscreen'])();

0 commit comments

Comments
 (0)