diff --git a/.eslintrc b/.eslintrc index 2b69a4c02110e8ec2d010f561beab4940fb49614..fd6debbbe6a11c6cd3a9a3eec705f77c35543b42 100644 --- a/.eslintrc +++ b/.eslintrc @@ -2,7 +2,7 @@ "parser": "babel-eslint", "env": { "node": true, - "mocha": true, + "jest": true, "es6": true }, "extends": "eslint:recommended", diff --git a/.gitignore b/.gitignore index a00cc16074dcbe47747f5bcac7f019307b6bf19a..8cf7040dfef230e04591162507ab152ebbdbec5b 100644 --- a/.gitignore +++ b/.gitignore @@ -178,3 +178,5 @@ jspm_packages # Optional REPL history .node_repl_history + +coverage/ \ No newline at end of file diff --git a/babel.config.js b/babel.config.js index f25946816ed74ef62f5963a02af894c66d23227b..0d6fb00044fee1942eee57bfbbada719fab6de12 100644 --- a/babel.config.js +++ b/babel.config.js @@ -2,7 +2,8 @@ module.exports = function (api) { api && api.cache(false); return { presets: [ - 'module:metro-react-native-babel-preset' + 'module:metro-react-native-babel-preset', + '@babel/preset-env' ], plugins: [ '@babel/plugin-proposal-export-namespace-from', diff --git a/package.json b/package.json index 2a9f248321c376ad840ee4660ae0c230eaca41a9..d1a3386f7f7144c5363d96b68b7a8b17aa5d01b6 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,7 @@ "main": "lib/src/index", "scripts": { "pretest": "./node_modules/.bin/eslint *.js test", - "test": "./node_modules/.bin/mocha --compilers js:babel-register --reporter spec \"test/*.spec.js\"", + "test": "jest", "start": "node ./scripts/start", "test-e2e-ios": "node ./scripts/test-e2e --ios" }, @@ -41,6 +41,7 @@ "@types/react-test-renderer": "16.x.x", "@babel/plugin-proposal-export-default-from": "7.2.0", "@babel/plugin-proposal-export-namespace-from": "7.2.0", + "@babel/preset-env": "7.5.0", "typescript": "3.2.2", "babel-eslint": "9.0.0", "chai": "^3.5.0", @@ -55,7 +56,8 @@ "react": "16.8.6", "detox": "12.x.x", "jest": "24.8.0", - "metro-react-native-babel-preset": "0.55.x" + "metro-react-native-babel-preset": "0.55.x", + "@babel/register": "7.4.4" }, "publishConfig": { "registry": "https://registry.npmjs.org/" @@ -101,10 +103,11 @@ "^.+\\.js$": "/node_modules/react-native/jest/preprocessor.js" }, "roots": [ - "/node_modules/" + "/node_modules/", + "/test/" ], "collectCoverageFrom": [ - "lib/dist/**/*.js", + "lib/src/**/*.js", "integration/**/*.js", "!lib/dist/index.js", "!lib/dist/Navigation.js", diff --git a/test/index.android.spec.js b/test/index.android.spec.js index d0855132304f11826ab6f9e8841e5c831e09d873..618502f3fbcf940a36be92f6bc75516d890a5f47 100644 --- a/test/index.android.spec.js +++ b/test/index.android.spec.js @@ -1,26 +1,20 @@ -'use strict'; -let expect = require('chai').use(require('sinon-chai')).expect; -import proxyquire from 'proxyquire'; -import sinon from 'sinon'; - -describe('Notifications-Android > ', () => { - proxyquire.noCallThru(); - +describe('Notifications-Android', () => { let refreshTokenStub; let getInitialNotificationStub; let postLocalNotificationStub; let cancelLocalNotificationStub; let deviceEventEmitterListenerStub; let libUnderTest; + beforeEach(() => { - refreshTokenStub = sinon.stub(); - getInitialNotificationStub = sinon.stub(); - postLocalNotificationStub = sinon.stub(); - cancelLocalNotificationStub = sinon.stub(); - deviceEventEmitterListenerStub = sinon.stub(); - - libUnderTest = proxyquire('../index.android', { - 'react-native': { + refreshTokenStub = jest.fn(); + getInitialNotificationStub = jest.fn(); + postLocalNotificationStub = jest.fn(); + cancelLocalNotificationStub = jest.fn(); + deviceEventEmitterListenerStub = jest.fn(); + + jest.mock('react-native', () => { + return { NativeModules: { WixRNNotifications: { refreshToken: refreshTokenStub, @@ -32,162 +26,162 @@ describe('Notifications-Android > ', () => { DeviceEventEmitter: { addListener: deviceEventEmitterListenerStub } - }, - './notification': require('../notification.android') + }; }); + libUnderTest = require('../lib/src/index.android'); }); describe('Registration token API', () => { it('should assign callback to native event upon listener registration', () => { - expect(deviceEventEmitterListenerStub).to.not.have.been.called; + expect(deviceEventEmitterListenerStub).toHaveBeenCalledTimes(0); const userListener = () => {}; - + console.log(libUnderTest); libUnderTest.NotificationsAndroid.setRegistrationTokenUpdateListener(userListener); - expect(deviceEventEmitterListenerStub).to.have.been.calledWith('remoteNotificationsRegistered', userListener); - expect(deviceEventEmitterListenerStub).to.have.been.calledOnce; + expect(deviceEventEmitterListenerStub).toHaveBeenCalledWith('remoteNotificationsRegistered', userListener); + expect(deviceEventEmitterListenerStub).toHaveBeenCalledTimes(1); }); it('should clear native event listener upon listener deregister', () => { - expect(deviceEventEmitterListenerStub).to.not.have.been.called; + expect(deviceEventEmitterListenerStub).toHaveBeenCalledTimes(0); const userListener = () => {}; const nativeListener = { - remove: sinon.spy() + remove: jest.fn() }; - deviceEventEmitterListenerStub.returns(nativeListener); + deviceEventEmitterListenerStub.mockReturnValueOnce(nativeListener); libUnderTest.NotificationsAndroid.setRegistrationTokenUpdateListener(userListener); libUnderTest.NotificationsAndroid.clearRegistrationTokenUpdateListener(); - expect(nativeListener.remove).to.have.been.calledOnce; + expect(nativeListener.remove).toHaveBeenCalledTimes(1); }); it('shouldn`t fail if deregister without registering', () => { libUnderTest.NotificationsAndroid.clearRegistrationTokenUpdateListener(); - expect(deviceEventEmitterListenerStub).to.not.have.been.called; + expect(deviceEventEmitterListenerStub).toHaveBeenCalledTimes(0); }); }); describe('notification-opening API', () => { it('should assign callback to native event upon registration', () => { - expect(deviceEventEmitterListenerStub).to.not.have.been.called; - const userListenerStub = sinon.stub(); + expect(deviceEventEmitterListenerStub).toHaveBeenCalledTimes(0); + const userListenerStub = jest.fn(); libUnderTest.NotificationsAndroid.setNotificationOpenedListener(userListenerStub); - expect(deviceEventEmitterListenerStub).to.have.been.calledOnce; - expect(deviceEventEmitterListenerStub).to.have.been.calledWith('notificationOpened', sinon.match.func); + expect(deviceEventEmitterListenerStub).toHaveBeenCalledTimes(1); + expect(deviceEventEmitterListenerStub).toHaveBeenCalledWith('notificationOpened', expect.any(Function)); }); it('should assign a wrapper-callback upon registration', () => { - expect(deviceEventEmitterListenerStub).to.not.have.been.called; - const userListenerStub = sinon.stub(); + expect(deviceEventEmitterListenerStub).toHaveBeenCalledTimes(0); + const userListenerStub = jest.fn(); const notification = { foo: 'bar' }; libUnderTest.NotificationsAndroid.setNotificationOpenedListener(userListenerStub); - expect(userListenerStub).to.not.have.been.called; - deviceEventEmitterListenerStub.args[0][1](notification); - expect(userListenerStub).to.have.been.calledOnce; - expect(userListenerStub.args[0][0].getData()).to.equal(notification); + expect(userListenerStub).toHaveBeenCalledTimes(0); + deviceEventEmitterListenerStub.mock.calls[0][1](notification); + expect(userListenerStub).toHaveBeenCalledTimes(1); + expect(userListenerStub.mock.calls[0][0].getData()).toEqual(notification); }); it('should clear native event listener upon listener deregister', () => { - expect(deviceEventEmitterListenerStub).to.not.have.been.called; + expect(deviceEventEmitterListenerStub).toHaveBeenCalledTimes(0); const userListener = () => {}; const nativeListener = { - remove: sinon.spy() + remove: jest.fn() }; - deviceEventEmitterListenerStub.returns(nativeListener); + deviceEventEmitterListenerStub.mockReturnValueOnce(nativeListener); libUnderTest.NotificationsAndroid.setNotificationOpenedListener(userListener); libUnderTest.NotificationsAndroid.clearNotificationOpenedListener(); - expect(nativeListener.remove).to.have.been.calledOnce; + expect(nativeListener.remove).toHaveBeenCalledTimes(1); }); it('shouldnt fail if deregister without registering', () => { libUnderTest.NotificationsAndroid.clearNotificationOpenedListener(); - expect(deviceEventEmitterListenerStub).to.not.have.been.called; + expect(deviceEventEmitterListenerStub).toHaveBeenCalledTimes(0); }); }); describe('notification-receive API', () => { it('should assign callback to native event upon registration', () => { - expect(deviceEventEmitterListenerStub).to.not.have.been.called; - const userListenerStub = sinon.stub(); + expect(deviceEventEmitterListenerStub).toHaveBeenCalledTimes(0); + const userListenerStub = jest.fn(); libUnderTest.NotificationsAndroid.setNotificationReceivedListener(userListenerStub); - expect(deviceEventEmitterListenerStub).to.have.been.calledOnce; - expect(deviceEventEmitterListenerStub).to.have.been.calledWith('notificationReceived', sinon.match.func); + expect(deviceEventEmitterListenerStub).toHaveBeenCalledTimes(1); + expect(deviceEventEmitterListenerStub).toHaveBeenCalledWith('notificationReceived', expect.any(Function)); }); it('should assign a wrapper-callback upon registration', () => { - expect(deviceEventEmitterListenerStub).to.not.have.been.called; - const userListenerStub = sinon.stub(); + expect(deviceEventEmitterListenerStub).toHaveBeenCalledTimes(0); + const userListenerStub = jest.fn(); const notification = { foo: 'bar' }; libUnderTest.NotificationsAndroid.setNotificationReceivedListener(userListenerStub); - expect(userListenerStub).to.not.have.been.called; - deviceEventEmitterListenerStub.args[0][1](notification); - expect(userListenerStub).to.have.been.calledOnce; - expect(userListenerStub.args[0][0].getData()).to.equal(notification); + expect(userListenerStub).toHaveBeenCalledTimes(0); + deviceEventEmitterListenerStub.mock.calls[0][1](notification); + expect(userListenerStub).toHaveBeenCalledTimes(1); + expect(userListenerStub.mock.calls[0][0].getData()).toEqual(notification); }); it('should clear native event listener upon listener deregister', () => { - expect(deviceEventEmitterListenerStub).to.not.have.been.called; + expect(deviceEventEmitterListenerStub).toHaveBeenCalledTimes(0); const userListener = () => {}; const nativeListener = { - remove: sinon.spy() + remove: jest.fn() }; - deviceEventEmitterListenerStub.returns(nativeListener); + deviceEventEmitterListenerStub.mockReturnValueOnce(nativeListener); libUnderTest.NotificationsAndroid.setNotificationReceivedListener(userListener); libUnderTest.NotificationsAndroid.clearNotificationReceivedListener(); - expect(nativeListener.remove).to.have.been.calledOnce; + expect(nativeListener.remove).toHaveBeenCalledTimes(1); }); it('shouldn`t fail if deregister without registering', () => { libUnderTest.NotificationsAndroid.clearNotificationReceivedListener(); - expect(deviceEventEmitterListenerStub).to.not.have.been.called; + expect(deviceEventEmitterListenerStub).toHaveBeenCalledTimes(0); }); }); describe('Notification token', () => { it('should refresh notification token upon refreshing request by the user', () => { - expect(refreshTokenStub).to.not.have.been.called; + expect(refreshTokenStub).toHaveBeenCalledTimes(0); libUnderTest.NotificationsAndroid.refreshToken(); - expect(refreshTokenStub).to.have.been.calledOnce; + expect(refreshTokenStub).toHaveBeenCalledTimes(1); }); }); describe('Initial notification API', () => { it('should return initial notification data if available', (done) => { - expect(getInitialNotificationStub).to.not.have.been.called; + expect(getInitialNotificationStub).toHaveBeenCalledTimes(0); const rawNotification = {foo: 'bar'}; - getInitialNotificationStub.returns(Promise.resolve(rawNotification)); + getInitialNotificationStub.mockReturnValueOnce(Promise.resolve(rawNotification)); libUnderTest.PendingNotifications.getInitialNotification() .then((notification) => { - expect(notification.getData()).to.equal(rawNotification); + expect(notification.getData()).toEqual(rawNotification); done(); }) .catch((err) => done(err)); }); it('should return empty notification if not available', (done) => { - expect(getInitialNotificationStub).to.not.have.been.called; - getInitialNotificationStub.returns(Promise.resolve(null)); + expect(getInitialNotificationStub).toHaveBeenCalledTimes(0); + getInitialNotificationStub.mockReturnValueOnce(Promise.resolve(null)); libUnderTest.PendingNotifications.getInitialNotification() .then((notification) => { - expect(notification).to.be.undefined; + expect(notification).toBeUndefined(); done(); }) .catch((err) => done(err)); @@ -202,29 +196,29 @@ describe('Notifications-Android > ', () => { }; it('should get published when posted manually', () => { - expect(postLocalNotificationStub).to.not.have.been.called; + expect(postLocalNotificationStub).toHaveBeenCalledTimes(0); const id = libUnderTest.NotificationsAndroid.localNotification(notification); - expect(id).to.not.be.undefined; - expect(postLocalNotificationStub).to.have.been.calledWith(notification, id); + expect(id).toBeDefined(); + expect(postLocalNotificationStub).toHaveBeenCalledWith(notification, id); }); it('should be called with a unique ID', () => { - expect(postLocalNotificationStub).to.not.have.been.called; + expect(postLocalNotificationStub).toHaveBeenCalledTimes(0); const id = libUnderTest.NotificationsAndroid.localNotification(notification); const id2 = libUnderTest.NotificationsAndroid.localNotification(notification); - expect(id).to.not.be.undefined; - expect(id2).to.not.be.undefined; - expect(id).to.not.equal(id2); + expect(id).toBeDefined(); + expect(id2).toBeDefined(); + expect(id).not.toBe(id2); }); it('should be cancellable with an ID', () => { - expect(cancelLocalNotificationStub).to.not.have.been.called; + expect(cancelLocalNotificationStub).toHaveBeenCalledTimes(0); libUnderTest.NotificationsAndroid.cancelLocalNotification(666); - expect(cancelLocalNotificationStub).to.have.been.calledWith(666); + expect(cancelLocalNotificationStub).toHaveBeenCalledWith(666); }); }); }); diff --git a/test/index.ios.spec.js b/test/index.ios.spec.js index b540b42816d91c29a025489b5243ee4288dd542d..ea572f55aaae49cb1d951c5c8c85ebbefbc805fd 100644 --- a/test/index.ios.spec.js +++ b/test/index.ios.spec.js @@ -1,11 +1,5 @@ -'use strict'; -let expect = require('chai').use(require('sinon-chai')).expect; -import proxyquire from 'proxyquire'; -import sinon from 'sinon'; -/* eslint-disable no-unused-vars */ - -describe('NotificationsIOS', () => { +describe.only('NotificationsIOS', () => { let deviceEvents = [ 'pushKitRegistered', 'remoteNotificationsRegistered', @@ -15,158 +9,88 @@ describe('NotificationsIOS', () => { 'notificationOpened' ]; - /*eslint-disable indent*/ - let deviceAddEventListener, - deviceRemoveEventListener, - nativeAppAddEventListener, - nativeAppRemoveEventListener, - nativeRequestPermissionsWithCategories, - nativeAbandonPermissions, - nativeRegisterPushKit, - nativeBackgroundTimeRemaining, - nativeConsumeBackgroundQueue, - nativeLocalNotification, - nativeCancelLocalNotification, - nativeCancelAllLocalNotifications, - nativeGetBadgesCount, - nativeSetBadgesCount, - nativeIsRegisteredForRemoteNotifications, - nativeCheckPermissions, - nativeRemoveAllDeliveredNotifications, - nativeRemoveDeliveredNotifications, - nativeGetDeliveredNotifications; - let NotificationsIOS, NotificationAction, NotificationCategory; - let someHandler = () => {}; let constantGuid = 'some-random-uuid'; let identifiers = ['some-random-uuid', 'other-random-uuid']; - /*eslint-enable indent*/ - - before(() => { - deviceAddEventListener = sinon.spy(); - deviceRemoveEventListener = sinon.spy(); - nativeAppAddEventListener = sinon.spy(); - nativeAppRemoveEventListener = sinon.spy(); - nativeRequestPermissionsWithCategories = sinon.spy(); - nativeAbandonPermissions = sinon.spy(); - nativeRegisterPushKit = sinon.spy(); - nativeBackgroundTimeRemaining = sinon.spy(); - nativeConsumeBackgroundQueue = sinon.spy(); - nativeLocalNotification = sinon.spy(); - nativeCancelLocalNotification = sinon.spy(); - nativeCancelAllLocalNotifications = sinon.spy(); - nativeGetBadgesCount = sinon.spy(); - nativeSetBadgesCount = sinon.spy(); - nativeIsRegisteredForRemoteNotifications = sinon.spy(); - nativeCheckPermissions = sinon.spy(); - nativeRemoveAllDeliveredNotifications = sinon.spy(); - nativeRemoveDeliveredNotifications = sinon.spy(); - nativeGetDeliveredNotifications = sinon.spy(); - - let libUnderTest = proxyquire('../index.ios', { - 'uuid': { - v4: () => constantGuid - }, - 'react-native': { + let someHandler = () => {}; + let nativeAppAddEventListener; + let deviceAddEventListener; + let deviceRemoveEventListener; + let nativeAppRemoveEventListener; + let nativeModule; + + 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', () => { + return { NativeModules: { - RNBridgeModule: { - requestPermissionsWithCategories: nativeRequestPermissionsWithCategories, - abandonPermissions: nativeAbandonPermissions, - registerPushKit: nativeRegisterPushKit, - backgroundTimeRemaining: nativeBackgroundTimeRemaining, - consumeBackgroundQueue: nativeConsumeBackgroundQueue, - localNotification: nativeLocalNotification, - cancelLocalNotification: nativeCancelLocalNotification, - cancelAllLocalNotifications: nativeCancelAllLocalNotifications, - getBadgesCount: nativeGetBadgesCount, - setBadgesCount: nativeSetBadgesCount, - isRegisteredForRemoteNotifications: nativeIsRegisteredForRemoteNotifications, - checkPermissions: nativeCheckPermissions, - removeAllDeliveredNotifications: nativeRemoveAllDeliveredNotifications, - removeDeliveredNotifications: nativeRemoveDeliveredNotifications, - getDeliveredNotifications: nativeGetDeliveredNotifications - } + RNBridgeModule }, NativeAppEventEmitter: { - addListener: (...args) => { - nativeAppAddEventListener(...args); - - return { remove: nativeAppRemoveEventListener }; - } + addListener: nativeAppAddEventListener }, DeviceEventEmitter: { - addListener: (...args) => { - deviceAddEventListener(...args); + addListener: deviceAddEventListener + } + }; + }); + nativeModule = RNBridgeModule; - return { remove: deviceRemoveEventListener }; - } - }, - '@noCallThru': true - } + jest.mock('uuid', () => { + return { + v4: () => constantGuid + }; }); + let libUnderTest = require('../lib/src/index.ios'); NotificationsIOS = libUnderTest.default; NotificationAction = libUnderTest.NotificationAction; NotificationCategory = libUnderTest.NotificationCategory; }); - afterEach(() => { - deviceAddEventListener.reset(); - deviceRemoveEventListener.reset(); - nativeAppAddEventListener.reset(); - nativeAppRemoveEventListener.reset(); - nativeRequestPermissionsWithCategories.reset(); - nativeAbandonPermissions.reset(); - nativeRegisterPushKit.reset(); - nativeBackgroundTimeRemaining.reset(); - nativeConsumeBackgroundQueue.reset(); - nativeLocalNotification.reset(); - nativeCancelLocalNotification.reset(); - nativeCancelAllLocalNotifications.reset(); - nativeIsRegisteredForRemoteNotifications.reset(); - nativeCheckPermissions.reset(); - nativeRemoveAllDeliveredNotifications.reset(); - nativeRemoveDeliveredNotifications.reset(); - nativeGetDeliveredNotifications.reset(); - }); - - after(() => { - deviceAddEventListener = null; - deviceRemoveEventListener = null; - nativeAppAddEventListener = null; - nativeAppRemoveEventListener = null; - nativeRequestPermissionsWithCategories = null; - nativeAbandonPermissions = null; - nativeRegisterPushKit = null; - nativeBackgroundTimeRemaining = null; - nativeConsumeBackgroundQueue = null; - nativeLocalNotification = null; - nativeCancelLocalNotification = null; - nativeCancelAllLocalNotifications = null; - nativeIsRegisteredForRemoteNotifications = null; - nativeCheckPermissions = null; - nativeRemoveAllDeliveredNotifications = null; - nativeRemoveDeliveredNotifications = null; - nativeGetDeliveredNotifications = null; - - NotificationsIOS = null; - NotificationAction = null; - NotificationCategory = null; - }); - describe('Add Event Listener', () => { deviceEvents.forEach(event => { it(`should subscribe the given handler to device event: ${event}`, () => { NotificationsIOS.addEventListener(event, someHandler); - expect(deviceAddEventListener).to.have.been.calledWith(event, sinon.match.func); + expect(deviceAddEventListener).toHaveBeenCalledWith(event, expect.any(Function)); }); }); it('should not subscribe to unknown device events', () => { NotificationsIOS.addEventListener('someUnsupportedEvent', someHandler); - expect(deviceAddEventListener).to.not.have.been.called; + expect(deviceAddEventListener).toHaveBeenCalledTimes(0); }); }); @@ -176,7 +100,7 @@ describe('NotificationsIOS', () => { NotificationsIOS.addEventListener(event, someHandler); NotificationsIOS.removeEventListener(event, someHandler); - expect(deviceRemoveEventListener).to.have.been.calledOnce; + expect(deviceRemoveEventListener).toHaveBeenCalledTimes(1); }); }); @@ -185,7 +109,7 @@ describe('NotificationsIOS', () => { NotificationsIOS.addEventListener(someUnsupportedEvent, someHandler); NotificationsIOS.removeEventListener(someUnsupportedEvent, someHandler); - expect(deviceRemoveEventListener).to.not.have.been.called; + expect(deviceRemoveEventListener).toHaveBeenCalledTimes(0); }); }); @@ -213,7 +137,7 @@ describe('NotificationsIOS', () => { it('should call native request permissions with array of categories', () => { NotificationsIOS.requestPermissions([someCategory]); - expect(nativeRequestPermissionsWithCategories).to.have.been.calledWith([{ + expect(nativeModule.requestPermissionsWithCategories).toHaveBeenCalledWith([{ identifier: 'SOME_CATEGORY', actions: [actionOpts], context: 'default' @@ -223,22 +147,23 @@ describe('NotificationsIOS', () => { it('should call native request permissions with empty array if no categories specified', () => { NotificationsIOS.requestPermissions(); - expect(nativeRequestPermissionsWithCategories).to.have.been.calledWith([]); + expect(nativeModule.requestPermissionsWithCategories).toHaveBeenCalledWith([]); }); it('should subscribe to notificationActionReceived event once, with a single event handler', () => { NotificationsIOS.requestPermissions([someCategory]); - expect(nativeAppAddEventListener).to.have.been.calledOnce; - expect(nativeAppAddEventListener).to.have.been.calledWith('notificationActionReceived', sinon.match.func); + 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).to.have.been.calledOnce; + expect(nativeAppRemoveEventListener).toHaveBeenCalledTimes(1); }); }); @@ -247,7 +172,7 @@ describe('NotificationsIOS', () => { const callback = (count) => console.log(count); NotificationsIOS.getBadgesCount(callback); - expect(nativeGetBadgesCount).to.have.been.calledWith(callback); + expect(nativeModule.getBadgesCount).toHaveBeenCalledWith(callback); }); }); @@ -255,7 +180,7 @@ describe('NotificationsIOS', () => { it('should call native setBadgesCount', () => { NotificationsIOS.setBadgesCount(44); - expect(nativeSetBadgesCount).to.have.been.calledWith(44); + expect(nativeModule.setBadgesCount).toHaveBeenCalledWith(44); }); }); @@ -265,7 +190,7 @@ describe('NotificationsIOS', () => { it('should call native register push kit method', function () { NotificationsIOS.registerPushKit(); - expect(nativeRegisterPushKit).to.have.been.called; + expect(nativeModule.registerPushKit).toHaveBeenCalledTimes(1); }); }); @@ -273,23 +198,23 @@ describe('NotificationsIOS', () => { it('should call native abandon permissions method', () => { NotificationsIOS.abandonPermissions(); - expect(nativeAbandonPermissions).to.have.been.called; + expect(nativeModule.abandonPermissions).toHaveBeenCalledTimes(1); }); }); describe('Get background remaining time', () => { it('should call native background remaining time method', () => { - let someCallback = (time) => { }; + let someCallback = () => {}; NotificationsIOS.backgroundTimeRemaining(someCallback); - expect(nativeBackgroundTimeRemaining).to.have.been.calledWith(someCallback); + expect(nativeModule.backgroundTimeRemaining).toHaveBeenCalledWith(someCallback); }); }); describe('Dispatch local notification', () => { it('should return generated notification guid', () => { - expect(NotificationsIOS.localNotification({})).to.equal(constantGuid); + expect(NotificationsIOS.localNotification({})).toEqual(constantGuid); }); it('should call native local notification method with generated notification guid and notification object', () => { @@ -306,7 +231,7 @@ describe('NotificationsIOS', () => { NotificationsIOS.localNotification(someLocalNotification); - expect(nativeLocalNotification).to.have.been.calledWith(someLocalNotification, constantGuid); + expect(nativeModule.localNotification).toHaveBeenCalledWith(someLocalNotification, constantGuid); }); }); @@ -314,7 +239,7 @@ describe('NotificationsIOS', () => { it('should call native cancel local notification method', () => { NotificationsIOS.cancelLocalNotification(constantGuid); - expect(nativeCancelLocalNotification).to.have.been.calledWith(constantGuid); + expect(nativeModule.cancelLocalNotification).toHaveBeenCalledWith(constantGuid); }); }); @@ -322,14 +247,14 @@ describe('NotificationsIOS', () => { it('should call native cancel all local notifications method', () => { NotificationsIOS.cancelAllLocalNotifications(); - expect(nativeCancelAllLocalNotifications).to.have.been.calledWith(); + expect(nativeModule.cancelAllLocalNotifications).toHaveBeenCalledWith(); }); }); describe('Is registered for remote notifications ', () => { it('should call native is registered for remote notifications', () => { NotificationsIOS.isRegisteredForRemoteNotifications(); - expect(nativeIsRegisteredForRemoteNotifications).to.have.been.calledWith(); + expect(nativeModule.isRegisteredForRemoteNotifications).toHaveBeenCalledWith(); }); }); @@ -337,7 +262,7 @@ describe('NotificationsIOS', () => { describe('Check permissions ', () => { it('should call native check permissions', () => { NotificationsIOS.checkPermissions(); - expect(nativeCheckPermissions).to.have.been.calledWith(); + expect(nativeModule.checkPermissions).toHaveBeenCalledWith(); }); }); @@ -346,7 +271,7 @@ describe('NotificationsIOS', () => { it('should call native remove all delivered notifications method', () => { NotificationsIOS.removeAllDeliveredNotifications(); - expect(nativeRemoveAllDeliveredNotifications).to.have.been.calledWith(); + expect(nativeModule.removeAllDeliveredNotifications).toHaveBeenCalledWith(); }); }); @@ -354,7 +279,7 @@ describe('NotificationsIOS', () => { it('should call native remove delivered notifications method', () => { NotificationsIOS.removeDeliveredNotifications(identifiers); - expect(nativeRemoveDeliveredNotifications).to.have.been.calledWith(identifiers); + expect(nativeModule.removeDeliveredNotifications).toHaveBeenCalledWith(identifiers); }); }); @@ -363,7 +288,7 @@ describe('NotificationsIOS', () => { const callback = (notifications) => console.log(notifications); NotificationsIOS.getDeliveredNotifications(callback); - expect(nativeGetDeliveredNotifications).to.have.been.calledWith(callback); + expect(nativeModule.getDeliveredNotifications).toHaveBeenCalledWith(callback); }); }); }); diff --git a/test/notification.ios.spec.js b/test/notification.ios.spec.js index fb116a90b404406ca44b7e8df36fda483706184a..3879453055b0bf4a90440e950d267de2490ec43a 100644 --- a/test/notification.ios.spec.js +++ b/test/notification.ios.spec.js @@ -1,8 +1,6 @@ -'use strict'; -import { expect } from 'chai'; -import IOSNotification from '../notification.ios'; +import IOSNotification from '../lib/src/notification.ios'; -describe('iOS Notification Object', () => { +describe.only('iOS Notification Object', () => { let notification; let someBadgeCount = 123, someSound = 'someSound', someCategory = 'some_notification_category', someThread = 'thread-1'; @@ -48,31 +46,31 @@ describe('iOS Notification Object', () => { }); it('should return regular type', function () { - expect(notification.getType()).to.equal('regular'); + expect(notification.getType()).toEqual('regular'); }); it('should return the alert object', () => { - expect(notification.getMessage()).to.deep.equal(nativeNotification.aps.alert); + expect(notification.getMessage()).toEqual(nativeNotification.aps.alert); }); it('should return the sound', () => { - expect(notification.getSound()).to.equal(someSound); + expect(notification.getSound()).toEqual(someSound); }); it('should return the badge count', () => { - expect(notification.getBadgeCount()).to.equal(someBadgeCount); + expect(notification.getBadgeCount()).toEqual(someBadgeCount); }); it('should return the category', () => { - expect(notification.getCategory()).to.equal(someCategory); + expect(notification.getCategory()).toEqual(someCategory); }); it('should return the thread', () => { - expect(notification.getThread()).to.equal('thread-1'); + expect(notification.getThread()).toEqual('thread-1'); }); it('should return the custom data', () => { - expect(notification.getData()).to.deep.equal({ key1: 'value1', key2: 'value2' }); + expect(notification.getData()).toEqual({ key1: 'value1', key2: 'value2' }); }); }); }); @@ -102,27 +100,27 @@ describe('iOS Notification Object', () => { }); it('should return managed type', function () { - expect(notification.getType()).to.equal('managed'); + expect(notification.getType()).toEqual('managed'); }); it('should return the alert object', () => { - expect(notification.getMessage()).to.equal(managedNativeNotification.managedAps.alert); + expect(notification.getMessage()).toEqual(managedNativeNotification.managedAps.alert); }); it('should return the sound', () => { - expect(notification.getSound()).to.equal(someSound); + expect(notification.getSound()).toEqual(someSound); }); it('should return the badge count', () => { - expect(notification.getBadgeCount()).to.equal(someBadgeCount); + expect(notification.getBadgeCount()).toEqual(someBadgeCount); }); it('should return the category', () => { - expect(notification.getCategory()).to.equal(someCategory); + expect(notification.getCategory()).toEqual(someCategory); }); it('should return the custom data', () => { - expect(notification.getData()).to.deep.equal({ managedAps: managedNativeNotification.managedAps, key1: 'value1', key2: 'value2' }); + expect(notification.getData()).toEqual({ managedAps: managedNativeNotification.managedAps, key1: 'value1', key2: 'value2' }); }); }); });