diff --git a/lib/src/adapters/NativeCommandsSender.ts b/lib/src/adapters/NativeCommandsSender.ts index ee4d01e9ab56299e1e178631504c45821dca7fa1..14001530a21cc67a03647b695714d9b07a08ca61 100644 --- a/lib/src/adapters/NativeCommandsSender.ts +++ b/lib/src/adapters/NativeCommandsSender.ts @@ -5,6 +5,7 @@ interface NativeCommandsModule { getInitialNotification(): Promise; localNotification(notification: Notification, id: string): Promise; requestPermissionsWithCategories(categories: any): Promise; + abandonPermissions(): Promise; } export class NativeCommandsSender { @@ -24,4 +25,8 @@ export class NativeCommandsSender { requestPermissions() { return this.nativeCommandsModule.requestPermissionsWithCategories([]); } + + abandonPermissions() { + return this.nativeCommandsModule.abandonPermissions(); + } } diff --git a/lib/src/adapters/NativeEventsReceiver.mock.ts b/lib/src/adapters/NativeEventsReceiver.mock.ts new file mode 100644 index 0000000000000000000000000000000000000000..69e8f0e976a58d147c3376944090f586a1f42a95 --- /dev/null +++ b/lib/src/adapters/NativeEventsReceiver.mock.ts @@ -0,0 +1 @@ +export const { NativeEventsReceiver } = jest.genMockFromModule('./NativeEventsReceiver'); diff --git a/lib/src/adapters/NativeEventsReceiver.ts b/lib/src/adapters/NativeEventsReceiver.ts index a7f8236b957c19df1d59e269572ba935092897a5..a8b3f535aa6f00f591352dc970780adde056c1a1 100644 --- a/lib/src/adapters/NativeEventsReceiver.ts +++ b/lib/src/adapters/NativeEventsReceiver.ts @@ -13,7 +13,7 @@ export class NativeEventsReceiver { return this.emitter.addListener('remoteNotificationsRegistered', callback); } - public registerRemoteNotificationsReceived(callback: (event: NotificationReceived) => void): EmitterSubscription { + public registerRemoteNotificationReceived(callback: (event: NotificationReceived) => void): EmitterSubscription { return this.emitter.addListener('notificationReceivedForeground', callback); } } diff --git a/lib/src/commands/Commands.test.ts b/lib/src/commands/Commands.test.ts new file mode 100644 index 0000000000000000000000000000000000000000..11995b6cf8f1651d4d4ae3d1becfda00ed62ab59 --- /dev/null +++ b/lib/src/commands/Commands.test.ts @@ -0,0 +1,56 @@ +import * as _ from 'lodash'; +import { mock, verify, instance, deepEqual, when, anything, anyString } from 'ts-mockito'; + +import { Commands } from './Commands'; +import { NativeCommandsSender } from '../adapters/NativeCommandsSender'; +import { Notification } from '../interfaces/Notification'; + +describe('Commands', () => { + let uut: Commands; + let mockedNativeCommandsSender: NativeCommandsSender; + + beforeEach(() => { + mockedNativeCommandsSender = mock(NativeCommandsSender); + + uut = new Commands( + instance(mockedNativeCommandsSender) + ); + }); + + describe('getInitialNotification', () => { + it('sends getInitialNotification to native', () => { + uut.getInitialNotification(); + verify(mockedNativeCommandsSender.getInitialNotification()).called(); + }); + + it('returns a promise with the initial notification', async () => { + when(mockedNativeCommandsSender.getInitialNotification()).thenResolve( + {data: {}} + ); + const result = await uut.getInitialNotification(); + expect(result).toEqual({data: {}}); + }); + }); + + describe('requestPermissions', () => { + it('sends requestPermissions to native', () => { + uut.requestPermissions(); + verify(mockedNativeCommandsSender.requestPermissions()).called(); + }); + }); + + describe('abandonPermissions', () => { + it('sends abandonPermissions to native', () => { + uut.abandonPermissions(); + verify(mockedNativeCommandsSender.abandonPermissions()).called(); + }); + }); + + describe('sendLocalNotification', () => { + it('sends sendLocalNotification to native', () => { + const notification: Notification = {data: {}, alert: 'alert'}; + uut.sendLocalNotification(notification); + verify(mockedNativeCommandsSender.sendLocalNotification(notification, 'id')).called(); + }); + }); +}); diff --git a/lib/src/commands/Commands.ts b/lib/src/commands/Commands.ts index 2ab6c702fd8796adfb8efd78cfaaf47867d40b29..cdb545fbda478a0993090b5bef35d19761df5419 100644 --- a/lib/src/commands/Commands.ts +++ b/lib/src/commands/Commands.ts @@ -22,4 +22,9 @@ export class Commands { const result = this.nativeCommandsSender.requestPermissions(); return result; } + + public abandonPermissions() { + const result = this.nativeCommandsSender.abandonPermissions(); + return result; + } } diff --git a/lib/src/events/EventsRegistry.test.tsx b/lib/src/events/EventsRegistry.test.tsx new file mode 100644 index 0000000000000000000000000000000000000000..2d323b8bf83341af4662d687199b1d1f495f9724 --- /dev/null +++ b/lib/src/events/EventsRegistry.test.tsx @@ -0,0 +1,25 @@ +import { EventsRegistry } from './EventsRegistry'; +import { NativeEventsReceiver } from '../adapters/NativeEventsReceiver.mock'; + +describe('EventsRegistry', () => { + let uut: EventsRegistry; + const mockNativeEventsReceiver = new NativeEventsReceiver(); + + beforeEach(() => { + uut = new EventsRegistry(mockNativeEventsReceiver); + }); + + it('delegates registerRemoteNotificationsRegistered to nativeEventsReceiver', () => { + const cb = jest.fn(); + uut.registerRemoteNotificationsRegistered(cb); + expect(mockNativeEventsReceiver.registerRemoteNotificationsRegistered).toHaveBeenCalledTimes(1); + expect(mockNativeEventsReceiver.registerRemoteNotificationsRegistered).toHaveBeenCalledWith(cb); + }); + + it('delegates registerRemoteNotificationsReceived to nativeEventsReceiver', () => { + const cb = jest.fn(); + uut.registerNotificationReceived(cb); + expect(mockNativeEventsReceiver.registerRemoteNotificationReceived).toHaveBeenCalledTimes(1); + expect(mockNativeEventsReceiver.registerRemoteNotificationReceived).toHaveBeenCalledWith(cb); + }); +}); diff --git a/lib/src/events/EventsRegistry.ts b/lib/src/events/EventsRegistry.ts index fe0998525509099c9f59b782880e6d3db2000164..cd7054031cf099711714a5a345dfc37a15c3f8b4 100644 --- a/lib/src/events/EventsRegistry.ts +++ b/lib/src/events/EventsRegistry.ts @@ -12,7 +12,7 @@ export class EventsRegistry { return this.nativeEventsReceiver.registerRemoteNotificationsRegistered(callback); } - public registerNotificationsReceived(callback: (event: NotificationReceived) => void): EmitterSubscription { - return this.nativeEventsReceiver.registerRemoteNotificationsReceived(callback); + public registerNotificationReceived(callback: (event: NotificationReceived) => void): EmitterSubscription { + return this.nativeEventsReceiver.registerRemoteNotificationReceived(callback); } } diff --git a/lib/src/interfaces/Notification.ts b/lib/src/interfaces/Notification.ts index 45ee724c49cebb52bf717db5f3f0e451f05c73db..e24c46b1d6ca6352246469894bf3a21a8d1852ff 100644 --- a/lib/src/interfaces/Notification.ts +++ b/lib/src/interfaces/Notification.ts @@ -1,8 +1,8 @@ export interface Notification { - data: any; + data: object; alert: string - sound: string; - badge: number; - type: string; - thread: string; + sound?: string; + badge?: number; + type?: string; + thread?: string; } diff --git a/package.json b/package.json index 5f4fa48a057316f692defc992458827aa098a4e9..e5c48e4f95c7fc6dbd8ce4ca6aeec65bac5860a2 100644 --- a/package.json +++ b/package.json @@ -55,6 +55,7 @@ "chai": "^3.5.0", "chokidar-cli": "^1.2.0", "tslint": "5.x.x", + "ts-mockito": "^2.3.1", "mocha": "^2.5.3", "proxyquire": "^1.7.4", "sinon": "^1.17.3", @@ -119,7 +120,7 @@ "lib/src/**/*.js", "integration/**/*.js", "!lib/dist/index.js", - "!lib/dist/Navigation.js", + "!lib/dist/Notifications.js", "!lib/dist/adapters/**/*", "!lib/dist/interfaces/**/*", "!lib/dist/**/*.test.*", diff --git a/scripts/test-js.js b/scripts/test-js.js new file mode 100644 index 0000000000000000000000000000000000000000..953a9e54065cfaad9c04b04b53cb44a836637057 --- /dev/null +++ b/scripts/test-js.js @@ -0,0 +1,30 @@ +const exec = require('shell-utils').exec; +const _ = require('lodash'); + +const fix = _.includes(process.argv, '--fix') ? '--fix' : ''; + +const dirs = [ + 'lib', + 'integration', + 'e2e', + 'scripts', + 'playground/src' +]; + +run(); + +function run() { + const paths = _.chain(dirs).map((d) => d === 'e2e' ? `${d}/**/*.[tj]s` : `${d}/**/*.[tj]sx?`).join(' ').value(); + exec.execSync(`tslint ${paths} ${fix} --format verbose`); + assertAllTsFilesInSrc(); + exec.execSync(`jest --coverage`); +} + +function assertAllTsFilesInSrc() { + const allFiles = exec.execSyncRead('find ./lib/src -type f'); + const lines = _.split(allFiles, '\n'); + const offenders = _.filter(lines, (f) => !f.endsWith('.ts') && !f.endsWith('.tsx')); + if (offenders.length) { + throw new Error(`\n\nOnly ts/tsx files are allowed:\n${offenders.join('\n')}\n\n\n`); + } +} diff --git a/wallaby.js b/wallaby.js index 890e42a1c721d811cf45be67b873896dc7a59c5b..52a225952c3c0e945d7b7942e8735ffb6f72476a 100644 --- a/wallaby.js +++ b/wallaby.js @@ -1,4 +1,4 @@ -const babelOptions = require('./package.json').babel; +const babelOptions = require('./babel.config')(); module.exports = function (wallaby) { return { @@ -13,11 +13,20 @@ module.exports = function (wallaby) { 'package.json', 'lib/src/**/*.js', 'lib/src/**/*.ts', - 'lib/src/**/*.tsx' + 'lib/src/**/*.tsx', + '!lib/src/Notifications.ts', + '!lib/src/**/*.test.tsx', + '!lib/src/**/*.test.js', + '!lib/src/**/*.test.ts', + 'integration/**/*.js', + '!integration/**/*.test.js' ], tests: [ - 'test/**/*.spec.js' + 'lib/src/**/*.test.js', + 'lib/src/**/*.test.ts', + 'lib/src/**/*.test.tsx', + 'integration/**/*.test.js' ], compilers: {