Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jaclarke committed May 8, 2019
1 parent c6bf270 commit 30fc84d
Show file tree
Hide file tree
Showing 7 changed files with 1,003 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CronosExpression } from './expression'
import { CronosTask } from './scheduler'
import { CronosDate, CronosTimezone } from './date'
import { CronosTimezone } from './date'

export function scheduleTask(
cronString: string,
Expand Down Expand Up @@ -28,4 +28,4 @@ export function validate(cronString: string) {
return true
}

export { CronosExpression, CronosTask, CronosDate, CronosTimezone }
export { CronosExpression, CronosTask, CronosTimezone }
67 changes: 67 additions & 0 deletions tests/api.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
const { CronosExpression, validate } = require('../lib')
const { CronosDate } = require('../lib/date')

describe('CronosExpression.nextDate() defaults', () => {
const RealDate = Date

function mockDate (isoDate) {
global.Date = class extends RealDate {
constructor(...theArgs) {
if (theArgs.length) {
return new RealDate(...theArgs);
}
return new RealDate(isoDate);
}

static now() {
return new RealDate(isoDate).getTime();
}
}
}

afterEach(() => {
global.Date = RealDate
})

test('', () => {
mockDate('2019-04-21T11:23:45Z')
expect(
CronosExpression.parse('* * * * *').nextDate()
).toEqual(new Date('2019-04-21T11:24:00Z'))
})

test('', () => {
mockDate('2019-04-21T11:23:45Z')
expect(
CronosExpression.parse('* * * * *').nextNDates()
).toEqual([
new Date('2019-04-21T11:24:00Z'),
new Date('2019-04-21T11:25:00Z'),
new Date('2019-04-21T11:26:00Z'),
new Date('2019-04-21T11:27:00Z'),
new Date('2019-04-21T11:28:00Z')
])
})
})

describe('Validate cron string', () => {
test('Valid string', () => {
expect(validate('0 10 16 4,L Jun * 2035')).toEqual(true)
})

test('Invalid string', () => {
expect(validate('0 10W 16 4,L Jun * 2035')).toEqual(false)
})
})

test('CronosDate.copyWith()', () => {
const date = new CronosDate(2019, 4, 21, 11, 23, 45)

expect(date.copyWith()).toEqual(date)

expect(
date.copyWith({
year: 2020, month: 5, day: 22, hour: 12, minute: 24, second: 46
})
).toEqual(new CronosDate(2020, 5, 22, 12, 24, 46))
})
149 changes: 149 additions & 0 deletions tests/extended.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
const { CronosExpression } = require('../lib/expression')

test('6 stars (* * * * * *)', () => {
expect(
CronosExpression.parse('* * * * * *')
.nextNDates(new Date(2019, 3, 21, 11, 23, 45), 5)
).toEqual([
new Date(2019, 3, 21, 11, 23, 46),
new Date(2019, 3, 21, 11, 23, 47),
new Date(2019, 3, 21, 11, 23, 48),
new Date(2019, 3, 21, 11, 23, 49),
new Date(2019, 3, 21, 11, 23, 50)
])
})

describe('Last day (L)', () => {
test('Last day of month', () => {
expect(
CronosExpression.parse('0 0 L * *')
.nextNDates(new Date(2019, 0, 21, 11, 23, 45), 5)
).toEqual([
new Date(2019, 0, 31, 0, 0, 0),
new Date(2019, 1, 28, 0, 0, 0),
new Date(2019, 2, 31, 0, 0, 0),
new Date(2019, 3, 30, 0, 0, 0),
new Date(2019, 4, 31, 0, 0, 0)
])
})

test('Last wednesday of month', () => {
expect(
CronosExpression.parse('0 0 * * WedL')
.nextNDates(new Date(2019, 0, 21, 11, 23, 45), 5)
).toEqual([
new Date(2019, 0, 30, 0, 0, 0),
new Date(2019, 1, 27, 0, 0, 0),
new Date(2019, 2, 27, 0, 0, 0),
new Date(2019, 3, 24, 0, 0, 0),
new Date(2019, 4, 29, 0, 0, 0)
])
})
})

describe('Nearest weekday (W)', () => {
test('Nearest weekday to 1st', () => {
expect(
CronosExpression.parse('0 0 1W * *')
.nextNDates(new Date(2019, 0, 21, 11, 23, 45), 5)
).toEqual([
new Date(2019, 1, 1, 0, 0, 0),
new Date(2019, 2, 1, 0, 0, 0),
new Date(2019, 3, 1, 0, 0, 0),
new Date(2019, 4, 1, 0, 0, 0),
new Date(2019, 5, 3, 0, 0, 0)
])
})

test('Nearest weekday to 30th', () => {
expect(
CronosExpression.parse('0 0 30W * *')
.nextNDates(new Date(2019, 1, 21, 11, 23, 45), 5)
).toEqual([
new Date(2019, 1, 28, 0, 0, 0),
new Date(2019, 2, 29, 0, 0, 0),
new Date(2019, 3, 30, 0, 0, 0),
new Date(2019, 4, 30, 0, 0, 0),
new Date(2019, 5, 28, 0, 0, 0),
])
})

test('Last weekday of month', () => {
expect(
CronosExpression.parse('0 0 LW * *')
.nextNDates(new Date(2019, 0, 21, 11, 23, 45), 5)
).toEqual([
new Date(2019, 0, 31, 0, 0, 0),
new Date(2019, 1, 28, 0, 0, 0),
new Date(2019, 2, 29, 0, 0, 0),
new Date(2019, 3, 30, 0, 0, 0),
new Date(2019, 4, 31, 0, 0, 0)
])
})
})

describe('Nth of month (#)', () => {
test('2st Fri of month', () => {
expect(
CronosExpression.parse('0 0 * * Fri#2')
.nextNDates(new Date(2019, 3, 21, 11, 23, 45), 5)
).toEqual([
new Date(2019, 4, 10, 0, 0, 0),
new Date(2019, 5, 14, 0, 0, 0),
new Date(2019, 6, 12, 0, 0, 0),
new Date(2019, 7, 9, 0, 0, 0),
new Date(2019, 8, 13, 0, 0, 0)
])
})

test('5th Thu of month', () => {
expect(
CronosExpression.parse('0 0 * * Thu#5')
.nextNDates(new Date(2019, 3, 21, 11, 23, 45), 5)
).toEqual([
new Date(2019, 4, 30, 0, 0, 0),
new Date(2019, 7, 29, 0, 0, 0),
new Date(2019, 9, 31, 0, 0, 0),
new Date(2020, 0, 30, 0, 0, 0),
new Date(2020, 3, 30, 0, 0, 0)
])
})

test('0th Tue of month is invalid', () => {
expect(
() => CronosExpression.parse('0 0 * * Tue#0')
).toThrow()
})

test('6th Mon of month is invalid', () => {
expect(
() => CronosExpression.parse('0 0 * * Mon#6')
).toThrow()
})

test('3.5th Tue of month is invalid', () => {
expect(
() => CronosExpression.parse('0 0 * * Tue#3.5')
).toThrow()
})

test('"fourth" Mon of month is invalid', () => {
expect(
() => CronosExpression.parse('0 0 * * Mon#fourth')
).toThrow()
})

})

describe('Year field', () => {
test('16:10, 4th and last of Jun, 2035', () => {
expect(
CronosExpression.parse('0 10 16 4,L Jun * 2035')
.nextNDates(new Date(2019, 3, 21, 11, 23, 45))
).toEqual([
new Date(2035, 5, 4, 16, 10, 0),
new Date(2035, 5, 30, 16, 10, 0)
])
})

})
Loading

0 comments on commit 30fc84d

Please sign in to comment.