Commit 8099dbfc authored by yogevbd's avatar yogevbd

Add unit tests

parent a972d1ee
......@@ -5,6 +5,7 @@ interface NativeCommandsModule {
getInitialNotification(): Promise<any>;
localNotification(notification: Notification, id: string): Promise<Notification>;
requestPermissionsWithCategories(categories: any): Promise<any>;
abandonPermissions(): Promise<any>;
}
export class NativeCommandsSender {
......@@ -24,4 +25,8 @@ export class NativeCommandsSender {
requestPermissions() {
return this.nativeCommandsModule.requestPermissionsWithCategories([]);
}
abandonPermissions() {
return this.nativeCommandsModule.abandonPermissions();
}
}
export const { NativeEventsReceiver } = jest.genMockFromModule('./NativeEventsReceiver');
......@@ -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);
}
}
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();
});
});
});
......@@ -22,4 +22,9 @@ export class Commands {
const result = this.nativeCommandsSender.requestPermissions();
return result;
}
public abandonPermissions() {
const result = this.nativeCommandsSender.abandonPermissions();
return result;
}
}
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);
});
});
......@@ -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);
}
}
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;
}
......@@ -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.*",
......
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`);
}
}
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: {
......
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