Skip to content

Commit

Permalink
Added unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
HaaLeo committed Jan 28, 2019
1 parent 76a2563 commit a5879ff
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
45 changes: 45 additions & 0 deletions src/test/utiltest/timeConverter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,36 @@ describe('TimeConverter', () => {
});
});

describe('isoDurationToEpoch', () => {
let testObject: TimeConverter;

beforeEach('Set up test object.', () => {
testObject = new TimeConverter();
});

it('Should convert to epoch correctly as s.', () => {
const result = testObject.isoDurationToEpoch('P1Y2M3DT4H5M6S', 's');

assert.strictEqual(result, '37080306');
});

it('Should convert to epoch correctly as ms.', () => {
const result = testObject.isoDurationToEpoch('P1Y2M3DT4H5M6S', 'ms');

assert.strictEqual(result, '37080306000');
});

it('Should convert to epoch correctly as ns.', () => {
const result = testObject.isoDurationToEpoch('P1Y2M3DT4H5M6S', 'ns');

assert.strictEqual(result, '37080306000000000');
});

it('Should throw an error if option is unknown.', () => {
assert.throws(() => testObject.isoDurationToEpoch('P1Y2M3DT4H5M6S', undefined));
});
});

describe('isoRfcToEpoch', () => {
let testObject: TimeConverter;

Expand Down Expand Up @@ -157,6 +187,21 @@ describe('TimeConverter', () => {
});
});

describe('isValidIsoDuration', () => {
let testObject: TimeConverter;

beforeEach('Set up test object.', () => {
testObject = new TimeConverter();
});

it('Should return true if it is a valid ISO duration.', () => {
assert.strictEqual(testObject.isValidISODuration('P1Y2M3DT4H5M6S'), true);
});

it('Should return false if it is an invalid ISO duration.', () => {
assert.strictEqual(testObject.isValidIsoRfc('123456789'), false);
});
});
describe('isValidCustom', () => {
let testObject: TimeConverter;

Expand Down
4 changes: 1 addition & 3 deletions src/util/timeConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ class TimeConverter {

let text = '';

if (text !== '') {
text += ', ' + Math.floor(duration.asDays() / 7) + 'w';
} else if (Math.floor(duration.asDays() / 7) !== 0) {
if (Math.floor(duration.asDays() / 7) !== 0) {
text += Math.floor(duration.asDays() / 7) + 'w';
}
if (text !== '') {
Expand Down

0 comments on commit a5879ff

Please sign in to comment.