Skip to content

Commit

Permalink
Merge pull request #517 from jakubroztocil/fix-invalid-dates
Browse files Browse the repository at this point in the history
Fix invalid date formats in tests
  • Loading branch information
davidgoli authored Jun 9, 2022
2 parents 218cc58 + b315bdf commit f066c4e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
3 changes: 3 additions & 0 deletions src/datewithzone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ export class DateWithZone {
public tzid?: string | null

constructor(date: Date, tzid?: string | null) {
if (isNaN(date.getTime())) {
throw new RangeError('Invalid date passed to DateWithZone')
}
this.date = date
this.tzid = tzid
}
Expand Down
10 changes: 8 additions & 2 deletions test/datewithzone.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ it('returns the time of the date', () => {
expect(dt.getTime()).to.equal(d.getTime())
})

it('rejects invalid dates', () => {
expect(() => new DateWithZone(new Date(undefined))).to.throw(
'Invalid date passed to DateWithZone'
)
})

describe('rezonedDate', () => {
it('returns the original date when no zone is given', () => {
const d = new Date(Date.UTC(2010, 9, 5, 11, 0, 0))
Expand All @@ -42,11 +48,11 @@ describe('rezonedDate', () => {
const currentLocalDate = new Date(2000, 1, 6, 1, 0, 0)
setMockDate(currentLocalDate)

const d = new Date(Date.parse('20101005T110000'))
const d = new Date(Date.parse('2010-10-05T11:00:00'))
const dt = new DateWithZone(d, targetZone)
expect(dt.rezonedDate()).to.deep.equal(
expectedDate(
new Date(Date.parse('20101005T110000')),
new Date(Date.parse('2010-10-05T11:00:00')),
currentLocalDate,
targetZone
)
Expand Down
20 changes: 10 additions & 10 deletions test/rruleset.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -622,11 +622,11 @@ describe('RRuleSet', function () {

set.tzid(targetZone)

set.rdate(new Date(Date.parse('20020301T090000')))
set.rdate(new Date(Date.parse('2002-03-01T09:00:00')))

expect(set.all()).to.deep.equal([
expectedDate(
new Date(Date.parse('20020301T090000')),
new Date(Date.parse('2002-03-01T09:00:00')),
currentLocalDate,
targetZone
),
Expand Down Expand Up @@ -826,8 +826,8 @@ describe('RRuleSet', function () {

describe('getters', () => {
it('rrules()', () => {
let set = new RRuleSet()
let rrule = new RRule({
const set = new RRuleSet()
const rrule = new RRule({
freq: RRule.YEARLY,
count: 2,
dtstart: parse('19600101T090000'),
Expand All @@ -839,8 +839,8 @@ describe('RRuleSet', function () {
})

it('exrules()', () => {
let set = new RRuleSet()
let rrule = new RRule({
const set = new RRuleSet()
const rrule = new RRule({
freq: RRule.YEARLY,
count: 2,
dtstart: parse('19600101T090000'),
Expand All @@ -852,16 +852,16 @@ describe('RRuleSet', function () {
})

it('rdates()', () => {
let set = new RRuleSet()
let dt = parse('19610201T090000')
const set = new RRuleSet()
const dt = parse('19610201T090000')
set.rdate(dt)

expect(set.rdates()).eql([dt])
})

it('exdates()', () => {
let set = new RRuleSet()
let dt = parse('19610201T090000')
const set = new RRuleSet()
const dt = parse('19610201T090000')
set.exdate(dt)

expect(set.exdates()).eql([dt])
Expand Down

0 comments on commit f066c4e

Please sign in to comment.