Skip to content

Commit c08b3f6

Browse files
committed
chore(tests): add spy to confirm DB is not accessed
1 parent 52d5594 commit c08b3f6

File tree

1 file changed

+55
-14
lines changed

1 file changed

+55
-14
lines changed

spec/CloudCode.spec.js

Lines changed: 55 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -202,60 +202,101 @@ describe('Cloud Code', () => {
202202
}
203203
});
204204
it('beforeFind can return object without DB operation', async () => {
205-
Parse.Cloud.beforeFind('beforeFind', () => {
205+
const config = Config.get('test');
206+
const databaseAdapter = config.database.adapter;
207+
const findSpy = spyOn(databaseAdapter, 'find').and.callThrough();
208+
209+
Parse.Cloud.beforeFind('TestObject', () => {
206210
return new Parse.Object('TestObject', { foo: 'bar' });
207211
});
208-
Parse.Cloud.afterFind('beforeFind', req => {
212+
213+
Parse.Cloud.afterFind('TestObject', req => {
209214
expect(req.objects).toBeDefined();
210215
expect(req.objects[0].get('foo')).toBe('bar');
211216
});
212-
const newObj = await new Parse.Query('beforeFind').first();
217+
218+
const newObj = await new Parse.Query('TestObject').first();
213219
expect(newObj.className).toBe('TestObject');
214220
expect(newObj.toJSON()).toEqual({ foo: 'bar' });
221+
222+
expect(findSpy).not.toHaveBeenCalled();
223+
215224
await newObj.save();
216225
});
217226

218227
it('beforeFind can return array of objects without DB operation', async () => {
219-
Parse.Cloud.beforeFind('beforeFind', () => {
228+
const config = Config.get('test');
229+
const databaseAdapter = config.database.adapter;
230+
const findSpy = spyOn(databaseAdapter, 'find').and.callThrough();
231+
232+
Parse.Cloud.beforeFind('TestObject', () => {
220233
return [new Parse.Object('TestObject', { foo: 'bar' })];
221234
});
222-
Parse.Cloud.afterFind('beforeFind', req => {
235+
236+
Parse.Cloud.afterFind('TestObject', req => {
223237
expect(req.objects).toBeDefined();
224238
expect(req.objects[0].get('foo')).toBe('bar');
225239
});
226-
const newObj = await new Parse.Query('beforeFind').first();
240+
241+
const newObj = await new Parse.Query('TestObject').first();
227242
expect(newObj.className).toBe('TestObject');
228243
expect(newObj.toJSON()).toEqual({ foo: 'bar' });
244+
245+
expect(findSpy).not.toHaveBeenCalled();
246+
229247
await newObj.save();
230248
});
231249

232250
it('beforeFind can return object for get query without DB operation', async () => {
233-
Parse.Cloud.beforeFind('beforeFind', () => {
251+
const config = Config.get('test');
252+
const databaseAdapter = config.database.adapter;
253+
const findSpy = spyOn(databaseAdapter, 'find').and.callThrough();
254+
255+
Parse.Cloud.beforeFind('TestObject', () => {
234256
return [new Parse.Object('TestObject', { foo: 'bar' })];
235257
});
236-
Parse.Cloud.afterFind('beforeFind', req => {
258+
259+
Parse.Cloud.afterFind('TestObject', req => {
237260
expect(req.objects).toBeDefined();
238261
expect(req.objects[0].get('foo')).toBe('bar');
239262
});
240-
const testObj = new Parse.Object('beforeFind');
263+
264+
const testObj = new Parse.Object('TestObject');
241265
await testObj.save();
242-
const newObj = await new Parse.Query('beforeFind').get(testObj.id);
266+
267+
findSpy.calls.reset();
268+
269+
const newObj = await new Parse.Query('TestObject').get(testObj.id);
243270
expect(newObj.className).toBe('TestObject');
244271
expect(newObj.toJSON()).toEqual({ foo: 'bar' });
272+
273+
expect(findSpy).not.toHaveBeenCalled();
274+
245275
await newObj.save();
246276
});
247277

248278
it('beforeFind can return empty array without DB operation', async () => {
249-
Parse.Cloud.beforeFind('beforeFind', () => {
279+
const config = Config.get('test');
280+
const databaseAdapter = config.database.adapter;
281+
const findSpy = spyOn(databaseAdapter, 'find').and.callThrough();
282+
283+
Parse.Cloud.beforeFind('TestObject', () => {
250284
return [];
251285
});
252-
Parse.Cloud.afterFind('beforeFind', req => {
286+
287+
Parse.Cloud.afterFind('TestObject', req => {
253288
expect(req.objects.length).toBe(0);
254289
});
255-
const obj = new Parse.Object('beforeFind');
290+
291+
const obj = new Parse.Object('TestObject');
256292
await obj.save();
257-
const newObj = await new Parse.Query('beforeFind').first();
293+
294+
findSpy.calls.reset();
295+
296+
const newObj = await new Parse.Query('TestObject').first();
258297
expect(newObj).toBeUndefined();
298+
299+
expect(findSpy).not.toHaveBeenCalled();
259300
});
260301

261302
const { maybeRunAfterFindTrigger } = require('../lib/triggers');

0 commit comments

Comments
 (0)