test(datetime): add daysInMonth tests (#17558)

* fix(Datetime): correct days in month calculation

* test(DateTimeUtil): daysInMonth

* revert fn changes, keep tests
This commit is contained in:
Bengt Weiße
2019-03-22 17:59:43 +01:00
committed by Liam DeBeasi
parent 71e5994c94
commit 424879df8e

View File

@@ -1,19 +1,19 @@
import { DatetimeData, getDateValue, getLocalDateTime } from '../datetime-util';
import { DatetimeData, daysInMonth, getDateValue, getLocalDateTime } from '../datetime-util';
describe('Datetime', () => {
describe('getDateValue()', () => {
it('it should return the date value for the current day', () => {
const today = new Date();
const dayValue = getDateValue({}, 'DD');
const monthvalue = getDateValue({}, 'MM');
const yearValue = getDateValue({}, 'YYYY');
expect(dayValue).toEqual(today.getDate());
expect(monthvalue).toEqual(today.getMonth() + 1);
expect(yearValue).toEqual(today.getFullYear());
});
it('it should return the date value for a given day', () => {
const date = new Date('15 October 1995');
const dateTimeData: DatetimeData = {
@@ -21,20 +21,20 @@ describe('Datetime', () => {
month: date.getMonth() + 1,
day: date.getDate()
}
const dayValue = getDateValue(dateTimeData, 'DD');
const monthvalue = getDateValue(dateTimeData, 'MM');
const yearValue = getDateValue(dateTimeData, 'YYYY');
expect(dayValue).toEqual(date.getDate());
expect(monthvalue).toEqual(date.getMonth() + 1);
expect(yearValue).toEqual(date.getFullYear());
});
});
describe('getLocalDateTime()', () => {
it('should format a datetime string according to the local timezone', () => {
const dateStringTests = [
{ expectedHourUTC: 12, input: `2019-03-02T12:08:06.601-00:00`, expectedOutput: `2019-03-02T%HOUR%:08:06.601Z` },
{ expectedHourUTC: 12, input: `2019-11-02T12:08:06.601-00:00`, expectedOutput: `2019-11-02T%HOUR%:08:06.601Z` },
@@ -42,19 +42,37 @@ describe('Datetime', () => {
{ expectedHourUTC: 18, input: `1994-12-15T13:47:20.789-05:00`, expectedOutput: `1994-12-15T%HOUR%:47:20.789Z` },
{ expectedHourUTC: 9, input: `2019-02-14T09:00:00.000Z`, expectedOutput: `2019-02-14T%HOUR%:00:00.000Z` }
];
dateStringTests.forEach(test => {
const convertToLocal = getLocalDateTime(test.input);
const timeZoneOffset = convertToLocal.getTimezoneOffset() / 60;
const expectedDateString = test.expectedOutput.replace('%HOUR%', padNumber(test.expectedHourUTC - timeZoneOffset));
expect(convertToLocal.toISOString()).toEqual(expectedDateString);
});
});
});
describe('daysInMonth()', () => {
it('should return correct days in month for month and year', () => {
expect(daysInMonth(1, 2019)).toBe(31);
expect(daysInMonth(2, 2019)).toBe(28);
expect(daysInMonth(3, 2019)).toBe(31);
expect(daysInMonth(4, 2019)).toBe(30);
expect(daysInMonth(5, 2019)).toBe(31);
expect(daysInMonth(6, 2019)).toBe(30);
expect(daysInMonth(7, 2019)).toBe(31);
expect(daysInMonth(8, 2019)).toBe(31);
expect(daysInMonth(9, 2019)).toBe(30);
expect(daysInMonth(10, 2019)).toBe(31);
expect(daysInMonth(11, 2019)).toBe(30);
expect(daysInMonth(12, 2019)).toBe(31);
expect(daysInMonth(2, 2020)).toBe(29);
});
});
});
function padNumber(number: number, totalLength: number = 2): string {
return number.toString().padStart(totalLength, '0');
}
}