Repository: parse-community/Parse-SDK-JS
Pull request #856 (2019-07-10 22:00:06)

Support isDataAvailable
Closes: https://github.com/parse-community/Parse-SDK-JS/issues/851

Checks if an object has been fetched from the server or not.

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

```diff
@@            Coverage Diff             @@
##           master     #856      +/-   ##
==========================================
+ Coverage   92.07%   92.08%   +<.01%     
==========================================
  Files          54       54              
  Lines        4973     4976       +3     
  Branches     1109     1109              
==========================================
+ Hits         4579     4582       +3     
  Misses        394      394
```


| [Impacted Files](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/856?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [src/ParseObject.js](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/856/diff?src=pr&el=tree#diff-c3JjL1BhcnNlT2JqZWN0Lmpz) | `90.1% <100%> (+0.03%)` | :arrow_up: |

------

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


Patch:
diff --git a/src/ParseObject.js b/src/ParseObject.js
index cdd6156..2f8fa69 100644
--- a/src/ParseObject.js
+++ b/src/ParseObject.js
@@ -524,6 +524,15 @@ class ParseObject {
     return Object.keys(keys);
   }
 
+  /**
+   * Returns true if the object has been fetched.
+   * @return {Boolean}
+   */
+  isDataAvailable(): boolean {
+    const serverData = this._getServerData();
+    return !!Object.keys(serverData).length;
+  }
+
   /**
    * Gets a Pointer referencing this Object.
    * @return {Pointer}
@@ -1980,7 +1989,7 @@ const DefaultController = {
             'All objects must have an ID'
           );
         }
-        if (forceFetch || Object.keys(el._getServerData()).length === 0) {
+        if (forceFetch || !el.isDataAvailable()) {
           ids.push(el.id);
           objs.push(el);
         }


Test patch:
diff --git a/integration/test/ParseObjectTest.js b/integration/test/ParseObjectTest.js
index 7877a36..47df112 100644
--- a/integration/test/ParseObjectTest.js
+++ b/integration/test/ParseObjectTest.js
@@ -1663,4 +1663,37 @@ describe('Parse Object', () => {
 
     done();
   });
+
+  it('isDataAvailable', async () => {
+    const child = new TestObject({ foo: 'bar' });
+    assert.equal(child.isDataAvailable(), false);
+
+    const parent = new TestObject({ child });
+    await parent.save();
+
+    assert.equal(child.isDataAvailable(), true);
+    assert.equal(parent.isDataAvailable(), true);
+
+    const query = new Parse.Query(TestObject);
+    const fetched = await query.get(parent.id);
+    const unfetched = fetched.get('child');
+
+    assert.equal(fetched.isDataAvailable(), true);
+    assert.equal(unfetched.isDataAvailable(), false);
+  });
+
+  it('isDataAvailable user', async () => {
+    let user = new Parse.User();
+    user.set('username', 'plain');
+    user.set('password', 'plain');
+    await user.signUp();
+    assert.equal(user.isDataAvailable(), true);
+
+    user = await Parse.User.logIn('plain', 'plain');
+    assert.equal(user.isDataAvailable(), true);
+
+    const query = new Parse.Query(Parse.User);
+    const fetched = await query.get(user.id);
+    assert.equal(fetched.isDataAvailable(), true);
+  });
 });
diff --git a/src/__tests__/ParseObject-test.js b/src/__tests__/ParseObject-test.js
index e27a14f..8b74298 100644
--- a/src/__tests__/ParseObject-test.js
+++ b/src/__tests__/ParseObject-test.js
@@ -916,6 +916,18 @@ describe('ParseObject', () => {
     expect(p.op('age')).toBe(undefined);
   });
 
+  it('isDataAvailable', () => {
+    const p = new ParseObject('Person');
+    p.id = 'isdataavailable';
+    p.set('age', 24);
+    expect(p.isDataAvailable()).toBe(false);
+    const updated = new Date();
+    p._handleSaveResponse({
+      updatedAt: { __type: 'Date', iso: updated.toISOString() }
+    });
+    expect(p.isDataAvailable()).toBe(true);
+  });
+
   it('handles ACL when saved', () => {
     const p = new ParseObject('Person');