diff --git a/src/apis/NetInfo/__tests__/index-test.js b/src/apis/NetInfo/__tests__/index-test.js index 46ff365e..0ab0b941 100644 --- a/src/apis/NetInfo/__tests__/index-test.js +++ b/src/apis/NetInfo/__tests__/index-test.js @@ -1,5 +1,33 @@ /* eslint-env mocha */ +import assert from 'assert'; +import NetInfo from '..'; + suite('apis/NetInfo', () => { - test.skip('NO TEST COVERAGE', () => {}); + suite('isConnected', () => { + const handler = () => {}; + + teardown(() => { + try { NetInfo.isConnected.removeEventListener('change', handler); } catch (e) {} + }); + + suite('addEventListener', () => { + test('throws if the provided "eventType" is not supported', () => { + assert.throws(() => NetInfo.isConnected.addEventListener('foo', handler)); + assert.doesNotThrow(() => NetInfo.isConnected.addEventListener('change', handler)); + }); + }); + + suite('removeEventListener', () => { + test('throws if the handler is not registered', () => { + assert.throws(() => NetInfo.isConnected.removeEventListener('change', handler)); + }); + + test('throws if the provided "eventType" is not supported', () => { + NetInfo.isConnected.addEventListener('change', handler); + assert.throws(() => NetInfo.isConnected.removeEventListener('foo', handler)); + assert.doesNotThrow(() => NetInfo.isConnected.removeEventListener('change', handler)); + }); + }); + }); }); diff --git a/src/apis/NetInfo/index.js b/src/apis/NetInfo/index.js index bcae6907..cad84df0 100644 --- a/src/apis/NetInfo/index.js +++ b/src/apis/NetInfo/index.js @@ -7,6 +7,7 @@ */ import ExecutionEnvironment from 'fbjs/lib/ExecutionEnvironment'; +import findIndex from 'lodash/findIndex'; import invariant from 'fbjs/lib/invariant'; const connection = ExecutionEnvironment.canUseDOM && ( @@ -17,6 +18,8 @@ const connection = ExecutionEnvironment.canUseDOM && ( const eventTypes = [ 'change' ]; +const connectionListeners = []; + /** * Navigator online: https://developer.mozilla.org/en-US/docs/Web/API/NavigatorOnLine/onLine * Network Connection API: https://developer.mozilla.org/en-US/docs/Web/API/NetworkInformation @@ -56,8 +59,12 @@ const NetInfo = { isConnected: { addEventListener(type: string, handler: Function): { remove: () => void } { invariant(eventTypes.indexOf(type) !== -1, 'Trying to subscribe to unknown event: "%s"', type); - window.addEventListener('online', handler.bind(null, true), false); - window.addEventListener('offline', handler.bind(null, false), false); + const onlineCallback = () => handler(true); + const offlineCallback = () => handler(false); + connectionListeners.push([ handler, onlineCallback, offlineCallback ]); + + window.addEventListener('online', onlineCallback, false); + window.addEventListener('offline', offlineCallback, false); return { remove: () => NetInfo.isConnected.removeEventListener(type, handler) @@ -66,8 +73,15 @@ const NetInfo = { removeEventListener(type: string, handler: Function): void { invariant(eventTypes.indexOf(type) !== -1, 'Trying to subscribe to unknown event: "%s"', type); - window.removeEventListener('online', handler.bind(null, true), false); - window.removeEventListener('offline', handler.bind(null, false), false); + + const listenerIndex = findIndex(connectionListeners, (pair) => pair[0] === handler); + invariant(listenerIndex !== -1, 'Trying to remove NetInfo connection listener for unregistered handler'); + const [ , onlineCallback, offlineCallback ] = connectionListeners[listenerIndex]; + + window.removeEventListener('online', onlineCallback, false); + window.removeEventListener('offline', offlineCallback, false); + + connectionListeners.splice(listenerIndex, 1); }, fetch(): Promise {