|
| 1 | +import { renderHook, act } from '@testing-library/react-hooks'; |
| 2 | +import useDocumentVisibility from '../src/useDocumentVisibility'; |
| 3 | + |
| 4 | +describe('useDocumentVisibility', () => { |
| 5 | + const originalVisibilityState = document.visibilityState; |
| 6 | + |
| 7 | + afterEach(() => { |
| 8 | + Object.defineProperty(document, 'visibilityState', { |
| 9 | + configurable: true, |
| 10 | + value: originalVisibilityState, |
| 11 | + }); |
| 12 | + }); |
| 13 | + |
| 14 | + it('should be defined', () => { |
| 15 | + expect(useDocumentVisibility).toBeDefined(); |
| 16 | + }); |
| 17 | + |
| 18 | + it('should return false initially', () => { |
| 19 | + const { result } = renderHook(() => useDocumentVisibility()); |
| 20 | + |
| 21 | + expect(result.current).toBe(false); |
| 22 | + }); |
| 23 | + |
| 24 | + it('should return true initially when initialState is true', () => { |
| 25 | + const { result } = renderHook(() => useDocumentVisibility(true)); |
| 26 | + |
| 27 | + expect(result.current).toBe(true); |
| 28 | + }); |
| 29 | + |
| 30 | + it('should return false initially when initialState is false', () => { |
| 31 | + const { result } = renderHook(() => useDocumentVisibility(false)); |
| 32 | + |
| 33 | + expect(result.current).toBe(false); |
| 34 | + }); |
| 35 | + |
| 36 | + it('should return true when document becomes visible', () => { |
| 37 | + const { result } = renderHook(() => useDocumentVisibility(true)); |
| 38 | + |
| 39 | + act(() => { |
| 40 | + Object.defineProperty(document, 'visibilityState', { |
| 41 | + configurable: true, |
| 42 | + value: 'visible', |
| 43 | + }); |
| 44 | + document.dispatchEvent(new Event('visibilitychange')); |
| 45 | + }); |
| 46 | + |
| 47 | + expect(result.current).toBe(true); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should return false when document becomes hidden', () => { |
| 51 | + const { result } = renderHook(() => useDocumentVisibility()); |
| 52 | + |
| 53 | + act(() => { |
| 54 | + Object.defineProperty(document, 'visibilityState', { |
| 55 | + configurable: true, |
| 56 | + value: 'visible', |
| 57 | + }); |
| 58 | + document.dispatchEvent(new Event('visibilitychange')); |
| 59 | + }); |
| 60 | + expect(result.current).toBe(true); |
| 61 | + |
| 62 | + act(() => { |
| 63 | + Object.defineProperty(document, 'visibilityState', { |
| 64 | + configurable: true, |
| 65 | + value: 'hidden', |
| 66 | + }); |
| 67 | + document.dispatchEvent(new Event('visibilitychange')); |
| 68 | + }); |
| 69 | + expect(result.current).toBe(false); |
| 70 | + }); |
| 71 | + |
| 72 | + it('should add event listener on mount', () => { |
| 73 | + const addEventListenerSpy = jest.spyOn(document, 'addEventListener'); |
| 74 | + |
| 75 | + renderHook(() => useDocumentVisibility()); |
| 76 | + |
| 77 | + expect(addEventListenerSpy).toHaveBeenCalledWith('visibilitychange', expect.any(Function)); |
| 78 | + |
| 79 | + addEventListenerSpy.mockRestore(); |
| 80 | + }); |
| 81 | + |
| 82 | + it('should remove event listener on unmount', () => { |
| 83 | + const removeEventListenerSpy = jest.spyOn(document, 'removeEventListener'); |
| 84 | + |
| 85 | + const { unmount } = renderHook(() => useDocumentVisibility()); |
| 86 | + unmount(); |
| 87 | + |
| 88 | + expect(removeEventListenerSpy).toHaveBeenCalledWith('visibilitychange', expect.any(Function)); |
| 89 | + |
| 90 | + removeEventListenerSpy.mockRestore(); |
| 91 | + }); |
| 92 | +}); |
0 commit comments