Skip to content

Commit

Permalink
test: add tests for matchDate day of month / weekday behaviour
Browse files Browse the repository at this point in the history
See #270
  • Loading branch information
P4sca1 committed Apr 27, 2022
1 parent 8851590 commit c2dcdf1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/cron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ export class Cron {
startDay: number
): number | undefined {
if (startDay < 1) throw new Error('startDay must not be smaller than 1.')
// If only days are restricted: allow day based on day constraint only..
// If only days are restricted: allow day based on day constraint only.
// If only weekdays are restricted: allow day based on weekday constraint only.
// If both are restricted: allow day based on both day and weekday constraint. pick day that is closer to startDay.
// If none are restricted: return the day closest to startDay (respecting dir) that is allowed (or startDay itself).
Expand Down
18 changes: 18 additions & 0 deletions test/cron.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -528,4 +528,22 @@ describe('matchDate', () => {
expect(cron.matchDate(new Date(2001, 2, 12, 0, 38, 48))).toBeFalsy()
expect(cron.matchDate(new Date(2001, 2, 12, 0, 37, 50))).toBeFalsy()
})

// https://github.com/P4sca1/cron-schedule/issues/270
test('It should only match on weekday, when day of month is not restricted', () => {
const cron = parseCronExpression('* * * * 6') // Only match on Saturdays.
expect(cron.matchDate(new Date(2022, 4, 18))).toBeFalsy() // Monday
expect(cron.matchDate(new Date(2022, 4, 19))).toBeFalsy()
expect(cron.matchDate(new Date(2022, 4, 20))).toBeFalsy()
expect(cron.matchDate(new Date(2022, 4, 21))).toBeFalsy()
expect(cron.matchDate(new Date(2022, 4, 22))).toBeFalsy()
expect(cron.matchDate(new Date(2022, 4, 23))).toBeTruthy()
expect(cron.matchDate(new Date(2022, 4, 24))).toBeFalsy()
})

test('It should only match on day of month, when weekday is not restricted', () => {
const cron = parseCronExpression('* * 1 * *') // Only match on the first of every month.
expect(cron.matchDate(new Date(2022, 4, 1))).toBeTruthy()
expect(cron.matchDate(new Date(2022, 4, 2))).toBeFalsy()
})
})

0 comments on commit c2dcdf1

Please sign in to comment.