Skip to content

Jest Tests #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ aliases:
name: Flow Checks
command: yarn test:flow

- &javascript
name: Javascript Tests
command: yarn test:js
- &jest
name: Jest Unit Tests
command: yarn test:jest

# -------------------------
# JOBS
Expand Down Expand Up @@ -110,12 +110,12 @@ jobs:
at: ~/react-native-netinfo
- run: *flow

javascript:
jest:
<<: *linux_defaults
steps:
- attach_workspace:
at: ~/react-native-netinfo
- run: *javascript
- run: *jest

android-compile:
<<: *android_defaults
Expand Down Expand Up @@ -172,7 +172,7 @@ workflows:
- flow:
requires:
- linux-checkout
- javascript:
- jest:
requires:
- linux-checkout
- android-compile:
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,6 @@ buck-out/

# Editor config
.vscode

# Outputs
coverage
25 changes: 25 additions & 0 deletions jest.setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* 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';

// Mock the RNCNetInfo native module to allow us to unit test the JavaScript code
NativeModules.RNCNetInfo = {
getCurrentConnectivity: jest.fn(),
isConnectionMetered: jest.fn(),
addListener: jest.fn(),
removeListeners: jest.fn(),
};

// Reset the mocks before each test
global.beforeEach(() => {
jest.resetAllMocks();
});
111 changes: 111 additions & 0 deletions js/__tests__/eventListenerCallbacks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/**
* 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';

describe('react-native-netinfo', () => {
describe('Event listener callbacks', () => {
it('should call the listener when the native event is emmitted', () => {
const listener = jest.fn();
NetInfo.addEventListener('connectionChange', listener);

const expectedConnectionType = 'cellular';
const expectedEffectiveConnectionType = '3g';

NetInfoEventEmitter.emit(NetInfo.Events.NetworkStatusDidChange, {
connectionType: expectedConnectionType,
effectiveConnectionType: expectedEffectiveConnectionType,
});

expect(listener).toBeCalledWith({
type: expectedConnectionType,
effectiveType: expectedEffectiveConnectionType,
});
});

it('should call the listener multiple times when multiple native events are emmitted', () => {
const listener = jest.fn();
NetInfo.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.addEventListener('connectionChange', listener1);
NetInfo.addEventListener('connectionChange', listener2);

const expectedConnectionType = 'cellular';
const expectedEffectiveConnectionType = '3g';

NetInfoEventEmitter.emit(NetInfo.Events.NetworkStatusDidChange, {
connectionType: expectedConnectionType,
effectiveConnectionType: expectedEffectiveConnectionType,
});

const expectedResults = {
type: expectedConnectionType,
effectiveType: expectedEffectiveConnectionType,
};

expect(listener1).toBeCalledWith(expectedResults);
expect(listener2).toBeCalledWith(expectedResults);
});

it('should not call the listener after being removed', () => {
const listener = jest.fn();
NetInfo.addEventListener('connectionChange', listener);
NetInfo.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.addEventListener('connectionChange', listener1);
NetInfo.addEventListener('connectionChange', listener2);

NetInfo.removeEventListener('connectionChange', listener1);

const expectedConnectionType = 'cellular';
const expectedEffectiveConnectionType = '3g';

NetInfoEventEmitter.emit(NetInfo.Events.NetworkStatusDidChange, {
connectionType: expectedConnectionType,
effectiveConnectionType: expectedEffectiveConnectionType,
});

expect(listener1).not.toBeCalled();
expect(listener2).toBeCalledWith({
type: expectedConnectionType,
effectiveType: expectedEffectiveConnectionType,
});
});
});
});
60 changes: 60 additions & 0 deletions js/__tests__/eventListenerManagement.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* 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';

describe('react-native-netinfo', () => {
describe('Event listener management', () => {
it('should add the listener to the native module when passing the correct event name', () => {
NetInfo.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.addEventListener('WRONGNAME', jest.fn());
expect(NativeModules.RNCNetInfo.addListener).not.toBeCalled();
});

it('should not error when calling remove on an invalid subscription', () => {
// $FlowExpectedError We are testing passing in the wrong name
const subscription = NetInfo.addEventListener('WRONGNAME', jest.fn());
subscription.remove();
expect(NativeModules.RNCNetInfo.addListener).not.toBeCalled();
});

it('should remove the listener from the native module when calling removeEventListener', () => {
const listener = jest.fn();
NetInfo.addEventListener('connectionChange', listener);
NetInfo.removeEventListener('connectionChange', listener);
expect(NativeModules.RNCNetInfo.removeListeners).toBeCalled();
});

it('should not call the native module if asked to remove a listener which was never added', () => {
NetInfo.removeEventListener('connectionChange', jest.fn());
expect(NativeModules.RNCNetInfo.removeListeners).not.toBeCalled();
});

it('should remove the listener from the native module when calling remove on the returned subscription', () => {
const listener = jest.fn();
const subscription = NetInfo.addEventListener(
'connectionChange',
listener,
);
subscription.remove();
expect(NativeModules.RNCNetInfo.removeListeners).toBeCalled();
});
});
});
40 changes: 40 additions & 0 deletions js/__tests__/getConnectionInfo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* 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 NetInfo from '../index';
import {RNCNetInfo} from '../nativeInterface';

describe('react-native-netinfo', () => {
describe('getConnectionInfo', () => {
it('should pass on the responses when the library promise returns', () => {
const expectedConnectionType = 'cellular';
const expectedEffectiveConnectionType = '3g';

RNCNetInfo.getCurrentConnectivity.mockResolvedValue({
connectionType: expectedConnectionType,
effectiveConnectionType: expectedEffectiveConnectionType,
});

return expect(NetInfo.getConnectionInfo()).resolves.toEqual({
type: expectedConnectionType,
effectiveType: expectedEffectiveConnectionType,
});
});

it('should pass on errors through the promise chain', () => {
const expectedError = new Error('A test error');

RNCNetInfo.getCurrentConnectivity.mockRejectedValue(expectedError);

return expect(NetInfo.getConnectionInfo()).rejects.toBe(expectedError);
});
});
});
Loading