feat: Add ParseQuery.watch to trigger LiveQuery only on update of specific fields (#8028)

This commit is contained in:
Daniel
2023-01-16 22:32:22 +11:00
committed by GitHub
parent 62b3426b14
commit fc92faac75
3 changed files with 117 additions and 0 deletions

View File

@@ -1087,6 +1087,61 @@ describe('ParseLiveQueryServer', function () {
done();
});
it('can handle create command with watch', async () => {
jasmine.restoreLibrary('../lib/LiveQuery/Client', 'Client');
const Client = require('../lib/LiveQuery/Client').Client;
const parseLiveQueryServer = new ParseLiveQueryServer({});
// Make mock request message
const message = generateMockMessage();
const clientId = 1;
const parseWebSocket = {
clientId,
send: jasmine.createSpy('send'),
};
const client = new Client(clientId, parseWebSocket);
spyOn(client, 'pushCreate').and.callThrough();
parseLiveQueryServer.clients.set(clientId, client);
// Add mock subscription
const requestId = 2;
const query = {
className: testClassName,
where: {
key: 'value',
},
watch: ['yolo'],
};
await addMockSubscription(parseLiveQueryServer, clientId, requestId, parseWebSocket, query);
// Mock _matchesSubscription to return matching
parseLiveQueryServer._matchesSubscription = function (parseObject) {
if (!parseObject) {
return false;
}
return true;
};
parseLiveQueryServer._matchesACL = function () {
return Promise.resolve(true);
};
parseLiveQueryServer._onAfterSave(message);
// Make sure we send create command to client
await timeout();
expect(client.pushCreate).not.toHaveBeenCalled();
message.currentParseObject.set('yolo', 'test');
parseLiveQueryServer._onAfterSave(message);
await timeout();
const args = parseWebSocket.send.calls.mostRecent().args;
const toSend = JSON.parse(args[0]);
expect(toSend.object).toBeDefined();
expect(toSend.original).toBeUndefined();
});
it('can match subscription for null or undefined parse object', function () {
const parseLiveQueryServer = new ParseLiveQueryServer({});
// Make mock subscription