Skip to content

Commit dfed3ee

Browse files
committed
chore: refactor schedule method in schedule.ts and add tests in pulse.spec.ts
1 parent 52eda2e commit dfed3ee

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

src/pulse/schedule.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export const schedule = function schedule<T extends JobAttributesData>(
1717
this: Pulse,
1818
when: string | Date,
1919
names: string | string[],
20-
data?: T,
20+
data?: T
2121
) {
2222
/**
2323
* Internal method that creates a job with given date
@@ -26,7 +26,11 @@ export const schedule = function schedule<T extends JobAttributesData>(
2626
* @param data data to send to job
2727
* @returns instance of new job
2828
*/
29-
const createJob = async <T extends JobAttributesData>(when: string | Date, name: string, data: T): Promise<Job<T>> => {
29+
const createJob = async <T extends JobAttributesData>(
30+
when: string | Date,
31+
name: string,
32+
data: T
33+
): Promise<Job<T>> => {
3034
const job = this.create(name, data);
3135

3236
await job.schedule(when).save();
@@ -59,7 +63,7 @@ export const schedule = function schedule<T extends JobAttributesData>(
5963

6064
if (typeof names === 'string') {
6165
debug('Pulse.schedule(%s, %O, [%O], cb)', when, names);
62-
return createJob(when, names, data || {} as T);
66+
return createJob(when, names, data || ({} as T));
6367
}
6468

6569
if (Array.isArray(names)) {

test/unit/pulse.spec.ts

+21
Original file line numberDiff line numberDiff line change
@@ -376,5 +376,26 @@ describe('Test Pulse', () => {
376376
expect(count).toBe(1);
377377
});
378378
});
379+
380+
describe('Test schedule method', () => {
381+
test('creates and schedules a job', async () => {
382+
await globalPulseInstance.schedule('2024-06-03T10:00:00Z', 'sendEmail', { to: 'some guy' });
383+
const jobs = await globalPulseInstance.jobs({ name: 'sendEmail' });
384+
expect(jobs.length).toBe(1);
385+
});
386+
387+
test('creates and schedules multiple jobs', async () => {
388+
await globalPulseInstance.schedule('2024-06-03T10:00:00Z', ['sendEmail', 'some job'], { to: 'some guy' });
389+
const jobs = await globalPulseInstance.jobs();
390+
expect(jobs.length).toBe(2);
391+
});
392+
393+
test('checks if job is scheduled correctly', async () => {
394+
await globalPulseInstance.schedule('2024-06-03T10:00:00Z', 'sendEmail', { to: 'some guy' });
395+
const jobs = await globalPulseInstance.jobs({ name: 'sendEmail' });
396+
const job = jobs[0];
397+
expect(job.attrs.nextRunAt).toEqual(new Date('2024-06-03T10:00:00Z'));
398+
});
399+
});
379400
});
380401
});

0 commit comments

Comments
 (0)