11var GCM = require ( '../src/GCM' ) ;
22
3+ function mockSender ( gcm ) {
4+ return spyOn ( gcm . sender , 'send' ) . and . callFake ( function ( message , options , timeout , cb ) {
5+ /*{ "multicast_id":7680139367771848000,
6+ "success":0,
7+ "failure":4,
8+ "canonical_ids":0,
9+ "results":[ {"error":"InvalidRegistration"},
10+ {"error":"InvalidRegistration"},
11+ {"error":"InvalidRegistration"},
12+ {"error":"InvalidRegistration"}] }*/
13+
14+ let tokens = options . registrationTokens ;
15+ const response = {
16+ multicast_id : 7680139367771848000 ,
17+ success : tokens . length ,
18+ failure : 0 ,
19+ cannonical_ids : 0 ,
20+ results : tokens . map ( ( token , index ) => {
21+ return {
22+ message_id : 7680139367771848000 + '' + index ,
23+ registration_id : token
24+ }
25+ } )
26+ }
27+ process . nextTick ( ( ) => {
28+ cb ( null , response ) ;
29+ } ) ;
30+ } ) ;
31+ }
32+
333describe ( 'GCM' , ( ) => {
434 it ( 'can initialize' , ( done ) => {
535 var args = {
@@ -15,6 +45,16 @@ describe('GCM', () => {
1545 expect ( function ( ) {
1646 new GCM ( args ) ;
1747 } ) . toThrow ( ) ;
48+ args = {
49+ apisKey : 'apiKey'
50+ } ;
51+ expect ( function ( ) {
52+ new GCM ( args ) ;
53+ } ) . toThrow ( ) ;
54+ args = undefined ;
55+ expect ( function ( ) {
56+ new GCM ( args ) ;
57+ } ) . toThrow ( ) ;
1858 done ( ) ;
1959 } ) ;
2060
@@ -194,19 +234,77 @@ describe('GCM', () => {
194234 deviceToken : 'token4'
195235 }
196236 ] ;
197-
237+ mockSender ( gcm ) ;
198238 gcm . send ( data , devices ) . then ( ( response ) => {
199239 expect ( Array . isArray ( response ) ) . toBe ( true ) ;
200240 expect ( response . length ) . toEqual ( devices . length ) ;
201241 expect ( response . length ) . toEqual ( 4 ) ;
202242 response . forEach ( ( res , index ) => {
203- expect ( res . transmitted ) . toEqual ( false ) ;
243+ expect ( res . transmitted ) . toEqual ( true ) ;
204244 expect ( res . device ) . toEqual ( devices [ index ] ) ;
205245 } )
206246 done ( ) ;
207247 } )
208248 } ) ;
209249
250+ it ( 'can send GCM request with slices' , ( done ) => {
251+ let originalMax = GCM . GCMRegistrationTokensMax ;
252+ GCM . GCMRegistrationTokensMax = 2 ;
253+ var gcm = new GCM ( {
254+ apiKey : 'apiKey'
255+ } ) ;
256+ // Mock data
257+ var expirationTime = 2454538822113 ;
258+ var data = {
259+ 'expiration_time' : expirationTime ,
260+ 'data' : {
261+ 'alert' : 'alert'
262+ }
263+ }
264+ // Mock devices
265+ var devices = [
266+ {
267+ deviceToken : 'token'
268+ } ,
269+ {
270+ deviceToken : 'token2'
271+ } ,
272+ {
273+ deviceToken : 'token3'
274+ } ,
275+ {
276+ deviceToken : 'token4'
277+ } ,
278+ {
279+ deviceToken : 'token5'
280+ } ,
281+ {
282+ deviceToken : 'token6'
283+ } ,
284+ {
285+ deviceToken : 'token7'
286+ } ,
287+ {
288+ deviceToken : 'token8'
289+ }
290+ ] ;
291+ spyOn ( gcm , 'send' ) . and . callThrough ( ) ;
292+ gcm . send ( data , devices ) . then ( ( response ) => {
293+ expect ( Array . isArray ( response ) ) . toBe ( true ) ;
294+ expect ( response . length ) . toEqual ( devices . length ) ;
295+ expect ( response . length ) . toEqual ( 8 ) ;
296+ response . forEach ( ( res , index ) => {
297+ expect ( res . transmitted ) . toEqual ( false ) ;
298+ expect ( res . device ) . toEqual ( devices [ index ] ) ;
299+ } ) ;
300+ // 1 original call
301+ // 4 calls (1 per slice of 2)
302+ expect ( gcm . send . calls . count ( ) ) . toBe ( 1 + 4 ) ;
303+ GCM . GCMRegistrationTokensMax = originalMax ;
304+ done ( ) ;
305+ } )
306+ } ) ;
307+
210308 it ( 'can slice devices' , ( done ) => {
211309 // Mock devices
212310 var devices = [ makeDevice ( 1 ) , makeDevice ( 2 ) , makeDevice ( 3 ) , makeDevice ( 4 ) ] ;
0 commit comments