Skip to content

Commit b0aa375

Browse files
committed
increase test coverage
1 parent 3d950ab commit b0aa375

File tree

2 files changed

+63
-12
lines changed

2 files changed

+63
-12
lines changed

src/LiveQueryClient.ts

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ const SUBSCRIPTION_EMMITER_TYPES = {
5959
};
6060

6161
// Exponentially-growing random delay
62-
const generateInterval = k => {
62+
const generateInterval = (k: number): number => {
6363
return Math.random() * Math.min(30, Math.pow(2, k) - 1) * 1000;
6464
};
6565

@@ -126,13 +126,15 @@ class LiveQueryClient {
126126
emit: any;
127127

128128
/**
129-
* @param {object} options
130-
* @param {string} options.applicationId - applicationId of your Parse app
131-
* @param {string} options.serverURL - <b>the URL of your LiveQuery server</b>
132-
* @param {string} options.javascriptKey (optional)
133-
* @param {string} options.masterKey (optional) Your Parse Master Key. (Node.js only!)
134-
* @param {string} options.sessionToken (optional)
135-
* @param {string} options.installationId (optional)
129+
* Creates a new LiveQueryClient instance.
130+
*
131+
* @param options - Configuration options for the LiveQuery client
132+
* @param options.applicationId - The applicationId of your Parse app
133+
* @param options.serverURL - The URL of your LiveQuery server (must start with 'ws' or 'wss')
134+
* @param options.javascriptKey - (Optional) The JavaScript key for your Parse app
135+
* @param options.masterKey - (Optional) Your Parse Master Key (Node.js only!)
136+
* @param options.sessionToken - (Optional) Session token for authenticated requests
137+
* @param options.installationId - (Optional) Installation ID for the client
136138
*/
137139
constructor({
138140
applicationId,
@@ -141,6 +143,13 @@ class LiveQueryClient {
141143
masterKey,
142144
sessionToken,
143145
installationId,
146+
}: {
147+
applicationId: string;
148+
serverURL: string;
149+
javascriptKey?: string;
150+
masterKey?: string;
151+
sessionToken?: string;
152+
installationId?: string;
144153
}) {
145154
if (!serverURL || serverURL.indexOf('ws') !== 0) {
146155
throw new Error(
@@ -165,8 +174,8 @@ class LiveQueryClient {
165174
const EventEmitter = CoreManager.getEventEmitter();
166175
this.emitter = new EventEmitter();
167176

168-
this.on = (eventName, listener) => this.emitter.on(eventName, listener);
169-
this.emit = (eventName, ...args) => this.emitter.emit(eventName, ...args);
177+
this.on = (eventName: string, listener: (...args: unknown[]) => void) => this.emitter.on(eventName, listener);
178+
this.emit = (eventName: string, ...args: unknown[]) => this.emitter.emit(eventName, ...args);
170179
// adding listener so process does not crash
171180
// best practice is for developer to register their own listener
172181
this.on('error', () => {});
@@ -222,7 +231,7 @@ class LiveQueryClient {
222231
.then(() => {
223232
this.socket.send(JSON.stringify(subscribeRequest));
224233
})
225-
.catch(error => {
234+
.catch((error: Error) => {
226235
subscription.subscribePromise.reject(error);
227236
});
228237

@@ -430,7 +439,7 @@ class LiveQueryClient {
430439
}
431440
case OP_EVENTS.RESULT: {
432441
if (subscription) {
433-
const objects = data.results.map(json => {
442+
const objects = data.results.map((json: Record<string, unknown>) => {
434443
if (!json.className && subscription.query) {
435444
json.className = subscription.query.className;
436445
}

src/__tests__/LiveQueryClient-test.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,6 +1131,48 @@ describe('LiveQueryClient', () => {
11311131
expect(isChecked).toBe(true);
11321132
});
11331133

1134+
it('can handle WebSocket result response message with missing className', () => {
1135+
const liveQueryClient = new LiveQueryClient({
1136+
applicationId: 'applicationId',
1137+
serverURL: 'ws://test',
1138+
javascriptKey: 'javascriptKey',
1139+
masterKey: 'masterKey',
1140+
sessionToken: 'sessionToken',
1141+
});
1142+
// Add mock subscription with query
1143+
const subscription = new events.EventEmitter();
1144+
const query = new ParseQuery('TestClass');
1145+
subscription.query = query;
1146+
liveQueryClient.subscriptions.set(1, subscription);
1147+
1148+
// Create results without className property
1149+
const data = {
1150+
op: 'result',
1151+
clientId: 1,
1152+
requestId: 1,
1153+
results: [
1154+
{ objectId: 'obj1', key: 'value1' },
1155+
{ objectId: 'obj2', key: 'value2' },
1156+
],
1157+
};
1158+
const event = {
1159+
data: JSON.stringify(data),
1160+
};
1161+
1162+
// Register checked in advance
1163+
let isChecked = false;
1164+
subscription.on('result', function (objects) {
1165+
isChecked = true;
1166+
expect(objects.length).toBe(2);
1167+
expect(objects[0].className).toEqual('TestClass');
1168+
expect(objects[1].className).toEqual('TestClass');
1169+
});
1170+
1171+
liveQueryClient._handleWebSocketMessage(event);
1172+
1173+
expect(isChecked).toBe(true);
1174+
});
1175+
11341176
it('LiveQuerySubscription class has find method', () => {
11351177
expect(typeof LiveQuerySubscription.prototype.find).toBe('function');
11361178
});

0 commit comments

Comments
 (0)