@@ -51,6 +51,18 @@ const events = require('events');
5151
5252CoreManager . setLocalDatastore ( mockLocalDatastore ) ;
5353
54+ function resolvingPromise ( ) {
55+ let res ;
56+ let rej ;
57+ const promise = new Promise ( ( resolve , reject ) => {
58+ res = resolve ;
59+ rej = reject ;
60+ } ) ;
61+ promise . resolve = res ;
62+ promise . reject = rej ;
63+ return promise ;
64+ }
65+
5466describe ( 'LiveQueryClient' , ( ) => {
5567 beforeEach ( ( ) => {
5668 mockLocalDatastore . isEnabled = false ;
@@ -152,6 +164,8 @@ describe('LiveQueryClient', () => {
152164 } ) ;
153165 // Add mock subscription
154166 const subscription = new events . EventEmitter ( ) ;
167+ subscription . subscribePromise = resolvingPromise ( ) ;
168+
155169 liveQueryClient . subscriptions . set ( 1 , subscription ) ;
156170 const data = {
157171 op : 'subscribed' ,
@@ -200,6 +214,39 @@ describe('LiveQueryClient', () => {
200214 expect ( isChecked ) . toBe ( true ) ;
201215 } ) ;
202216
217+ it ( 'can handle WebSocket error while subscribing' , ( ) => {
218+ const liveQueryClient = new LiveQueryClient ( {
219+ applicationId : 'applicationId' ,
220+ serverURL : 'ws://test' ,
221+ javascriptKey : 'javascriptKey' ,
222+ masterKey : 'masterKey' ,
223+ sessionToken : 'sessionToken'
224+ } ) ;
225+ const subscription = new events . EventEmitter ( ) ;
226+ subscription . subscribePromise = resolvingPromise ( ) ;
227+ liveQueryClient . subscriptions . set ( 1 , subscription ) ;
228+
229+ const data = {
230+ op : 'error' ,
231+ clientId : 1 ,
232+ requestId : 1 ,
233+ error : 'error thrown'
234+ } ;
235+ const event = {
236+ data : JSON . stringify ( data )
237+ }
238+ // Register checked in advance
239+ let isChecked = false ;
240+ subscription . on ( 'error' , function ( error ) {
241+ isChecked = true ;
242+ expect ( error ) . toEqual ( 'error thrown' ) ;
243+ } ) ;
244+
245+ liveQueryClient . _handleWebSocketMessage ( event ) ;
246+
247+ expect ( isChecked ) . toBe ( true ) ;
248+ } ) ;
249+
203250 it ( 'can handle WebSocket event response message' , ( ) => {
204251 const liveQueryClient = new LiveQueryClient ( {
205252 applicationId : 'applicationId' ,
@@ -457,9 +504,13 @@ describe('LiveQueryClient', () => {
457504 const query = new ParseQuery ( 'Test' ) ;
458505 query . equalTo ( 'key' , 'value' ) ;
459506
460- const subscription = liveQueryClient . subscribe ( query ) ;
507+ const subscribePromise = liveQueryClient . subscribe ( query ) ;
508+ const clientSub = liveQueryClient . subscriptions . get ( 1 ) ;
509+ clientSub . subscribePromise . resolve ( ) ;
510+
511+ const subscription = await subscribePromise ;
461512 liveQueryClient . connectPromise . resolve ( ) ;
462- expect ( subscription ) . toBe ( liveQueryClient . subscriptions . get ( 1 ) ) ;
513+ expect ( subscription ) . toBe ( clientSub ) ;
463514 expect ( liveQueryClient . requestId ) . toBe ( 2 ) ;
464515 await liveQueryClient . connectPromise ;
465516 const messageStr = liveQueryClient . socket . send . mock . calls [ 0 ] [ 0 ] ;
0 commit comments