Commit c97e559a authored by yogevbd's avatar yogevbd

Fix unit tests

parent 13863605
......@@ -37,8 +37,6 @@ class NotificationsExampleApp extends Component {
NotificationsIOS.addEventListener('remoteNotificationsRegistered', this.onPushRegistered.bind(this));
NotificationsIOS.addEventListener('remoteNotificationsRegistrationFailed', this.onPushRegisteredFailed.bind(this));
NotificationsIOS.consumeBackgroundQueue();
NotificationsIOS.addEventListener('pushKitRegistered', this.onPushKitRegistered.bind(this));
NotificationsIOS.registerPushKit();
......
......@@ -2,7 +2,7 @@
* @flow
*/
'use strict';
import { NativeModules, DeviceEventEmitter, NativeAppEventEmitter } from 'react-native';
import { NativeModules, DeviceEventEmitter } from 'react-native';
import Map from 'core-js/library/es6/map';
import uuid from 'uuid';
const NativeRNNotifications = NativeModules.RNBridgeModule; // eslint-disable-line no-unused-vars
......
......@@ -22,7 +22,7 @@ export default class IOSNotification {
this._badge = notification.aps.badge;
this._category = notification.managedAps.category;
this._type = 'managed';
this._thread = notification.aps['thread-id'];
this._thread = notification.aps.thread;
} else if (
notification.aps &&
notification.aps.alert) {
......@@ -30,12 +30,12 @@ export default class IOSNotification {
this._alert = notification.aps.alert;
this._sound = notification.aps.sound;
this._badge = notification.aps.badge;
this._category = notification.category;
this._category = notification.aps.category;
this._type = 'regular';
this._thread = notification.thread;
this._thread = notification.aps.thread;
}
Object.keys(notification).filter(key => key !== 'userInfo').forEach(key => {
Object.keys(notification).filter(key => key !== 'aps').forEach(key => {
this._data[key] = notification[key];
});
}
......
describe('Notifications-Android', () => {
let refreshTokenStub;
let getInitialNotificationStub;
let postLocalNotificationStub;
let cancelLocalNotificationStub;
let deviceEventEmitterListenerStub;
let libUnderTest;
let deviceEventEmitterListenerStub;
let WixRNNotifications;
beforeEach(() => {
refreshTokenStub = jest.fn();
getInitialNotificationStub = jest.fn();
postLocalNotificationStub = jest.fn();
cancelLocalNotificationStub = jest.fn();
deviceEventEmitterListenerStub = jest.fn();
jest.mock('react-native', () => {
return {
NativeModules: {
WixRNNotifications: {
refreshToken: refreshTokenStub,
getInitialNotification: getInitialNotificationStub,
postLocalNotification: postLocalNotificationStub,
cancelLocalNotification: cancelLocalNotificationStub
refreshToken: jest.fn(),
getInitialNotification: jest.fn(),
postLocalNotification: jest.fn(),
cancelLocalNotification: jest.fn()
}
},
DeviceEventEmitter: {
addListener: deviceEventEmitterListenerStub
addListener: jest.fn()
}
};
});
deviceEventEmitterListenerStub = require('react-native').DeviceEventEmitter.addListener;
WixRNNotifications = require('react-native').NativeModules.WixRNNotifications;
libUnderTest = require('../lib/src/index.android');
});
......@@ -154,17 +149,17 @@ describe('Notifications-Android', () => {
describe('Notification token', () => {
it('should refresh notification token upon refreshing request by the user', () => {
expect(refreshTokenStub).toHaveBeenCalledTimes(0);
expect(WixRNNotifications.refreshToken).toHaveBeenCalledTimes(0);
libUnderTest.NotificationsAndroid.refreshToken();
expect(refreshTokenStub).toHaveBeenCalledTimes(1);
expect(WixRNNotifications.refreshToken).toHaveBeenCalledTimes(1);
});
});
describe('Initial notification API', () => {
it('should return initial notification data if available', (done) => {
expect(getInitialNotificationStub).toHaveBeenCalledTimes(0);
expect(WixRNNotifications.getInitialNotification).toHaveBeenCalledTimes(0);
const rawNotification = {foo: 'bar'};
getInitialNotificationStub.mockReturnValueOnce(Promise.resolve(rawNotification));
WixRNNotifications.getInitialNotification.mockReturnValueOnce(Promise.resolve(rawNotification));
libUnderTest.PendingNotifications.getInitialNotification()
.then((notification) => {
......@@ -175,8 +170,8 @@ describe('Notifications-Android', () => {
});
it('should return empty notification if not available', (done) => {
expect(getInitialNotificationStub).toHaveBeenCalledTimes(0);
getInitialNotificationStub.mockReturnValueOnce(Promise.resolve(null));
expect(WixRNNotifications.getInitialNotification).toHaveBeenCalledTimes(0);
WixRNNotifications.getInitialNotification.mockReturnValueOnce(Promise.resolve(null));
libUnderTest.PendingNotifications.getInitialNotification()
.then((notification) => {
......@@ -195,15 +190,15 @@ describe('Notifications-Android', () => {
};
it('should get published when posted manually', () => {
expect(postLocalNotificationStub).toHaveBeenCalledTimes(0);
expect(WixRNNotifications.postLocalNotification).toHaveBeenCalledTimes(0);
const id = libUnderTest.NotificationsAndroid.localNotification(notification);
expect(id).toBeDefined();
expect(postLocalNotificationStub).toHaveBeenCalledWith(notification, id);
expect(WixRNNotifications.postLocalNotification).toHaveBeenCalledWith(notification, id);
});
it('should be called with a unique ID', () => {
expect(postLocalNotificationStub).toHaveBeenCalledTimes(0);
expect(WixRNNotifications.postLocalNotification).toHaveBeenCalledTimes(0);
const id = libUnderTest.NotificationsAndroid.localNotification(notification);
const id2 = libUnderTest.NotificationsAndroid.localNotification(notification);
......@@ -213,11 +208,11 @@ describe('Notifications-Android', () => {
});
it('should be cancellable with an ID', () => {
expect(cancelLocalNotificationStub).toHaveBeenCalledTimes(0);
expect(WixRNNotifications.cancelLocalNotification).toHaveBeenCalledTimes(0);
libUnderTest.NotificationsAndroid.cancelLocalNotification(666);
expect(cancelLocalNotificationStub).toHaveBeenCalledWith(666);
expect(WixRNNotifications.cancelLocalNotification).toHaveBeenCalledWith(666);
});
});
});
......@@ -4,7 +4,6 @@ describe('NotificationsIOS', () => {
'remoteNotificationsRegistered',
'remoteNotificationsRegistrationFailed',
'notificationReceivedForeground',
'notificationReceivedBackground',
'notificationOpened'
];
......@@ -12,26 +11,12 @@ describe('NotificationsIOS', () => {
let constantGuid = 'some-random-uuid';
let identifiers = ['some-random-uuid', 'other-random-uuid'];
let someHandler = () => {};
let nativeAppAddEventListener;
let deviceAddEventListener;
let deviceRemoveEventListener;
let nativeAppRemoveEventListener;
let nativeModule;
let NativeAppEventEmitter;
let DeviceEventEmitter;
beforeEach(() => {
deviceRemoveEventListener = jest.fn();
nativeAppRemoveEventListener = jest.fn();
nativeAppAddEventListener = jest.fn(() => {
return {
remove: nativeAppRemoveEventListener
};
});
deviceAddEventListener = jest.fn(() => {
return {
remove: deviceRemoveEventListener
};
});
jest.mock('react-native', () => {
const RNBridgeModule = {
requestPermissionsWithCategories: jest.fn(),
abandonPermissions: jest.fn(),
......@@ -49,25 +34,33 @@ describe('NotificationsIOS', () => {
removeDeliveredNotifications: jest.fn(),
getDeliveredNotifications: jest.fn()
};
jest.mock('react-native', () => {
return {
NativeModules: {
RNBridgeModule
},
NativeAppEventEmitter: {
addListener: nativeAppAddEventListener
addListener: jest.fn(() => {
return {
remove: jest.fn()
};
})
},
DeviceEventEmitter: {
addListener: deviceAddEventListener
addListener: jest.fn(() => {
return {
remove: jest.fn()
};
})
}
};
});
nativeModule = RNBridgeModule;
nativeModule = require('react-native').NativeModules.RNBridgeModule;
NativeAppEventEmitter = require('react-native').NativeAppEventEmitter;
DeviceEventEmitter = require('react-native').DeviceEventEmitter;
jest.mock('uuid', () => {
return {
v4: () => constantGuid
v4: () => 'some-random-uuid'
};
});
......@@ -81,34 +74,42 @@ describe('NotificationsIOS', () => {
deviceEvents.forEach(event => {
it(`should subscribe the given handler to device event: ${event}`, () => {
NotificationsIOS.addEventListener(event, someHandler);
expect(deviceAddEventListener).toHaveBeenCalledWith(event, expect.any(Function));
expect(DeviceEventEmitter.addListener).toHaveBeenCalledWith(event, expect.any(Function));
});
});
it('should not subscribe to unknown device events', () => {
NotificationsIOS.addEventListener('someUnsupportedEvent', someHandler);
expect(deviceAddEventListener).toHaveBeenCalledTimes(0);
expect(DeviceEventEmitter.addListener).toHaveBeenCalledTimes(0);
});
});
describe('Remove Event Listener', () => {
deviceEvents.forEach(event => {
it(`should unsubscribe the given handler from device event: ${event}`, () => {
const removeCallback = jest.fn();
DeviceEventEmitter.addListener.mockReturnValueOnce({
remove: removeCallback
});
NotificationsIOS.addEventListener(event, someHandler);
NotificationsIOS.removeEventListener(event, someHandler);
expect(deviceRemoveEventListener).toHaveBeenCalledTimes(1);
expect(removeCallback).toHaveBeenCalledTimes(1);
});
});
it('should not unsubscribe to unknown device events', () => {
let someUnsupportedEvent = 'someUnsupportedEvent';
const removeCallback = jest.fn();
DeviceEventEmitter.addListener.mockReturnValueOnce({
remove: removeCallback
});
NotificationsIOS.addEventListener(someUnsupportedEvent, someHandler);
NotificationsIOS.removeEventListener(someUnsupportedEvent, someHandler);
expect(deviceRemoveEventListener).toHaveBeenCalledTimes(0);
expect(removeCallback).toHaveBeenCalledTimes(0);
});
});
......@@ -148,22 +149,6 @@ describe('NotificationsIOS', () => {
expect(nativeModule.requestPermissionsWithCategories).toHaveBeenCalledWith([]);
});
it('should subscribe to notificationActionReceived event once, with a single event handler', () => {
NotificationsIOS.requestPermissions([someCategory]);
expect(nativeAppAddEventListener).toHaveBeenCalledTimes(1);
expect(nativeAppAddEventListener).toHaveBeenCalledWith('notificationActionReceived', expect.any(Function));
});
});
describe('reset categories', () => {
it('should remove notificationActionReceived event handler', () => {
NotificationsIOS.requestPermissions([someCategory]);
NotificationsIOS.resetCategories();
expect(nativeAppRemoveEventListener).toHaveBeenCalledTimes(1);
});
});
describe('get badges count', () => {
......
......@@ -16,7 +16,7 @@ describe('iOS Notification Object', () => {
badge: someBadgeCount,
sound: someSound,
category: someCategory,
'thread-id': someThread
thread: someThread
},
key1: 'value1',
key2: 'value2'
......@@ -33,7 +33,7 @@ describe('iOS Notification Object', () => {
badge: someBadgeCount,
sound: someSound,
category: someCategory,
'thread-id': someThread
thread: someThread
},
key1: 'value1',
key2: 'value2'
......
const babelOptions = require('./package.json').babel;
module.exports = function (wallaby) {
return {
env: {
type: 'node',
runner: 'node'
},
testFramework: 'jest',
files: [
'package.json',
'lib/src/**/*.js',
'lib/src/**/*.ts',
'lib/src/**/*.tsx'
],
tests: [
'test/**/*.spec.js'
],
compilers: {
'**/*.js': wallaby.compilers.babel(babelOptions),
'**/*.ts?(x)': wallaby.compilers.typeScript({
module: 'commonjs',
jsx: 'React'
})
},
setup: (w) => {
w.testFramework.configure(require('./package.json').jest);
}
};
};
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment