Skip to content

Commit 00b104e

Browse files
committed
test: Add direct function tests for maybeRunAfterFindTrigger
1 parent 546e5b0 commit 00b104e

File tree

1 file changed

+167
-0
lines changed

1 file changed

+167
-0
lines changed

spec/CloudCode.spec.js

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,173 @@ describe('Cloud Code', () => {
258258
expect(newObj).toBeUndefined();
259259
});
260260

261+
const { maybeRunAfterFindTrigger } = require('../lib/triggers');
262+
263+
describe('maybeRunAfterFindTrigger - direct function tests', () => {
264+
const testConfig = {
265+
applicationId: 'test',
266+
logLevels: { triggerBeforeSuccess: 'info', triggerAfter: 'info' },
267+
};
268+
269+
it('should convert Parse.Object instances to JSON when no trigger defined', async () => {
270+
const className = 'TestParseObjectDirect_' + Date.now();
271+
272+
const parseObj1 = new Parse.Object(className);
273+
parseObj1.set('name', 'test1');
274+
parseObj1.id = 'obj1';
275+
276+
const parseObj2 = new Parse.Object(className);
277+
parseObj2.set('name', 'test2');
278+
parseObj2.id = 'obj2';
279+
280+
const result = await maybeRunAfterFindTrigger(
281+
'afterFind',
282+
null,
283+
className,
284+
[parseObj1, parseObj2],
285+
testConfig,
286+
null,
287+
{}
288+
);
289+
290+
expect(result).toBeDefined();
291+
expect(Array.isArray(result)).toBe(true);
292+
expect(result.length).toBe(2);
293+
expect(result[0].name).toBe('test1');
294+
expect(result[1].name).toBe('test2');
295+
});
296+
297+
it('should handle null/undefined objectsInput when no trigger', async () => {
298+
const className = 'TestNullDirect_' + Date.now();
299+
300+
// Test null
301+
const resultNull = await maybeRunAfterFindTrigger(
302+
'afterFind',
303+
null,
304+
className,
305+
null, // null objectsInput
306+
testConfig,
307+
null,
308+
{}
309+
);
310+
expect(resultNull).toEqual([]);
311+
312+
const resultUndefined = await maybeRunAfterFindTrigger(
313+
'afterFind',
314+
null,
315+
className,
316+
undefined,
317+
testConfig,
318+
null,
319+
{}
320+
);
321+
expect(resultUndefined).toEqual([]);
322+
323+
const resultEmpty = await maybeRunAfterFindTrigger(
324+
'afterFind',
325+
null,
326+
className,
327+
[], // empty array
328+
testConfig,
329+
null,
330+
{}
331+
);
332+
expect(resultEmpty).toEqual([]);
333+
});
334+
335+
it('should handle plain object query with where clause', async () => {
336+
const className = 'TestQueryWhereDirect_' + Date.now();
337+
let receivedQuery = null;
338+
339+
Parse.Cloud.afterFind(className, req => {
340+
receivedQuery = req.query;
341+
return req.objects;
342+
});
343+
344+
const mockObject = { id: 'test123', className: className, name: 'test' };
345+
346+
const result = await maybeRunAfterFindTrigger(
347+
'afterFind',
348+
null,
349+
className,
350+
[mockObject],
351+
testConfig,
352+
{ where: { name: 'test' }, limit: 10 },
353+
{}
354+
);
355+
356+
expect(receivedQuery).toBeInstanceOf(Parse.Query);
357+
expect(result).toBeDefined();
358+
});
359+
360+
it('should handle plain object query without where clause', async () => {
361+
const className = 'TestQueryNoWhereDirect_' + Date.now();
362+
let receivedQuery = null;
363+
364+
Parse.Cloud.afterFind(className, req => {
365+
receivedQuery = req.query;
366+
return req.objects;
367+
});
368+
369+
const mockObject = { id: 'test456', className: className, name: 'test' };
370+
371+
const result = await maybeRunAfterFindTrigger(
372+
'afterFind',
373+
null,
374+
className,
375+
[mockObject],
376+
testConfig,
377+
{ limit: 5, skip: 0 },
378+
{}
379+
);
380+
381+
expect(receivedQuery).toBeInstanceOf(Parse.Query);
382+
expect(result).toBeDefined();
383+
});
384+
385+
it('should create default query for invalid query parameter', async () => {
386+
const className = 'TestInvalidQueryDirect_' + Date.now();
387+
let receivedQuery = null;
388+
389+
Parse.Cloud.afterFind(className, req => {
390+
receivedQuery = req.query;
391+
return req.objects;
392+
});
393+
394+
const mockObject = { id: 'test789', className: className, name: 'test' };
395+
396+
// Test avec string (invalide)
397+
await maybeRunAfterFindTrigger(
398+
'afterFind',
399+
null,
400+
className,
401+
[mockObject],
402+
testConfig,
403+
'invalid_query_string',
404+
{}
405+
);
406+
407+
expect(receivedQuery).toBeInstanceOf(Parse.Query);
408+
expect(receivedQuery.className).toBe(className);
409+
410+
// Reset pour test suivant
411+
receivedQuery = null;
412+
413+
await maybeRunAfterFindTrigger(
414+
'afterFind',
415+
null,
416+
className,
417+
[mockObject],
418+
testConfig,
419+
null,
420+
{}
421+
);
422+
423+
expect(receivedQuery).toBeInstanceOf(Parse.Query);
424+
expect(receivedQuery.className).toBe(className);
425+
});
426+
});
427+
261428
it('beforeSave rejection with custom error code', function (done) {
262429
Parse.Cloud.beforeSave('BeforeSaveFailWithErrorCode', function () {
263430
throw new Parse.Error(999, 'Nope');

0 commit comments

Comments
 (0)