Skip to content

Commit af0f831

Browse files
author
Marvel Mathew
committed
Improve coverage for relativeTimeToDate
1 parent 5f7d26b commit af0f831

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

spec/MongoTransform.spec.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -353,25 +353,32 @@ describe('relativeTimeToDate', () => {
353353

354354
describe('In the future', () => {
355355
it('should parse valid natural time', () => {
356-
const text = 'in 12 days 10 hours 24 minutes';
356+
const text = 'in 12 days 10 hours 24 minutes 30 seconds';
357357
const { result, status, info } = transform.relativeTimeToDate(text, now);
358-
expect(result.toISOString()).toBe('2017-10-08T23:52:16.617Z');
358+
expect(result.toISOString()).toBe('2017-10-08T23:52:46.617Z');
359359
expect(status).toBe('success');
360360
expect(info).toBe('future');
361361
});
362362
});
363363

364364
describe('In the past', () => {
365365
it('should parse valid natural time', () => {
366-
const text = '2 days 12 hours 1 minute ago';
366+
const text = '2 days 12 hours 1 minute 12 seconds ago';
367367
const { result, status, info } = transform.relativeTimeToDate(text, now);
368-
expect(result.toISOString()).toBe('2017-09-24T01:27:16.617Z');
368+
expect(result.toISOString()).toBe('2017-09-24T01:27:04.617Z');
369369
expect(status).toBe('success');
370370
expect(info).toBe('past');
371371
});
372372
});
373373

374374
describe('Error cases', () => {
375+
it('should error if string is completely gibberish', () => {
376+
expect(transform.relativeTimeToDate('gibberishasdnklasdnjklasndkl123j123')).toEqual({
377+
status: 'error',
378+
info: "Time should either start with 'in' or end with 'ago'",
379+
});
380+
});
381+
375382
it('should error if string contains neither `ago` nor `in`', () => {
376383
expect(transform.relativeTimeToDate('12 hours 1 minute')).toEqual({
377384
status: 'error',

src/Adapters/Storage/Mongo/MongoTransform.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -537,9 +537,6 @@ function relativeTimeToDate(text, now = new Date()) {
537537
text = text.toLowerCase();
538538

539539
let parts = text.split(' ');
540-
if (!parts.length) {
541-
return { status: 'error', info: 'Not a time string' };
542-
}
543540

544541
// Filter out whitespace
545542
parts = parts.filter((part) => part !== '');

0 commit comments

Comments
 (0)