1+ jest . dontMock ( '../LiveQuerySubscription' ) ;
12jest . dontMock ( '../LiveQueryClient' ) ;
23jest . dontMock ( '../arrayContainsObject' ) ;
34jest . dontMock ( '../canBeSerialized' ) ;
@@ -23,7 +24,6 @@ jest.dontMock('../UniqueInstanceStateController');
2324jest . dontMock ( '../unsavedChildren' ) ;
2425jest . dontMock ( '../ParseACL' ) ;
2526jest . dontMock ( '../ParseQuery' ) ;
26- jest . dontMock ( '../LiveQuerySubscription' ) ;
2727jest . dontMock ( '../LocalDatastore' ) ;
2828jest . dontMock ( '../WebSocketController' ) ;
2929
@@ -38,6 +38,7 @@ jest.setMock('../LocalDatastore', mockLocalDatastore);
3838const CoreManager = require ( '../CoreManager' ) . default ;
3939const EventEmitter = require ( '../EventEmitter' ) . default ;
4040const LiveQueryClient = require ( '../LiveQueryClient' ) . default ;
41+ const LiveQuerySubscription = require ( '../LiveQuerySubscription' ) . default ;
4142const ParseObject = require ( '../ParseObject' ) . default ;
4243const ParseQuery = require ( '../ParseQuery' ) . default ;
4344const WebSocketController = require ( '../WebSocketController' ) . default ;
@@ -1091,4 +1092,94 @@ describe('LiveQueryClient', () => {
10911092 const subscription = liveQueryClient . subscribe ( ) ;
10921093 expect ( subscription ) . toBe ( undefined ) ;
10931094 } ) ;
1095+
1096+ it ( 'can handle WebSocket result response message' , ( ) => {
1097+ const liveQueryClient = new LiveQueryClient ( {
1098+ applicationId : 'applicationId' ,
1099+ serverURL : 'ws://test' ,
1100+ javascriptKey : 'javascriptKey' ,
1101+ masterKey : 'masterKey' ,
1102+ sessionToken : 'sessionToken' ,
1103+ } ) ;
1104+ // Add mock subscription
1105+ const subscription = new events . EventEmitter ( ) ;
1106+ liveQueryClient . subscriptions . set ( 1 , subscription ) ;
1107+ const object1 = new ParseObject ( 'Test' ) ;
1108+ object1 . set ( 'key' , 'value1' ) ;
1109+ const object2 = new ParseObject ( 'Test' ) ;
1110+ object2 . set ( 'key' , 'value2' ) ;
1111+ const data = {
1112+ op : 'result' ,
1113+ clientId : 1 ,
1114+ requestId : 1 ,
1115+ results : [ object1 . _toFullJSON ( ) , object2 . _toFullJSON ( ) ] ,
1116+ } ;
1117+ const event = {
1118+ data : JSON . stringify ( data ) ,
1119+ } ;
1120+ // Register checked in advance
1121+ let isChecked = false ;
1122+ subscription . on ( 'result' , function ( objects ) {
1123+ isChecked = true ;
1124+ expect ( objects . length ) . toBe ( 2 ) ;
1125+ expect ( objects [ 0 ] . get ( 'key' ) ) . toEqual ( 'value1' ) ;
1126+ expect ( objects [ 1 ] . get ( 'key' ) ) . toEqual ( 'value2' ) ;
1127+ } ) ;
1128+
1129+ liveQueryClient . _handleWebSocketMessage ( event ) ;
1130+
1131+ expect ( isChecked ) . toBe ( true ) ;
1132+ } ) ;
1133+
1134+ it ( 'LiveQuerySubscription class has find method' , ( ) => {
1135+ expect ( typeof LiveQuerySubscription . prototype . find ) . toBe ( 'function' ) ;
1136+ } ) ;
1137+
1138+ it ( 'subscription has find method' , ( ) => {
1139+ const liveQueryClient = new LiveQueryClient ( {
1140+ applicationId : 'applicationId' ,
1141+ serverURL : 'ws://test' ,
1142+ javascriptKey : 'javascriptKey' ,
1143+ masterKey : 'masterKey' ,
1144+ sessionToken : 'sessionToken' ,
1145+ } ) ;
1146+ const query = new ParseQuery ( 'Test' ) ;
1147+ query . equalTo ( 'key' , 'value' ) ;
1148+
1149+ const subscription = liveQueryClient . subscribe ( query ) ;
1150+
1151+ expect ( subscription ) . toBeInstanceOf ( LiveQuerySubscription ) ;
1152+ expect ( typeof subscription . find ) . toBe ( 'function' ) ;
1153+ } ) ;
1154+
1155+ it ( 'can send query message via subscription' , async ( ) => {
1156+ const liveQueryClient = new LiveQueryClient ( {
1157+ applicationId : 'applicationId' ,
1158+ serverURL : 'ws://test' ,
1159+ javascriptKey : 'javascriptKey' ,
1160+ masterKey : 'masterKey' ,
1161+ sessionToken : 'sessionToken' ,
1162+ } ) ;
1163+ liveQueryClient . socket = {
1164+ send : jest . fn ( ) ,
1165+ } ;
1166+ const query = new ParseQuery ( 'Test' ) ;
1167+ query . equalTo ( 'key' , 'value' ) ;
1168+
1169+ const subscription = liveQueryClient . subscribe ( query ) ;
1170+ liveQueryClient . connectPromise . resolve ( ) ;
1171+ await liveQueryClient . connectPromise ;
1172+
1173+ subscription . find ( ) ;
1174+
1175+ // Need to wait for the sendMessage promise to resolve
1176+ await Promise . resolve ( ) ;
1177+
1178+ const messageStr = liveQueryClient . socket . send . mock . calls [ 1 ] [ 0 ] ;
1179+ const message = JSON . parse ( messageStr ) ;
1180+ expect ( message ) . toEqual ( {
1181+ op : 'query' ,
1182+ requestId : 1 ,
1183+ } ) ;
1184+ } ) ;
10941185} ) ;
0 commit comments