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