Skip to content

Support isDataAvailable #856

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions integration/test/ParseObjectTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
11 changes: 10 additions & 1 deletion src/ParseObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -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);
}
Expand Down
12 changes: 12 additions & 0 deletions src/__tests__/ParseObject-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down