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' , ( ) => {
9
2
let refreshTokenStub ;
10
3
let getInitialNotificationStub ;
11
4
let postLocalNotificationStub ;
12
5
let cancelLocalNotificationStub ;
13
6
let deviceEventEmitterListenerStub ;
14
7
let libUnderTest ;
8
+
15
9
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 {
24
18
NativeModules : {
25
19
WixRNNotifications : {
26
20
refreshToken : refreshTokenStub ,
@@ -32,162 +26,162 @@ describe('Notifications-Android > ', () => {
32
26
DeviceEventEmitter : {
33
27
addListener : deviceEventEmitterListenerStub
34
28
}
35
- } ,
36
- './notification' : require ( '../notification.android' )
29
+ } ;
37
30
} ) ;
31
+ libUnderTest = require ( '../lib/src/index.android' ) ;
38
32
} ) ;
39
33
40
34
describe ( 'Registration token API' , ( ) => {
41
35
it ( 'should assign callback to native event upon listener registration' , ( ) => {
42
- expect ( deviceEventEmitterListenerStub ) . to . not . have . been . called ;
36
+ expect ( deviceEventEmitterListenerStub ) . toHaveBeenCalledTimes ( 0 ) ;
43
37
const userListener = ( ) => { } ;
44
-
38
+ console . log ( libUnderTest ) ;
45
39
libUnderTest . NotificationsAndroid . setRegistrationTokenUpdateListener ( userListener ) ;
46
40
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 ) ;
49
43
} ) ;
50
44
51
45
it ( 'should clear native event listener upon listener deregister' , ( ) => {
52
- expect ( deviceEventEmitterListenerStub ) . to . not . have . been . called ;
46
+ expect ( deviceEventEmitterListenerStub ) . toHaveBeenCalledTimes ( 0 ) ;
53
47
const userListener = ( ) => { } ;
54
48
const nativeListener = {
55
- remove : sinon . spy ( )
49
+ remove : jest . fn ( )
56
50
} ;
57
- deviceEventEmitterListenerStub . returns ( nativeListener ) ;
51
+ deviceEventEmitterListenerStub . mockReturnValueOnce ( nativeListener ) ;
58
52
59
53
libUnderTest . NotificationsAndroid . setRegistrationTokenUpdateListener ( userListener ) ;
60
54
libUnderTest . NotificationsAndroid . clearRegistrationTokenUpdateListener ( ) ;
61
55
62
- expect ( nativeListener . remove ) . to . have . been . calledOnce ;
56
+ expect ( nativeListener . remove ) . toHaveBeenCalledTimes ( 1 ) ;
63
57
} ) ;
64
58
65
59
it ( 'shouldn`t fail if deregister without registering' , ( ) => {
66
60
libUnderTest . NotificationsAndroid . clearRegistrationTokenUpdateListener ( ) ;
67
61
68
- expect ( deviceEventEmitterListenerStub ) . to . not . have . been . called ;
62
+ expect ( deviceEventEmitterListenerStub ) . toHaveBeenCalledTimes ( 0 ) ;
69
63
} ) ;
70
64
} ) ;
71
65
72
66
describe ( 'notification-opening API' , ( ) => {
73
67
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 ( ) ;
76
70
77
71
libUnderTest . NotificationsAndroid . setNotificationOpenedListener ( userListenerStub ) ;
78
72
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 ) ) ;
81
75
} ) ;
82
76
83
77
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 ( ) ;
86
80
const notification = { foo : 'bar' } ;
87
81
88
82
libUnderTest . NotificationsAndroid . setNotificationOpenedListener ( userListenerStub ) ;
89
83
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 ) ;
94
88
} ) ;
95
89
96
90
it ( 'should clear native event listener upon listener deregister' , ( ) => {
97
- expect ( deviceEventEmitterListenerStub ) . to . not . have . been . called ;
91
+ expect ( deviceEventEmitterListenerStub ) . toHaveBeenCalledTimes ( 0 ) ;
98
92
const userListener = ( ) => { } ;
99
93
const nativeListener = {
100
- remove : sinon . spy ( )
94
+ remove : jest . fn ( )
101
95
} ;
102
- deviceEventEmitterListenerStub . returns ( nativeListener ) ;
96
+ deviceEventEmitterListenerStub . mockReturnValueOnce ( nativeListener ) ;
103
97
104
98
libUnderTest . NotificationsAndroid . setNotificationOpenedListener ( userListener ) ;
105
99
libUnderTest . NotificationsAndroid . clearNotificationOpenedListener ( ) ;
106
100
107
- expect ( nativeListener . remove ) . to . have . been . calledOnce ;
101
+ expect ( nativeListener . remove ) . toHaveBeenCalledTimes ( 1 ) ;
108
102
} ) ;
109
103
110
104
it ( 'shouldnt fail if deregister without registering' , ( ) => {
111
105
libUnderTest . NotificationsAndroid . clearNotificationOpenedListener ( ) ;
112
106
113
- expect ( deviceEventEmitterListenerStub ) . to . not . have . been . called ;
107
+ expect ( deviceEventEmitterListenerStub ) . toHaveBeenCalledTimes ( 0 ) ;
114
108
} ) ;
115
109
} ) ;
116
110
117
111
describe ( 'notification-receive API' , ( ) => {
118
112
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 ( ) ;
121
115
122
116
libUnderTest . NotificationsAndroid . setNotificationReceivedListener ( userListenerStub ) ;
123
117
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 ) ) ;
126
120
} ) ;
127
121
128
122
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 ( ) ;
131
125
const notification = { foo : 'bar' } ;
132
126
133
127
libUnderTest . NotificationsAndroid . setNotificationReceivedListener ( userListenerStub ) ;
134
128
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 ) ;
139
133
} ) ;
140
134
141
135
it ( 'should clear native event listener upon listener deregister' , ( ) => {
142
- expect ( deviceEventEmitterListenerStub ) . to . not . have . been . called ;
136
+ expect ( deviceEventEmitterListenerStub ) . toHaveBeenCalledTimes ( 0 ) ;
143
137
const userListener = ( ) => { } ;
144
138
const nativeListener = {
145
- remove : sinon . spy ( )
139
+ remove : jest . fn ( )
146
140
} ;
147
- deviceEventEmitterListenerStub . returns ( nativeListener ) ;
141
+ deviceEventEmitterListenerStub . mockReturnValueOnce ( nativeListener ) ;
148
142
149
143
libUnderTest . NotificationsAndroid . setNotificationReceivedListener ( userListener ) ;
150
144
libUnderTest . NotificationsAndroid . clearNotificationReceivedListener ( ) ;
151
145
152
- expect ( nativeListener . remove ) . to . have . been . calledOnce ;
146
+ expect ( nativeListener . remove ) . toHaveBeenCalledTimes ( 1 ) ;
153
147
} ) ;
154
148
155
149
it ( 'shouldn`t fail if deregister without registering' , ( ) => {
156
150
libUnderTest . NotificationsAndroid . clearNotificationReceivedListener ( ) ;
157
151
158
- expect ( deviceEventEmitterListenerStub ) . to . not . have . been . called ;
152
+ expect ( deviceEventEmitterListenerStub ) . toHaveBeenCalledTimes ( 0 ) ;
159
153
} ) ;
160
154
} ) ;
161
155
162
156
describe ( 'Notification token' , ( ) => {
163
157
it ( 'should refresh notification token upon refreshing request by the user' , ( ) => {
164
- expect ( refreshTokenStub ) . to . not . have . been . called ;
158
+ expect ( refreshTokenStub ) . toHaveBeenCalledTimes ( 0 ) ;
165
159
libUnderTest . NotificationsAndroid . refreshToken ( ) ;
166
- expect ( refreshTokenStub ) . to . have . been . calledOnce ;
160
+ expect ( refreshTokenStub ) . toHaveBeenCalledTimes ( 1 ) ;
167
161
} ) ;
168
162
} ) ;
169
163
170
164
describe ( 'Initial notification API' , ( ) => {
171
165
it ( 'should return initial notification data if available' , ( done ) => {
172
- expect ( getInitialNotificationStub ) . to . not . have . been . called ;
166
+ expect ( getInitialNotificationStub ) . toHaveBeenCalledTimes ( 0 ) ;
173
167
const rawNotification = { foo : 'bar' } ;
174
- getInitialNotificationStub . returns ( Promise . resolve ( rawNotification ) ) ;
168
+ getInitialNotificationStub . mockReturnValueOnce ( Promise . resolve ( rawNotification ) ) ;
175
169
176
170
libUnderTest . PendingNotifications . getInitialNotification ( )
177
171
. then ( ( notification ) => {
178
- expect ( notification . getData ( ) ) . to . equal ( rawNotification ) ;
172
+ expect ( notification . getData ( ) ) . toEqual ( rawNotification ) ;
179
173
done ( ) ;
180
174
} )
181
175
. catch ( ( err ) => done ( err ) ) ;
182
176
} ) ;
183
177
184
178
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 ) ) ;
187
181
188
182
libUnderTest . PendingNotifications . getInitialNotification ( )
189
183
. then ( ( notification ) => {
190
- expect ( notification ) . to . be . undefined ;
184
+ expect ( notification ) . toBeUndefined ( ) ;
191
185
done ( ) ;
192
186
} )
193
187
. catch ( ( err ) => done ( err ) ) ;
@@ -202,29 +196,29 @@ describe('Notifications-Android > ', () => {
202
196
} ;
203
197
204
198
it ( 'should get published when posted manually' , ( ) => {
205
- expect ( postLocalNotificationStub ) . to . not . have . been . called ;
199
+ expect ( postLocalNotificationStub ) . toHaveBeenCalledTimes ( 0 ) ;
206
200
207
201
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 ) ;
210
204
} ) ;
211
205
212
206
it ( 'should be called with a unique ID' , ( ) => {
213
- expect ( postLocalNotificationStub ) . to . not . have . been . called ;
207
+ expect ( postLocalNotificationStub ) . toHaveBeenCalledTimes ( 0 ) ;
214
208
215
209
const id = libUnderTest . NotificationsAndroid . localNotification ( notification ) ;
216
210
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 ) ;
220
214
} ) ;
221
215
222
216
it ( 'should be cancellable with an ID' , ( ) => {
223
- expect ( cancelLocalNotificationStub ) . to . not . have . been . called ;
217
+ expect ( cancelLocalNotificationStub ) . toHaveBeenCalledTimes ( 0 ) ;
224
218
225
219
libUnderTest . NotificationsAndroid . cancelLocalNotification ( 666 ) ;
226
220
227
- expect ( cancelLocalNotificationStub ) . to . have . been . calledWith ( 666 ) ;
221
+ expect ( cancelLocalNotificationStub ) . toHaveBeenCalledWith ( 666 ) ;
228
222
} ) ;
229
223
} ) ;
230
224
} ) ;
0 commit comments