Skip to content

Commit ef2b483

Browse files
committed
Cleaning up ParseLiveQuery, adding some test coverage
1 parent 8a3d011 commit ef2b483

File tree

2 files changed

+140
-23
lines changed

2 files changed

+140
-23
lines changed

src/ParseLiveQuery.js

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,26 @@
1+
/**
2+
* Copyright (c) 2015-present, Parse, LLC.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*
9+
* @flow
10+
*/
11+
112
import EventEmitter from './EventEmitter';
213
import LiveQueryClient from './LiveQueryClient';
314
import CoreManager from './CoreManager';
415
import ParsePromise from './ParsePromise';
516

617
function open() {
7-
var LiveQueryController = CoreManager.getLiveQueryController();
18+
const LiveQueryController = CoreManager.getLiveQueryController();
819
LiveQueryController.open();
920
}
1021

1122
function close() {
12-
var LiveQueryController = CoreManager.getLiveQueryController();
23+
const LiveQueryController = CoreManager.getLiveQueryController();
1324
LiveQueryController.close();
1425
}
1526

@@ -69,23 +80,19 @@ LiveQuery.on('error', () => {
6980

7081
export default LiveQuery;
7182

72-
let getSessionToken = () => {
73-
let promiseUser = CoreManager.getUserController().currentUserAsync();
74-
return promiseUser.then((currentUser) => {
75-
return ParsePromise.as(currentUser ? currentUser.sessionToken : undefined);
76-
}).then((sessionToken) => {
77-
return ParsePromise.as(sessionToken);
83+
function getSessionToken() {
84+
const controller = CoreManager.getUserController();
85+
return controller.currentUserAsync().then((currentUser) => {
86+
return currentUser ? currentUser.getSessionToken() : undefined;
7887
});
79-
};
88+
}
8089

81-
let getLiveQueryClient = () => {
82-
return CoreManager.getLiveQueryController().getDefaultLiveQueryClient().then((defaultLiveQueryClient) => {
83-
return ParsePromise.as(defaultLiveQueryClient);
84-
});
85-
};
90+
function getLiveQueryClient() {
91+
return CoreManager.getLiveQueryController().getDefaultLiveQueryClient();
92+
}
8693

8794
let defaultLiveQueryClient;
88-
let DefaultLiveQueryController = {
95+
const DefaultLiveQueryController = {
8996
setDefaultLiveQueryClient(liveQueryClient: any) {
9097
defaultLiveQueryClient = liveQueryClient;
9198
},
@@ -94,12 +101,13 @@ let DefaultLiveQueryController = {
94101
return ParsePromise.as(defaultLiveQueryClient);
95102
}
96103

97-
let sessionTokenPromise = getSessionToken();
98-
return sessionTokenPromise.then((sessionToken) => {
104+
return getSessionToken().then((sessionToken) => {
99105
let liveQueryServerURL = CoreManager.get('LIVEQUERY_SERVER_URL');
100106

101107
if (liveQueryServerURL && liveQueryServerURL.indexOf('ws') !== 0) {
102-
throw new Error('You need to set a proper Parse LiveQuery server url before using LiveQueryClient');
108+
throw new Error(
109+
'You need to set a proper Parse LiveQuery server url before using LiveQueryClient'
110+
);
103111
}
104112

105113
// If we can not find Parse.liveQueryServerURL, we try to extract it from Parse.serverURL
@@ -115,9 +123,9 @@ let DefaultLiveQueryController = {
115123
CoreManager.set('LIVEQUERY_SERVER_URL', liveQueryServerURL);
116124
}
117125

118-
let applicationId = CoreManager.get('APPLICATION_ID');
119-
let javascriptKey = CoreManager.get('JAVASCRIPT_KEY');
120-
let masterKey = CoreManager.get('MASTER_KEY');
126+
const applicationId = CoreManager.get('APPLICATION_ID');
127+
const javascriptKey = CoreManager.get('JAVASCRIPT_KEY');
128+
const masterKey = CoreManager.get('MASTER_KEY');
121129
// Get currentUser sessionToken if possible
122130
defaultLiveQueryClient = new LiveQueryClient({
123131
applicationId,
@@ -138,7 +146,7 @@ let DefaultLiveQueryController = {
138146
LiveQuery.emit('close');
139147
});
140148

141-
return ParsePromise.as(defaultLiveQueryClient);
149+
return defaultLiveQueryClient;
142150
});
143151
},
144152
open() {
@@ -198,7 +206,10 @@ let DefaultLiveQueryController = {
198206
getLiveQueryClient().then((liveQueryClient) => {
199207
this.resolve(liveQueryClient.unsubscribe(subscription));
200208
});
201-
}
209+
},
210+
_clearCachedDefaultClient() {
211+
defaultLiveQueryClient = null;
212+
},
202213
};
203214

204215
CoreManager.setLiveQueryController(DefaultLiveQueryController);

src/__tests__/ParseLiveQuery-test.js

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/**
2+
* Copyright (c) 2015-present, Parse, LLC.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
10+
jest.dontMock('../ParseLiveQuery');
11+
jest.dontMock('../CoreManager');
12+
jest.dontMock('../ParsePromise');
13+
jest.dontMock('../LiveQueryClient');
14+
jest.dontMock('../ParseObject');
15+
16+
jest.dontMock('./test_helpers/asyncHelper');
17+
18+
const ParseLiveQuery = require('../ParseLiveQuery');
19+
const CoreManager = require('../CoreManager');
20+
const ParsePromise = require('../ParsePromise');
21+
22+
const asyncHelper = require('./test_helpers/asyncHelper');
23+
24+
describe('ParseLiveQuery', () => {
25+
beforeEach(() => {
26+
const controller = CoreManager.getLiveQueryController();
27+
controller._clearCachedDefaultClient();
28+
});
29+
30+
it('fails with an invalid livequery server url', asyncHelper((done) => {
31+
CoreManager.set('UserController', {
32+
currentUserAsync() {
33+
return ParsePromise.as(undefined);
34+
}
35+
});
36+
CoreManager.set('LIVEQUERY_SERVER_URL', 'notaurl');
37+
const controller = CoreManager.getLiveQueryController();
38+
controller.getDefaultLiveQueryClient().fail((err) => {
39+
expect(err.message).toBe(
40+
'You need to set a proper Parse LiveQuery server url before using LiveQueryClient'
41+
);
42+
done();
43+
});
44+
}));
45+
46+
it('initializes the client', asyncHelper((done) => {
47+
CoreManager.set('UserController', {
48+
currentUserAsync() {
49+
return ParsePromise.as(undefined);
50+
}
51+
});
52+
CoreManager.set('APPLICATION_ID', 'appid');
53+
CoreManager.set('JAVASCRIPT_KEY', 'jskey');
54+
CoreManager.set('LIVEQUERY_SERVER_URL', 'wss://live.example.com/parse');
55+
const controller = CoreManager.getLiveQueryController();
56+
controller.getDefaultLiveQueryClient().then((client) => {
57+
expect(client.serverURL).toBe('wss://live.example.com/parse');
58+
expect(client.applicationId).toBe('appid');
59+
expect(client.javascriptKey).toBe('jskey');
60+
expect(client.sessionToken).toBe(undefined);
61+
done();
62+
});
63+
}));
64+
65+
it('automatically generates a websocket url', asyncHelper((done) => {
66+
CoreManager.set('UserController', {
67+
currentUserAsync() {
68+
return ParsePromise.as(undefined);
69+
}
70+
});
71+
CoreManager.set('APPLICATION_ID', 'appid');
72+
CoreManager.set('JAVASCRIPT_KEY', 'jskey');
73+
CoreManager.set('LIVEQUERY_SERVER_URL', null);
74+
const controller = CoreManager.getLiveQueryController();
75+
controller.getDefaultLiveQueryClient().then((client) => {
76+
expect(client.serverURL).toBe('wss://api.parse.com/1');
77+
expect(client.applicationId).toBe('appid');
78+
expect(client.javascriptKey).toBe('jskey');
79+
expect(client.sessionToken).toBe(undefined);
80+
done();
81+
});
82+
}));
83+
84+
it('populates the session token', asyncHelper((done) => {
85+
CoreManager.set('UserController', {
86+
currentUserAsync() {
87+
return ParsePromise.as({
88+
getSessionToken() {
89+
return 'token';
90+
}
91+
});
92+
}
93+
});
94+
CoreManager.set('APPLICATION_ID', 'appid');
95+
CoreManager.set('JAVASCRIPT_KEY', 'jskey');
96+
CoreManager.set('LIVEQUERY_SERVER_URL', null);
97+
const controller = CoreManager.getLiveQueryController();
98+
controller.getDefaultLiveQueryClient().then((client) => {
99+
expect(client.serverURL).toBe('wss://api.parse.com/1');
100+
expect(client.applicationId).toBe('appid');
101+
expect(client.javascriptKey).toBe('jskey');
102+
expect(client.sessionToken).toBe('token');
103+
done();
104+
});
105+
}));
106+
});

0 commit comments

Comments
 (0)