Skip to content

Fix 477 #492

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 4 commits into from
Oct 21, 2017
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
15 changes: 10 additions & 5 deletions src/ParseQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,19 @@ function handleSelectResult(data: any, select: Array<string>){

pathComponents.forEach((component, index, arr) => {
// add keys if the expected data is missing
if (!obj[component]) {
obj[component] = (index == arr.length-1) ? undefined : {};
if (obj && !obj.hasOwnProperty(component)) {
obj[component] = undefined;
}
if (obj !== undefined) {
obj = obj[component];
}
obj = obj[component];

//add this path component to the server mask so we can fill it in later if needed
if (index < arr.length-1) {
if (!serverMask[component]) {
serverMask[component] = {};
}
serverMask = serverMask[component];
}
});
}
Expand All @@ -100,8 +103,10 @@ function handleSelectResult(data: any, select: Array<string>){
}
}
for (var key in mask) {
//traverse into objects as needed
copyMissingDataWithMask(src[key], dest[key], mask[key], true);
if (dest[key] !== undefined && dest[key] !== null && src !== undefined && src !== null) {
//traverse into objects as needed
copyMissingDataWithMask(src[key], dest[key], mask[key], true);
}
}
}

Expand Down
101 changes: 101 additions & 0 deletions src/__tests__/ParseQuery-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1584,4 +1584,105 @@ describe('ParseQuery', () => {
});

});

it('selecting sub-objects does not inject objects when sub-object does not exist', (done) => {
jest.dontMock("../ParseObject");
jest.resetModules();
ParseObject = require('../ParseObject').default;
CoreManager = require('../CoreManager');
ParseQuery = require('../ParseQuery').default;

ParseObject.enableSingleInstance();

var objectToReturn = {
objectId: 'T01',
name: 'Name',
tbd: 'exists',
className:"Thing",
createdAt: '2017-01-10T10:00:00Z'
};

CoreManager.setQueryController({
find(className, params, options) {
return ParsePromise.as({
results: [objectToReturn]
});
}
});

var q = new ParseQuery("Thing");
q.select("other", "tbd", "subObject.key1")
var testObject;
return q.find().then((results) => {
testObject = results[0];

expect(testObject.get("name")).toBe("Name");
expect(testObject.has("other")).toBe(false);
expect(testObject.has("subObject")).toBe(false);

}).then(() => {
done();
}, (error) => {
done.fail(error);
});
});

it('removes missing sub objects from the cached object when they are selected', (done) => {
jest.dontMock("../ParseObject");
jest.resetModules();
ParseObject = require('../ParseObject').default;
CoreManager = require('../CoreManager');
ParseQuery = require('../ParseQuery').default;

ParseObject.enableSingleInstance();

var objectToReturn = {
objectId: 'T01',
name: 'Name',
tbd: 'exists',
className:"Thing",
subObject1: {foo:"bar"},
subObject2: {foo:"bar"},
subObject3: {foo:"bar"},
subObject5: {subSubObject:{foo:"foo", bar:"bar"}},
createdAt: '2017-01-10T10:00:00Z'
};

CoreManager.setQueryController({
find(className, params, options) {
return ParsePromise.as({
results: [objectToReturn]
});
}
});

var q = new ParseQuery("Thing");
var testObject;
return q.find().then((results) => {
testObject = results[0];

expect(testObject.has("subObject1")).toBe(true);
expect(testObject.has("subObject2")).toBe(true);
expect(testObject.has("subObject3")).toBe(true);
expect(testObject.has("subObject4")).toBe(false);

var q2 = new ParseQuery("Thing");
q.select("name","subObject1", "subObject2.foo", "subObject4.foo", "subObject5.subSubObject.foo");
objectToReturn = { objectId: 'T01', name:"Name", subObject4: {foo:"bar"}, subObject5: {subSubObject:{}}};
return q.find();
}).then((results)=>{
expect(testObject.has("subObject1")).toBe(false); //selected and not returned
expect(testObject.has("subObject2")).toBe(false); //selected and not returned
expect(testObject.has("subObject3")).toBe(true); //not selected, so should still be there
expect(testObject.has("subObject4")).toBe(true); //selected and just added
expect(testObject.has("subObject5")).toBe(true);
expect(testObject.get("subObject5").subSubObject).toBeDefined();
expect(testObject.get("subObject5").subSubObject.bar).toBeDefined(); //not selected but a sibiling was, so should still be there
}).then(() => {
done();
}, (error) => {
done.fail(error);
});
});

});