Skip to content

Commit

Permalink
tests(Tools): Improve test coverage for formatDate()
Browse files Browse the repository at this point in the history
  • Loading branch information
sebbo2002 committed Mar 21, 2021
1 parent 6343895 commit e147be8
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 5 deletions.
20 changes: 16 additions & 4 deletions src/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ export function formatDate (timezone: string | null, d: ICalDateTimeValue, dateo
}
else if(isMoment(d)) {
// @see https://momentjs.com/timezone/docs/#/using-timezones/parsing-in-zone/
const m = timezone ? (isMomentTZ(d) && !d.tz() ? d.clone().tz(timezone) : d) : d.utc();
const m = timezone ? (isMomentTZ(d) && !d.tz() ? d.clone().tz(timezone) : d) : (floating ? d : d.utc());
return m.format('YYYYMMDD') + (!dateonly ? (
'T' + m.format('HHmmss') + (floating || timezone ? '' : 'Z')
) : '');
}
else if(isLuxonDate(d)) {
let m = timezone ? d.setZone(timezone) : d.setZone('utc');
let m = timezone ? d.setZone(timezone) : (floating ? d : d.setZone('utc'));
if(!m.isValid) {
m = d;
}
Expand All @@ -60,13 +60,25 @@ export function formatDate (timezone: string | null, d: ICalDateTimeValue, dateo
}
else {
// @see https://day.js.org/docs/en/plugin/utc
// @ts-ignore
let m = typeof d.utc === 'function' ? d.utc() : d;

let m = d;
if(timezone) {
// @see https://day.js.org/docs/en/plugin/timezone
// @ts-ignore
m = typeof d.tz === 'function' ? d.tz(timezone) : d;
}
else if(floating) {
// m = d;
}

// @ts-ignore
else if (typeof d.utc === 'function') {
// @ts-ignore
m = d.utc();
}
else {
throw new Error('Unable to convert dayjs object to UTC value: UTC plugin is not available!');
}

return m.format('YYYYMMDD') + (!dateonly ? (
'T' + m.format('HHmmss') + (floating || timezone ? '' : 'Z')
Expand Down
1 change: 0 additions & 1 deletion test/issues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ describe('Issues', function () {
});

const str = calendar.toString();
console.log(str);
assert.ok(str.indexOf('DTSTART;VALUE=DATE:20160501') > -1);
});
it('should work with Brazil/East', function () {
Expand Down
53 changes: 53 additions & 0 deletions test/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@

import assert from 'assert';
import moment from 'moment-timezone';
import {DateTime} from 'luxon';
import dayjs from 'dayjs';
import dayJsUTCPlugin from 'dayjs/plugin/utc';
import dayJsTimezonePlugin from 'dayjs/plugin/timezone';
import { formatDate, formatDateTZ, foldLines, escape } from '../src/tools';

dayjs.extend(dayJsUTCPlugin);
dayjs.extend(dayJsTimezonePlugin);

describe('ICalTools', function () {
describe('formatDate()', function () {
describe('Date / String', function () {
Expand Down Expand Up @@ -69,6 +76,52 @@ describe('ICalTools', function () {
'20180705T122400'
);
});
it('should work with floating flag', function () {
assert.strictEqual(
formatDate(null, moment('2018-07-05T18:24:00.052'), false, true),
'20180705T182400'
);
});
});
describe('Luxon', function () {
it('should work without setting a timezone', function () {
assert.strictEqual(
formatDate(null, DateTime.fromISO('2018-07-05T18:24:00.052Z'), false, false),
'20180705T182400Z'
);
});
it('should work with timezone in event / calendar (with moment-timezone)', function () {
assert.strictEqual(
formatDate('Canada/Saskatchewan', DateTime.fromISO('2018-07-05T18:24:00.052Z'), false, false),
'20180705T122400'
);
});
it('should work with floating flag', function () {
assert.strictEqual(
formatDate(null, DateTime.fromISO('2018-07-05T18:24:00.052'), false, true),
'20180705T182400'
);
});
});
describe('Day.js', function () {
it('should work without setting a timezone', function () {
assert.strictEqual(
formatDate(null, dayjs('2018-07-05T18:24:00.052Z'), false, false),
'20180705T182400Z'
);
});
it('should work with timezone in event / calendar (with moment-timezone)', function () {
assert.strictEqual(
formatDate('Canada/Saskatchewan', dayjs('2018-07-05T18:24:00.052Z'), false, false),
'20180705T122400'
);
});
it('should work with floating flag', function () {
assert.strictEqual(
formatDate(null, dayjs('2018-07-05T18:24:00.052'), false, true),
'20180705T182400'
);
});
});
});

Expand Down

0 comments on commit e147be8

Please sign in to comment.