Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Niloofar/ useFullScreen test cases #144

Merged
merged 3 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions src/hooks/__tests__/useFullScreen.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { act, renderHook } from '@testing-library/react';
import { useFullScreen } from '../custom-hooks';

describe('useFullScreen hook', () => {
niloofar-deriv marked this conversation as resolved.
Show resolved Hide resolved
afterEach(() => {
jest.restoreAllMocks();
});

it('adds and removes fullscreen event listeners', () => {
niloofar-deriv marked this conversation as resolved.
Show resolved Hide resolved
const addEventListenerSpy = jest.spyOn(document, 'addEventListener');
const removeEventListenerSpy = jest.spyOn(document, 'removeEventListener');

const { unmount } = renderHook(() => useFullScreen());

expect(addEventListenerSpy).toHaveBeenCalledWith('fullscreenchange', expect.any(Function), false);
expect(addEventListenerSpy).toHaveBeenCalledWith('webkitfullscreenchange', expect.any(Function), false);
expect(addEventListenerSpy).toHaveBeenCalledWith('mozfullscreenchange', expect.any(Function), false);
expect(addEventListenerSpy).toHaveBeenCalledWith('MSFullscreenChange', expect.any(Function), false);

unmount();

expect(removeEventListenerSpy).toHaveBeenCalledWith('fullscreenchange', expect.any(Function), false);
expect(removeEventListenerSpy).toHaveBeenCalledWith('webkitfullscreenchange', expect.any(Function), false);
expect(removeEventListenerSpy).toHaveBeenCalledWith('mozfullscreenchange', expect.any(Function), false);
expect(removeEventListenerSpy).toHaveBeenCalledWith('MSFullscreenChange', expect.any(Function), false);
});

it('calls requestFullscreen when trying to enter fullscreen', () => {
const requestFullscreenMock = jest.fn();
document.documentElement.requestFullscreen = requestFullscreenMock;
const { result } = renderHook(() => useFullScreen());

act(() => {
result.current.toggleFullScreenMode();
});

expect(requestFullscreenMock).toHaveBeenCalled();
});

it('cleans up event listeners on unmount', () => {
niloofar-deriv marked this conversation as resolved.
Show resolved Hide resolved
const removeEventListenerSpy = jest.spyOn(document, 'removeEventListener');
const { unmount } = renderHook(() => useFullScreen());

unmount();

expect(removeEventListenerSpy).toHaveBeenCalledTimes(4);
});
});
14 changes: 10 additions & 4 deletions src/hooks/custom-hooks/useFullScreen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,19 @@ const useFullScreen = () => {
screenChange.forEach(event => {
document.addEventListener(event, onFullScreen, false);
});

return () => {
screenChange.forEach(event => {
document.removeEventListener(event, onFullScreen, false);
});
};
}, [onFullScreen, screenChange]);

const toggleFullScreenMode = (event: MouseEvent<HTMLButtonElement>) => {
event.stopPropagation();
const toggleFullScreenMode = (event?: MouseEvent<HTMLButtonElement>) => {
event?.stopPropagation();

const exitFullScreen = exit.find(element => document[element as keyof Document]);
const requestFullScreen = request.find(element => document.documentElement[element as keyof HTMLElement]);
const exitFullScreen = exit.find(method => document[method as keyof Document]);
const requestFullScreen = request.find(method => document.documentElement[method as keyof HTMLElement]);

if (isInFullScreenMode && exitFullScreen) {
(document[exitFullScreen as keyof Document] as Document['exitFullscreen'])();
Expand Down
Loading