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

test: refactor tests for the parse util #2

Open
wants to merge 4 commits into
base: v0
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
40 changes: 29 additions & 11 deletions packages/time/src/tests/isValidDate.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,34 @@
import {describe, expect, test} from 'vitest';
import {isValidDate} from '../utils/isValidDate';
import { describe, expect, test } from 'vitest'
import { isValidDate } from '../utils/isValidDate'

describe('isValidDate', () => {
test('should return true for a valid date', () => {
expect(isValidDate(new Date())).toBe(true);
});
const date = new Date();
expect(isValidDate(date)).toBe(true)
})

test('should return false for an invalid date', () => {
expect(isValidDate(new Date("invalid"))).toBe(false);
});
test.each([
'2021-10-10',
new Date('invalid'),
{},
undefined,
null,
NaN,
0,
])('should return false for invalid date %p', (date) => {
expect(isValidDate(date)).toBe(false)
})

test("should return false for null", () => {
expect(isValidDate(null)).toBe(false);
});
});
test('should assert type guards correctly', () => {
const notADate = 'not a date';
if (isValidDate(notADate)) {
expect(notADate).toBeInstanceOf(Date)
notADate.getDate()
} else {
expect(() => {
// @ts-expect-error
notADate.getTime()
}).toThrowError()
}
})
})
164 changes: 88 additions & 76 deletions packages/time/src/tests/parse.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {describe, expect, test} from 'vitest';
import {parse} from '../utils/parse';
import { describe, expect, test } from 'vitest'
import { parse } from '../utils/parse'

const dateTimeFormat = new Intl.DateTimeFormat('en-US', {
year: 'numeric',
Expand All @@ -9,124 +9,136 @@ const dateTimeFormat = new Intl.DateTimeFormat('en-US', {
minute: '2-digit',
second: '2-digit',
timeZoneName: 'short',
timeZone: 'America/New_York'
});
timeZone: 'America/New_York',
})

const timeOnlyFormat = new Intl.DateTimeFormat('en-US', {
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
//timeZoneName: 'short',
timeZone: 'America/New_York'
});
timeZone: 'America/New_York',
})

describe('parse', () => {
test('should parse a valid year only date string', () => {
const date = parse('2021');
expect(dateTimeFormat.format(date)).toBe('01/01/2021, 12:00:00 AM EST');
});
const date = parse('2021')
expect(dateTimeFormat.format(date)).toBe('01/01/2021, 12:00:00 AM EST')
})

test('should parse a valid year and month only date string', () => {
const date = parse('2021-03');
expect(dateTimeFormat.format(date)).toBe('03/01/2021, 12:00:00 AM EST');
});
const date = parse('2021-03')
expect(dateTimeFormat.format(date)).toBe('03/01/2021, 12:00:00 AM EST')
})

test('should parse a valid year, month and day only date string', () => {
const date = parse('2021-03-12');
expect(dateTimeFormat.format(date)).toBe('03/12/2021, 12:00:00 AM EST');
});
const date = parse('2021-03-12')
expect(dateTimeFormat.format(date)).toBe('03/12/2021, 12:00:00 AM EST')
})

test('should cast date only string to UTC', () => {
const date = parse('2021-03-12TZ');
expect(dateTimeFormat.format(date)).toBe('03/11/2021, 07:00:00 PM EST');
});
const date = parse('2021-03-12TZ')
expect(dateTimeFormat.format(date)).toBe('03/11/2021, 07:00:00 PM EST')
})

test('should cast date only string to offset', () => {
const date = parse('2021-03-12T-07:00');
expect(dateTimeFormat.format(date)).toBe('03/12/2021, 02:00:00 AM EST');
});
const date = parse('2021-03-12T-07:00')
expect(dateTimeFormat.format(date)).toBe('03/12/2021, 02:00:00 AM EST')
})

test('should parse a valid year, month, day and hour only date/time string with separator', () => {
const date = parse('2021-03-12T14');
expect(dateTimeFormat.format(date)).toBe('03/12/2021, 02:00:00 PM EST');
});
const date = parse('2021-03-12T14')
expect(dateTimeFormat.format(date)).toBe('03/12/2021, 02:00:00 PM EST')
})

test('should parse a valid year, month, day and hour only date/time string without separator', () => {
const date = parse('2021-03-12 16');
expect(dateTimeFormat.format(date)).toBe('03/12/2021, 04:00:00 PM EST');
});
const date = parse('2021-03-12 16')
expect(dateTimeFormat.format(date)).toBe('03/12/2021, 04:00:00 PM EST')
})

test('should parse a valid year, month, day, hour and minute only date/time string', () => {
const date = parse('2021-03-12T14:42');
expect(dateTimeFormat.format(date)).toBe('03/12/2021, 02:42:00 PM EST');
});
const date = parse('2021-03-12T14:42')
expect(dateTimeFormat.format(date)).toBe('03/12/2021, 02:42:00 PM EST')
})

test('should parse a valid year, month, day, hour, minute and seconds only date/time string', () => {
const date = parse('2021-03-12T14:42:12');
expect(dateTimeFormat.format(date)).toBe('03/12/2021, 02:42:12 PM EST');
});
const date = parse('2021-03-12T14:42:12')
expect(dateTimeFormat.format(date)).toBe('03/12/2021, 02:42:12 PM EST')
})

