Skip to content

Commit

Permalink
ci: Improve test coverage (#2002)
Browse files Browse the repository at this point in the history
  • Loading branch information
dplewis committed Aug 28, 2023
1 parent c7b391d commit 94e7248
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 23 deletions.
4 changes: 3 additions & 1 deletion src/ObjectStateMutations.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,9 @@ export function estimateAttributes(
function nestedSet(obj, key, value) {
const path = key.split('.');
for (let i = 0; i < path.length - 1; i++) {
if (!(path[i] in obj)) obj[path[i]] = {};
if (!(path[i] in obj)) {
obj[path[i]] = {};
}
obj = obj[path[i]];
}
if (typeof value === 'undefined') {
Expand Down
2 changes: 1 addition & 1 deletion src/OfflineQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ function matchesKeyConstraints(className, object, objects, key, constraints) {
const distance = point.radiansTo(centerPoint);
return distance <= maxDistance;
}
break;
return false;
}
case '$geoIntersects': {
const polygon = new ParsePolygon(object[key].coordinates);
Expand Down
40 changes: 40 additions & 0 deletions src/__tests__/LiveQueryClient-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,21 @@ describe('LiveQueryClient', () => {
spy.mockRestore();
});

it('can handle WebSocket disconnect if already disconnected', async () => {
const liveQueryClient = new LiveQueryClient({
applicationId: 'applicationId',
serverURL: 'ws://test',
javascriptKey: 'javascriptKey',
masterKey: 'masterKey',
sessionToken: 'sessionToken',
});
const spy = jest.spyOn(liveQueryClient, '_handleReconnect');
liveQueryClient.state = 'disconnected';
liveQueryClient._handleWebSocketClose();
expect(liveQueryClient._handleReconnect).toHaveBeenCalledTimes(0);
spy.mockRestore();
});

it('can subscribe', async () => {
const liveQueryClient = new LiveQueryClient({
applicationId: 'applicationId',
Expand Down Expand Up @@ -886,6 +901,31 @@ describe('LiveQueryClient', () => {
expect(liveQueryClient.socket.send).toHaveBeenCalledTimes(0);
});

it('cannot subscribe on connection error', async () => {
const liveQueryClient = new LiveQueryClient({
applicationId: 'applicationId',
serverURL: 'ws://test',
javascriptKey: 'javascriptKey',
masterKey: 'masterKey',
sessionToken: 'sessionToken',
});
liveQueryClient.socket = {
send: jest.fn(),
};
const query = new ParseQuery('Test');
query.equalTo('key', 'value');

const subscription = liveQueryClient.subscribe(query);
liveQueryClient.connectPromise.reject(new Error('Unable to connect'));
liveQueryClient.connectPromise.catch(() => {});
try {
await subscription.subscribePromise;
expect(true).toBeFalse();
} catch (e) {
expect(e.message).toBe('Unable to connect');
}
});

it('can resubscribe', async () => {
const liveQueryClient = new LiveQueryClient({
applicationId: 'applicationId',
Expand Down
11 changes: 11 additions & 0 deletions src/__tests__/ObjectStateMutations-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,17 @@ describe('ObjectStateMutations', () => {
expect(objectCache).toEqual({ data: '{"count":5}' });
});

it('can commit nested changes from the server', () => {
const serverData = {};
const objectCache = {};
ObjectStateMutations.commitServerChanges(serverData, objectCache, {
'name.foo': 'bar',
data: { count: 5 },
});
expect(serverData).toEqual({ name: { foo: 'bar' }, data: { count: 5 } });
expect(objectCache).toEqual({ data: '{"count":5}' });
});

it('can generate a default state for implementations', () => {
expect(ObjectStateMutations.defaultState()).toEqual({
serverData: {},
Expand Down
27 changes: 27 additions & 0 deletions src/__tests__/OfflineQuery-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,33 @@ describe('OfflineQuery', () => {
expect(matchesQuery(q.className, obj3, [], q)).toBe(false);
});

it('should not support invalid $geoWithin query', () => {
const sacramento = new ParseObject('Location');
sacramento.set('location', new ParseGeoPoint(38.52, -121.5));
sacramento.set('name', 'Sacramento');

const honolulu = new ParseObject('Location');
honolulu.set('location', new ParseGeoPoint(21.35, -157.93));
honolulu.set('name', 'Honolulu');

const sf = new ParseObject('Location');
sf.set('location', new ParseGeoPoint(37.75, -122.68));
sf.set('name', 'San Francisco');

const points = [
new ParseGeoPoint(37.85, -122.33),
new ParseGeoPoint(37.85, -122.9),
new ParseGeoPoint(37.68, -122.9),
new ParseGeoPoint(37.68, -122.33),
];
const q = new ParseQuery('Location');
q._addCondition('location', '$geoWithin', { $unknown: points });

expect(matchesQuery(q.className, sacramento, [], q)).toBe(false);
expect(matchesQuery(q.className, honolulu, [], q)).toBe(false);
expect(matchesQuery(q.className, sf, [], q)).toBe(false);
});

it('should validate query', () => {
let query = new ParseQuery('TestObject');
query.equalTo('foo', 'bar');
Expand Down
3 changes: 2 additions & 1 deletion src/__tests__/ParseObject-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2302,13 +2302,14 @@ describe('ParseObject', () => {
});
const p = new ParseObject('Person');
p.id = 'pid';
const result = p.destroy().then(() => {
const result = p.destroy({ sessionToken: 't_1234' }).then(() => {
expect(xhr.open.mock.calls[0]).toEqual([
'POST',
'https://api.parse.com/1/classes/Person/pid',
true,
]);
expect(JSON.parse(xhr.send.mock.calls[0])._method).toBe('DELETE');
expect(JSON.parse(xhr.send.mock.calls[0])._SessionToken).toBe('t_1234');
});
jest.runAllTicks();
await flushPromises();
Expand Down
37 changes: 17 additions & 20 deletions src/__tests__/ParseUser-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -832,7 +832,7 @@ describe('ParseUser', () => {
});
});

it('removes the current user from disk when destroyed', done => {
it('removes the current user from disk when destroyed', async () => {
ParseUser.enableUnsafeCurrentUser();
ParseUser._clearCache();
Storage._clear();
Expand All @@ -848,25 +848,21 @@ describe('ParseUser', () => {
ajax() {},
});

ParseUser.signUp('destroyed', 'password')
.then(u => {
expect(u.isCurrent()).toBe(true);
CoreManager.setRESTController({
request() {
return Promise.resolve({}, 200);
},
ajax() {},
});
return u.destroy();
})
.then(() => {
expect(ParseUser.current()).toBe(null);
return ParseUser.currentAsync();
})
.then(current => {
expect(current).toBe(null);
done();
});
const u = await ParseUser.signUp('destroyed', 'password');
expect(u.isCurrent()).toBe(true);
CoreManager.setRESTController({
request() {
return Promise.resolve({}, 200);
},
ajax() {},
});
await u.destroy();

expect(ParseUser.current()).toBe(null);
const current = await ParseUser.currentAsync();

expect(current).toBe(null);
await u.destroy();
});

it('updates the current user on disk when fetched', done => {
Expand Down Expand Up @@ -1531,6 +1527,7 @@ describe('ParseUser', () => {

user.set('authData', { customAuth: true });
expect(user._isLinked(provider)).toBe(true);
expect(user._isLinked('customAuth')).toBe(true);

user.set('authData', 1234);
expect(user._isLinked(provider)).toBe(false);
Expand Down

0 comments on commit 94e7248

Please sign in to comment.