Skip to content

Commit

Permalink
fix: Support Jest mocked dates
Browse files Browse the repository at this point in the history
Jest provides two different implementations of fake timers - legacy and modern. The legacy implementation works with
RRule, while the modern one does not, as it uses a strict instance check. Compare the toString result of the object if
the value is not a Date instance.
  • Loading branch information
vbudovski committed Jun 25, 2022
1 parent 6de8945 commit 3d3c334
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 12 deletions.
10 changes: 5 additions & 5 deletions src/cache.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import IterResult, { IterArgs } from './iterresult'
import { clone, cloneDates } from './dateutil'
import { clone, cloneDates, isDate } from './dateutil'
import { isArray } from './helpers'

export type CacheKeys = 'before' | 'after' | 'between'
Expand All @@ -14,8 +14,8 @@ function argsMatch(
return left.every((date, i) => date.getTime() === right[i].getTime())
}

if (left instanceof Date) {
return right instanceof Date && left.getTime() === right.getTime()
if (isDate(left)) {
return isDate(right) && left.getTime() === right.getTime()
}

return left === right
Expand All @@ -38,7 +38,7 @@ export class Cache {
args?: Partial<IterArgs>
) {
if (value) {
value = value instanceof Date ? clone(value) : cloneDates(value)
value = isDate(value) ? clone(value) : cloneDates(value)
}

if (what === 'all') {
Expand Down Expand Up @@ -99,7 +99,7 @@ export class Cache {

return isArray(cached)
? cloneDates(cached)
: cached instanceof Date
: isDate(cached)
? clone(cached)
: cached
}
Expand Down
2 changes: 1 addition & 1 deletion src/dateutil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export const isLeapYear = function (year: number) {
}

export const isDate = function (value: unknown): value is Date {
return value instanceof Date
return value instanceof Date || (typeof value === 'object' && Object.prototype.toString.call(value) === '[object Date]')
}

export const isValidDate = function (value: unknown): value is Date {
Expand Down
4 changes: 2 additions & 2 deletions src/rruleset.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { RRule } from './rrule'
import { sort, timeToUntilString } from './dateutil'
import { sort, timeToUntilString, isDate } from './dateutil'
import { includes } from './helpers'
import IterResult from './iterresult'
import { iterSet } from './iterset'
Expand Down Expand Up @@ -206,7 +206,7 @@ function _addRule(rrule: RRule, collection: RRule[]) {
}

function _addDate(date: Date, collection: Date[]) {
if (!(date instanceof Date)) {
if (!isDate(date)) {
throw new TypeError(String(date) + ' is not Date instance')
}
if (!includes(collection.map(Number), Number(date))) {
Expand Down
7 changes: 3 additions & 4 deletions test/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { expect } from 'chai'
import { ExclusiveTestFunction, TestFunction } from 'mocha'
export { datetime } from '../../src/dateutil'
import { datetime } from '../../src/dateutil'
import { RRule, RRuleSet } from '../../src'
import { datetime, RRule, RRuleSet } from '../../src'
import { isDate } from '../../src/dateutil'

const assertDatesEqual = function (
actual: Date | Date[],
Expand All @@ -25,7 +24,7 @@ const assertDatesEqual = function (
for (let i = 0; i < expected.length; i++) {
const act = actual[i]
const exp = expected[i]
expect(exp instanceof Date ? exp.toString() : exp).to.equal(
expect(isDate(exp) ? exp.toString() : exp).to.equal(
act.toString(),
msg + (i + 1) + '/' + expected.length
)
Expand Down

0 comments on commit 3d3c334

Please sign in to comment.