@@ -33,14 +33,29 @@ jest.dontMock('../unsavedChildren');
3333jest . dontMock ( '../ParseACL' ) ;
3434jest . dontMock ( '../ParseQuery' ) ;
3535jest . dontMock ( '../LiveQuerySubscription' ) ;
36+ jest . dontMock ( '../LocalDatastore' ) ;
37+
3638jest . useFakeTimers ( ) ;
3739
40+ const mockLocalDatastore = {
41+ isEnabled : false ,
42+ _updateObjectIfPinned : jest . fn ( ) ,
43+ } ;
44+ jest . setMock ( '../LocalDatastore' , mockLocalDatastore ) ;
45+
46+ const CoreManager = require ( '../CoreManager' ) ;
3847const LiveQueryClient = require ( '../LiveQueryClient' ) . default ;
3948const ParseObject = require ( '../ParseObject' ) . default ;
4049const ParseQuery = require ( '../ParseQuery' ) . default ;
4150const events = require ( 'events' ) ;
4251
52+ CoreManager . setLocalDatastore ( mockLocalDatastore ) ;
53+
4354describe ( 'LiveQueryClient' , ( ) => {
55+ beforeEach ( ( ) => {
56+ mockLocalDatastore . isEnabled = false ;
57+ } ) ;
58+
4459 it ( 'can connect to server' , ( ) => {
4560 const liveQueryClient = new LiveQueryClient ( {
4661 applicationId : 'applicationId' ,
@@ -264,6 +279,63 @@ describe('LiveQueryClient', () => {
264279 expect ( isChecked ) . toBe ( true ) ;
265280 } ) ;
266281
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+
267339 it ( 'can handle WebSocket response unset field' , async ( ) => {
268340 const liveQueryClient = new LiveQueryClient ( {
269341 applicationId : 'applicationId' ,
0 commit comments