Skip to content

Commit

Permalink
Replace mocha with jest
Browse files Browse the repository at this point in the history
  • Loading branch information
yogevbd committed Jul 7, 2019
1 parent bb5b455 commit a0777af
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 251 deletions.
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"parser": "babel-eslint",
"env": {
"node": true,
"mocha": true,
"jest": true,
"es6": true
},
"extends": "eslint:recommended",
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,5 @@ jspm_packages

# Optional REPL history
.node_repl_history

coverage/
3 changes: 2 additions & 1 deletion babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ module.exports = function (api) {
api && api.cache(false);
return {
presets: [
'module:metro-react-native-babel-preset'
'module:metro-react-native-babel-preset',
'@babel/preset-env'
],
plugins: [
'@babel/plugin-proposal-export-namespace-from',
Expand Down
11 changes: 7 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"main": "lib/src/index",
"scripts": {
"pretest": "./node_modules/.bin/eslint *.js test",
"test": "./node_modules/.bin/mocha --compilers js:babel-register --reporter spec \"test/*.spec.js\"",
"test": "jest",
"start": "node ./scripts/start",
"test-e2e-ios": "node ./scripts/test-e2e --ios"
},
Expand All @@ -41,6 +41,7 @@
"@types/react-test-renderer": "16.x.x",
"@babel/plugin-proposal-export-default-from": "7.2.0",
"@babel/plugin-proposal-export-namespace-from": "7.2.0",
"@babel/preset-env": "7.5.0",
"typescript": "3.2.2",
"babel-eslint": "9.0.0",
"chai": "^3.5.0",
Expand All @@ -55,7 +56,8 @@
"react": "16.8.6",
"detox": "12.x.x",
"jest": "24.8.0",
"metro-react-native-babel-preset": "0.55.x"
"metro-react-native-babel-preset": "0.55.x",
"@babel/register": "7.4.4"
},
"publishConfig": {
"registry": "https://registry.npmjs.org/"
Expand Down Expand Up @@ -101,10 +103,11 @@
"^.+\\.js$": "<rootDir>/node_modules/react-native/jest/preprocessor.js"
},
"roots": [
"<rootDir>/node_modules/"
"<rootDir>/node_modules/",
"<rootDir>/test/"
],
"collectCoverageFrom": [
"lib/dist/**/*.js",
"lib/src/**/*.js",
"integration/**/*.js",
"!lib/dist/index.js",
"!lib/dist/Navigation.js",
Expand Down
142 changes: 68 additions & 74 deletions test/index.android.spec.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,20 @@
'use strict';
let expect = require('chai').use(require('sinon-chai')).expect;
import proxyquire from 'proxyquire';
import sinon from 'sinon';

describe('Notifications-Android > ', () => {
proxyquire.noCallThru();

describe('Notifications-Android', () => {
let refreshTokenStub;
let getInitialNotificationStub;
let postLocalNotificationStub;
let cancelLocalNotificationStub;
let deviceEventEmitterListenerStub;
let libUnderTest;

beforeEach(() => {
refreshTokenStub = sinon.stub();
getInitialNotificationStub = sinon.stub();
postLocalNotificationStub = sinon.stub();
cancelLocalNotificationStub = sinon.stub();
deviceEventEmitterListenerStub = sinon.stub();

libUnderTest = proxyquire('../index.android', {
'react-native': {
refreshTokenStub = jest.fn();
getInitialNotificationStub = jest.fn();
postLocalNotificationStub = jest.fn();
cancelLocalNotificationStub = jest.fn();
deviceEventEmitterListenerStub = jest.fn();

jest.mock('react-native', () => {
return {
NativeModules: {
WixRNNotifications: {
refreshToken: refreshTokenStub,
Expand All @@ -32,162 +26,162 @@ describe('Notifications-Android > ', () => {
DeviceEventEmitter: {
addListener: deviceEventEmitterListenerStub
}
},
'./notification': require('../notification.android')
};
});
libUnderTest = require('../lib/src/index.android');
});

describe('Registration token API', () => {
it('should assign callback to native event upon listener registration', () => {
expect(deviceEventEmitterListenerStub).to.not.have.been.called;
expect(deviceEventEmitterListenerStub).toHaveBeenCalledTimes(0);
const userListener = () => {};

console.log(libUnderTest);
libUnderTest.NotificationsAndroid.setRegistrationTokenUpdateListener(userListener);

expect(deviceEventEmitterListenerStub).to.have.been.calledWith('remoteNotificationsRegistered', userListener);
expect(deviceEventEmitterListenerStub).to.have.been.calledOnce;
expect(deviceEventEmitterListenerStub).toHaveBeenCalledWith('remoteNotificationsRegistered', userListener);
expect(deviceEventEmitterListenerStub).toHaveBeenCalledTimes(1);
});

it('should clear native event listener upon listener deregister', () => {
expect(deviceEventEmitterListenerStub).to.not.have.been.called;
expect(deviceEventEmitterListenerStub).toHaveBeenCalledTimes(0);
const userListener = () => {};
const nativeListener = {
remove: sinon.spy()
remove: jest.fn()
};
deviceEventEmitterListenerStub.returns(nativeListener);
deviceEventEmitterListenerStub.mockReturnValueOnce(nativeListener);

libUnderTest.NotificationsAndroid.setRegistrationTokenUpdateListener(userListener);
libUnderTest.NotificationsAndroid.clearRegistrationTokenUpdateListener();

expect(nativeListener.remove).to.have.been.calledOnce;
expect(nativeListener.remove).toHaveBeenCalledTimes(1);
});

it('shouldn`t fail if deregister without registering', () => {
libUnderTest.NotificationsAndroid.clearRegistrationTokenUpdateListener();

expect(deviceEventEmitterListenerStub).to.not.have.been.called;
expect(deviceEventEmitterListenerStub).toHaveBeenCalledTimes(0);
});
});

describe('notification-opening API', () => {
it('should assign callback to native event upon registration', () => {
expect(deviceEventEmitterListenerStub).to.not.have.been.called;
const userListenerStub = sinon.stub();
expect(deviceEventEmitterListenerStub).toHaveBeenCalledTimes(0);
const userListenerStub = jest.fn();

libUnderTest.NotificationsAndroid.setNotificationOpenedListener(userListenerStub);

expect(deviceEventEmitterListenerStub).to.have.been.calledOnce;
expect(deviceEventEmitterListenerStub).to.have.been.calledWith('notificationOpened', sinon.match.func);
expect(deviceEventEmitterListenerStub).toHaveBeenCalledTimes(1);
expect(deviceEventEmitterListenerStub).toHaveBeenCalledWith('notificationOpened', expect.any(Function));
});

it('should assign a wrapper-callback upon registration', () => {
expect(deviceEventEmitterListenerStub).to.not.have.been.called;
const userListenerStub = sinon.stub();
expect(deviceEventEmitterListenerStub).toHaveBeenCalledTimes(0);
const userListenerStub = jest.fn();
const notification = { foo: 'bar' };

libUnderTest.NotificationsAndroid.setNotificationOpenedListener(userListenerStub);

expect(userListenerStub).to.not.have.been.called;
deviceEventEmitterListenerStub.args[0][1](notification);
expect(userListenerStub).to.have.been.calledOnce;
expect(userListenerStub.args[0][0].getData()).to.equal(notification);
expect(userListenerStub).toHaveBeenCalledTimes(0);
deviceEventEmitterListenerStub.mock.calls[0][1](notification);
expect(userListenerStub).toHaveBeenCalledTimes(1);
expect(userListenerStub.mock.calls[0][0].getData()).toEqual(notification);
});

it('should clear native event listener upon listener deregister', () => {
expect(deviceEventEmitterListenerStub).to.not.have.been.called;
expect(deviceEventEmitterListenerStub).toHaveBeenCalledTimes(0);
const userListener = () => {};
const nativeListener = {
remove: sinon.spy()
remove: jest.fn()
};
deviceEventEmitterListenerStub.returns(nativeListener);
deviceEventEmitterListenerStub.mockReturnValueOnce(nativeListener);

libUnderTest.NotificationsAndroid.setNotificationOpenedListener(userListener);
libUnderTest.NotificationsAndroid.clearNotificationOpenedListener();

expect(nativeListener.remove).to.have.been.calledOnce;
expect(nativeListener.remove).toHaveBeenCalledTimes(1);
});

it('shouldnt fail if deregister without registering', () => {
libUnderTest.NotificationsAndroid.clearNotificationOpenedListener();

expect(deviceEventEmitterListenerStub).to.not.have.been.called;
expect(deviceEventEmitterListenerStub).toHaveBeenCalledTimes(0);
});
});

describe('notification-receive API', () => {
it('should assign callback to native event upon registration', () => {
expect(deviceEventEmitterListenerStub).to.not.have.been.called;
const userListenerStub = sinon.stub();
expect(deviceEventEmitterListenerStub).toHaveBeenCalledTimes(0);
const userListenerStub = jest.fn();

libUnderTest.NotificationsAndroid.setNotificationReceivedListener(userListenerStub);

expect(deviceEventEmitterListenerStub).to.have.been.calledOnce;
expect(deviceEventEmitterListenerStub).to.have.been.calledWith('notificationReceived', sinon.match.func);
expect(deviceEventEmitterListenerStub).toHaveBeenCalledTimes(1);
expect(deviceEventEmitterListenerStub).toHaveBeenCalledWith('notificationReceived', expect.any(Function));
});

it('should assign a wrapper-callback upon registration', () => {
expect(deviceEventEmitterListenerStub).to.not.have.been.called;
const userListenerStub = sinon.stub();
expect(deviceEventEmitterListenerStub).toHaveBeenCalledTimes(0);
const userListenerStub = jest.fn();
const notification = { foo: 'bar' };

libUnderTest.NotificationsAndroid.setNotificationReceivedListener(userListenerStub);

expect(userListenerStub).to.not.have.been.called;
deviceEventEmitterListenerStub.args[0][1](notification);
expect(userListenerStub).to.have.been.calledOnce;
expect(userListenerStub.args[0][0].getData()).to.equal(notification);
expect(userListenerStub).toHaveBeenCalledTimes(0);
deviceEventEmitterListenerStub.mock.calls[0][1](notification);
expect(userListenerStub).toHaveBeenCalledTimes(1);
expect(userListenerStub.mock.calls[0][0].getData()).toEqual(notification);
});

it('should clear native event listener upon listener deregister', () => {
expect(deviceEventEmitterListenerStub).to.not.have.been.called;
expect(deviceEventEmitterListenerStub).toHaveBeenCalledTimes(0);
const userListener = () => {};
const nativeListener = {
remove: sinon.spy()
remove: jest.fn()
};
deviceEventEmitterListenerStub.returns(nativeListener);
deviceEventEmitterListenerStub.mockReturnValueOnce(nativeListener);

libUnderTest.NotificationsAndroid.setNotificationReceivedListener(userListener);
libUnderTest.NotificationsAndroid.clearNotificationReceivedListener();

expect(nativeListener.remove).to.have.been.calledOnce;
expect(nativeListener.remove).toHaveBeenCalledTimes(1);
});

it('shouldn`t fail if deregister without registering', () => {
libUnderTest.NotificationsAndroid.clearNotificationReceivedListener();

expect(deviceEventEmitterListenerStub).to.not.have.been.called;
expect(deviceEventEmitterListenerStub).toHaveBeenCalledTimes(0);
});
});

describe('Notification token', () => {
it('should refresh notification token upon refreshing request by the user', () => {
expect(refreshTokenStub).to.not.have.been.called;
expect(refreshTokenStub).toHaveBeenCalledTimes(0);
libUnderTest.NotificationsAndroid.refreshToken();
expect(refreshTokenStub).to.have.been.calledOnce;
expect(refreshTokenStub).toHaveBeenCalledTimes(1);
});
});

describe('Initial notification API', () => {
it('should return initial notification data if available', (done) => {
expect(getInitialNotificationStub).to.not.have.been.called;
expect(getInitialNotificationStub).toHaveBeenCalledTimes(0);
const rawNotification = {foo: 'bar'};
getInitialNotificationStub.returns(Promise.resolve(rawNotification));
getInitialNotificationStub.mockReturnValueOnce(Promise.resolve(rawNotification));

libUnderTest.PendingNotifications.getInitialNotification()
.then((notification) => {
expect(notification.getData()).to.equal(rawNotification);
expect(notification.getData()).toEqual(rawNotification);
done();
})
.catch((err) => done(err));
});

it('should return empty notification if not available', (done) => {
expect(getInitialNotificationStub).to.not.have.been.called;
getInitialNotificationStub.returns(Promise.resolve(null));
expect(getInitialNotificationStub).toHaveBeenCalledTimes(0);
getInitialNotificationStub.mockReturnValueOnce(Promise.resolve(null));

libUnderTest.PendingNotifications.getInitialNotification()
.then((notification) => {
expect(notification).to.be.undefined;
expect(notification).toBeUndefined();
done();
})
.catch((err) => done(err));
Expand All @@ -202,29 +196,29 @@ describe('Notifications-Android > ', () => {
};

it('should get published when posted manually', () => {
expect(postLocalNotificationStub).to.not.have.been.called;
expect(postLocalNotificationStub).toHaveBeenCalledTimes(0);

const id = libUnderTest.NotificationsAndroid.localNotification(notification);
expect(id).to.not.be.undefined;
expect(postLocalNotificationStub).to.have.been.calledWith(notification, id);
expect(id).toBeDefined();
expect(postLocalNotificationStub).toHaveBeenCalledWith(notification, id);
});

it('should be called with a unique ID', () => {
expect(postLocalNotificationStub).to.not.have.been.called;
expect(postLocalNotificationStub).toHaveBeenCalledTimes(0);

const id = libUnderTest.NotificationsAndroid.localNotification(notification);
const id2 = libUnderTest.NotificationsAndroid.localNotification(notification);
expect(id).to.not.be.undefined;
expect(id2).to.not.be.undefined;
expect(id).to.not.equal(id2);
expect(id).toBeDefined();
expect(id2).toBeDefined();
expect(id).not.toBe(id2);
});

it('should be cancellable with an ID', () => {
expect(cancelLocalNotificationStub).to.not.have.been.called;
expect(cancelLocalNotificationStub).toHaveBeenCalledTimes(0);

libUnderTest.NotificationsAndroid.cancelLocalNotification(666);

expect(cancelLocalNotificationStub).to.have.been.calledWith(666);
expect(cancelLocalNotificationStub).toHaveBeenCalledWith(666);
});
});
});
Loading

0 comments on commit a0777af

Please sign in to comment.