Skip to content

Commit a0777af

Browse files
committed
Replace mocha with jest
1 parent bb5b455 commit a0777af

7 files changed

+174
-251
lines changed

.eslintrc

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"parser": "babel-eslint",
33
"env": {
44
"node": true,
5-
"mocha": true,
5+
"jest": true,
66
"es6": true
77
},
88
"extends": "eslint:recommended",

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -178,3 +178,5 @@ jspm_packages
178178

179179
# Optional REPL history
180180
.node_repl_history
181+
182+
coverage/

babel.config.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ module.exports = function (api) {
22
api && api.cache(false);
33
return {
44
presets: [
5-
'module:metro-react-native-babel-preset'
5+
'module:metro-react-native-babel-preset',
6+
'@babel/preset-env'
67
],
78
plugins: [
89
'@babel/plugin-proposal-export-namespace-from',

package.json

+7-4
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"main": "lib/src/index",
2323
"scripts": {
2424
"pretest": "./node_modules/.bin/eslint *.js test",
25-
"test": "./node_modules/.bin/mocha --compilers js:babel-register --reporter spec \"test/*.spec.js\"",
25+
"test": "jest",
2626
"start": "node ./scripts/start",
2727
"test-e2e-ios": "node ./scripts/test-e2e --ios"
2828
},
@@ -41,6 +41,7 @@
4141
"@types/react-test-renderer": "16.x.x",
4242
"@babel/plugin-proposal-export-default-from": "7.2.0",
4343
"@babel/plugin-proposal-export-namespace-from": "7.2.0",
44+
"@babel/preset-env": "7.5.0",
4445
"typescript": "3.2.2",
4546
"babel-eslint": "9.0.0",
4647
"chai": "^3.5.0",
@@ -55,7 +56,8 @@
5556
"react": "16.8.6",
5657
"detox": "12.x.x",
5758
"jest": "24.8.0",
58-
"metro-react-native-babel-preset": "0.55.x"
59+
"metro-react-native-babel-preset": "0.55.x",
60+
"@babel/register": "7.4.4"
5961
},
6062
"publishConfig": {
6163
"registry": "https://registry.npmjs.org/"
@@ -101,10 +103,11 @@
101103
"^.+\\.js$": "<rootDir>/node_modules/react-native/jest/preprocessor.js"
102104
},
103105
"roots": [
104-
"<rootDir>/node_modules/"
106+
"<rootDir>/node_modules/",
107+
"<rootDir>/test/"
105108
],
106109
"collectCoverageFrom": [
107-
"lib/dist/**/*.js",
110+
"lib/src/**/*.js",
108111
"integration/**/*.js",
109112
"!lib/dist/index.js",
110113
"!lib/dist/Navigation.js",

test/index.android.spec.js

+68-74
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,20 @@
1-
'use strict';
2-
let expect = require('chai').use(require('sinon-chai')).expect;
3-
import proxyquire from 'proxyquire';
4-
import sinon from 'sinon';
5-
6-
describe('Notifications-Android > ', () => {
7-
proxyquire.noCallThru();
8-
1+
describe('Notifications-Android', () => {
92
let refreshTokenStub;
103
let getInitialNotificationStub;
114
let postLocalNotificationStub;
125
let cancelLocalNotificationStub;
136
let deviceEventEmitterListenerStub;
147
let libUnderTest;
8+
159
beforeEach(() => {
16-
refreshTokenStub = sinon.stub();
17-
getInitialNotificationStub = sinon.stub();
18-
postLocalNotificationStub = sinon.stub();
19-
cancelLocalNotificationStub = sinon.stub();
20-
deviceEventEmitterListenerStub = sinon.stub();
21-
22-
libUnderTest = proxyquire('../index.android', {
23-
'react-native': {
10+
refreshTokenStub = jest.fn();
11+
getInitialNotificationStub = jest.fn();
12+
postLocalNotificationStub = jest.fn();
13+
cancelLocalNotificationStub = jest.fn();
14+
deviceEventEmitterListenerStub = jest.fn();
15+
16+
jest.mock('react-native', () => {
17+
return {
2418
NativeModules: {
2519
WixRNNotifications: {
2620
refreshToken: refreshTokenStub,
@@ -32,162 +26,162 @@ describe('Notifications-Android > ', () => {
3226
DeviceEventEmitter: {
3327
addListener: deviceEventEmitterListenerStub
3428
}
35-
},
36-
'./notification': require('../notification.android')
29+
};
3730
});
31+
libUnderTest = require('../lib/src/index.android');
3832
});
3933

4034
describe('Registration token API', () => {
4135
it('should assign callback to native event upon listener registration', () => {
42-
expect(deviceEventEmitterListenerStub).to.not.have.been.called;
36+
expect(deviceEventEmitterListenerStub).toHaveBeenCalledTimes(0);
4337
const userListener = () => {};
44-
38+
console.log(libUnderTest);
4539
libUnderTest.NotificationsAndroid.setRegistrationTokenUpdateListener(userListener);
4640

47-
expect(deviceEventEmitterListenerStub).to.have.been.calledWith('remoteNotificationsRegistered', userListener);
48-
expect(deviceEventEmitterListenerStub).to.have.been.calledOnce;
41+
expect(deviceEventEmitterListenerStub).toHaveBeenCalledWith('remoteNotificationsRegistered', userListener);
42+
expect(deviceEventEmitterListenerStub).toHaveBeenCalledTimes(1);
4943
});
5044

5145
it('should clear native event listener upon listener deregister', () => {
52-
expect(deviceEventEmitterListenerStub).to.not.have.been.called;
46+
expect(deviceEventEmitterListenerStub).toHaveBeenCalledTimes(0);
5347
const userListener = () => {};
5448
const nativeListener = {
55-
remove: sinon.spy()
49+
remove: jest.fn()
5650
};
57-
deviceEventEmitterListenerStub.returns(nativeListener);
51+
deviceEventEmitterListenerStub.mockReturnValueOnce(nativeListener);
5852

5953
libUnderTest.NotificationsAndroid.setRegistrationTokenUpdateListener(userListener);
6054
libUnderTest.NotificationsAndroid.clearRegistrationTokenUpdateListener();
6155

62-
expect(nativeListener.remove).to.have.been.calledOnce;
56+
expect(nativeListener.remove).toHaveBeenCalledTimes(1);
6357
});
6458

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

68-
expect(deviceEventEmitterListenerStub).to.not.have.been.called;
62+
expect(deviceEventEmitterListenerStub).toHaveBeenCalledTimes(0);
6963
});
7064
});
7165

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

7771
libUnderTest.NotificationsAndroid.setNotificationOpenedListener(userListenerStub);
7872

79-
expect(deviceEventEmitterListenerStub).to.have.been.calledOnce;
80-
expect(deviceEventEmitterListenerStub).to.have.been.calledWith('notificationOpened', sinon.match.func);
73+
expect(deviceEventEmitterListenerStub).toHaveBeenCalledTimes(1);
74+
expect(deviceEventEmitterListenerStub).toHaveBeenCalledWith('notificationOpened', expect.any(Function));
8175
});
8276

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

8882
libUnderTest.NotificationsAndroid.setNotificationOpenedListener(userListenerStub);
8983

90-
expect(userListenerStub).to.not.have.been.called;
91-
deviceEventEmitterListenerStub.args[0][1](notification);
92-
expect(userListenerStub).to.have.been.calledOnce;
93-
expect(userListenerStub.args[0][0].getData()).to.equal(notification);
84+
expect(userListenerStub).toHaveBeenCalledTimes(0);
85+
deviceEventEmitterListenerStub.mock.calls[0][1](notification);
86+
expect(userListenerStub).toHaveBeenCalledTimes(1);
87+
expect(userListenerStub.mock.calls[0][0].getData()).toEqual(notification);
9488
});
9589

9690
it('should clear native event listener upon listener deregister', () => {
97-
expect(deviceEventEmitterListenerStub).to.not.have.been.called;
91+
expect(deviceEventEmitterListenerStub).toHaveBeenCalledTimes(0);
9892
const userListener = () => {};
9993
const nativeListener = {
100-
remove: sinon.spy()
94+
remove: jest.fn()
10195
};
102-
deviceEventEmitterListenerStub.returns(nativeListener);
96+
deviceEventEmitterListenerStub.mockReturnValueOnce(nativeListener);
10397

10498
libUnderTest.NotificationsAndroid.setNotificationOpenedListener(userListener);
10599
libUnderTest.NotificationsAndroid.clearNotificationOpenedListener();
106100

107-
expect(nativeListener.remove).to.have.been.calledOnce;
101+
expect(nativeListener.remove).toHaveBeenCalledTimes(1);
108102
});
109103

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

113-
expect(deviceEventEmitterListenerStub).to.not.have.been.called;
107+
expect(deviceEventEmitterListenerStub).toHaveBeenCalledTimes(0);
114108
});
115109
});
116110

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

122116
libUnderTest.NotificationsAndroid.setNotificationReceivedListener(userListenerStub);
123117

124-
expect(deviceEventEmitterListenerStub).to.have.been.calledOnce;
125-
expect(deviceEventEmitterListenerStub).to.have.been.calledWith('notificationReceived', sinon.match.func);
118+
expect(deviceEventEmitterListenerStub).toHaveBeenCalledTimes(1);
119+
expect(deviceEventEmitterListenerStub).toHaveBeenCalledWith('notificationReceived', expect.any(Function));
126120
});
127121

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

133127
libUnderTest.NotificationsAndroid.setNotificationReceivedListener(userListenerStub);
134128

135-
expect(userListenerStub).to.not.have.been.called;
136-
deviceEventEmitterListenerStub.args[0][1](notification);
137-
expect(userListenerStub).to.have.been.calledOnce;
138-
expect(userListenerStub.args[0][0].getData()).to.equal(notification);
129+
expect(userListenerStub).toHaveBeenCalledTimes(0);
130+
deviceEventEmitterListenerStub.mock.calls[0][1](notification);
131+
expect(userListenerStub).toHaveBeenCalledTimes(1);
132+
expect(userListenerStub.mock.calls[0][0].getData()).toEqual(notification);
139133
});
140134

