Commit ceaeb80a authored by yogevbd's avatar yogevbd

Add test coverage for Notification.ts, clean old js files

parent 4e661458
import { Notification } from './Notification';
describe('Notification', () => {
it('Should create notification with payload', () => {
const payload = { p: 'p' };
const notification = new Notification(payload);
expect(notification.data).toEqual(payload);
});
it('Should create notification with identifier', () => {
const payload = { identifier: 'identifier' };
const notification = new Notification(payload);
expect(notification.identifier).toEqual(payload.identifier);
});
it('Should return title from payload', () => {
const payload = { title: 'title' };
const notification = new Notification(payload);
expect(notification.title).toEqual(payload.title);
});
it('Should return body from payload', () => {
const payload = { body: 'body' };
const notification = new Notification(payload);
expect(notification.body).toEqual(payload.body);
});
it('Should return sound from payload', () => {
const payload = { sound: 'sound.mp4' };
const notification = new Notification(payload);
expect(notification.sound).toEqual(payload.sound);
});
it('Should return badge from payload', () => {
const payload = { badge: 1 };
const notification = new Notification(payload);
expect(notification.badge).toEqual(payload.badge);
});
it('Should return type from payload', () => {
const payload = { type: 'type' };
const notification = new Notification(payload);
expect(notification.type).toEqual(payload.type);
});
it('Should return thread from payload', () => {
const payload = { thread: 'thread' };
const notification = new Notification(payload);
expect(notification.thread).toEqual(payload.thread);
});
});
......@@ -2,11 +2,6 @@ export class Notification {
identifier: string;
private _data?: any;
sound?: string;
badge?: number;
type?: string;
thread?: string;
constructor(payload: object) {
this._data = payload;
this.identifier = this._data.identifier;
......@@ -23,4 +18,20 @@ export class Notification {
get body(): string {
return this._data.body;
}
get sound(): string {
return this._data.sound;
}
get badge(): number {
return this._data.badge;
}
get type(): string {
return this._data.type;
}
get thread(): string {
return this._data.thread;
}
}
......@@ -2,7 +2,7 @@ import { NativeCommandsSender } from './adapters/NativeCommandsSender';
import { NativeEventsReceiver } from './adapters/NativeEventsReceiver';
import { Commands } from './commands/Commands';
import { EventsRegistry } from './events/EventsRegistry';
import { Notification } from './interfaces/Notification';
import { Notification } from './DTO/Notification';
import { UniqueIdProvider } from './adapters/UniqueIdProvider';
import { CompletionCallbackWrapper } from './adapters/CompletionCallbackWrapper';
import { NotificationCategory } from './interfaces/NotificationCategory';
......
import { NativeCommandsSender } from './NativeCommandsSender';
import { Notification } from '../interfaces/Notification';
import { Notification } from '../DTO/Notification';
import { NotificationCompletion } from '../interfaces/NotificationCompletion';
import { Platform } from 'react-native';
......
import { NativeModules } from 'react-native';
import { Notification } from '../interfaces/Notification';
import { Notification } from '../DTO/Notification';
import { NotificationCompletion } from '../interfaces/NotificationCompletion';
import { NotificationPermissions } from '../interfaces/NotificationPermissions';
import { NotificationCategory } from '../interfaces/NotificationCategory';
......
......@@ -2,7 +2,7 @@ import { NativeModules, NativeEventEmitter, EventEmitter, EmitterSubscription }
import {
Registered, RegistrationError, RegisteredPushKit
} from '../interfaces/NotificationEvents';
import { Notification } from '../interfaces/Notification';
import { Notification } from '../DTO/Notification';
export class NativeEventsReceiver {
private emitter: EventEmitter;
......
......@@ -3,7 +3,7 @@ import { mock, verify, instance, when, anyNumber } from 'ts-mockito';
import { Commands } from './Commands';
import { NativeCommandsSender } from '../adapters/NativeCommandsSender';
import { Notification } from '../interfaces/Notification';
import { Notification } from '../DTO/Notification';
import { UniqueIdProvider } from '../adapters/UniqueIdProvider';
import { NotificationCategory } from '../interfaces/NotificationCategory';
import { NotificationPermissions } from '../interfaces/NotificationPermissions';
......
import * as _ from 'lodash';
import { NativeCommandsSender } from '../adapters/NativeCommandsSender';
import { Notification } from '../interfaces/Notification';
import { Notification } from '../DTO/Notification';
import { NotificationCategory } from '../interfaces/NotificationCategory';
import { NotificationPermissions } from '../interfaces/NotificationPermissions';
import { UniqueIdProvider } from '../adapters/UniqueIdProvider';
......
import { EventsRegistry } from './EventsRegistry';
import { NativeEventsReceiver } from '../adapters/NativeEventsReceiver.mock';
import { Notification } from '../interfaces/Notification';
import { Notification } from '../DTO/Notification';
import { CompletionCallbackWrapper } from '../adapters/CompletionCallbackWrapper';
import { NativeCommandsSender } from '../adapters/NativeCommandsSender.mock';
import { NotificationResponse } from '../interfaces/NotificationEvents';
......
......@@ -7,7 +7,7 @@ import {
NotificationResponse
} from '../interfaces/NotificationEvents';
import { CompletionCallbackWrapper } from '../adapters/CompletionCallbackWrapper';
import { Notification } from '../interfaces/Notification';
import { Notification } from '../DTO/Notification';
import { NotificationCompletion } from '../interfaces/NotificationCompletion';
export class EventsRegistry {
......
import {NativeModules, DeviceEventEmitter} from 'react-native';
import NotificationAndroid from './notification';
const RNNotifications = NativeModules.WixRNNotifications;
let notificationReceivedListener;
let notificationReceivedInForegroundListener;
let notificationOpenedListener;
let registrationTokenUpdateListener;
export class NotificationsAndroid {
static setNotificationOpenedListener(listener) {
notificationOpenedListener = DeviceEventEmitter.addListener('notificationOpened', (notification) => listener(new NotificationAndroid(notification)));
}
static clearNotificationOpenedListener() {
if (notificationOpenedListener) {
notificationOpenedListener.remove();
notificationOpenedListener = null;
}
}
static setNotificationReceivedListener(listener) {
notificationReceivedListener = DeviceEventEmitter.addListener('notificationReceived', (notification) => listener(new NotificationAndroid(notification)));
}
static setNotificationReceivedInForegroundListener(listener) {
notificationReceivedInForegroundListener = DeviceEventEmitter.addListener('notificationReceivedInForeground', (notification) => listener(new NotificationAndroid(notification)));
}
static clearNotificationReceivedListener() {
if (notificationReceivedListener) {
notificationReceivedListener.remove();
notificationReceivedListener = null;
}
}
static clearNotificationReceivedInForegroundListener() {
if (notificationReceivedInForegroundListener) {
notificationReceivedInForegroundListener.remove();
notificationReceivedInForegroundListener = null;
}
}
static setRegistrationTokenUpdateListener(listener) {
registrationTokenUpdateListener = DeviceEventEmitter.addListener('remoteNotificationsRegistered', listener);
}
static clearRegistrationTokenUpdateListener() {
if (registrationTokenUpdateListener) {
registrationTokenUpdateListener.remove();
registrationTokenUpdateListener = null;
}
}
static async isRegisteredForRemoteNotifications() {
return await RNNotifications.isRegisteredForRemoteNotifications();
}
static refreshToken() {
RNNotifications.refreshToken();
}
static localNotification(notification: Object) {
const id = Math.random() * 100000000 | 0; // Bitwise-OR forces value onto a 32bit limit
RNNotifications.postLocalNotification(notification, id);
return id;
}
static cancelLocalNotification(id) {
RNNotifications.cancelLocalNotification(id);
}
}
export class PendingNotifications {
static getInitialNotification() {
return RNNotifications.getInitialNotification()
.then((rawNotification) => {
return rawNotification ? new NotificationAndroid(rawNotification) : undefined;
});
}
}
/**
* @flow
*/
'use strict';
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
import IOSNotification from './notification.ios';
export const DEVICE_REMOTE_NOTIFICATIONS_REGISTERED_EVENT = 'remoteNotificationsRegistered';
export const DEVICE_REMOTE_NOTIFICATIONS_REGISTRATION_FAILED_EVENT = 'remoteNotificationsRegistrationFailed';
export const DEVICE_PUSH_KIT_REGISTERED_EVENT = 'pushKitRegistered';
export const DEVICE_NOTIFICATION_RECEIVED_FOREGROUND_EVENT = 'notificationReceivedForeground';
export const DEVICE_NOTIFICATION_OPENED_EVENT = 'notificationOpened';
export const DEVICE_PUSH_KIT_NOTIFICATION_RECEIVED_EVENT = 'pushKitNotificationReceived';
const _exportedEvents = [
DEVICE_REMOTE_NOTIFICATIONS_REGISTERED_EVENT,
DEVICE_REMOTE_NOTIFICATIONS_REGISTRATION_FAILED_EVENT,
DEVICE_PUSH_KIT_REGISTERED_EVENT,
DEVICE_NOTIFICATION_RECEIVED_FOREGROUND_EVENT,
DEVICE_NOTIFICATION_OPENED_EVENT,
DEVICE_PUSH_KIT_NOTIFICATION_RECEIVED_EVENT
];
const _notificationHandlers = new Map();
const _actionHandlers = new Map();
export class NotificationAction {
options: Object;
constructor(options: Object) {
this.options = options;
}
}
export class NotificationCategory {
options: Object;
constructor(options: Object) {
this.options = options;
}
}
export default class NotificationsIOS {
/**
* Attaches a listener to remote notification events while the app is running
* in the foreground or the background.
*
* Valid events are:
*
* - `remoteNotificationsRegistered` : Fired when the user registers for remote notifications. The handler will be invoked with a hex string representing the deviceToken.
* - `notificationReceivedForeground` : Fired when a notification (local / remote) is received when app is on foreground state.
* - `notificationOpened`: Fired when a notification (local / remote) is opened.
* - `pushKitNotificationReceived` : Fired when a pushKit notification received when app is both on foreground and background state.
*/
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,
registration => handler(registration.deviceToken)
);
} else if (type === DEVICE_REMOTE_NOTIFICATIONS_REGISTRATION_FAILED_EVENT) {
listener = DeviceEventEmitter.addListener(
DEVICE_REMOTE_NOTIFICATIONS_REGISTRATION_FAILED_EVENT,
error => handler(error)
);
} else if (type === DEVICE_PUSH_KIT_REGISTERED_EVENT) {
listener = DeviceEventEmitter.addListener(
DEVICE_PUSH_KIT_REGISTERED_EVENT,
registration => handler(registration.pushKitToken)
);
} else if (type === DEVICE_NOTIFICATION_RECEIVED_FOREGROUND_EVENT) {
listener = DeviceEventEmitter.addListener(
type,
({payload, identifier}) => handler(new IOSNotification(payload), (presentingOptions) => {
NativeRNNotifications.finishPresentingNotification(identifier, presentingOptions);
})
);
} else if (type === DEVICE_NOTIFICATION_OPENED_EVENT) {
listener = DeviceEventEmitter.addListener(
type,
({payload, identifier, action}) => handler(new IOSNotification(payload), () => {
NativeRNNotifications.finishHandlingAction(identifier);
}, action)
);
} else if (type === DEVICE_PUSH_KIT_NOTIFICATION_RECEIVED_EVENT) {
listener = DeviceEventEmitter.addListener(
type,
(payload) => handler(new IOSNotification(payload))
);
}
_notificationHandlers.set(handler, listener);
}
}
/**
* Removes the event listener. Do this in `componentWillUnmount` to prevent
* memory leaks
*/
static removeEventListener(type: string, handler: Function) {
if (_exportedEvents.indexOf(type) !== -1) {
const listener = _notificationHandlers.get(handler);
if (listener) {
listener.remove();
_notificationHandlers.delete(handler);
}
}
}
/**
* Sets the notification categories
*/
static requestPermissions(categories: Array<NotificationCategory>) {
let notificationCategories = [];
if (categories) {
notificationCategories = categories.map(category => {
return Object.assign({}, category.options, {
actions: category.options.actions.map(action => {
// subscribe to action event
_actionHandlers.set(action.options.identifier, action.handler);
return action.options;
})
});
});
}
NativeRNNotifications.requestPermissionsWithCategories(notificationCategories);
}
/**
* Unregister for all remote notifications received via Apple Push Notification service.
*/
static abandonPermissions() {
NativeRNNotifications.abandonPermissions();
}
/**
* Removes the event listener. Do this in `componentWillUnmount` to prevent
* memory leaks
*/
static resetCategories() {
_actionHandlers.clear();
}
static getBadgesCount(callback: Function) {
NativeRNNotifications.getBadgesCount(callback);
}
static setBadgesCount(count: number) {
NativeRNNotifications.setBadgesCount(count);
}
static registerPushKit() {
NativeRNNotifications.registerPushKit();
}
static backgroundTimeRemaining(callback: Function) {
NativeRNNotifications.backgroundTimeRemaining(callback);
}
static log(message: string) {
NativeRNNotifications.log(message);
}
static async getInitialNotification() {
const notification = await NativeRNNotifications.getInitialNotification();
if (notification) {
return new IOSNotification(notification);
} else {
return undefined;
}
}
/**
* Presenting local notification
*
* notification is an object containing:
*
* - `body` : The message displayed in the notification alert.
* - `title` : The message title displayed in the notification.
* - `alertAction` : The 'action' displayed beneath an actionable notification. Defaults to 'view';
* - `sound` : The sound played when the notification is fired (optional).
* - `silent` : If true, the notification sound will be suppressed (optional).
* - `category` : The category of this notification, required for actionable notifications (optional).
* - `userInfo` : An optional object containing additional notification data.
* - `fireDate` : The date and time when the system should deliver the notification. if not specified, the notification will be dispatched immediately.
*/
static localNotification(notification: Object) {
const notificationId = uuid.v4();
NativeRNNotifications.localNotification(notification, notificationId);
return notificationId;
}
static cancelLocalNotification(notificationId: String) {
NativeRNNotifications.cancelLocalNotification(notificationId);
}
static cancelAllLocalNotifications() {
NativeRNNotifications.cancelAllLocalNotifications();
}
static isRegisteredForRemoteNotifications() {
return NativeRNNotifications.isRegisteredForRemoteNotifications();
}
static checkPermissions() {
return NativeRNNotifications.checkPermissions();
}
/**
* Remove all delivered notifications from Notification Center
*/
static removeAllDeliveredNotifications() {
return NativeRNNotifications.removeAllDeliveredNotifications();
}
/**
* Removes the specified notifications from Notification Center
*
* @param identifiers Array of notification identifiers
*/
static removeDeliveredNotifications(identifiers: Array<String>) {
return NativeRNNotifications.removeDeliveredNotifications(identifiers);
}
/**
* Provides you with a list of the app’s notifications that are still displayed in Notification Center
*
* @param callback Function which receive an array of delivered notifications
*
* A delivered notification is an object containing:
*
* - `identifier` : The identifier of this notification.
* - `body` : The message displayed in the notification alert.
* - `title` : The message title displayed in the notification.
* - `category` : The category of this notification, if has one.
* - `userInfo` : An optional object containing additional notification data.
* - `thread-id` : The thread identifier of this notification, if has one.
* - `fireDate` : The date and time when the system should deliver the notification. if not specified, the notification will be dispatched immediately.
*/
static getDeliveredNotifications(callback: (notifications: Array<Object>) => void) {
return NativeRNNotifications.getDeliveredNotifications(callback);
}
}
......@@ -4,5 +4,5 @@ const notificationsSingleton = new NotificationsRoot();
export const Notifications = notificationsSingleton;
export * from './interfaces/EventSubscription';
export * from './interfaces/Notification';
export * from './DTO/Notification';
export * from './interfaces/NotificationEvents';
import { Notification } from './Notification';
import { Notification } from '../DTO/Notification';
import { NotificationActionResponse } from './NotificationActionResponse';
export interface Registered {
......
/** A wrapper to align Android with iOS in terms on notification structure. */
export default class NotificationAndroid {
constructor(notification) {
this.data = notification;
}
getData() {
return this.data;
}
getTitle() {
return this.data.title;
}
getMessage() {
return this.data.body;
}
}
export default class IOSNotification {
_data: Object;
_alert: string | Object;
_sound: string;
_badge: number;
_category: string;
_type: string; // regular / managed
_thread: string;
constructor(notification: Object) {
this._data = {};
if (notification.aps &&
notification.aps['content-available'] &&
notification.aps['content-available'] === 1 &&
!notification.aps.alert &&
!notification.aps.sound &&
notification.managedAps) {
// managed notification
this._alert = notification.managedAps.alert;
this._sound = notification.managedAps.sound;
this._badge = notification.aps.badge;
this._category = notification.managedAps.category;
this._type = 'managed';
this._thread = notification.aps.thread;
} else if (
notification.aps) {
// regular notification
this._alert = notification.aps.alert;
this._sound = notification.aps.sound;
this._badge = notification.aps.badge;
this._category = notification.aps.category;
this._type = 'regular';
this._thread = notification.aps.thread;
}
Object.keys(notification).filter(key => key !== 'aps').forEach(key => {
this._data[key] = notification[key];
});
}
getMessage(): ?string | ?Object {
return this._alert;
}
getSound(): ?string {
return this._sound;
}
getBadgeCount(): ?number {
return this._badge;
}
getCategory(): ?string {
return this._category;
}
getData(): ?Object {
return this._data;
}
getType(): ?string {
return this._type;
}
getThread(): ?string {
return this._thread;
}
}
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