Skip to content

Commit

Permalink
test: Add tests for isGet parameter in Cloud Code trigger `beforeFi…
Browse files Browse the repository at this point in the history
…nd` (#8738)
  • Loading branch information
mtrezza authored Sep 6, 2023
1 parent 5954f0f commit 977edea
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions spec/CloudCode.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2398,6 +2398,56 @@ describe('beforeFind hooks', () => {
});
});

it('sets correct beforeFind trigger isGet parameter for Parse.Object.fetch request', async () => {
const hook = {
method: req => {
expect(req.isGet).toEqual(true);
return Promise.resolve();
},
};
spyOn(hook, 'method').and.callThrough();
Parse.Cloud.beforeFind('MyObject', hook.method);
const obj = new Parse.Object('MyObject');
await obj.save();
const getObj = await obj.fetch();
expect(getObj).toBeInstanceOf(Parse.Object);
expect(hook.method).toHaveBeenCalledTimes(1);
});

it('sets correct beforeFind trigger isGet parameter for Parse.Query.get request', async () => {
const hook = {
method: req => {
expect(req.isGet).toEqual(false);
return Promise.resolve();
},
};
spyOn(hook, 'method').and.callThrough();
Parse.Cloud.beforeFind('MyObject', hook.method);
const obj = new Parse.Object('MyObject');
await obj.save();
const query = new Parse.Query('MyObject');
const getObj = await query.get(obj.id);
expect(getObj).toBeInstanceOf(Parse.Object);
expect(hook.method).toHaveBeenCalledTimes(1);
});

it('sets correct beforeFind trigger isGet parameter for Parse.Query.find request', async () => {
const hook = {
method: req => {
expect(req.isGet).toEqual(false);
return Promise.resolve();
},
};
spyOn(hook, 'method').and.callThrough();
Parse.Cloud.beforeFind('MyObject', hook.method);
const obj = new Parse.Object('MyObject');
await obj.save();
const query = new Parse.Query('MyObject');
const findObjs = await query.find();
expect(findObjs?.[0]).toBeInstanceOf(Parse.Object);
expect(hook.method).toHaveBeenCalledTimes(1);
});

it('should have request headers', done => {
Parse.Cloud.beforeFind('MyObject', req => {
expect(req.headers).toBeDefined();
Expand Down

0 comments on commit 977edea

Please sign in to comment.