Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Support Jest mocked dates #526

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
6 changes: 5 additions & 1 deletion src/dateutil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ 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]')
)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like the lodash isDate implementation - nice

}

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
4 changes: 2 additions & 2 deletions test/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { expect } from 'chai'
import { ExclusiveTestFunction, TestFunction } from 'mocha'
export { datetime } from '../../src/dateutil'
import { datetime } from '../../src/dateutil'
import { datetime, isDate } from '../../src/dateutil'
import { RRule, RRuleSet } from '../../src'

const assertDatesEqual = function (
Expand All @@ -25,7 +25,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