Commit c97e559a authored by yogevbd's avatar yogevbd

Fix unit tests

parent 13863605
...@@ -37,8 +37,6 @@ class NotificationsExampleApp extends Component { ...@@ -37,8 +37,6 @@ class NotificationsExampleApp extends Component {
NotificationsIOS.addEventListener('remoteNotificationsRegistered', this.onPushRegistered.bind(this)); NotificationsIOS.addEventListener('remoteNotificationsRegistered', this.onPushRegistered.bind(this));
NotificationsIOS.addEventListener('remoteNotificationsRegistrationFailed', this.onPushRegisteredFailed.bind(this)); NotificationsIOS.addEventListener('remoteNotificationsRegistrationFailed', this.onPushRegisteredFailed.bind(this));
NotificationsIOS.consumeBackgroundQueue();
NotificationsIOS.addEventListener('pushKitRegistered', this.onPushKitRegistered.bind(this)); NotificationsIOS.addEventListener('pushKitRegistered', this.onPushKitRegistered.bind(this));
NotificationsIOS.registerPushKit(); NotificationsIOS.registerPushKit();
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
* @flow * @flow
*/ */
'use strict'; 'use strict';
import { NativeModules, DeviceEventEmitter, NativeAppEventEmitter } from 'react-native'; import { NativeModules, DeviceEventEmitter } from 'react-native';
import Map from 'core-js/library/es6/map'; import Map from 'core-js/library/es6/map';
import uuid from 'uuid'; import uuid from 'uuid';
const NativeRNNotifications = NativeModules.RNBridgeModule; // eslint-disable-line no-unused-vars const NativeRNNotifications = NativeModules.RNBridgeModule; // eslint-disable-line no-unused-vars
......
...@@ -22,7 +22,7 @@ export default class IOSNotification { ...@@ -22,7 +22,7 @@ export default class IOSNotification {
this._badge = notification.aps.badge; this._badge = notification.aps.badge;
this._category = notification.managedAps.category; this._category = notification.managedAps.category;
this._type = 'managed'; this._type = 'managed';
this._thread = notification.aps['thread-id']; this._thread = notification.aps.thread;
} else if ( } else if (
notification.aps && notification.aps &&
notification.aps.alert) { notification.aps.alert) {
...@@ -30,12 +30,12 @@ export default class IOSNotification { ...@@ -30,12 +30,12 @@ export default class IOSNotification {
this._alert = notification.aps.alert; this._alert = notification.aps.alert;
this._sound = notification.aps.sound; this._sound = notification.aps.sound;
this._badge = notification.aps.badge; this._badge = notification.aps.badge;
this._category = notification.category; this._category = notification.aps.category;
this._type = 'regular'; 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]; this._data[key] = notification[key];
}); });
} }
......
describe('Notifications-Android', () => { describe('Notifications-Android', () => {
let refreshTokenStub;
let getInitialNotificationStub;
let postLocalNotificationStub;
let cancelLocalNotificationStub;
let deviceEventEmitterListenerStub;
let libUnderTest; let libUnderTest;
let deviceEventEmitterListenerStub;
let WixRNNotifications;
beforeEach(() => { beforeEach(() => {
refreshTokenStub = jest.fn();
getInitialNotificationStub = jest.fn();
postLocalNotificationStub = jest.fn();
cancelLocalNotificationStub = jest.fn();
deviceEventEmitterListenerStub = jest.fn();
jest.mock('react-native', () => { jest.mock('react-native', () => {
return { return {
NativeModules: { NativeModules: {
WixRNNotifications: { WixRNNotifications: {
refreshToken: refreshTokenStub, refreshToken: jest.fn(),
getInitialNotification: getInitialNotificationStub, getInitialNotification: jest.fn(),
postLocalNotification: postLocalNotificationStub, postLocalNotification: jest.fn(),
cancelLocalNotification: cancelLocalNotificationStub cancelLocalNotification: jest.fn()
} }
}, },
DeviceEventEmitter: { DeviceEventEmitter: {
addListener: deviceEventEmitterListenerStub addListener: jest.fn()
} }
}; };
}); });
deviceEventEmitterListenerStub = require('react-native').DeviceEventEmitter.addListener;
WixRNNotifications = require('react-native').NativeModules.WixRNNotifications;
libUnderTest = require('../lib/src/index.android'); libUnderTest = require('../lib/src/index.android');
}); });
...@@ -154,17 +149,17 @@ describe('Notifications-Android', () => { ...@@ -154,17 +149,17 @@ describe('Notifications-Android', () => {
describe('Notification token', () => { describe('Notification token', () => {
it('should refresh notification token upon refreshing request by the user', () => { it('should refresh notification token upon refreshing request by the user', () => {
expect(refreshTokenStub).toHaveBeenCalledTimes(0); expect(WixRNNotifications.refreshToken).toHaveBeenCalledTimes(0);
libUnderTest.NotificationsAndroid.refreshToken(); libUnderTest.NotificationsAndroid.refreshToken();
expect(refreshTokenStub).toHaveBeenCalledTimes(1); expect(WixRNNotifications.refreshToken).toHaveBeenCalledTimes(1);
}); });
}); });
describe('Initial notification API', () => { describe('Initial notification API', () => {
it('should return initial notification data if available', (done) => { it('should return initial notification data if available', (done) => {
expect(getInitialNotificationStub).toHaveBeenCalledTimes(0); expect(WixRNNotifications.getInitialNotification).toHaveBeenCalledTimes(0);
const rawNotification = {foo: 'bar'}; const rawNotification = {foo: 'bar'};
getInitialNotificationStub.mockReturnValueOnce(Promise.resolve(rawNotification)); WixRNNotifications.getInitialNotification.mockReturnValueOnce(Promise.resolve(rawNotification));
libUnderTest.PendingNotifications.getInitialNotification() libUnderTest.PendingNotifications.getInitialNotification()
.then((notification) => { .then((notification) => {
...@@ -175,8 +170,8 @@ describe('Notifications-Android', () => { ...@@ -175,8 +170,8 @@ describe('Notifications-Android', () => {
}); });
it('should return empty notification if not available', (done) => { it('should return empty notification if not available', (done) => {
expect(getInitialNotificationStub).toHaveBeenCalledTimes(0); expect(WixRNNotifications.getInitialNotification).toHaveBeenCalledTimes(0);
getInitialNotificationStub.mockReturnValueOnce(Promise.resolve(null)); WixRNNotifications.getInitialNotification.mockReturnValueOnce(Promise.resolve(null));
libUnderTest.PendingNotifications.getInitialNotification() libUnderTest.PendingNotifications.getInitialNotification()
.then((notification) => { .then((notification) => {
...@@ -195,15 +190,15 @@ describe('Notifications-Android', () => { ...@@ -195,15 +190,15 @@ describe('Notifications-Android', () => {
}; };
it('should get published when posted manually', () => { it('should get published when posted manually', () => {
expect(postLocalNotificationStub).toHaveBeenCalledTimes(0); expect(WixRNNotifications.postLocalNotification).toHaveBeenCalledTimes(0);
const id = libUnderTest.NotificationsAndroid.localNotification(notification); const id = libUnderTest.NotificationsAndroid.localNotification(notification);
expect(id).toBeDefined(); expect(id).toBeDefined();
expect(postLocalNotificationStub).toHaveBeenCalledWith(notification, id); expect(WixRNNotifications.postLocalNotification).toHaveBeenCalledWith(notification, id);
}); });
it('should be called with a unique 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 id = libUnderTest.NotificationsAndroid.localNotification(notification);
const id2 = libUnderTest.NotificationsAndroid.localNotification(notification); const id2 = libUnderTest.NotificationsAndroid.localNotification(notification);
...@@ -213,11 +208,11 @@ describe('Notifications-Android', () => { ...@@ -213,11 +208,11 @@ describe('Notifications-Android', () => {
}); });
it('should be cancellable with an ID', () => { it('should be cancellable with an ID', () => {
expect(cancelLocalNotificationStub).toHaveBeenCalledTimes(0); expect(WixRNNotifications.cancelLocalNotification).toHaveBeenCalledTimes(0);
libUnderTest.NotificationsAndroid.cancelLocalNotification(666); libUnderTest.NotificationsAndroid.cancelLocalNotification(666);
expect(cancelLocalNotificationStub).toHaveBeenCalledWith(666); expect(WixRNNotifications.cancelLocalNotification).toHaveBeenCalledWith(666);
}); });
}); });
}); });
...@@ -4,7 +4,6 @@ describe('NotificationsIOS', () => { ...@@ -4,7 +4,6 @@ describe('NotificationsIOS', () => {
'remoteNotificationsRegistered', 'remoteNotificationsRegistered',
'remoteNotificationsRegistrationFailed', 'remoteNotificationsRegistrationFailed',
'notificationReceivedForeground', 'notificationReceivedForeground',
'notificationReceivedBackground',
'notificationOpened' 'notificationOpened'
]; ];
...@@ -12,26 +11,12 @@ describe('NotificationsIOS', () => { ...@@ -12,26 +11,12 @@ describe('NotificationsIOS', () => {
let constantGuid = 'some-random-uuid'; let constantGuid = 'some-random-uuid';
let identifiers = ['some-random-uuid', 'other-random-uuid']; let identifiers = ['some-random-uuid', 'other-random-uuid'];
let someHandler = () => {}; let someHandler = () => {};
let nativeAppAddEventListener;
let deviceAddEventListener;
let deviceRemoveEventListener;
let nativeAppRemoveEventListener;
let nativeModule; let nativeModule;
let NativeAppEventEmitter;
let DeviceEventEmitter;
beforeEach(() => { beforeEach(() => {
deviceRemoveEventListener = jest.fn(); jest.mock('react-native', () => {
nativeAppRemoveEventListener = jest.fn();
nativeAppAddEventListener = jest.fn(() => {
return {
remove: nativeAppRemoveEventListener
};
});
deviceAddEventListener = jest.fn(() => {
return {
remove: deviceRemoveEventListener
};
});
const RNBridgeModule = { const RNBridgeModule = {
requestPermissionsWithCategories: jest.fn(), requestPermissionsWithCategories: jest.fn(),
abandonPermissions: jest.fn(), abandonPermissions: jest.fn(),
...@@ -49,25 +34,33 @@ describe('NotificationsIOS', () => { ...@@ -49,25 +34,33 @@ describe('NotificationsIOS', () => {
removeDeliveredNotifications: jest.fn(), removeDeliveredNotifications: jest.fn(),
getDeliveredNotifications: jest.fn() getDeliveredNotifications: jest.fn()
}; };
jest.mock('react-native', () => {
return { return {
NativeModules: { NativeModules: {
RNBridgeModule RNBridgeModule
}, },
NativeAppEventEmitter: { NativeAppEventEmitter: {
addListener: nativeAppAddEventListener addListener: jest.fn(() => {
return {
remove: jest.fn()
};
})
}, },
DeviceEventEmitter: { 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', () => { jest.mock('uuid', () => {
return { return {
v4: () => constantGuid v4: () => 'some-random-uuid'
}; };
}); });
...@@ -81,34 +74,42 @@ describe('NotificationsIOS', () => { ...@@ -81,34 +74,42 @@ describe('NotificationsIOS', () => {
deviceEvents.forEach(event => { deviceEvents.forEach(event => {
it(`should subscribe the given handler to device event: ${event}`, () => { it(`should subscribe the given handler to device event: ${event}`, () => {
NotificationsIOS.addEventListener(event, someHandler); NotificationsIOS.addEventListener(event, someHandler);
expect(DeviceEventEmitter.addListener).toHaveBeenCalledWith(event, expect.any(Function));
expect(deviceAddEventListener).toHaveBeenCalledWith(event, expect.any(Function));
}); });
}); });
it('should not subscribe to unknown device events', () => { it('should not subscribe to unknown device events', () => {
NotificationsIOS.addEventListener('someUnsupportedEvent', someHandler); NotificationsIOS.addEventListener('someUnsupportedEvent', someHandler);
expect(deviceAddEventListener).toHaveBeenCalledTimes(0); expect(DeviceEventEmitter.addListener).toHaveBeenCalledTimes(0);
}); });
}); });
describe('Remove Event Listener', () => { describe('Remove Event Listener', () => {
deviceEvents.forEach(event => { deviceEvents.forEach(event => {
it(`should unsubscribe the given handler from device event: ${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.addEventListener(event, someHandler);
NotificationsIOS.removeEventListener(event, someHandler); NotificationsIOS.removeEventListener(event, someHandler);
expect(deviceRemoveEventListener).toHaveBeenCalledTimes(1); expect(removeCallback).toHaveBeenCalledTimes(1);
}); });
}); });
it('should not unsubscribe to unknown device events', () => { it('should not unsubscribe to unknown device events', () => {
let someUnsupportedEvent = 'someUnsupportedEvent'; let someUnsupportedEvent = 'someUnsupportedEvent';
const removeCallback = jest.fn();
DeviceEventEmitter.addListener.mockReturnValueOnce({
remove: removeCallback
});
NotificationsIOS.addEventListener(someUnsupportedEvent, someHandler); NotificationsIOS.addEventListener(someUnsupportedEvent, someHandler);
NotificationsIOS.removeEventListener(someUnsupportedEvent, someHandler); NotificationsIOS.removeEventListener(someUnsupportedEvent, someHandler);
expect(deviceRemoveEventListener).toHaveBeenCalledTimes(0); expect(removeCallback).toHaveBeenCalledTimes(0);
}); });
}); });
...@@ -148,22 +149,6 @@ describe('NotificationsIOS', () => { ...@@ -148,22 +149,6 @@ describe('NotificationsIOS', () => {
expect(nativeModule.requestPermissionsWithCategories).toHaveBeenCalledWith([]); 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', () => { describe('get badges count', () => {
......
...@@ -16,7 +16,7 @@ describe('iOS Notification Object', () => { ...@@ -16,7 +16,7 @@ describe('iOS Notification Object', () => {
badge: someBadgeCount, badge: someBadgeCount,
sound: someSound, sound: someSound,
category: someCategory, category: someCategory,
'thread-id': someThread thread: someThread
}, },
key1: 'value1', key1: 'value1',
key2: 'value2' key2: 'value2'
...@@ -33,7 +33,7 @@ describe('iOS Notification Object', () => { ...@@ -33,7 +33,7 @@ describe('iOS Notification Object', () => {
badge: someBadgeCount, badge: someBadgeCount,
sound: someSound, sound: someSound,
category: someCategory, category: someCategory,
'thread-id': someThread thread: someThread
}, },
key1: 'value1', key1: 'value1',
key2: 'value2' 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