test('should parse a valid UTC date/time string', () => {
const date = parse('2021-03-12T14:42:12Z');
expect(dateTimeFormat.format(date)).toBe('03/12/2021, 09:42:12 AM EST');
});
const date = parse('2021-03-12T14:42:12Z')
expect(dateTimeFormat.format(date)).toBe('03/12/2021, 09:42:12 AM EST')
})

test('should parse a valid UTC date/time string with lower case separator and zulu', () => {
const date = parse('2021-03-12t14:42:12z');
expect(dateTimeFormat.format(date)).toBe('03/12/2021, 09:42:12 AM EST');
});
const date = parse('2021-03-12t14:42:12z')
expect(dateTimeFormat.format(date)).toBe('03/12/2021, 09:42:12 AM EST')
})

test('should parse a valid date/time string with offset', () => {
const date = parse('2021-03-12T14:42:12+09:00');
expect(dateTimeFormat.format(date)).toBe('03/12/2021, 12:42:12 AM EST');
});
const date = parse('2021-03-12T14:42:12+09:00')
expect(dateTimeFormat.format(date)).toBe('03/12/2021, 12:42:12 AM EST')
})

test('should parse a valid epoch date/time', () => {
const date = parse(1711639239717);
expect(dateTimeFormat.format(date)).toBe('03/28/2024, 11:20:39 AM EDT');
});
const date = parse(1711639239717)
expect(dateTimeFormat.format(date)).toBe('03/28/2024, 11:20:39 AM EDT')
})

test('should parse a valid hour and minute time string', () => {
const date = parse('14:00');
expect(timeOnlyFormat.format(date)).toBe('02:00:00 PM');
});
const date = parse('14:00')
expect(timeOnlyFormat.format(date)).toBe('02:00:00 PM')
})

test('should parse a valid hour, minute and second time string', () => {
const date = parse('14:14:34');
expect(timeOnlyFormat.format(date)).toBe('02:14:34 PM');
});
const date = parse('14:14:34')
expect(timeOnlyFormat.format(date)).toBe('02:14:34 PM')
})

test('should parse a valid hour, minute and second time string with meridiem', () => {
const date = parse('08:14:34 PM');
expect(timeOnlyFormat.format(date)).toBe('08:14:34 PM');
});
const date = parse('08:14:34 PM')
expect(timeOnlyFormat.format(date)).toBe('08:14:34 PM')
})

test('should throw an error for an invalid date string', () => {
expect(() => parse('2021-15-37')).toThrowError('"2021-15-37" is an invalid RFC339 Internet Date Time string');
});
expect(() => parse('2021-15-37')).toThrowError(
'"2021-15-37" is an invalid RFC339 Internet Date Time string',
)
})

test('should throw an error for an invalid time string', () => {
expect(() => parse('27:62')).toThrowError('"27:62" is an invalid RFC339 Internet Date Time string');
});
expect(() => parse('27:62')).toThrowError(
'"27:62" is an invalid RFC339 Internet Date Time string',
)
})

test('should throw an error for an invalid time string', () => {
expect(() => parse('14:12 PM')).toThrowError('"14:12 PM" is an invalid time string');
});
expect(() => parse('14:12 PM')).toThrowError(
'"14:12 PM" is an invalid time string',
)
})

test('should throw an error for an invalid epoch', () => {
expect(() => parse(9274309587123413)).toThrowError('"9274309587123412" is an invalid epoch date value');
});

/* test('should parse a valid time string', () => {
const date = parse('00:00:00.000Z');
expect(dateTimeFormat.format(date)).toBe('12/31/2020, 7:00:00 PM EST');
});

test('should throw an error for an invalid date string', () => {
expect(() => parse('2021-01-01T00:00:00.000')).toThrowError('"2021-01-01T00:00:00.000" is an invalid RFC339 Internet Date Time string');
});

test('should throw an error for an invalid time string', () => {
expect(() => parse('00:00:00.000')).toThrowError('"00:00:00.000" is an invalid time string');
}); */
});
expect(() => parse(9274309587123410)).toThrowError(
'"9274309587123410" is an invalid epoch date value',
)
})

test.skip('should parse a valid time string', () => {
const date = parse('00:00:00.000Z')
expect(dateTimeFormat.format(date)).toBe('12/31/2020, 7:00:00 PM EST')
})

test.skip('should throw an error for an invalid date string', () => {
expect(() => parse('2021-01-01T00:00:00.000')).toThrowError(
'"2021-01-01T00:00:00.000" is an invalid RFC339 Internet Date Time string',
)
})

test.skip('should throw an error for an invalid time string', () => {
expect(() => parse('00:00:00.000')).toThrowError(
'"00:00:00.000" is an invalid time string',
)
})
})
9 changes: 3 additions & 6 deletions packages/time/src/utils/isValidDate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
* @param date Date
* @returns boolean
*/
export function isValidDate(date: any): boolean {
if (Object.prototype.toString.call(date) !== '[object Date]') {
return false;
}
return date.getTime() === date.getTime();
}
export function isValidDate(date: unknown): date is Date {
return date instanceof Date && !isNaN(date.getTime());
}