diff --git a/src/pulse/schedule.ts b/src/pulse/schedule.ts index 0d7c048..ca0b453 100644 --- a/src/pulse/schedule.ts +++ b/src/pulse/schedule.ts @@ -17,7 +17,7 @@ export const schedule = function schedule( this: Pulse, when: string | Date, names: string | string[], - data?: T, + data?: T ) { /** * Internal method that creates a job with given date @@ -26,7 +26,11 @@ export const schedule = function schedule( * @param data data to send to job * @returns instance of new job */ - const createJob = async (when: string | Date, name: string, data: T): Promise> => { + const createJob = async ( + when: string | Date, + name: string, + data: T + ): Promise> => { const job = this.create(name, data); await job.schedule(when).save(); @@ -59,7 +63,7 @@ export const schedule = function schedule( if (typeof names === 'string') { debug('Pulse.schedule(%s, %O, [%O], cb)', when, names); - return createJob(when, names, data || {} as T); + return createJob(when, names, data || ({} as T)); } if (Array.isArray(names)) { diff --git a/test/unit/pulse.spec.ts b/test/unit/pulse.spec.ts index ce03269..172b424 100644 --- a/test/unit/pulse.spec.ts +++ b/test/unit/pulse.spec.ts @@ -376,5 +376,26 @@ describe('Test Pulse', () => { expect(count).toBe(1); }); }); + + describe('Test schedule method', () => { + test('creates and schedules a job', async () => { + await globalPulseInstance.schedule('2024-06-03T10:00:00Z', 'sendEmail', { to: 'some guy' }); + const jobs = await globalPulseInstance.jobs({ name: 'sendEmail' }); + expect(jobs.length).toBe(1); + }); + + test('creates and schedules multiple jobs', async () => { + await globalPulseInstance.schedule('2024-06-03T10:00:00Z', ['sendEmail', 'some job'], { to: 'some guy' }); + const jobs = await globalPulseInstance.jobs(); + expect(jobs.length).toBe(2); + }); + + test('checks if job is scheduled correctly', async () => { + await globalPulseInstance.schedule('2024-06-03T10:00:00Z', 'sendEmail', { to: 'some guy' }); + const jobs = await globalPulseInstance.jobs({ name: 'sendEmail' }); + const job = jobs[0]; + expect(job.attrs.nextRunAt).toEqual(new Date('2024-06-03T10:00:00Z')); + }); + }); }); });