@exodus/netinfo
Version:
React Native Network Info API for iOS & Android
159 lines (131 loc) • 5.8 kB
JavaScript
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @flow
*/
/* eslint-env jest */
import {NativeModules} from 'react-native';
import NetInfo from '../index';
import {NetInfoEventEmitter} from '../nativeInterface';
const CONNECTED_STATES = [
{type: 'cellular', connected: true},
{type: 'wifi', connected: true},
{type: 'bluetooth', connected: true},
{type: 'ethernet', connected: true},
{type: 'wimax', connected: true},
{type: 'none', connected: false},
{type: 'unknown', connected: false},
];
describe('react-native-netinfo', () => {
describe('isConnected', () => {
describe('fetch', () => {
CONNECTED_STATES.map(({type, connected}) => {
it(`should resolve to ${connected.toString()} when the native module returns a ${type} state`, () => {
NativeModules.RNCNetInfo.getCurrentConnectivity.mockResolvedValue({
connectionType: type,
effectiveConnectionType: 'unknown',
});
return expect(NetInfo.isConnected.fetch()).resolves.toBe(connected);
});
});
it('should pass on errors through the promise chain', () => {
const expectedError = new Error('A test error');
NativeModules.RNCNetInfo.getCurrentConnectivity.mockRejectedValue(
expectedError,
);
return expect(NetInfo.getConnectionInfo()).rejects.toBe(expectedError);
});
});
describe('Event listener management', () => {
it('should add the listener to the native module when passing the correct event name', () => {
NetInfo.isConnected.addEventListener('connectionChange', jest.fn());
expect(NativeModules.RNCNetInfo.addListener).toBeCalledWith(
NetInfo.Events.NetworkStatusDidChange,
);
});
it('should do nothing when passing the wrong event name', () => {
// $FlowExpectedError We are testing passing in the wrong name
NetInfo.isConnected.addEventListener('WRONGNAME', jest.fn());
expect(NativeModules.RNCNetInfo.addListener).not.toBeCalled();
});
it('should remove the listener from the native module when calling removeEventListener', () => {
const listener = jest.fn();
NetInfo.isConnected.addEventListener('connectionChange', listener);
NetInfo.isConnected.removeEventListener('connectionChange', listener);
expect(NativeModules.RNCNetInfo.removeListeners).toBeCalled();
});
it('should remove the listener from the native module when calling remove on the returned subscription', () => {
const listener = jest.fn();
const subscription = NetInfo.isConnected.addEventListener(
'connectionChange',
listener,
);
subscription.remove();
expect(NativeModules.RNCNetInfo.removeListeners).toBeCalled();
});
});
describe('Event listener callbacks', () => {
it('should call the listener when the native event is emmitted', () => {
const listener = jest.fn();
NetInfo.isConnected.addEventListener('connectionChange', listener);
NetInfoEventEmitter.emit(NetInfo.Events.NetworkStatusDidChange, {
connectionType: 'cellular',
effectiveConnectionType: '4g',
});
expect(listener).toBeCalledWith(true);
});
it('should call the listener multiple times when multiple native events are emmitted', () => {
const listener = jest.fn();
NetInfo.isConnected.addEventListener('connectionChange', listener);
NetInfoEventEmitter.emit(NetInfo.Events.NetworkStatusDidChange, {
connectionType: 'cellular',
effectiveConnectionType: '3g',
});
NetInfoEventEmitter.emit(NetInfo.Events.NetworkStatusDidChange, {
connectionType: 'wifi',
effectiveConnectionType: 'unknown',
});
expect(listener).toBeCalledTimes(2);
});
it('should call all listeners when the native event is emmitted', () => {
const listener1 = jest.fn();
const listener2 = jest.fn();
NetInfo.isConnected.addEventListener('connectionChange', listener1);
NetInfo.isConnected.addEventListener('connectionChange', listener2);
NetInfoEventEmitter.emit(NetInfo.Events.NetworkStatusDidChange, {
connectionType: 'cellular',
effectiveConnectionType: '2g',
});
expect(listener1).toBeCalledWith(true);
expect(listener2).toBeCalledWith(true);
});
it('should not call the listener after being removed', () => {
const listener = jest.fn();
NetInfo.isConnected.addEventListener('connectionChange', listener);
NetInfo.isConnected.removeEventListener('connectionChange', listener);
NetInfoEventEmitter.emit(NetInfo.Events.NetworkStatusDidChange, {
connectionType: 'cellular',
effectiveConnectionType: '3g',
});
expect(listener).not.toBeCalled();
});
it('should call the remaining listeners when one has been removed', () => {
const listener1 = jest.fn();
const listener2 = jest.fn();
NetInfo.isConnected.addEventListener('connectionChange', listener1);
NetInfo.isConnected.addEventListener('connectionChange', listener2);
NetInfo.isConnected.removeEventListener('connectionChange', listener1);
NetInfoEventEmitter.emit(NetInfo.Events.NetworkStatusDidChange, {
connectionType: 'unknown',
effectiveConnectionType: 'unknown',
});
expect(listener1).not.toBeCalled();
expect(listener2).toBeCalledWith(false);
});
});
});
});