From c97e559ad586588402e373fc3ad1e71ed4a638bb Mon Sep 17 00:00:00 2001 From: yogevbd Date: Tue, 9 Jul 2019 14:45:17 +0300 Subject: [PATCH] Fix unit tests --- example/index.ios.js | 2 - lib/src/index.ios.js | 4 +- lib/src/notification.ios.js | 8 +-- test/index.android.spec.js | 49 +++++++--------- test/index.ios.spec.js | 107 +++++++++++++++------------------- test/notification.ios.spec.js | 4 +- wallaby.js | 35 +++++++++++ 7 files changed, 111 insertions(+), 98 deletions(-) create mode 100644 wallaby.js diff --git a/example/index.ios.js b/example/index.ios.js index 1a251cf..c9e90b9 100644 --- a/example/index.ios.js +++ b/example/index.ios.js @@ -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(); diff --git a/lib/src/index.ios.js b/lib/src/index.ios.js index d9b80a7..3e87af9 100644 --- a/lib/src/index.ios.js +++ b/lib/src/index.ios.js @@ -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 @@ -58,7 +58,7 @@ export default class NotificationsIOS { static addEventListener(type: string, handler: Function) { if (_exportedEvents.indexOf(type) !== -1) { let listener; - + if (type === DEVICE_REMOTE_NOTIFICATIONS_REGISTERED_EVENT) { listener = DeviceEventEmitter.addListener( DEVICE_REMOTE_NOTIFICATIONS_REGISTERED_EVENT, diff --git a/lib/src/notification.ios.js b/lib/src/notification.ios.js index f8d0da8..39ee60c 100644 --- a/lib/src/notification.ios.js +++ b/lib/src/notification.ios.js @@ -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]; }); } diff --git a/test/index.android.spec.js b/test/index.android.spec.js index 77af60f..020e558 100644 --- a/test/index.android.spec.js +++ b/test/index.android.spec.js @@ -1,33 +1,28 @@ 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); }); }); }); diff --git a/test/index.ios.spec.js b/test/index.ios.spec.js index a273f7c..46c995b 100644 --- a/test/index.ios.spec.js +++ b/test/index.ios.spec.js @@ -4,7 +4,6 @@ describe('NotificationsIOS', () => { 'remoteNotificationsRegistered', 'remoteNotificationsRegistrationFailed', 'notificationReceivedForeground', - 'notificationReceivedBackground', 'notificationOpened' ]; @@ -12,62 +11,56 @@ 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 - }; - }); - const RNBridgeModule = { - requestPermissionsWithCategories: jest.fn(), - abandonPermissions: jest.fn(), - registerPushKit: jest.fn(), - backgroundTimeRemaining: jest.fn(), - consumeBackgroundQueue: jest.fn(), - localNotification: jest.fn(), - cancelLocalNotification: jest.fn(), - cancelAllLocalNotifications: jest.fn(), - getBadgesCount: jest.fn(), - setBadgesCount: jest.fn(), - isRegisteredForRemoteNotifications: jest.fn(), - checkPermissions: jest.fn(), - removeAllDeliveredNotifications: jest.fn(), - removeDeliveredNotifications: jest.fn(), - getDeliveredNotifications: jest.fn() - }; - jest.mock('react-native', () => { + const RNBridgeModule = { + requestPermissionsWithCategories: jest.fn(), + abandonPermissions: jest.fn(), + registerPushKit: jest.fn(), + backgroundTimeRemaining: jest.fn(), + consumeBackgroundQueue: jest.fn(), + localNotification: jest.fn(), + cancelLocalNotification: jest.fn(), + cancelAllLocalNotifications: jest.fn(), + getBadgesCount: jest.fn(), + setBadgesCount: jest.fn(), + isRegisteredForRemoteNotifications: jest.fn(), + checkPermissions: jest.fn(), + removeAllDeliveredNotifications: jest.fn(), + removeDeliveredNotifications: jest.fn(), + getDeliveredNotifications: jest.fn() + }; 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', () => { diff --git a/test/notification.ios.spec.js b/test/notification.ios.spec.js index 40184b7..04441aa 100644 --- a/test/notification.ios.spec.js +++ b/test/notification.ios.spec.js @@ -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' diff --git a/wallaby.js b/wallaby.js new file mode 100644 index 0000000..890e42a --- /dev/null +++ b/wallaby.js @@ -0,0 +1,35 @@ +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); + } + }; +}; -- 2.26.2