Skip to content
This repository has been archived by the owner on Feb 10, 2021. It is now read-only.

Commit

Permalink
test(misc): write tests for the parsing of dates
Browse files Browse the repository at this point in the history
  • Loading branch information
jordond committed May 26, 2019
1 parent ce8484a commit b2fba38
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 8 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ I did my best to correctly add types for all of the supported endpoints. However
Add using yarn or npm

```bash
yarn add darkskyy-api
yarn add darksky-api
```

## Usage
Expand Down Expand Up @@ -59,9 +59,9 @@ Either be a UNIX timestamp or a string formatted as follows:

The library will try it's best to parse the Date string you pass in, so you don't _need_ to supply it in the above format. But for safety its probably best.

Timezone should either be omitted (to refer to local time for the location being requested),
`Z` (referring to GMT time), or +[HH][mm] or -[HH][mm] for an offset from GMT
in hours and minutes.
> Timezone should either be omitted (to refer to local time for the location being requested),
> `Z` (referring to GMT time), or +[HH][mm] or -[HH][mm] for an offset from GMT
> in hours and minutes.
## 1. DarkSky class

Expand Down
10 changes: 6 additions & 4 deletions src/misc/util.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import format from 'date-fns/format'
import isDate from 'date-fns/is_date'
import isValid from 'date-fns/is_valid'
import parse from 'date-fns/parse'

import { NumberString, TimeMachineRequest } from '../types'
import { badRequest } from './errors'
Expand All @@ -16,8 +17,8 @@ export const DARKSKY_DATE_FORMAT = 'YYYY-MM-DDTHH:mm:ssZZ'
* @param date Date string or object to format using [[DARKSKY_DATE_FORMAT]].
*/
export function formatDateString(date: string | Date): string {
const target = isDate(date) ? (date as Date) : new Date(date)
if (!isValid(target)) {
const target: Date = isString(date) ? parse(date) : (date as Date)
if (!isDate(target) || !isValid(target)) {
throw badRequest(`'${target}' is not a valid Date object.`)
}

Expand All @@ -34,9 +35,10 @@ export function formatDateString(date: string | Date): string {
*/
export function formatTimeMachineTime({ time }: TimeMachineRequest) {
if (!time) return undefined
else if (Number.isInteger(time as number)) {
else if (isString(time) || isDate(time)) return formatDateString(time as string | Date)
else if (isNumber(time as number)) {
return formatDateString(new Date((time as number) * 1000))
} else if (isString(time) || isDate(time)) return formatDateString(time as string | Date)
}
}

/**
Expand Down
75 changes: 75 additions & 0 deletions test/misc/util.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import format from 'date-fns/format'

import {
DARKSKY_DATE_FORMAT,
formatDateString,
formatTimeMachineTime,
isNumber,
isString
} from '../../src/misc/util'

describe('Util', () => {
describe('isString', () => {
it('should return true on a valid string', () => {
expect(isString('I am a string')).toBeTruthy()
})

it('should return false on any other type', () => {
expect(isString(42)).toBeFalsy()
expect(isString({})).toBeFalsy()
expect(isString([42])).toBeFalsy()
})
})

describe('isNumber', () => {
it('should be true if it is a valid number', () => {
expect(isNumber(42)).toBeTruthy()
expect(isNumber(-42)).toBeTruthy()
expect(isNumber(42.24)).toBeTruthy()
expect(isNumber(-42.24)).toBeTruthy()
})

it('should parse a number string', () => {
expect(isNumber('42')).toBeTruthy()
})

it('should be false if passed an invalid number string', () => {
expect(isNumber('foo')).toBeFalsy()
expect(isNumber('')).toBeFalsy()
})
})

describe('formatTimeMachineTime', () => {
it('should handle an undefined time', () => {
expect(formatTimeMachineTime({} as any)).toBeUndefined()
})

it('should handle a UNIX timestamp', () => {
const expected = format(2222222 * 1000, DARKSKY_DATE_FORMAT)
const actual = formatTimeMachineTime({ time: 2222222 } as any)

expect(actual).toEqual(expected)
})

it('should handle a date object', () => {
const time = new Date()
const expected = format(time, DARKSKY_DATE_FORMAT)
const actual = formatTimeMachineTime({ time } as any)

expect(actual).toEqual(expected)
})

it('should handle a date string', () => {
const time = 'May 5 2019'
const expected = format(time, DARKSKY_DATE_FORMAT)
const actual = formatTimeMachineTime({ time } as any)

expect(actual).toEqual(expected)
})

it('should handle invalid type', () => {
expect(() => formatDateString({ time: 'imabaddate' } as any)).toThrow()
expect(formatTimeMachineTime({ time: { foo: 42 } } as any)).toBeUndefined()
})
})
})

0 comments on commit b2fba38

Please sign in to comment.