Commit f666b856 authored by yogevbd's avatar yogevbd

Done converting the js part to typescript

parent 6c99242e
......@@ -3,18 +3,22 @@ import { NativeEventsReceiver } from './adapters/NativeEventsReceiver';
import { Commands } from './commands/Commands';
import { EventsRegistry } from './events/EventsRegistry';
import { Notification, NotificationCategory } from './interfaces/Notification';
import { UniqueIdProvider } from './adapters/UniqueIdProvider';
export class NotificationsRoot {
private readonly nativeEventsReceiver: NativeEventsReceiver;
private readonly nativeCommandsSender: NativeCommandsSender;
private readonly commands: Commands;
private readonly eventsRegistry: EventsRegistry;
private readonly uniqueIdProvider: UniqueIdProvider;
constructor() {
this.nativeEventsReceiver = new NativeEventsReceiver();
this.nativeCommandsSender = new NativeCommandsSender();
this.uniqueIdProvider = new UniqueIdProvider();
this.commands = new Commands(
this.nativeCommandsSender
this.nativeCommandsSender,
this.uniqueIdProvider
);
this.eventsRegistry = new EventsRegistry(this.nativeEventsReceiver);
}
......@@ -36,8 +40,8 @@ export class NotificationsRoot {
/**
* Reset the app to a new layout
*/
public localNotification(notification: Notification) {
return this.commands.sendLocalNotification(notification);
public localNotification(notification: Notification, id: string) {
return this.commands.sendLocalNotification(notification, id);
}
/**
......@@ -86,8 +90,8 @@ export class NotificationsRoot {
/**
* isRegisteredForRemoteNotifications
*/
public isRegisteredForRemoteNotifications(): Promise<any> {
this.commands.isRegisteredForRemoteNotifications();
public isRegisteredForRemoteNotifications(): Promise<boolean> {
return this.commands.isRegisteredForRemoteNotifications();
}
/**
......
import * as _ from 'lodash';
export class UniqueIdProvider {
generate(prefix?: string): string {
return _.uniqueId(prefix);
}
}
......@@ -4,16 +4,20 @@ import { mock, verify, instance, deepEqual, when, anything, anyString } from 'ts
import { Commands } from './Commands';
import { NativeCommandsSender } from '../adapters/NativeCommandsSender';
import { Notification, NotificationCategory, NotificationPermissions } from '../interfaces/Notification';
import { UniqueIdProvider } from '../adapters/UniqueIdProvider';
describe('Commands', () => {
let uut: Commands;
let mockedNativeCommandsSender: NativeCommandsSender;
let mockedUniqueIdProvider: UniqueIdProvider;
beforeEach(() => {
mockedNativeCommandsSender = mock(NativeCommandsSender);
mockedUniqueIdProvider = mock(UniqueIdProvider);
when(mockedUniqueIdProvider.generate(anything())).thenCall((prefix) => `${prefix}+UNIQUE_ID`);
uut = new Commands(
instance(mockedNativeCommandsSender)
instance(mockedNativeCommandsSender),
instance(mockedUniqueIdProvider)
);
});
......@@ -24,11 +28,12 @@ describe('Commands', () => {
});
it('returns a promise with the initial notification', async () => {
const expectedNotification: Notification = {data: {}, alert: 'alert'};
when(mockedNativeCommandsSender.getInitialNotification()).thenResolve(
{data: {}}
expectedNotification
);
const result = await uut.getInitialNotification();
expect(result).toEqual({data: {}});
expect(result).toEqual(expectedNotification);
});
});
......@@ -72,7 +77,20 @@ describe('Commands', () => {
it('sends to native', () => {
const notification: Notification = {data: {}, alert: 'alert'};
uut.sendLocalNotification(notification);
verify(mockedNativeCommandsSender.sendLocalNotification(notification, 'id')).called();
verify(mockedNativeCommandsSender.sendLocalNotification(notification, anyString())).called();
});
it('generates unique identifier', () => {
const notification: Notification = {data: {}, alert: 'alert'};
uut.sendLocalNotification(notification);
verify(mockedNativeCommandsSender.sendLocalNotification(notification, `Notification_+UNIQUE_ID`)).called();
});
it('use passed notification id', () => {
const notification: Notification = {data: {}, alert: 'alert'};
const passedId: string = "passedId";
uut.sendLocalNotification(notification, passedId);
verify(mockedNativeCommandsSender.sendLocalNotification(notification, passedId)).called();
});
});
......
import * as _ from 'lodash';
import { NativeCommandsSender } from '../adapters/NativeCommandsSender';
import { Notification, NotificationCategory, NotificationPermissions } from '../interfaces/Notification';
import { UniqueIdProvider } from '../adapters/UniqueIdProvider';
export class Commands {
constructor(
private readonly nativeCommandsSender: NativeCommandsSender
private readonly nativeCommandsSender: NativeCommandsSender,
private readonly uniqueIdProvider: UniqueIdProvider
) {}
public sendLocalNotification(notification: Notification) {
const notificationId = 'id';
public sendLocalNotification(notification: Notification, id?: string) {
const notificationId = id ? id : this.uniqueIdProvider.generate('Notification_');
const result = this.nativeCommandsSender.sendLocalNotification(notification, notificationId);
return result;
}
......
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