From 8099dbfca120ec851f518d10f4a828f8033b841b Mon Sep 17 00:00:00 2001 From: yogevbd Date: Wed, 31 Jul 2019 11:28:46 +0300 Subject: [PATCH] Add unit tests --- lib/src/adapters/NativeCommandsSender.ts | 5 ++ lib/src/adapters/NativeEventsReceiver.mock.ts | 1 + lib/src/adapters/NativeEventsReceiver.ts | 2 +- lib/src/commands/Commands.test.ts | 56 +++++++++++++++++++ lib/src/commands/Commands.ts | 5 ++ lib/src/events/EventsRegistry.test.tsx | 25 +++++++++ lib/src/events/EventsRegistry.ts | 4 +- lib/src/interfaces/Notification.ts | 10 ++-- package.json | 3 +- scripts/test-js.js | 30 ++++++++++ wallaby.js | 15 ++++- 11 files changed, 144 insertions(+), 12 deletions(-) create mode 100644 lib/src/adapters/NativeEventsReceiver.mock.ts create mode 100644 lib/src/commands/Commands.test.ts create mode 100644 lib/src/events/EventsRegistry.test.tsx create mode 100644 scripts/test-js.js diff --git a/lib/src/adapters/NativeCommandsSender.ts b/lib/src/adapters/NativeCommandsSender.ts index ee4d01e..1400153 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 0000000..69e8f0e --- /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 a7f8236..a8b3f53 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 0000000..11995b6 --- /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 2ab6c70..cdb545f 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 0000000..2d323b8 --- /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 fe09985..cd70540 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 45ee724..e24c46b 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 5f4fa48..e5c48e4 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 0000000..953a9e5 --- /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 890e42a..52a2259 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: { -- 2.26.2