Skip to content

Commit f50b94e

Browse files
authored
Improve Code Coverage (#1241)
* Improve Coverage * 96% * Fix bugs * User tests * Lint * ParseObject test * ObjectController Tests
1 parent edd25f5 commit f50b94e

40 files changed

+2439
-48
lines changed

src/LiveQueryClient.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,10 @@ class LiveQueryClient extends EventEmitter {
167167
this.connectPromise = resolvingPromise();
168168
this.subscriptions = new Map();
169169
this.state = CLIENT_STATE.INITIALIZED;
170+
171+
// adding listener so process does not crash
172+
// best practice is for developer to register their own listener
173+
this.on('error', () => {});
170174
}
171175

172176
shouldOpen(): any {
@@ -447,7 +451,7 @@ class LiveQueryClient extends EventEmitter {
447451
_handleWebSocketError(error: any) {
448452
this.emit(CLIENT_EMMITER_TYPES.ERROR, error);
449453
for (const subscription of this.subscriptions.values()) {
450-
subscription.emit(SUBSCRIPTION_EMMITER_TYPES.ERROR);
454+
subscription.emit(SUBSCRIPTION_EMMITER_TYPES.ERROR, error);
451455
}
452456
this._handleReconnect();
453457
}

src/ParseACL.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,6 @@ class ParseACL {
4646
} else {
4747
for (const userId in arg1) {
4848
const accessList = arg1[userId];
49-
if (typeof userId !== 'string') {
50-
throw new TypeError(
51-
'Tried to create an ACL with an invalid user id.'
52-
);
53-
}
5449
this.permissionsById[userId] = {};
5550
for (const permission in accessList) {
5651
const allowed = accessList[permission];

src/ParseConfig.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,15 @@ class ParseConfig {
116116
return Promise.reject(error);
117117
});
118118
}
119+
120+
/**
121+
* Used for testing
122+
*
123+
* @private
124+
*/
125+
static _clearCache() {
126+
currentConfig = null;
127+
}
119128
}
120129

121130
let currentConfig = null;
@@ -141,9 +150,8 @@ const DefaultController = {
141150

142151
const config = new ParseConfig();
143152
const storagePath = Storage.generatePath(CURRENT_CONFIG_KEY);
144-
let configData;
145153
if (!Storage.async()) {
146-
configData = Storage.getItem(storagePath);
154+
const configData = Storage.getItem(storagePath);
147155

148156
if (configData) {
149157
const attributes = decodePayload(configData);

src/ParseFile.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,9 +441,9 @@ const DefaultController = {
441441
const base64Data = await new Promise((res, rej) => {
442442
// eslint-disable-next-line no-undef
443443
const reader = new FileReader();
444-
reader.readAsDataURL(source.file);
445444
reader.onload = () => res(reader.result);
446445
reader.onerror = error => rej(error);
446+
reader.readAsDataURL(source.file);
447447
});
448448
// we only want the data after the comma
449449
// For example: "data:application/pdf;base64,JVBERi0xLjQKJ..." we would only want "JVBERi0xLjQKJ..."
@@ -556,8 +556,13 @@ const DefaultController = {
556556
_setXHR(xhr: any) {
557557
XHR = xhr;
558558
},
559+
560+
_getXHR() {
561+
return XHR;
562+
},
559563
};
560564

561565
CoreManager.setFileController(DefaultController);
562566

563567
export default ParseFile;
568+
exports.b64Digit = b64Digit;

src/ParseInstallation.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export default class Installation extends ParseObject {
1818
super('_Installation');
1919
if (attributes && typeof attributes === 'object'){
2020
if (!this.set(attributes || {})) {
21-
throw new Error('Can\'t create an invalid Session');
21+
throw new Error('Can\'t create an invalid Installation');
2222
}
2323
}
2424
}

src/ParseQuery.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -820,7 +820,9 @@ class ParseQuery {
820820
}
821821

822822
if (Object.keys(this._where || {}).length) {
823-
if(!Array.isArray(pipeline)) pipeline = [pipeline];
823+
if (!Array.isArray(pipeline)) {
824+
pipeline = [pipeline];
825+
}
824826
pipeline.unshift({ match: this._where });
825827
}
826828

src/ParseUser.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ class ParseUser extends ParseObject {
105105
return authType;
106106
},
107107
};
108-
authProviders[authType] = authProvider;
108+
authProviders[authProvider.getAuthType()] = authProvider;
109109
provider = authProvider;
110110
}
111111
} else {

src/Socket.weapp.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@ module.exports = class SocketWeapp {
55
this.onclose = () => {}
66
this.onerror = () => {}
77

8-
wx.connectSocket({
9-
url: serverURL
10-
})
11-
128
wx.onSocketOpen(() => {
139
this.onopen();
1410
})
@@ -19,11 +15,15 @@ module.exports = class SocketWeapp {
1915

2016
wx.onSocketClose(() => {
2117
this.onclose();
22-
})
18+
});
2319

2420
wx.onSocketError((error) => {
2521
this.onerror(error);
26-
})
22+
});
23+
24+
wx.connectSocket({
25+
url: serverURL,
26+
});
2727
}
2828

2929
send(data) {

src/__tests__/Cloud-test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ jest.dontMock('../Cloud');
1111
jest.dontMock('../CoreManager');
1212
jest.dontMock('../decode');
1313
jest.dontMock('../encode');
14+
jest.dontMock('../ParseError');
15+
jest.dontMock('../ParseObject');
16+
jest.dontMock('../ParseQuery');
1417

1518
const Cloud = require('../Cloud');
1619
const CoreManager = require('../CoreManager');
@@ -222,4 +225,24 @@ describe('CloudController', () => {
222225
// Validate
223226
expect(controller.request.mock.calls[0][3].context).toEqual(context);
224227
});
228+
229+
it('can get job status', async () => {
230+
const request = jest.fn();
231+
request.mockReturnValue(Promise.resolve({
232+
results: [{ className: '_JobStatus', objectId: 'jobId1234' }],
233+
}));
234+
CoreManager.setRESTController({ request: request, ajax: jest.fn() });
235+
236+
await Cloud.getJobStatus('jobId1234');
237+
const [ method, path, data, options] = request.mock.calls[0];
238+
expect(method).toBe('GET');
239+
expect(path).toBe('classes/_JobStatus');
240+
expect(data).toEqual({
241+
limit: 1,
242+
where: {
243+
objectId: 'jobId1234',
244+
},
245+
});
246+
expect(options.useMasterKey).toBe(true);
247+
});
225248
});

src/__tests__/Hooks-test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@ jest.dontMock('../ParseHooks');
1111
jest.dontMock('../CoreManager');
1212
jest.dontMock('../decode');
1313
jest.dontMock('../encode');
14+
jest.dontMock('../ParseError');
1415

1516
const Hooks = require('../ParseHooks');
1617
const CoreManager = require('../CoreManager');
1718

1819
const defaultController = CoreManager.getHooksController();
20+
const { sendRequest } = defaultController;
1921

2022
describe('Hooks', () => {
2123
beforeEach(() => {
@@ -200,5 +202,27 @@ describe('Hooks', () => {
200202
done();
201203
})
202204

205+
it('should sendRequest', async () => {
206+
defaultController.sendRequest = sendRequest;
207+
const request = function() {
208+
return Promise.resolve(12);
209+
};
210+
CoreManager.setRESTController({ request, ajax: jest.fn() });
211+
const decoded = await defaultController.sendRequest('POST', 'hooks/triggers/myhook');
212+
expect(decoded).toBe(12);
213+
});
203214

215+
it('handle sendRequest error', async () => {
216+
defaultController.sendRequest = sendRequest;
217+
const request = function() {
218+
return Promise.resolve(undefined);
219+
};
220+
CoreManager.setRESTController({ request, ajax: jest.fn() });
221+
try {
222+
await defaultController.sendRequest('POST', 'hooks/triggers/myhook');
223+
expect(false).toBe(true);
224+
} catch (e) {
225+
expect(e.message).toBe('The server returned an invalid response.');
226+
}
227+
});
204228
});

src/__tests__/InstallationController-test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,13 @@ describe('InstallationController', () => {
5959
done();
6060
});
6161
});
62+
63+
it('can set installation id', (done) => {
64+
const iid = '12345678';
65+
InstallationController._setInstallationIdCache(iid);
66+
InstallationController.currentInstallationId().then((i) => {
67+
expect(i).toBe(iid);
68+
done();
69+
});
70+
});
6271
});

0 commit comments

Comments
 (0)