-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #665 from magiclabs/rominhalltari-sc-75722-react-n…
…ative-sdk-add-support-for-general Add `useInternetConnection` hook to track internet connectivity changes
- Loading branch information
Showing
18 changed files
with
505 additions
and
16 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
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
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,16 @@ | ||
import { useEffect, useState } from 'react'; | ||
import NetInfo, { NetInfoState } from '@react-native-community/netinfo'; | ||
|
||
export const useInternetConnection = () => { | ||
const [isConnected, setIsConnected] = useState(true); | ||
useEffect(() => { | ||
const handleConnectionChange = (connectionInfo: NetInfoState) => { | ||
setIsConnected(!!connectionInfo.isConnected); | ||
}; | ||
|
||
// Subscribe to connection changes and cleanup on unmount | ||
return NetInfo.addEventListener(handleConnectionChange); | ||
}, []); | ||
|
||
return isConnected; | ||
}; |
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
54 changes: 54 additions & 0 deletions
54
packages/@magic-sdk/react-native-bare/test/spec/hooks.spec.ts
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,54 @@ | ||
import { act, renderHook } from '@testing-library/react-native'; | ||
import NetInfo, { NetInfoStateType } from '@react-native-community/netinfo'; | ||
import { useInternetConnection } from '../../src/hooks'; | ||
|
||
beforeAll(() => { | ||
// @ts-ignore mock resolved value | ||
NetInfo.getCurrentState.mockResolvedValue({ | ||
type: NetInfoStateType.cellular, | ||
isConnected: true, | ||
isInternetReachable: true, | ||
details: { | ||
isConnectionExpensive: true, | ||
cellularGeneration: '4g', | ||
}, | ||
}); | ||
}); | ||
|
||
describe('useInternetConnection', () => { | ||
it('should initialize with true when connected', async () => { | ||
const { result } = renderHook(() => useInternetConnection()); | ||
|
||
expect(result.current).toBe(true); | ||
}); | ||
|
||
it('should call the listener when the connection changes', async () => { | ||
NetInfo.addEventListener = jest.fn(); | ||
|
||
const { result } = renderHook(() => useInternetConnection()); | ||
|
||
// Initial render, assuming it's connected | ||
expect(result.current).toBe(true); | ||
|
||
// Simulate a change in connection status | ||
act(() => { | ||
// @ts-ignore mock calls | ||
NetInfo.addEventListener.mock.calls[0][0]({ | ||
type: 'cellular', | ||
isConnected: false, | ||
isInternetReachable: true, | ||
details: { | ||
isConnectionExpensive: true, | ||
}, | ||
}); | ||
}); | ||
|
||
// Wait for the next tick of the event loop to allow state update | ||
await act(async () => { | ||
await new Promise((resolve) => setTimeout(resolve, 0)); // or setImmediate | ||
}); | ||
|
||
// Check if the hook state has been updated | ||
expect(result.current).toBe(false); | ||
}); | ||
}); |
Oops, something went wrong.