-
Notifications
You must be signed in to change notification settings - Fork 271
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bug: checkInternetConnection only works once (#146)
* Refactoring function and adding unit tests. * Documentation * Minor version bump
- Loading branch information
Showing
4 changed files
with
71 additions
and
31 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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "react-native-offline", | ||
"version": "4.0.0", | ||
"version": "4.1.0", | ||
"description": "Handy toolbelt to deal with offline mode in React Native applications. Cross-platform, provides a smooth redux integration.", | ||
"main": "./src/index.js", | ||
"author": "Raul Gomez Acuña <[email protected]> (https://github.com/rgommezz)", | ||
|
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 |
---|---|---|
@@ -1,44 +1,26 @@ | ||
/* @flow */ | ||
|
||
import { Platform, NetInfo } from 'react-native'; | ||
import { NetInfo } from 'react-native'; | ||
import checkInternetAccess from './checkInternetAccess'; | ||
import { DEFAULT_PING_SERVER_URL, DEFAULT_TIMEOUT } from './constants'; | ||
|
||
/** | ||
* Utility that allows to query for internet connectivity on demand | ||
* On iOS, the listener is fired immediately after registration | ||
* On Android, we need to use `isConnected.fetch`, that returns a promise which resolves with a boolean | ||
* @param url | ||
* @param timeout | ||
* @param shouldPing | ||
* @returns {Promise<boolean>} | ||
*/ | ||
export default function checkInternetConnection( | ||
export default async function checkInternetConnection( | ||
url: string = DEFAULT_PING_SERVER_URL, | ||
timeout: number = DEFAULT_TIMEOUT, | ||
shouldPing: boolean = true, | ||
): Promise<boolean> { | ||
let connectionChecked: Promise<boolean>; | ||
if (Platform.OS === 'ios') { | ||
connectionChecked = new Promise((resolve: Function) => { | ||
const handleFirstConnectivityChangeIOS = (isConnected: boolean) => { | ||
NetInfo.isConnected.removeEventListener( | ||
'connectionChange', | ||
handleFirstConnectivityChangeIOS, | ||
); | ||
resolve(isConnected); | ||
}; | ||
NetInfo.isConnected.addEventListener( | ||
'connectionChange', | ||
handleFirstConnectivityChangeIOS, | ||
); | ||
}); | ||
} else { | ||
connectionChecked = NetInfo.isConnected.fetch(); | ||
} | ||
|
||
return connectionChecked.then((isConnected: boolean) => { | ||
if (isConnected) { | ||
return checkInternetAccess({ timeout, url }); | ||
return NetInfo.isConnected.fetch().then(async (isConnected: boolean) => { | ||
if (shouldPing) { | ||
const hasInternetAccess = await checkInternetAccess({ timeout, url }); | ||
return hasInternetAccess; | ||
} | ||
return Promise.resolve(false); | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { NetInfo } from 'react-native'; | ||
import checkInternetConnection from '../src/utils/checkInternetConnection'; | ||
import checkInternetAccess from '../src/utils/checkInternetAccess'; | ||
import { | ||
DEFAULT_PING_SERVER_URL, | ||
DEFAULT_TIMEOUT, | ||
} from '../src/utils/constants'; | ||
|
||
jest.mock('../src/utils/checkInternetAccess'); | ||
|
||
checkInternetAccess.mockResolvedValue(true); | ||
|
||
describe('checkInternetConnection', () => { | ||
afterEach(() => { | ||
checkInternetAccess.mockClear(); | ||
}); | ||
describe('shouldPing = true', () => { | ||
it(`calls checkInternetAccess and resolves the promise with its returned value`, async () => { | ||
NetInfo.isConnected.fetch.mockImplementationOnce(() => | ||
Promise.resolve(true), | ||
); | ||
const isConnected = await checkInternetConnection('foo.com', 3000, true); | ||
expect(checkInternetAccess).toHaveBeenCalledWith({ | ||
timeout: 3000, | ||
url: 'foo.com', | ||
}); | ||
expect(isConnected).toBe(true); | ||
}); | ||
}); | ||
|
||
describe('shouldPing = false', () => { | ||
it(`does NOT call checkInternetAccess and directly resolves the promise with a boolean`, async () => { | ||
NetInfo.isConnected.fetch.mockImplementationOnce(() => | ||
Promise.resolve(false), | ||
); | ||
const isConnected = await checkInternetConnection('foo.com', 3000, false); | ||
expect(checkInternetAccess).not.toHaveBeenCalled(); | ||
expect(isConnected).toBe(false); | ||
}); | ||
}); | ||
|
||
it('default parameters', async () => { | ||
NetInfo.isConnected.fetch.mockImplementationOnce(() => | ||
Promise.resolve(true), | ||
); | ||
const isConnected = await checkInternetConnection(); | ||
expect(checkInternetAccess).toHaveBeenCalledWith({ | ||
timeout: DEFAULT_TIMEOUT, | ||
url: DEFAULT_PING_SERVER_URL, | ||
}); | ||
expect(isConnected).toBe(true); | ||
}); | ||
}); |