Repository: parse-community/Parse-SDK-JS
Pull request #791 (2019-04-11 17:37:57)

LiveQuery subscribe with sessionToken
Adds sessionToken parameter to `query.subscribe()`
As described in the guide for Advanced API https://docs.parseplatform.org/js/guide/#sessiontoken

This is also a failing test for https://github.com/parse-community/Parse-SDK-JS/issues/778



codecov[bot]: # [Codecov](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/791?src=pr&el=h1) Report
> Merging [#791](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/791?src=pr&el=desc) into [master](https://codecov.io/gh/parse-community/Parse-SDK-JS/commit/c91ebd6489c125772b0a86a916ac9a2d783f985f?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/791/graphs/tree.svg?width=650&token=8nCENM0QHj&height=150&src=pr)](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/791?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master     #791      +/-   ##
==========================================
+ Coverage   92.01%   92.01%   +<.01%     
==========================================
  Files          54       54              
  Lines        4908     4909       +1     
  Branches     1087     1088       +1     
==========================================
+ Hits         4516     4517       +1     
  Misses        392      392
```


| [Impacted Files](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/791?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [src/ParseQuery.js](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/791/diff?src=pr&el=tree#diff-c3JjL1BhcnNlUXVlcnkuanM=) | `96% <100%> (ø)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/791?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/791?src=pr&el=footer). Last update [c91ebd6...a334630](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/791?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/src/ParseQuery.js b/src/ParseQuery.js
index 300e1c2..b8dd6d0 100644
--- a/src/ParseQuery.js
+++ b/src/ParseQuery.js
@@ -1483,13 +1483,15 @@ class ParseQuery {
   /**
    * Subscribe this query to get liveQuery updates
    *
+   * @param {String} sessionToken (optional) Defaults to the currentUser
    * @return {Promise<LiveQuerySubscription>} Returns the liveQuerySubscription, it's an event emitter
    * which can be used to get liveQuery updates.
    */
-  async subscribe(): Promise<LiveQuerySubscription> {
+  async subscribe(sessionToken?: string): Promise<LiveQuerySubscription> {
     const currentUser = await CoreManager.getUserController().currentUserAsync();
-    const sessionToken =  currentUser ? currentUser.getSessionToken() : undefined;
-
+    if (!sessionToken) {
+      sessionToken =  currentUser ? currentUser.getSessionToken() : undefined;
+    }
     const liveQueryClient = await CoreManager.getLiveQueryController().getDefaultLiveQueryClient();
     if (liveQueryClient.shouldOpen()) {
       liveQueryClient.open();


Test patch:
diff --git a/integration/test/ParseLiveQueryTest.js b/integration/test/ParseLiveQueryTest.js
index dd8f65c..5c2a89e 100644
--- a/integration/test/ParseLiveQueryTest.js
+++ b/integration/test/ParseLiveQueryTest.js
@@ -12,6 +12,7 @@ describe('Parse LiveQuery', () => {
   beforeEach((done) => {
     Parse.initialize('integration');
     Parse.CoreManager.set('SERVER_URL', 'http://localhost:1337/parse');
+    Parse.User.enableUnsafeCurrentUser();
     Parse.Storage._clear();
     clear().then(done).catch(done.fail);
   });
@@ -135,4 +136,23 @@ describe('Parse LiveQuery', () => {
     await sleep(1000);
     assert.equal(count, 1);
   });
+
+  it('can subscribe to ACL', async (done) => {
+    const user = await Parse.User.signUp('ooo', 'password');
+    const ACL = new Parse.ACL(user);
+
+    const object = new TestObject();
+    object.setACL(ACL);
+    await object.save();
+
+    const query = new Parse.Query(TestObject);
+    query.equalTo('objectId', object.id);
+    const subscription = await query.subscribe(user.getSessionToken());
+    subscription.on('update', async (object) => {
+      assert.equal(object.get('foo'), 'bar');
+      await Parse.User.logOut();
+      done();
+    })
+    await object.save({ foo: 'bar' });
+  });
 });
diff --git a/src/__tests__/ParseQuery-test.js b/src/__tests__/ParseQuery-test.js
index 7b82136..6c62346 100644
--- a/src/__tests__/ParseQuery-test.js
+++ b/src/__tests__/ParseQuery-test.js
@@ -2718,4 +2718,31 @@ describe('ParseQuery LocalDatastore', () => {
     expect(subscription.sessionToken).toBeUndefined();
     expect(subscription.query).toEqual(query);
   });
+
+  it('can subscribe to query with sessionToken parameter', async () => {
+    const mockLiveQueryClient = {
+      shouldOpen: function() {
+        return true;
+      },
+      open: function() {},
+      subscribe: function(query, sessionToken) {
+        return new LiveQuerySubscription('0', query, sessionToken);
+      },
+    };
+    CoreManager.set('UserController', {
+      currentUserAsync() {
+        return Promise.resolve(null);
+      }
+    });
+    CoreManager.set('LiveQueryController', {
+      getDefaultLiveQueryClient() {
+        return Promise.resolve(mockLiveQueryClient);
+      }
+    });
+    const query = new ParseQuery('TestObject');
+    const subscription = await query.subscribe('r:test');
+    expect(subscription.id).toBe('0');
+    expect(subscription.sessionToken).toBe('r:test');
+    expect(subscription.query).toEqual(query);
+  });
 });