Skip to content

Commit b157aa2

Browse files
committed
refactor(tests): move repetitive code into beforeEach block
1 parent 468a6ec commit b157aa2

File tree

1 file changed

+63
-67
lines changed

1 file changed

+63
-67
lines changed

spec/CloudCode.spec.js

Lines changed: 63 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -201,88 +201,84 @@ describe('Cloud Code', () => {
201201
done();
202202
}
203203
});
204-
// Helper function to set up database spy
205-
function setupDatabaseSpy() {
206-
const config = Config.get('test');
207-
const databaseAdapter = config.database.adapter;
208-
return spyOn(databaseAdapter, 'find').and.callThrough();
209-
}
210204

211-
it('beforeFind can return object without DB operation', async () => {
212-
const findSpy = setupDatabaseSpy();
205+
describe('beforeFind without DB operations', () => {
206+
let findSpy;
213207

214-
Parse.Cloud.beforeFind('TestObject', () => {
215-
return new Parse.Object('TestObject', { foo: 'bar' });
216-
});
217-
Parse.Cloud.afterFind('TestObject', req => {
218-
expect(req.objects).toBeDefined();
219-
expect(req.objects[0].get('foo')).toBe('bar');
208+
beforeEach(() => {
209+
const config = Config.get('test');
210+
const databaseAdapter = config.database.adapter;
211+
findSpy = spyOn(databaseAdapter, 'find').and.callThrough();
220212
});
221213

222-
const newObj = await new Parse.Query('TestObject').first();
223-
expect(newObj.className).toBe('TestObject');
224-
expect(newObj.toJSON()).toEqual({ foo: 'bar' });
225-
expect(findSpy).not.toHaveBeenCalled();
226-
await newObj.save();
227-
});
228-
229-
it('beforeFind can return array of objects without DB operation', async () => {
230-
const findSpy = setupDatabaseSpy();
214+
it('beforeFind can return object without DB operation', async () => {
215+
Parse.Cloud.beforeFind('TestObject', () => {
216+
return new Parse.Object('TestObject', { foo: 'bar' });
217+
});
218+
Parse.Cloud.afterFind('TestObject', req => {
219+
expect(req.objects).toBeDefined();
220+
expect(req.objects[0].get('foo')).toBe('bar');
221+
});
231222

232-
Parse.Cloud.beforeFind('TestObject', () => {
233-
return [new Parse.Object('TestObject', { foo: 'bar' })];
223+
const newObj = await new Parse.Query('TestObject').first();
224+
expect(newObj.className).toBe('TestObject');
225+
expect(newObj.toJSON()).toEqual({ foo: 'bar' });
226+
expect(findSpy).not.toHaveBeenCalled();
227+
await newObj.save();
234228
});
235-
Parse.Cloud.afterFind('TestObject', req => {
236-
expect(req.objects).toBeDefined();
237-
expect(req.objects[0].get('foo')).toBe('bar');
238-
});
239-
240-
const newObj = await new Parse.Query('TestObject').first();
241-
expect(newObj.className).toBe('TestObject');
242-
expect(newObj.toJSON()).toEqual({ foo: 'bar' });
243-
expect(findSpy).not.toHaveBeenCalled();
244-
await newObj.save();
245-
});
246229

247-
it('beforeFind can return object for get query without DB operation', async () => {
248-
const findSpy = setupDatabaseSpy();
230+
it('beforeFind can return array of objects without DB operation', async () => {
231+
Parse.Cloud.beforeFind('TestObject', () => {
232+
return [new Parse.Object('TestObject', { foo: 'bar' })];
233+
});
234+
Parse.Cloud.afterFind('TestObject', req => {
235+
expect(req.objects).toBeDefined();
236+
expect(req.objects[0].get('foo')).toBe('bar');
237+
});
249238

250-
Parse.Cloud.beforeFind('TestObject', () => {
251-
return [new Parse.Object('TestObject', { foo: 'bar' })];
239+
const newObj = await new Parse.Query('TestObject').first();
240+
expect(newObj.className).toBe('TestObject');
241+
expect(newObj.toJSON()).toEqual({ foo: 'bar' });
242+
expect(findSpy).not.toHaveBeenCalled();
243+
await newObj.save();
252244
});
253-
Parse.Cloud.afterFind('TestObject', req => {
254-
expect(req.objects).toBeDefined();
255-
expect(req.objects[0].get('foo')).toBe('bar');
256-
});
257-
258-
const testObj = new Parse.Object('TestObject');
259-
await testObj.save();
260-
findSpy.calls.reset();
261245

262-
const newObj = await new Parse.Query('TestObject').get(testObj.id);
263-
expect(newObj.className).toBe('TestObject');
264-
expect(newObj.toJSON()).toEqual({ foo: 'bar' });
265-
expect(findSpy).not.toHaveBeenCalled();
266-
await newObj.save();
267-
});
246+
it('beforeFind can return object for get query without DB operation', async () => {
247+
Parse.Cloud.beforeFind('TestObject', () => {
248+
return [new Parse.Object('TestObject', { foo: 'bar' })];
249+
});
250+
Parse.Cloud.afterFind('TestObject', req => {
251+
expect(req.objects).toBeDefined();
252+
expect(req.objects[0].get('foo')).toBe('bar');
253+
});
268254

269-
it('beforeFind can return empty array without DB operation', async () => {
270-
const findSpy = setupDatabaseSpy();
255+
const testObj = new Parse.Object('TestObject');
256+
await testObj.save();
257+
findSpy.calls.reset();
271258

272-
Parse.Cloud.beforeFind('TestObject', () => {
273-
return [];
274-
});
275-
Parse.Cloud.afterFind('TestObject', req => {
276-
expect(req.objects.length).toBe(0);
259+
const newObj = await new Parse.Query('TestObject').get(testObj.id);
260+
expect(newObj.className).toBe('TestObject');
261+
expect(newObj.toJSON()).toEqual({ foo: 'bar' });
262+
expect(findSpy).not.toHaveBeenCalled();
263+
await newObj.save();
277264
});
278265

279-
const obj = new Parse.Object('TestObject');
280-
await obj.save();
281-
findSpy.calls.reset();
266+
it('beforeFind can return empty array without DB operation', async () => {
267+
Parse.Cloud.beforeFind('TestObject', () => {
268+
return [];
269+
});
270+
Parse.Cloud.afterFind('TestObject', req => {
271+
expect(req.objects.length).toBe(0);
272+
});
282273

283-
const newObj = await new Parse.Query('TestObject').first();
284-
expect(newObj).toBeUndefined();
285-
expect(findSpy).not.toHaveBeenCalled();
274+
const obj = new Parse.Object('TestObject');
275+
await obj.save();
276+
findSpy.calls.reset();
277+
278+
const newObj = await new Parse.Query('TestObject').first();
279+
expect(newObj).toBeUndefined();
280+
expect(findSpy).not.toHaveBeenCalled();
281+
});
286282
});
287283
const { maybeRunAfterFindTrigger } = require('../lib/triggers');
288284

0 commit comments

Comments
 (0)