141135
it('should clear native event listener upon listener deregister', () => {
142-
expect(deviceEventEmitterListenerStub).to.not.have.been.called;
136+
expect(deviceEventEmitterListenerStub).toHaveBeenCalledTimes(0);
143137
const userListener = () => {};
144138
const nativeListener = {
145-
remove: sinon.spy()
139+
remove: jest.fn()
146140
};
147-
deviceEventEmitterListenerStub.returns(nativeListener);
141+
deviceEventEmitterListenerStub.mockReturnValueOnce(nativeListener);
148142

149143
libUnderTest.NotificationsAndroid.setNotificationReceivedListener(userListener);
150144
libUnderTest.NotificationsAndroid.clearNotificationReceivedListener();
151145

152-
expect(nativeListener.remove).to.have.been.calledOnce;
146+
expect(nativeListener.remove).toHaveBeenCalledTimes(1);
153147
});
154148

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

158-
expect(deviceEventEmitterListenerStub).to.not.have.been.called;
152+
expect(deviceEventEmitterListenerStub).toHaveBeenCalledTimes(0);
159153
});
160154
});
161155

162156
describe('Notification token', () => {
163157
it('should refresh notification token upon refreshing request by the user', () => {
164-
expect(refreshTokenStub).to.not.have.been.called;
158+
expect(refreshTokenStub).toHaveBeenCalledTimes(0);
165159
libUnderTest.NotificationsAndroid.refreshToken();
166-
expect(refreshTokenStub).to.have.been.calledOnce;
160+
expect(refreshTokenStub).toHaveBeenCalledTimes(1);
167161
});
168162
});
169163

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

