diff --git a/.eslintrc b/.eslintrc index f2a7a0ac5f4a006d35a045ef32167c4194a3cfa6..cfb22942b448eed15ffd00d3cf0c114eadea86f0 100644 --- a/.eslintrc +++ b/.eslintrc @@ -31,6 +31,7 @@ "eqeqeq": [ "error", "smart" - ] + ], + "no-console":0 } } diff --git a/README.md b/README.md index 549accbbaea6353017e2208fbe7b2f9e58a52eed..d2238231ffed4d6a6c9efa49291c62e57b8ba389 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/index.ios.js b/index.ios.js index ded6b6fbd884330da06d646214911a963848172e..be8adfc8e48d0d64a6f3d26661b10c53c171ab3f 100644 --- a/index.ios.js +++ b/index.ios.js @@ -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; diff --git a/package.json b/package.json index 6a449d47189dc9455c28165220db8223b1d2a9d6..90ec6664aef13173a59982ec22df8a4eeb95c991 100644 --- a/package.json +++ b/package.json @@ -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" + ] + } } diff --git a/test/index.ios.spec.js b/test/index.ios.spec.js index 23386a06aad00c643a8af1fbd19cdca33ca7064a..31133bf0b055af25056ad6498ab9c07b6c29fae3 100644 --- a/test/index.ios.spec.js +++ b/test/index.ios.spec.js @@ -1,7 +1,81 @@ -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; + }); }); });