-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split test file and move tests closer to the code
Also: - improve some tests - update JSDoc - trigger the Vitest GitHub action reporter when a test fails - add code coverage to Vitest UI - exclude types from code coverage - lint tests with more rules
- Loading branch information
Showing
18 changed files
with
796 additions
and
313 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { describe, expect, test } from 'vitest' | ||
|
||
import { datetime, setTimeSeparator } from '../index.js' | ||
|
||
import { setConfig } from './datetime.js' | ||
|
||
const togoIndependanceDay = new Date(1960, 3, 27) | ||
const date = togoIndependanceDay // alias for the sake of brevity | ||
|
||
describe('setTimeSeparator', () => { | ||
|
||
test('is a function', () => { | ||
expect(setTimeSeparator).toBeInstanceOf(Function) | ||
}) | ||
|
||
test('throws with invalid separator', () => { | ||
expect(() => setTimeSeparator(42)).toThrow(Error) | ||
}) | ||
|
||
test('no time separator', () => { | ||
setTimeSeparator() | ||
expect(datetime(date, 'datetime')).toBe('1960-04-27T00:00') | ||
}) | ||
|
||
test('T as time separator', () => { | ||
setTimeSeparator('T') | ||
expect(datetime(date, 'datetime')).toBe('1960-04-27T00:00') | ||
}) | ||
|
||
test('space as time separator', () => { | ||
setTimeSeparator(' ') | ||
expect(datetime(date, 'datetime')).toBe('1960-04-27 00:00') | ||
}) | ||
}) | ||
|
||
describe('setConfig', () => { | ||
|
||
test('is a function', () => { | ||
expect(setConfig).toBeInstanceOf(Function) | ||
}) | ||
|
||
test('throws without parameter', () => { | ||
expect(() => setConfig()).toThrow(TypeError) | ||
}) | ||
|
||
test('throws without proper parameter', () => { | ||
expect(() => setConfig({})).toThrow(Error) | ||
}) | ||
|
||
test('throws with invalid separator', () => { | ||
expect(() => setConfig({ separator: 42 })).toThrow(Error) | ||
}) | ||
|
||
test('T as time separator using datetime', () => { | ||
setConfig({ separator: 'T' }) | ||
expect(datetime(date, 'datetime')).toBe('1960-04-27T00:00') | ||
}) | ||
|
||
test('space as time separator using datetime', () => { | ||
setConfig({ separator: ' ' }) | ||
expect(datetime(date, 'datetime')).toBe('1960-04-27 00:00') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { describe, expect, test } from 'vitest' | ||
|
||
import { datetimeTz, tzOffset, setTzSeparator } from '../index.js' | ||
|
||
import { setTzConfig } from './tz.js' | ||
|
||
const togoIndependanceDay = new Date(1960, 3, 27) | ||
const date = togoIndependanceDay // alias for the sake of brevity | ||
|
||
describe('setTzSeparator', () => { | ||
test('is a function', () => expect(setTzSeparator).toBeInstanceOf(Function)) | ||
test('throws with invalid separator', () => expect(() => setTzSeparator(42)).toThrow(Error)) | ||
|
||
test('no timezone separator', () => { | ||
setTzSeparator() | ||
expect(tzOffset(3)).toBe('+0300') | ||
}) | ||
|
||
test('empty string timezone separator', () => { | ||
setTzSeparator('') | ||
expect(tzOffset(3)).toBe('+0300') | ||
}) | ||
|
||
test(': as timezone separator', () => { | ||
setTzSeparator(':') | ||
expect(tzOffset(3)).toBe('+03:00') | ||
}) | ||
|
||
test('empty string timezone separator with datetimeTz', () => { | ||
setTzSeparator('') | ||
expect(datetimeTz(date, 'datetime second', -6)).toBe('1960-04-27T00:00:00-0600') | ||
}) | ||
}) | ||
|
||
describe('setTzConfig', () => { | ||
test('is a function', () => expect(setTzConfig).toBeInstanceOf(Function)) | ||
test('throws without parameter', () => expect(() => setTzConfig()).toThrow(TypeError)) | ||
test('throws without proper parameter', () => expect(() => setTzConfig({})).toThrow(Error)) | ||
test('throws with invalid separator', () => expect(() => setTzConfig({ separator: 42 })).toThrow(Error)) | ||
|
||
test('empty string timezone separator using tzOffset', () => { | ||
setTzConfig({ separator: '' }) | ||
expect(tzOffset(3)).toBe('+0300') | ||
}) | ||
|
||
test(': as timezone separator using tzOffset', () => { | ||
setTzConfig({ separator: ':' }) | ||
expect(tzOffset(3)).toBe('+03:00') | ||
}) | ||
|
||
test(': as timezone separator with datetimeTz', () => { | ||
setTzConfig({ separator: ':' }) | ||
expect(datetimeTz(date, 'datetime second', -6)).toBe('1960-04-27T00:00:00-06:00') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { beforeEach, describe, expect, test } from 'vitest' | ||
|
||
import { DateTime, datetime } from './index.js' | ||
import { MILLISECONDS_PER_DAY } from './utils/const.js' | ||
|
||
const FOURTEEN_DAYS_IN_MS = MILLISECONDS_PER_DAY * 14 | ||
|
||
describe('DateTime class', () => { | ||
/** @type {Date} */ let summer | ||
/** @type {Date} */ let twoWeeksIntoSummer | ||
/** @type {Date} */ let winter | ||
/** @type {Date} */ let twoWeeksIntoWinter | ||
|
||
beforeEach(() => { | ||
summer = new DateTime(2021, 5, 21) | ||
twoWeeksIntoSummer = new DateTime(summer.getTime() + FOURTEEN_DAYS_IN_MS) // same as `new DateTime(2021, 6, 5)` | ||
|
||
winter = new DateTime(2021, 11, 21) | ||
twoWeeksIntoWinter = new DateTime(winter.getTime() + FOURTEEN_DAYS_IN_MS) // same as `new DateTime(2022, 1, 4)` | ||
}) | ||
|
||
test('extends the Date class', () => { | ||
expect(summer).toBeInstanceOf(DateTime) | ||
expect(summer).toBeInstanceOf(Date) | ||
}) | ||
|
||
describe('.to() == datetime()', () => { | ||
|
||
test('with default precision', () => { | ||
expect(summer.to()).toBe('2021-06-21') | ||
expect(datetime(summer)).toBe('2021-06-21') | ||
}) | ||
|
||
test(`with 'month' precision`, () => { | ||
expect(twoWeeksIntoSummer.to('month')).toBe('2021-07') | ||
expect(datetime(twoWeeksIntoSummer, 'month')).toBe('2021-07') | ||
}) | ||
|
||
test(`with 'week' precision, after the week is changed to a new year`, () => { | ||
winter.setDate(winter.getDate() + 14) | ||
|
||
expect(winter.to('week')).toBe(twoWeeksIntoWinter.to('week')) | ||
expect(winter.to('week')).toBe('2022-W01') | ||
expect(datetime(winter, 'week')).toBe('2022-W01') | ||
}) | ||
}) | ||
|
||
describe('.getWeek()', () => { | ||
|
||
test('returns the week number', () => { | ||
expect(summer.getWeek()).toBe(25) | ||
}) | ||
|
||
test('returns the week number after the date is changed', () => { | ||
summer.setDate(summer.getDate() + 14) | ||
|
||
expect(summer.getWeek()).toBe(twoWeeksIntoSummer.getWeek()) | ||
}) | ||
|
||
test('returns the week number after the date is changed to a new year', () => { | ||
winter.setDate(winter.getDate() + 14) | ||
|
||
expect(winter.getWeek()).toBe(twoWeeksIntoWinter.getWeek()) | ||
}) | ||
}) | ||
|
||
test('.setWeek() returns the timestamp of the changed date', () => { | ||
expect(summer.setWeek(27)).toBe(twoWeeksIntoSummer.getTime()) | ||
}) | ||
}) |
Oops, something went wrong.