Skip to content

Make sure the className is correct for results on unknown relations #179

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 1 commit into from
Jan 22, 2016
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
9 changes: 6 additions & 3 deletions src/ParseQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,24 +261,27 @@ export default class ParseQuery {
find(options?: FullOptions): ParsePromise {
options = options || {};

var findOptions = {};
let findOptions = {};
if (options.hasOwnProperty('useMasterKey')) {
findOptions.useMasterKey = options.useMasterKey;
}
if (options.hasOwnProperty('sessionToken')) {
findOptions.sessionToken = options.sessionToken;
}

var controller = CoreManager.getQueryController();
let controller = CoreManager.getQueryController();

return controller.find(
this.className,
this.toJSON(),
findOptions
).then((response) => {
return response.results.map((data) => {
// In cases of relations, the server may send back a className
// on the top level of the payload
let override = response.className || this.className;
if (!data.className) {
data.className = this.className;
data.className = override;
}
return ParseObject.fromJSON(data);
});
Expand Down
37 changes: 37 additions & 0 deletions src/__tests__/ParseQuery-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1298,4 +1298,41 @@ describe('ParseQuery', () => {
q = new ParseQuery('User');
expect(q.className).toBe('User');
});

it('does not override the className if it comes from the server', asyncHelper((done) => {
CoreManager.setQueryController({
find(className, params, options) {
return ParsePromise.as({
results: [
{ className: 'Product', objectId: 'P40', name: 'Product 40' },
]
});
}
});

var q = new ParseQuery('Item');
q.find().then((results) => {
expect(results[0].className).toBe('Product');
done();
});
}));

it('can override the className with a name from the server', asyncHelper((done) => {
CoreManager.setQueryController({
find(className, params, options) {
return ParsePromise.as({
results: [
{ objectId: 'P41', name: 'Product 41' },
],
className: 'Product'
});
}
});

var q = new ParseQuery('Item');
q.find().then((results) => {
expect(results[0].className).toBe('Product');
done();
});
}));
});