Skip to content

Commit

Permalink
chore: refactor schedule method in schedule.ts and add tests in pulse…
Browse files Browse the repository at this point in the history
….spec.ts
  • Loading branch information
code-xhyun committed Jun 2, 2024
1 parent 52eda2e commit dfed3ee
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/pulse/schedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const schedule = function schedule<T extends JobAttributesData>(
this: Pulse,
when: string | Date,
names: string | string[],
data?: T,
data?: T
) {
/**
* Internal method that creates a job with given date
Expand All @@ -26,7 +26,11 @@ export const schedule = function schedule<T extends JobAttributesData>(
* @param data data to send to job
* @returns instance of new job
*/
const createJob = async <T extends JobAttributesData>(when: string | Date, name: string, data: T): Promise<Job<T>> => {
const createJob = async <T extends JobAttributesData>(
when: string | Date,
name: string,
data: T
): Promise<Job<T>> => {
const job = this.create(name, data);

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

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)) {
Expand Down
21 changes: 21 additions & 0 deletions test/unit/pulse.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
});
});
});
});

1 comment on commit dfed3ee

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines Statements Branches Functions
Coverage: 54%
56.86% (580/1020) 41.91% (127/303) 51.57% (82/159)

Pulse Test Report

Tests Skipped Failures Errors Time
63 0 💤 0 ❌ 0 🔥 9.324s ⏱️

Please sign in to comment.