Skip to content

Commit 468a6ec

Browse files
committed
refactor(tests): extract database spy setup into a helper function for beforeFind tests
1 parent 37f74c1 commit 468a6ec

File tree

1 file changed

+10
-25
lines changed

1 file changed

+10
-25
lines changed

spec/CloudCode.spec.js

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -201,15 +201,19 @@ describe('Cloud Code', () => {
201201
done();
202202
}
203203
});
204-
it('beforeFind can return object without DB operation', async () => {
204+
// Helper function to set up database spy
205+
function setupDatabaseSpy() {
205206
const config = Config.get('test');
206207
const databaseAdapter = config.database.adapter;
207-
const findSpy = spyOn(databaseAdapter, 'find').and.callThrough();
208+
return spyOn(databaseAdapter, 'find').and.callThrough();
209+
}
210+
211+
it('beforeFind can return object without DB operation', async () => {
212+
const findSpy = setupDatabaseSpy();
208213

209214
Parse.Cloud.beforeFind('TestObject', () => {
210215
return new Parse.Object('TestObject', { foo: 'bar' });
211216
});
212-
213217
Parse.Cloud.afterFind('TestObject', req => {
214218
expect(req.objects).toBeDefined();
215219
expect(req.objects[0].get('foo')).toBe('bar');
@@ -218,21 +222,16 @@ describe('Cloud Code', () => {
218222
const newObj = await new Parse.Query('TestObject').first();
219223
expect(newObj.className).toBe('TestObject');
220224
expect(newObj.toJSON()).toEqual({ foo: 'bar' });
221-
222225
expect(findSpy).not.toHaveBeenCalled();
223-
224226
await newObj.save();
225227
});
226228

227229
it('beforeFind can return array of objects without DB operation', async () => {
228-
const config = Config.get('test');
229-
const databaseAdapter = config.database.adapter;
230-
const findSpy = spyOn(databaseAdapter, 'find').and.callThrough();
230+
const findSpy = setupDatabaseSpy();
231231

232232
Parse.Cloud.beforeFind('TestObject', () => {
233233
return [new Parse.Object('TestObject', { foo: 'bar' })];
234234
});
235-
236235
Parse.Cloud.afterFind('TestObject', req => {
237236
expect(req.objects).toBeDefined();
238237
expect(req.objects[0].get('foo')).toBe('bar');
@@ -241,64 +240,50 @@ describe('Cloud Code', () => {
241240
const newObj = await new Parse.Query('TestObject').first();
242241
expect(newObj.className).toBe('TestObject');
243242
expect(newObj.toJSON()).toEqual({ foo: 'bar' });
244-
245243
expect(findSpy).not.toHaveBeenCalled();
246-
247244
await newObj.save();
248245
});
249246

250247
it('beforeFind can return object for get query without DB operation', async () => {
251-
const config = Config.get('test');
252-
const databaseAdapter = config.database.adapter;
253-
const findSpy = spyOn(databaseAdapter, 'find').and.callThrough();
248+
const findSpy = setupDatabaseSpy();
254249

255250
Parse.Cloud.beforeFind('TestObject', () => {
256251
return [new Parse.Object('TestObject', { foo: 'bar' })];
257252
});
258-
259253
Parse.Cloud.afterFind('TestObject', req => {
260254
expect(req.objects).toBeDefined();
261255
expect(req.objects[0].get('foo')).toBe('bar');
262256
});
263257

264258
const testObj = new Parse.Object('TestObject');
265259
await testObj.save();
266-
267260
findSpy.calls.reset();
268261

269262
const newObj = await new Parse.Query('TestObject').get(testObj.id);
270263
expect(newObj.className).toBe('TestObject');
271264
expect(newObj.toJSON()).toEqual({ foo: 'bar' });
272-
273265
expect(findSpy).not.toHaveBeenCalled();
274-
275266
await newObj.save();
276267
});
277268

278269
it('beforeFind can return empty array without DB operation', async () => {
279-
const config = Config.get('test');
280-
const databaseAdapter = config.database.adapter;
281-
const findSpy = spyOn(databaseAdapter, 'find').and.callThrough();
270+
const findSpy = setupDatabaseSpy();
282271

283272
Parse.Cloud.beforeFind('TestObject', () => {
284273
return [];
285274
});
286-
287275
Parse.Cloud.afterFind('TestObject', req => {
288276
expect(req.objects.length).toBe(0);
289277
});
290278

291279
const obj = new Parse.Object('TestObject');
292280
await obj.save();
293-
294281
findSpy.calls.reset();
295282

296283
const newObj = await new Parse.Query('TestObject').first();
297284
expect(newObj).toBeUndefined();
298-
299285
expect(findSpy).not.toHaveBeenCalled();
300286
});
301-
302287
const { maybeRunAfterFindTrigger } = require('../lib/triggers');
303288

304289
describe('maybeRunAfterFindTrigger - direct function tests', () => {

0 commit comments

Comments
 (0)