|
| 1 | +import { renderHook } from '@testing-library/react'; |
| 2 | +import useConnectionBackOnlineTracker from '.'; |
| 3 | +import * as useCustomEventTrackerModule from '../useCustomEventTracker'; |
| 4 | +import useNetworkStatusTracker from '../useNetworkStatusTracker'; |
| 5 | + |
| 6 | +jest.mock('../useNetworkStatusTracker', () => ({ |
| 7 | + __esModule: true, |
| 8 | + default: jest.fn(() => ({ isOnline: true, networkType: 'unknown' })), |
| 9 | +})); |
| 10 | + |
| 11 | +jest.mock('../useTrackingToggle', () => ({ |
| 12 | + __esModule: true, |
| 13 | + default: jest.fn(() => ({ trackingIsEnabled: true })), |
| 14 | +})); |
| 15 | + |
| 16 | +const mockTrackEvent = jest.fn(); |
| 17 | +const mockUseCustomEventTracker = jest.spyOn( |
| 18 | + useCustomEventTrackerModule, |
| 19 | + 'default', |
| 20 | +); |
| 21 | + |
| 22 | +describe('useConnectionBackOnlineTracker', () => { |
| 23 | + let originalVisibility: PropertyDescriptor | undefined; |
| 24 | + |
| 25 | + beforeEach(() => { |
| 26 | + jest.clearAllMocks(); |
| 27 | + mockUseCustomEventTracker.mockReturnValue(mockTrackEvent); |
| 28 | + |
| 29 | + // Ensure page is considered visible for tracking to fire |
| 30 | + originalVisibility = Object.getOwnPropertyDescriptor( |
| 31 | + document, |
| 32 | + 'visibilityState', |
| 33 | + ); |
| 34 | + Object.defineProperty(document, 'visibilityState', { |
| 35 | + value: 'visible', |
| 36 | + configurable: true, |
| 37 | + }); |
| 38 | + }); |
| 39 | + |
| 40 | + afterEach(() => { |
| 41 | + if (originalVisibility) { |
| 42 | + Object.defineProperty(document, 'visibilityState', originalVisibility); |
| 43 | + } |
| 44 | + }); |
| 45 | + |
| 46 | + it('should initialize useCustomEventTracker with correct eventName', () => { |
| 47 | + renderHook(() => useConnectionBackOnlineTracker()); |
| 48 | + |
| 49 | + expect(mockUseCustomEventTracker).toHaveBeenCalledWith({ |
| 50 | + eventName: 'network-connection-back-online', |
| 51 | + }); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should track event when offline -> online transition happens', () => { |
| 55 | + const networkStatusMock = useNetworkStatusTracker as jest.Mock; |
| 56 | + |
| 57 | + // Initial render: offline |
| 58 | + networkStatusMock.mockImplementation(() => ({ |
| 59 | + isOnline: false, |
| 60 | + networkType: '3g', |
| 61 | + })); |
| 62 | + |
| 63 | + const { rerender } = renderHook(() => useConnectionBackOnlineTracker()); |
| 64 | + |
| 65 | + expect(mockTrackEvent).not.toHaveBeenCalled(); |
| 66 | + |
| 67 | + // Rerender: transition to online with new network type |
| 68 | + networkStatusMock.mockImplementation(() => ({ |
| 69 | + isOnline: true, |
| 70 | + networkType: '4g', |
| 71 | + })); |
| 72 | + |
| 73 | + rerender(); |
| 74 | + |
| 75 | + expect(mockTrackEvent).toHaveBeenCalledTimes(1); |
| 76 | + expect(mockTrackEvent).toHaveBeenCalledWith('4g'); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should not track event when offline -> online transition happens if page is not visible', () => { |
| 80 | + const networkStatusMock = useNetworkStatusTracker as jest.Mock; |
| 81 | + |
| 82 | + networkStatusMock.mockImplementation(() => ({ |
| 83 | + isOnline: false, |
| 84 | + networkType: '3g', |
| 85 | + })); |
| 86 | + |
| 87 | + const { rerender } = renderHook(() => useConnectionBackOnlineTracker()); |
| 88 | + |
| 89 | + expect(mockTrackEvent).not.toHaveBeenCalled(); |
| 90 | + |
| 91 | + Object.defineProperty(document, 'visibilityState', { |
| 92 | + value: 'hidden', |
| 93 | + configurable: true, |
| 94 | + }); |
| 95 | + |
| 96 | + networkStatusMock.mockImplementation(() => ({ |
| 97 | + isOnline: true, |
| 98 | + networkType: '4g', |
| 99 | + })); |
| 100 | + |
| 101 | + rerender(); |
| 102 | + |
| 103 | + expect(mockTrackEvent).not.toHaveBeenCalled(); |
| 104 | + }); |
| 105 | +}); |
0 commit comments