@@ -51,6 +51,18 @@ const events = require('events');
51
51
52
52
CoreManager . setLocalDatastore ( mockLocalDatastore ) ;
53
53
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
+
54
66
describe ( 'LiveQueryClient' , ( ) => {
55
67
beforeEach ( ( ) => {
56
68
mockLocalDatastore . isEnabled = false ;
@@ -152,6 +164,8 @@ describe('LiveQueryClient', () => {
152
164
} ) ;
153
165
// Add mock subscription
154
166
const subscription = new events . EventEmitter ( ) ;
167
+ subscription . subscribePromise = resolvingPromise ( ) ;
168
+
155
169
liveQueryClient . subscriptions . set ( 1 , subscription ) ;
156
170
const data = {
157
171
op : 'subscribed' ,
@@ -200,6 +214,39 @@ describe('LiveQueryClient', () => {
200
214
expect ( isChecked ) . toBe ( true ) ;
201
215
} ) ;
202
216
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
+
203
250
it ( 'can handle WebSocket event response message' , ( ) => {
204
251
const liveQueryClient = new LiveQueryClient ( {
205
252
applicationId : 'applicationId' ,
@@ -457,9 +504,13 @@ describe('LiveQueryClient', () => {
457
504
const query = new ParseQuery ( 'Test' ) ;
458
505
query . equalTo ( 'key' , 'value' ) ;
459
506
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 ;
461
512
liveQueryClient . connectPromise . resolve ( ) ;
462
- expect ( subscription ) . toBe ( liveQueryClient . subscriptions . get ( 1 ) ) ;
513
+ expect ( subscription ) . toBe ( clientSub ) ;
463
514
expect ( liveQueryClient . requestId ) . toBe ( 2 ) ;
464
515
await liveQueryClient . connectPromise ;
465
516
const messageStr = liveQueryClient . socket . send . mock . calls [ 0 ] [ 0 ] ;
0 commit comments