Skip to content

Commit

Permalink
Adds missing count property on beforeFind request object
Browse files Browse the repository at this point in the history
  • Loading branch information
flovilmart committed May 14, 2017
1 parent e1ed622 commit 6872cfe
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
32 changes: 32 additions & 0 deletions spec/CloudCode.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1470,4 +1470,36 @@ describe('afterFind hooks', () => {
});
});
});

it('should report count if passed', (done) => {
const hook = {
method: function(req) {
expect(req.count).toBe(true);
return Promise.resolve();
}
};
spyOn(hook, 'method').and.callThrough();
Parse.Cloud.beforeFind('Stuff', hook.method);
new Parse.Query('Stuff').count().then((count) => {
expect(count).toBe(0);
expect(hook.method).toHaveBeenCalled();
done();
});
});

it('should report count if passed', (done) => {
const hook = {
method: function(req) {
expect(req.count).toBe(false);
return Promise.resolve();
}
};
spyOn(hook, 'method').and.callThrough();
Parse.Cloud.beforeFind('Stuff', hook.method);
new Parse.Query('Stuff').find().then((res) => {
expect(res.length).toBe(0);
expect(hook.method).toHaveBeenCalled();
done();
});
});
});
7 changes: 5 additions & 2 deletions src/triggers.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,12 @@ export function getRequestObject(triggerType, auth, parseObject, originalParseOb
return request;
}

export function getRequestQueryObject(triggerType, auth, query, config) {
export function getRequestQueryObject(triggerType, auth, query, count, config) {
var request = {
triggerName: triggerType,
query: query,
master: false,
count: count,
log: config.loggerController
};

Expand Down Expand Up @@ -298,6 +299,7 @@ export function maybeRunQueryTrigger(triggerType, className, restWhere, restOpti
if (restWhere) {
parseQuery._where = restWhere;
}
let count = false;
if (restOptions) {
if (restOptions.include && restOptions.include.length > 0) {
parseQuery._include = restOptions.include.split(',');
Expand All @@ -308,8 +310,9 @@ export function maybeRunQueryTrigger(triggerType, className, restWhere, restOpti
if (restOptions.limit) {
parseQuery._limit = restOptions.limit;
}
count = !!restOptions.count;
}
const requestObject = getRequestQueryObject(triggerType, auth, parseQuery, config);
const requestObject = getRequestQueryObject(triggerType, auth, parseQuery, count, config);
return Promise.resolve().then(() => {
return trigger(requestObject);
}).then((result) => {
Expand Down

0 comments on commit 6872cfe

Please sign in to comment.