-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into chore/upgrade-assets-controllers-v35.0.0
- Loading branch information
Showing
7 changed files
with
234 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
import { renderHook, act } from '@testing-library/react-hooks'; | ||
import { useConnectionHandler } from '../../../util/navigation/useConnectionHandler'; | ||
import { MetaMetricsEvents } from '../../../components/hooks/useMetrics'; | ||
|
||
const mockTrackEvent = jest.fn(); | ||
|
||
jest.mock('../../../components/hooks/useMetrics', () => ({ | ||
useMetrics: () => ({ | ||
trackEvent: mockTrackEvent, | ||
}), | ||
MetaMetricsEvents: { | ||
CONNECTION_DROPPED: 'CONNECTION_DROPPED', | ||
CONNECTION_RESTORED: 'CONNECTION_RESTORED', | ||
}, | ||
})); | ||
|
||
describe('useConnectionHandler', () => { | ||
const mockNavigation = { navigate: jest.fn() }; | ||
|
||
beforeEach(() => { | ||
jest.useFakeTimers(); | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.useRealTimers(); | ||
}); | ||
|
||
it('should not navigate to OfflineModeView immediately when connection is lost', () => { | ||
const { result } = renderHook(() => useConnectionHandler(mockNavigation)); | ||
|
||
act(() => { | ||
result.current.connectionChangeHandler({ isConnected: false }); | ||
}); | ||
|
||
expect(mockNavigation.navigate).not.toHaveBeenCalled(); | ||
expect(mockTrackEvent).toHaveBeenCalledWith( | ||
MetaMetricsEvents.CONNECTION_DROPPED, | ||
); | ||
}); | ||
|
||
it('should navigate to OfflineModeView after sustained offline period', () => { | ||
const { result } = renderHook(() => useConnectionHandler(mockNavigation)); | ||
|
||
act(() => { | ||
result.current.connectionChangeHandler({ isConnected: false }); | ||
}); | ||
|
||
jest.advanceTimersByTime(3000); | ||
|
||
expect(mockNavigation.navigate).toHaveBeenCalledWith('OfflineModeView'); | ||
expect(mockTrackEvent).toHaveBeenCalledWith( | ||
MetaMetricsEvents.CONNECTION_DROPPED, | ||
); | ||
}); | ||
|
||
it('should not navigate to OfflineModeView if connection is restored within timeout', () => { | ||
const { result } = renderHook(() => useConnectionHandler(mockNavigation)); | ||
|
||
act(() => { | ||
result.current.connectionChangeHandler({ isConnected: false }); | ||
}); | ||
|
||
expect(mockTrackEvent).toHaveBeenCalledWith( | ||
MetaMetricsEvents.CONNECTION_DROPPED, | ||
); | ||
|
||
jest.advanceTimersByTime(1000); | ||
|
||
act(() => { | ||
result.current.connectionChangeHandler({ isConnected: true }); | ||
}); | ||
|
||
jest.advanceTimersByTime(2000); | ||
|
||
expect(mockNavigation.navigate).not.toHaveBeenCalled(); | ||
expect(mockTrackEvent).toHaveBeenCalledTimes(2); | ||
expect(mockTrackEvent).toHaveBeenNthCalledWith( | ||
2, | ||
MetaMetricsEvents.CONNECTION_RESTORED, | ||
); | ||
}); | ||
|
||
it('should do nothing if state is null', () => { | ||
const { result } = renderHook(() => useConnectionHandler(mockNavigation)); | ||
|
||
act(() => { | ||
result.current.connectionChangeHandler(null); | ||
}); | ||
|
||
expect(mockNavigation.navigate).not.toHaveBeenCalled(); | ||
expect(mockTrackEvent).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should not track events if connection state does not change', () => { | ||
const { result } = renderHook(() => useConnectionHandler(mockNavigation)); | ||
|
||
act(() => { | ||
result.current.connectionChangeHandler({ isConnected: true }); | ||
}); | ||
|
||
expect(mockTrackEvent).not.toHaveBeenCalled(); | ||
|
||
act(() => { | ||
result.current.connectionChangeHandler({ isConnected: true }); | ||
}); | ||
|
||
expect(mockTrackEvent).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should clear timeout if connection is restored before navigation', () => { | ||
const { result } = renderHook(() => useConnectionHandler(mockNavigation)); | ||
|
||
act(() => { | ||
result.current.connectionChangeHandler({ isConnected: false }); | ||
}); | ||
|
||
expect(mockTrackEvent).toHaveBeenCalledWith( | ||
MetaMetricsEvents.CONNECTION_DROPPED, | ||
); | ||
|
||
jest.advanceTimersByTime(2000); | ||
|
||
act(() => { | ||
result.current.connectionChangeHandler({ isConnected: true }); | ||
}); | ||
|
||
expect(mockTrackEvent).toHaveBeenCalledWith( | ||
MetaMetricsEvents.CONNECTION_RESTORED, | ||
); | ||
|
||
jest.advanceTimersByTime(1000); | ||
|
||
expect(mockNavigation.navigate).not.toHaveBeenCalled(); | ||
expect(mockTrackEvent).toHaveBeenCalledTimes(2); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { useRef, useCallback } from 'react'; | ||
import { | ||
useMetrics, | ||
MetaMetricsEvents, | ||
} from '../../components/hooks/useMetrics'; | ||
|
||
// TODO: Replace "any" with type | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export const useConnectionHandler = (navigation: any) => { | ||
const connectedRef = useRef(true); | ||
const timeoutRef = useRef<NodeJS.Timeout | null>(null); | ||
|
||
const { trackEvent } = useMetrics(); | ||
|
||
const connectionChangeHandler = useCallback( | ||
(state: { isConnected: boolean } | null) => { | ||
if (!state) return; | ||
const { isConnected } = state; | ||
|
||
if (timeoutRef.current) { | ||
clearTimeout(timeoutRef.current); | ||
timeoutRef.current = null; | ||
} | ||
|
||
if (connectedRef.current !== isConnected) { | ||
if (isConnected === false) { | ||
trackEvent(MetaMetricsEvents.CONNECTION_DROPPED); | ||
timeoutRef.current = setTimeout(() => { | ||
navigation.navigate('OfflineModeView'); | ||
}, 3000); | ||
} else { | ||
trackEvent(MetaMetricsEvents.CONNECTION_RESTORED); | ||
} | ||
connectedRef.current = isConnected; | ||
} | ||
}, | ||
[navigation, trackEvent], | ||
); | ||
|
||
return { connectionChangeHandler }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters