Skip to content

Commit

Permalink
Improve Code Coverage (#1241)
Browse files Browse the repository at this point in the history
* Improve Coverage

* 96%

* Fix bugs

* User tests

* Lint

* ParseObject test

* ObjectController Tests
  • Loading branch information
dplewis authored Oct 29, 2020
1 parent edd25f5 commit f50b94e
Show file tree
Hide file tree
Showing 40 changed files with 2,439 additions and 48 deletions.
6 changes: 5 additions & 1 deletion src/LiveQueryClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ class LiveQueryClient extends EventEmitter {
this.connectPromise = resolvingPromise();
this.subscriptions = new Map();
this.state = CLIENT_STATE.INITIALIZED;

// adding listener so process does not crash
// best practice is for developer to register their own listener
this.on('error', () => {});
}

shouldOpen(): any {
Expand Down Expand Up @@ -447,7 +451,7 @@ class LiveQueryClient extends EventEmitter {
_handleWebSocketError(error: any) {
this.emit(CLIENT_EMMITER_TYPES.ERROR, error);
for (const subscription of this.subscriptions.values()) {
subscription.emit(SUBSCRIPTION_EMMITER_TYPES.ERROR);
subscription.emit(SUBSCRIPTION_EMMITER_TYPES.ERROR, error);
}
this._handleReconnect();
}
Expand Down
5 changes: 0 additions & 5 deletions src/ParseACL.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,6 @@ class ParseACL {
} else {
for (const userId in arg1) {
const accessList = arg1[userId];
if (typeof userId !== 'string') {
throw new TypeError(
'Tried to create an ACL with an invalid user id.'
);
}
this.permissionsById[userId] = {};
for (const permission in accessList) {
const allowed = accessList[permission];
Expand Down
12 changes: 10 additions & 2 deletions src/ParseConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,15 @@ class ParseConfig {
return Promise.reject(error);
});
}

/**
* Used for testing
*
* @private
*/
static _clearCache() {
currentConfig = null;
}
}

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

const config = new ParseConfig();
const storagePath = Storage.generatePath(CURRENT_CONFIG_KEY);
let configData;
if (!Storage.async()) {
configData = Storage.getItem(storagePath);
const configData = Storage.getItem(storagePath);

if (configData) {
const attributes = decodePayload(configData);
Expand Down
7 changes: 6 additions & 1 deletion src/ParseFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -441,9 +441,9 @@ const DefaultController = {
const base64Data = await new Promise((res, rej) => {
// eslint-disable-next-line no-undef
const reader = new FileReader();
reader.readAsDataURL(source.file);
reader.onload = () => res(reader.result);
reader.onerror = error => rej(error);
reader.readAsDataURL(source.file);
});
// we only want the data after the comma
// For example: "data:application/pdf;base64,JVBERi0xLjQKJ..." we would only want "JVBERi0xLjQKJ..."
Expand Down Expand Up @@ -556,8 +556,13 @@ const DefaultController = {
_setXHR(xhr: any) {
XHR = xhr;
},

_getXHR() {
return XHR;
},
};

CoreManager.setFileController(DefaultController);

export default ParseFile;
exports.b64Digit = b64Digit;
2 changes: 1 addition & 1 deletion src/ParseInstallation.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default class Installation extends ParseObject {
super('_Installation');
if (attributes && typeof attributes === 'object'){
if (!this.set(attributes || {})) {
throw new Error('Can\'t create an invalid Session');
throw new Error('Can\'t create an invalid Installation');
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/ParseQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -820,7 +820,9 @@ class ParseQuery {
}

if (Object.keys(this._where || {}).length) {
if(!Array.isArray(pipeline)) pipeline = [pipeline];
if (!Array.isArray(pipeline)) {
pipeline = [pipeline];
}
pipeline.unshift({ match: this._where });
}

Expand Down
2 changes: 1 addition & 1 deletion src/ParseUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class ParseUser extends ParseObject {
return authType;
},
};
authProviders[authType] = authProvider;
authProviders[authProvider.getAuthType()] = authProvider;
provider = authProvider;
}
} else {
Expand Down
12 changes: 6 additions & 6 deletions src/Socket.weapp.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ module.exports = class SocketWeapp {
this.onclose = () => {}
this.onerror = () => {}

wx.connectSocket({
url: serverURL
})

wx.onSocketOpen(() => {
this.onopen();
})
Expand All @@ -19,11 +15,15 @@ module.exports = class SocketWeapp {

wx.onSocketClose(() => {
this.onclose();
})
});

wx.onSocketError((error) => {
this.onerror(error);
})
});

wx.connectSocket({
url: serverURL,
});
}

send(data) {
Expand Down
23 changes: 23 additions & 0 deletions src/__tests__/Cloud-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ jest.dontMock('../Cloud');
jest.dontMock('../CoreManager');
jest.dontMock('../decode');
jest.dontMock('../encode');
jest.dontMock('../ParseError');
jest.dontMock('../ParseObject');
jest.dontMock('../ParseQuery');

const Cloud = require('../Cloud');
const CoreManager = require('../CoreManager');
Expand Down Expand Up @@ -222,4 +225,24 @@ describe('CloudController', () => {
// Validate
expect(controller.request.mock.calls[0][3].context).toEqual(context);
});

it('can get job status', async () => {
const request = jest.fn();
request.mockReturnValue(Promise.resolve({
results: [{ className: '_JobStatus', objectId: 'jobId1234' }],
}));
CoreManager.setRESTController({ request: request, ajax: jest.fn() });

await Cloud.getJobStatus('jobId1234');
const [ method, path, data, options] = request.mock.calls[0];
expect(method).toBe('GET');
expect(path).toBe('classes/_JobStatus');
expect(data).toEqual({
limit: 1,
where: {
objectId: 'jobId1234',
},
});
expect(options.useMasterKey).toBe(true);
});
});
24 changes: 24 additions & 0 deletions src/__tests__/Hooks-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ jest.dontMock('../ParseHooks');
jest.dontMock('../CoreManager');
jest.dontMock('../decode');
jest.dontMock('../encode');
jest.dontMock('../ParseError');

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

const defaultController = CoreManager.getHooksController();
const { sendRequest } = defaultController;

describe('Hooks', () => {
beforeEach(() => {
Expand Down Expand Up @@ -200,5 +202,27 @@ describe('Hooks', () => {
done();
})

it('should sendRequest', async () => {
defaultController.sendRequest = sendRequest;
const request = function() {
return Promise.resolve(12);
};
CoreManager.setRESTController({ request, ajax: jest.fn() });
const decoded = await defaultController.sendRequest('POST', 'hooks/triggers/myhook');
expect(decoded).toBe(12);
});

it('handle sendRequest error', async () => {
defaultController.sendRequest = sendRequest;
const request = function() {
return Promise.resolve(undefined);
};
CoreManager.setRESTController({ request, ajax: jest.fn() });
try {
await defaultController.sendRequest('POST', 'hooks/triggers/myhook');
expect(false).toBe(true);
} catch (e) {
expect(e.message).toBe('The server returned an invalid response.');
}
});
});
9 changes: 9 additions & 0 deletions src/__tests__/InstallationController-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,13 @@ describe('InstallationController', () => {
done();
});
});

it('can set installation id', (done) => {
const iid = '12345678';
InstallationController._setInstallationIdCache(iid);
InstallationController.currentInstallationId().then((i) => {
expect(i).toBe(iid);
done();
});
});
});
Loading

0 comments on commit f50b94e

Please sign in to comment.