176170
libUnderTest.PendingNotifications.getInitialNotification()
177171
.then((notification) => {
178-
expect(notification.getData()).to.equal(rawNotification);
172+
expect(notification.getData()).toEqual(rawNotification);
179173
done();
180174
})
181175
.catch((err) => done(err));
182176
});
183177

184178
it('should return empty notification if not available', (done) => {
185-
expect(getInitialNotificationStub).to.not.have.been.called;
186-
getInitialNotificationStub.returns(Promise.resolve(null));
179+
expect(getInitialNotificationStub).toHaveBeenCalledTimes(0);
180+
getInitialNotificationStub.mockReturnValueOnce(Promise.resolve(null));
187181

188182
libUnderTest.PendingNotifications.getInitialNotification()
189183
.then((notification) => {
190-
expect(notification).to.be.undefined;
184+
expect(notification).toBeUndefined();
191185
done();
192186
})
193187
.catch((err) => done(err));
@@ -202,29 +196,29 @@ describe('Notifications-Android > ', () => {
202196
};
203197

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

207201
const id = libUnderTest.NotificationsAndroid.localNotification(notification);
208-
expect(id).to.not.be.undefined;
209-
expect(postLocalNotificationStub).to.have.been.calledWith(notification, id);
202+
expect(id).toBeDefined();
203+
expect(postLocalNotificationStub).toHaveBeenCalledWith(notification, id);
210204
});
211205

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

215209
const id = libUnderTest.NotificationsAndroid.localNotification(notification);
216210
const id2 = libUnderTest.NotificationsAndroid.localNotification(notification);
217-
expect(id).to.not.be.undefined;
218-
expect(id2).to.not.be.undefined;
219-
expect(id).to.not.equal(id2);
211+
expect(id).toBeDefined();
212+
expect(id2).toBeDefined();
213+
expect(id).not.toBe(id2);
220214
});
221215

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

225219
libUnderTest.NotificationsAndroid.cancelLocalNotification(666);
226220

227-
expect(cancelLocalNotificationStub).to.have.been.calledWith(666);
221+
expect(cancelLocalNotificationStub).toHaveBeenCalledWith(666);
228222
});
229223
});
230224
});

0 commit comments

Comments
 (0)