Commit a0777af7 authored by yogevbd's avatar yogevbd

Replace mocha with jest

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