Repository: parse-community/Parse-SDK-JS
Pull request #712 (2018-12-28 23:21:55)

Add original object to LiveQuery events
https://github.com/parse-community/parse-server/pull/5265

Let me know if more tests are needed

codecov[bot]: # [Codecov](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/712?src=pr&el=h1) Report
> Merging [#712](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/712?src=pr&el=desc) into [master](https://codecov.io/gh/parse-community/Parse-SDK-JS/commit/e3e105a0a3e2a1cee57c706035ca665ba42269e6?src=pr&el=desc) will **increase** coverage by `<.01%`.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/712/graphs/tree.svg?width=650&token=8nCENM0QHj&height=150&src=pr)](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/712?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master     #712      +/-   ##
==========================================
+ Coverage   88.57%   88.58%   +<.01%     
==========================================
  Files          53       53              
  Lines        4535     4538       +3     
  Branches     1052     1053       +1     
==========================================
+ Hits         4017     4020       +3     
  Misses        518      518
```


| [Impacted Files](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/712?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [src/LiveQuerySubscription.js](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/712/diff?src=pr&el=tree#diff-c3JjL0xpdmVRdWVyeVN1YnNjcmlwdGlvbi5qcw==) | `100% <ø> (ø)` | :arrow_up: |
| [src/LiveQueryClient.js](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/712/diff?src=pr&el=tree#diff-c3JjL0xpdmVRdWVyeUNsaWVudC5qcw==) | `87.27% <100%> (+0.23%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/712?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/712?src=pr&el=footer). Last update [e3e105a...78e68f6](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/712?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/src/LiveQueryClient.js b/src/LiveQueryClient.js
index 9904e3a..0d41f25 100644
--- a/src/LiveQueryClient.js
+++ b/src/LiveQueryClient.js
@@ -396,7 +396,11 @@ class LiveQueryClient extends EventEmitter {
       if (!subscription) {
         break;
       }
-      subscription.emit(data.op, parseObject);
+      if (data.original) {
+        delete data.original.__type;
+        data.original = ParseObject.fromJSON(data.original, false);
+      }
+      subscription.emit(data.op, parseObject, data.original);
     }
     }
   }
diff --git a/src/LiveQuerySubscription.js b/src/LiveQuerySubscription.js
index 3593bc7..b2013a2 100644
--- a/src/LiveQuerySubscription.js
+++ b/src/LiveQuerySubscription.js
@@ -37,22 +37,26 @@ import CoreManager from './CoreManager';
  *
  * });</pre></p>
  *
- * <p>Update Event - When an existing ParseObject which fulfills the ParseQuery you subscribe
+ * <p>Update Event - When an existing ParseObject (original) which fulfills the ParseQuery you subscribe
  * is updated (The ParseObject fulfills the ParseQuery before and after changes),
  * you'll get this event. The object is the ParseObject which is updated.
  * Its content is the latest value of the ParseObject.
  *
+ * Parse-Server 3.1.3+ Required for original object parameter
+ *
  * <pre>
- * subscription.on('update', (object) => {
+ * subscription.on('update', (object, original) => {
  *
  * });</pre></p>
  *
- * <p>Enter Event - When an existing ParseObject's old value doesn't fulfill the ParseQuery
+ * <p>Enter Event - When an existing ParseObject's (original) old value doesn't fulfill the ParseQuery
  * but its new value fulfills the ParseQuery, you'll get this event. The object is the
  * ParseObject which enters the ParseQuery. Its content is the latest value of the ParseObject.
  *
+ * Parse-Server 3.1.3+ Required for original object parameter
+ *
  * <pre>
- * subscription.on('enter', (object) => {
+ * subscription.on('enter', (object, original) => {
  *
  * });</pre></p>
  *


Test patch:
diff --git a/src/__tests__/LiveQueryClient-test.js b/src/__tests__/LiveQueryClient-test.js
index 6c289d7..ad2afaa 100644
--- a/src/__tests__/LiveQueryClient-test.js
+++ b/src/__tests__/LiveQueryClient-test.js
@@ -221,6 +221,49 @@ describe('LiveQueryClient', () => {
     expect(isChecked).toBe(true);
   });
 
+  it('can handle WebSocket response with original', () => {
+    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');
+    object.set('key', 'value');
+    original.set('key', 'old');
+    const data = {
+      op: 'update',
+      clientId: 1,
+      requestId: 1,
+      object: object._toFullJSON(),
+      original: original._toFullJSON(),
+    };
+    const event = {
+      data: JSON.stringify(data)
+    }
+    // Register checked in advance
+    let isChecked = false;
+    subscription.on('update', (parseObject, parseOriginalObject) => {
+      isChecked = true;
+      expect(parseObject.get('key')).toEqual('value');
+      expect(parseObject.get('className')).toBeUndefined();
+      expect(parseObject.get('__type')).toBeUndefined();
+
+      expect(parseOriginalObject.get('key')).toEqual('old');
+      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',