diff --git a/lib/src/Notifications.ts b/lib/src/Notifications.ts index 68b00e0ca2d98a85cee87efdf9c17cfe86941d8a..3d28969f9d7f760346ac199c1f39c7b4cf1d6094 100644 --- a/lib/src/Notifications.ts +++ b/lib/src/Notifications.ts @@ -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 { - this.commands.isRegisteredForRemoteNotifications(); + public isRegisteredForRemoteNotifications(): Promise { + return this.commands.isRegisteredForRemoteNotifications(); } /** diff --git a/lib/src/adapters/UniqueIdProvider.ts b/lib/src/adapters/UniqueIdProvider.ts new file mode 100644 index 0000000000000000000000000000000000000000..0eaaff6c672bff6278e2ea91dc9cd658503b9e24 --- /dev/null +++ b/lib/src/adapters/UniqueIdProvider.ts @@ -0,0 +1,7 @@ +import * as _ from 'lodash'; + +export class UniqueIdProvider { + generate(prefix?: string): string { + return _.uniqueId(prefix); + } +} diff --git a/lib/src/commands/Commands.test.ts b/lib/src/commands/Commands.test.ts index f1da2659955fd3bc6866e29088d35b48162813ca..aba9afc2d05f4dd8c6ffabef71499c0be624d2ef 100644 --- a/lib/src/commands/Commands.test.ts +++ b/lib/src/commands/Commands.test.ts @@ -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(); }); }); diff --git a/lib/src/commands/Commands.ts b/lib/src/commands/Commands.ts index 882a5e986998488446d1932a787a83aa09e2bd7f..07986a675d87581f5641eb47b8e27c49510764f6 100644 --- a/lib/src/commands/Commands.ts +++ b/lib/src/commands/Commands.ts @@ -1,14 +1,16 @@ 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; }