From ebf41e7dfd933cb30b97fdcec4d4034bf2ddfe2c Mon Sep 17 00:00:00 2001 From: Diamond Lewis Date: Fri, 4 Jan 2019 15:45:23 -0600 Subject: [PATCH 1/3] LiveQuery handle unset operation --- src/LiveQueryClient.js | 6 ++++ src/__tests__/LiveQueryClient-test.js | 49 +++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/src/LiveQueryClient.js b/src/LiveQueryClient.js index 0d41f2567..cf77e077d 100644 --- a/src/LiveQueryClient.js +++ b/src/LiveQueryClient.js @@ -392,6 +392,12 @@ class LiveQueryClient extends EventEmitter { // Does not override / clear server data delete data.object.__type; + for (const field in data.object) { + const value = data.object[field]; + if (typeof value === 'object' && value.__op && value.__op === 'Delete') { + data.object[field] = undefined; + } + } const parseObject = ParseObject.fromJSON(data.object, false); if (!subscription) { break; diff --git a/src/__tests__/LiveQueryClient-test.js b/src/__tests__/LiveQueryClient-test.js index ad2afaa54..c67175726 100644 --- a/src/__tests__/LiveQueryClient-test.js +++ b/src/__tests__/LiveQueryClient-test.js @@ -264,6 +264,55 @@ describe('LiveQueryClient', () => { expect(isChecked).toBe(true); }); + it('can handle WebSocket response unset pointer', async () => { + const liveQueryClient = new LiveQueryClient({ + applicationId: 'applicationId', + serverURL: 'ws://test', + javascriptKey: 'javascriptKey', + masterKey: 'masterKey', + sessionToken: 'sessionToken' + }); + // Add mock subscription + const subscription = new events.EventEmitter(); + liveQueryClient.subscriptions.set(1, subscription); + + const object = new ParseObject('Test'); + const original = new ParseObject('Test'); + const pointer = new ParseObject('PointerTest'); + pointer.id = '1234'; + + object.set('pointer', pointer); + object.unset('pointer'); + original.set('pointer', pointer); + + const data = { + op: 'update', + clientId: 1, + requestId: 1, + object: object._toFullJSON(), + original: original._toFullJSON(), + }; + expect(data.object.pointer.__op).toEqual('Delete'); + const event = { + data: JSON.stringify(data) + } + let isChecked = false; + subscription.on('update', (parseObject, parseOriginalObject) => { + isChecked = true; + expect(parseObject.get('pointer')).toBeUndefined(); + expect(parseObject.get('className')).toBeUndefined(); + expect(parseObject.get('__type')).toBeUndefined(); + + expect(parseOriginalObject.get('pointer').toJSON()).toEqual(pointer.toJSON()); + expect(parseOriginalObject.get('className')).toBeUndefined(); + expect(parseOriginalObject.get('__type')).toBeUndefined(); + }); + + liveQueryClient._handleWebSocketMessage(event); + + expect(isChecked).toBe(true); + }); + it('can handle WebSocket close message', () => { const liveQueryClient = new LiveQueryClient({ applicationId: 'applicationId', From 7c3e00605bfd2a4647e227f8d601ae899644b977 Mon Sep 17 00:00:00 2001 From: Diamond Lewis Date: Fri, 4 Jan 2019 19:25:26 -0600 Subject: [PATCH 2/3] change logic to original object --- src/LiveQueryClient.js | 19 +++++++++---------- src/__tests__/LiveQueryClient-test.js | 16 +++------------- 2 files changed, 12 insertions(+), 23 deletions(-) diff --git a/src/LiveQueryClient.js b/src/LiveQueryClient.js index cf77e077d..46a4561f2 100644 --- a/src/LiveQueryClient.js +++ b/src/LiveQueryClient.js @@ -389,23 +389,22 @@ class LiveQueryClient extends EventEmitter { break; default: { // create, update, enter, leave, delete cases - - // Does not override / clear server data - delete data.object.__type; - for (const field in data.object) { - const value = data.object[field]; - if (typeof value === 'object' && value.__op && value.__op === 'Delete') { - data.object[field] = undefined; - } - } - const parseObject = ParseObject.fromJSON(data.object, false); if (!subscription) { break; } if (data.original) { delete data.original.__type; + // Check for removed fields + for (const field in data.original) { + if (!(field in data.object)) { + data.object[field] = undefined; + } + } data.original = ParseObject.fromJSON(data.original, false); } + delete data.object.__type; + const parseObject = ParseObject.fromJSON(data.object, false); + subscription.emit(data.op, parseObject, data.original); } } diff --git a/src/__tests__/LiveQueryClient-test.js b/src/__tests__/LiveQueryClient-test.js index c67175726..54332a980 100644 --- a/src/__tests__/LiveQueryClient-test.js +++ b/src/__tests__/LiveQueryClient-test.js @@ -264,7 +264,7 @@ describe('LiveQueryClient', () => { expect(isChecked).toBe(true); }); - it('can handle WebSocket response unset pointer', async () => { + fit('can handle WebSocket response unset field', async () => { const liveQueryClient = new LiveQueryClient({ applicationId: 'applicationId', serverURL: 'ws://test', @@ -280,11 +280,7 @@ describe('LiveQueryClient', () => { const original = new ParseObject('Test'); const pointer = new ParseObject('PointerTest'); pointer.id = '1234'; - - object.set('pointer', pointer); - object.unset('pointer'); original.set('pointer', pointer); - const data = { op: 'update', clientId: 1, @@ -292,20 +288,14 @@ describe('LiveQueryClient', () => { object: object._toFullJSON(), original: original._toFullJSON(), }; - expect(data.object.pointer.__op).toEqual('Delete'); const event = { data: JSON.stringify(data) } let isChecked = false; subscription.on('update', (parseObject, parseOriginalObject) => { isChecked = true; - expect(parseObject.get('pointer')).toBeUndefined(); - expect(parseObject.get('className')).toBeUndefined(); - expect(parseObject.get('__type')).toBeUndefined(); - - expect(parseOriginalObject.get('pointer').toJSON()).toEqual(pointer.toJSON()); - expect(parseOriginalObject.get('className')).toBeUndefined(); - expect(parseOriginalObject.get('__type')).toBeUndefined(); + expect(parseObject.toJSON().pointer).toBeUndefined(); + expect(parseOriginalObject.toJSON().pointer.objectId).toEqual(pointer.id); }); liveQueryClient._handleWebSocketMessage(event); From 6ec47035c03d2307640e6490f1546a38d18bfe06 Mon Sep 17 00:00:00 2001 From: Diamond Lewis Date: Sat, 5 Jan 2019 12:57:16 -0600 Subject: [PATCH 3/3] Nit --- src/__tests__/LiveQueryClient-test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/__tests__/LiveQueryClient-test.js b/src/__tests__/LiveQueryClient-test.js index 54332a980..2c2685f92 100644 --- a/src/__tests__/LiveQueryClient-test.js +++ b/src/__tests__/LiveQueryClient-test.js @@ -264,7 +264,7 @@ describe('LiveQueryClient', () => { expect(isChecked).toBe(true); }); - fit('can handle WebSocket response unset field', async () => { + it('can handle WebSocket response unset field', async () => { const liveQueryClient = new LiveQueryClient({ applicationId: 'applicationId', serverURL: 'ws://test',