Skip to content

Commit

Permalink
chore: wrap setTime tests in describe and move down
Browse files Browse the repository at this point in the history
  • Loading branch information
sheerlox authored and intcreator committed Apr 22, 2023
1 parent 8e5fc90 commit 31989e0
Showing 1 changed file with 52 additions and 50 deletions.
102 changes: 52 additions & 50 deletions tests/cron.test.js
Expand Up @@ -452,56 +452,6 @@ describe('cron', () => {
});
});

it('should start, change time, start again', () => {
const callback = jest.fn();
const clock = sinon.useFakeTimers();

const job = new cron.CronJob('* * * * * *', callback);

job.start();
clock.tick(1000);

job.stop();
const time = cron.time('*/2 * * * * *');
job.setTime(time);
job.start();

clock.tick(4000);

clock.restore();
job.stop();
expect(callback).toHaveBeenCalledTimes(3);
});

it('should setTime with invalid object', () => {
const callback = jest.fn();
const job = new cron.CronJob('* * * * * *', callback);
expect(() => {
job.setTime(undefined);
}).toThrow();
});

it('should start, change time, exception', () => {
const callback = jest.fn();
const clock = sinon.useFakeTimers();

const job = new cron.CronJob('* * * * * *', callback);

const time = new Date();
job.start();

clock.tick(1000);

job.stop();
expect(() => {
job.setTime(time);
}).toThrow();

clock.restore();
job.stop();
expect(callback).toHaveBeenCalledTimes(1);
});

it('should scope onTick to running job', () => {
const clock = sinon.useFakeTimers();

Expand Down Expand Up @@ -894,6 +844,58 @@ describe('cron', () => {
});
});

describe('setTime', () => {
it('should start, change time, start again', () => {
const callback = jest.fn();
const clock = sinon.useFakeTimers();

const job = new cron.CronJob('* * * * * *', callback);

job.start();
clock.tick(1000);

job.stop();
const time = cron.time('*/2 * * * * *');
job.setTime(time);
job.start();

clock.tick(4000);

clock.restore();
job.stop();
expect(callback).toHaveBeenCalledTimes(3);
});

it('should setTime with invalid object', () => {
const callback = jest.fn();
const job = new cron.CronJob('* * * * * *', callback);
expect(() => {
job.setTime(undefined);
}).toThrow();
});

it('should start, change time, exception', () => {
const callback = jest.fn();
const clock = sinon.useFakeTimers();

const job = new cron.CronJob('* * * * * *', callback);

const time = new Date();
job.start();

clock.tick(1000);

job.stop();
expect(() => {
job.setTime(time);
}).toThrow();

clock.restore();
job.stop();
expect(callback).toHaveBeenCalledTimes(1);
});
});

it('should give the next date to run at', () => {
const callback = jest.fn();
const clock = sinon.useFakeTimers();
Expand Down

0 comments on commit 31989e0

Please sign in to comment.