|
| 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 | +}); |
0 commit comments