@@ -33,14 +33,29 @@ jest.dontMock('../unsavedChildren');
33
33
jest . dontMock ( '../ParseACL' ) ;
34
34
jest . dontMock ( '../ParseQuery' ) ;
35
35
jest . dontMock ( '../LiveQuerySubscription' ) ;
36
+ jest . dontMock ( '../LocalDatastore' ) ;
37
+
36
38
jest . useFakeTimers ( ) ;
37
39
40
+ const mockLocalDatastore = {
41
+ isEnabled : false ,
42
+ _updateObjectIfPinned : jest . fn ( ) ,
43
+ } ;
44
+ jest . setMock ( '../LocalDatastore' , mockLocalDatastore ) ;
45
+
46
+ const CoreManager = require ( '../CoreManager' ) ;
38
47
const LiveQueryClient = require ( '../LiveQueryClient' ) . default ;
39
48
const ParseObject = require ( '../ParseObject' ) . default ;
40
49
const ParseQuery = require ( '../ParseQuery' ) . default ;
41
50
const events = require ( 'events' ) ;
42
51
52
+ CoreManager . setLocalDatastore ( mockLocalDatastore ) ;
53
+
43
54
describe ( 'LiveQueryClient' , ( ) => {
55
+ beforeEach ( ( ) => {
56
+ mockLocalDatastore . isEnabled = false ;
57
+ } ) ;
58
+
44
59
it ( 'can connect to server' , ( ) => {
45
60
const liveQueryClient = new LiveQueryClient ( {
46
61
applicationId : 'applicationId' ,
@@ -264,6 +279,63 @@ describe('LiveQueryClient', () => {
264
279
expect ( isChecked ) . toBe ( true ) ;
265
280
} ) ;
266
281
282
+ it ( 'can handle WebSocket response override data on update' , ( ) => {
283
+ const liveQueryClient = new LiveQueryClient ( {
284
+ applicationId : 'applicationId' ,
285
+ serverURL : 'ws://test' ,
286
+ javascriptKey : 'javascriptKey' ,
287
+ masterKey : 'masterKey' ,
288
+ sessionToken : 'sessionToken'
289
+ } ) ;
290
+ // Add mock subscription
291
+ const subscription = new events . EventEmitter ( ) ;
292
+ liveQueryClient . subscriptions . set ( 1 , subscription ) ;
293
+ const object = new ParseObject ( 'Test' ) ;
294
+ const original = new ParseObject ( 'Test' ) ;
295
+ object . set ( 'key' , 'value' ) ;
296
+ original . set ( 'key' , 'old' ) ;
297
+ const data = {
298
+ op : 'update' ,
299
+ clientId : 1 ,
300
+ requestId : 1 ,
301
+ object : object . _toFullJSON ( ) ,
302
+ original : original . _toFullJSON ( ) ,
303
+ } ;
304
+ const event = {
305
+ data : JSON . stringify ( data )
306
+ }
307
+
308
+ jest . spyOn (
309
+ mockLocalDatastore ,
310
+ '_updateObjectIfPinned'
311
+ )
312
+ . mockImplementationOnce ( ( ) => Promise . resolve ( ) ) ;
313
+
314
+ const spy = jest . spyOn (
315
+ ParseObject ,
316
+ 'fromJSON'
317
+ )
318
+ . mockImplementationOnce ( ( ) => original )
319
+ . mockImplementationOnce ( ( ) => object ) ;
320
+
321
+
322
+ mockLocalDatastore . isEnabled = true ;
323
+
324
+ let isChecked = false ;
325
+ subscription . on ( 'update' , ( ) => {
326
+ isChecked = true ;
327
+ } ) ;
328
+
329
+ liveQueryClient . _handleWebSocketMessage ( event ) ;
330
+ const override = true ;
331
+
332
+ expect ( ParseObject . fromJSON . mock . calls [ 1 ] [ 1 ] ) . toEqual ( override ) ;
333
+ expect ( mockLocalDatastore . _updateObjectIfPinned ) . toHaveBeenCalledTimes ( 1 ) ;
334
+
335
+ expect ( isChecked ) . toBe ( true ) ;
336
+ spy . mockRestore ( ) ;
337
+ } ) ;
338
+
267
339
it ( 'can handle WebSocket response unset field' , async ( ) => {
268
340
const liveQueryClient = new LiveQueryClient ( {
269
341
applicationId : 'applicationId' ,
0 commit comments