Commit f07e731f authored by Lidan Hifi's avatar Lidan Hifi

moved to es6 syntax, fixed testing infra, added tests

parent a00f5358
......@@ -31,6 +31,7 @@
"eqeqeq": [
"error",
"smart"
]
],
"no-console":0
}
}
......@@ -5,7 +5,6 @@ Handle push notifications for your app, including remote and local notifications
**Work in progress, please notice that this library is not production-ready yet!**
## TODO
- Add tests.
- Return unified notification object for regular & managed notifications.
- Add permissions management.
- Add interactive notifications support.
......
......@@ -5,15 +5,15 @@
"use strict";
import { NativeModules, DeviceEventEmitter } from "react-native";
import Map from "core-js/library/es6/map";
var NativeRNNotifications = NativeModules.RNNotifications; // eslint-disable-line no-unused-vars
let NativeRNNotifications = NativeModules.RNNotifications; // eslint-disable-line no-unused-vars
var DEVICE_NOTIFICATION_RECEIVED_FOREGROUND_EVENT = "notificationReceivedForeground";
var DEVICE_NOTIFICATION_RECEIVED_BACKGROUND_EVENT = "notificationReceivedBackground";
var DEVICE_NOTIFICATION_OPENED_EVENT = "notificationOpened";
export const DEVICE_NOTIFICATION_RECEIVED_FOREGROUND_EVENT = "notificationReceivedForeground";
export const DEVICE_NOTIFICATION_RECEIVED_BACKGROUND_EVENT = "notificationReceivedBackground";
export const DEVICE_NOTIFICATION_OPENED_EVENT = "notificationOpened";
var _notificationHandlers = new Map();
let _notificationHandlers = new Map();
class NotificationsIOS {
export default class NotificationsIOS {
/**
* Attaches a listener to remote notification events while the app is running
* in the foreground or the background.
......@@ -28,11 +28,9 @@ class NotificationsIOS {
if (type === DEVICE_NOTIFICATION_RECEIVED_FOREGROUND_EVENT ||
type === DEVICE_NOTIFICATION_RECEIVED_BACKGROUND_EVENT ||
type === DEVICE_NOTIFICATION_OPENED_EVENT) {
var listener = DeviceEventEmitter.addListener(
let listener = DeviceEventEmitter.addListener(
type,
(notification) => {
handler(notification);
}
notification => handler(notification)
);
_notificationHandlers.set(handler, listener);
......@@ -47,7 +45,7 @@ class NotificationsIOS {
if (type === DEVICE_NOTIFICATION_RECEIVED_FOREGROUND_EVENT ||
type === DEVICE_NOTIFICATION_RECEIVED_BACKGROUND_EVENT ||
type === DEVICE_NOTIFICATION_OPENED_EVENT) {
var listener = _notificationHandlers.get(handler);
let listener = _notificationHandlers.get(handler);
if (!listener) {
return;
}
......@@ -57,5 +55,3 @@ class NotificationsIOS {
}
}
}
module.exports = NotificationsIOS;
......@@ -10,6 +10,7 @@
"react native",
"ios",
"push-notifications",
"push notifications",
"notifications",
"notification",
"react native notifications"
......@@ -23,14 +24,20 @@
},
"devDependencies": {
"babel-eslint": "^6.0.2",
"babel-preset-es2015": "^6.6.0",
"babel-preset-react": "^6.5.0",
"babel-register": "^6.7.2",
"chai": "^3.5.0",
"chokidar-cli": "^1.2.0",
"eslint": "^2.7.0",
"mocha": "^2.4.5"
"mocha": "^2.4.5",
"proxyquire": "^1.7.4",
"sinon": "^1.17.3",
"sinon-chai": "^2.8.0"
},
"scripts": {
"pretest": "./node_modules/.bin/eslint *.js test",
"test": "./node_modules/.bin/mocha --reporter spec \"test/*.spec.js\"",
"test": "./node_modules/.bin/mocha --compilers js:babel-register --reporter spec \"test/*.spec.js\"",
"start": "npm run test --silent; ./node_modules/.bin/chokidar \"test/*.js\" \"*.js\" -c 'npm run test --silent' --silent"
},
"publishConfig": {
......@@ -44,5 +51,11 @@
"bugs": {
"url": "https://github.com/wix/react-native-notifications/issues"
},
"main": "index.ios.js"
"main": "index.ios.js",
"babel": {
"presets": [
"es2015",
"react"
]
}
}
var expect = require("chai").expect;
let expect = require("chai").use(require("sinon-chai")).expect;
let proxyquire = require("proxyquire");
let sinon = require("sinon");
describe("NotificationsIOS", function () {
it("should pass", function () {
expect(true).to.be.true;
let deviceEvents = [
"notificationReceivedForeground",
"notificationReceivedBackground",
"notificationOpened"
];
let addEventListenerSpy, removeEventListenerSpy;
let notificationIOS;
let someHandler = () => {};
before(() => {
addEventListenerSpy = sinon.spy();
removeEventListenerSpy = sinon.spy();
notificationIOS = proxyquire("../index.ios", {
"react-native": {
NativeModules: {
RNNotifications: { }
},
DeviceEventEmitter: {
addListener: (...args) => {
addEventListenerSpy(...args);
return { remove: removeEventListenerSpy };
}
},
"@noCallThru": true
}
}).default;
});
afterEach(() => {
addEventListenerSpy.reset();
removeEventListenerSpy.reset();
});
after(() => {
addEventListenerSpy = null;
removeEventListenerSpy = null;
notificationIOS = null;
});
describe("Add Event Listener", function () {
deviceEvents.forEach(function(event) {
it(`should subscribe the given handler to device event: ${event}`, function () {
notificationIOS.addEventListener(event, someHandler);
expect(addEventListenerSpy).to.have.been.calledWith(event, sinon.match.func);
});
});
it("should not subscribe to unknown device events", function () {
notificationIOS.addEventListener("someUnsupportedEvent", someHandler);
expect(addEventListenerSpy).to.not.have.been.called;
});
});
describe("Remove Event Listener", function () {
deviceEvents.forEach(function(event) {
it(`should unsubscribe the given handler from device event: ${event}`, function () {
notificationIOS.addEventListener(event, someHandler);
notificationIOS.removeEventListener(event, someHandler);
expect(removeEventListenerSpy).to.have.been.calledOnce;
});
});
it("should not unsubscribe to unknown device events", function () {
let someUnsupportedEvent = "someUnsupportedEvent";
notificationIOS.addEventListener(someUnsupportedEvent, someHandler);
notificationIOS.removeEventListener(someUnsupportedEvent, someHandler);
expect(removeEventListenerSpy).to.not.have.been.called;
});
});
});
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