diff --git a/packages/bruno-app/src/utils/common/index.js b/packages/bruno-app/src/utils/common/index.js index f31dd228f..05f1bad2f 100644 --- a/packages/bruno-app/src/utils/common/index.js +++ b/packages/bruno-app/src/utils/common/index.js @@ -149,7 +149,9 @@ export const relativeDate = (dateString) => { }; export const humanizeDate = (dateString) => { - const date = new Date(dateString); + // See this discussion for why .split is necessary + // https://stackoverflow.com/questions/7556591/is-the-javascript-date-object-always-one-day-off + const date = new Date(dateString.split('-')); return date.toLocaleDateString('en-US', { year: 'numeric', month: 'long', diff --git a/packages/bruno-app/src/utils/common/index.spec.js b/packages/bruno-app/src/utils/common/index.spec.js index 3484fac9c..c4055c5d1 100644 --- a/packages/bruno-app/src/utils/common/index.spec.js +++ b/packages/bruno-app/src/utils/common/index.spec.js @@ -1,6 +1,6 @@ const { describe, it, expect } = require('@jest/globals'); -import { normalizeFileName, startsWith } from './index'; +import { normalizeFileName, startsWith, humanizeDate, relativeDate } from './index'; describe('common utils', () => { describe('normalizeFileName', () => { @@ -49,4 +49,50 @@ describe('common utils', () => { expect(startsWith('foo', 'foo')).toBe(true); }); }); + + describe('humanizeDate', () => { + it('should return a date string in the en-US locale', () => { + expect(humanizeDate('2024-03-17')).toBe('March 17, 2024'); + }); + + it('should return invalid date if the date is invalid', () => { + expect(humanizeDate('9999-99-99')).toBe('Invalid Date'); + }); + }); + + describe('relativeDate', () => { + it('should return few seconds ago', () => { + expect(relativeDate(new Date())).toBe('Few seconds ago'); + }); + + it('should return minutes ago', () => { + let date = new Date(); + date.setMinutes(date.getMinutes() - 30); + expect(relativeDate(date)).toBe('30 minutes ago'); + }); + + it('should return hours ago', () => { + let date = new Date(); + date.setHours(date.getHours() - 10); + expect(relativeDate(date)).toBe('10 hours ago'); + }); + + it('should return days ago', () => { + let date = new Date(); + date.setDate(date.getDate() - 5); + expect(relativeDate(date)).toBe('5 days ago'); + }); + + it('should return weeks ago', () => { + let date = new Date(); + date.setDate(date.getDate() - 8); + expect(relativeDate(date)).toBe('1 week ago'); + }); + + it('should return months ago', () => { + let date = new Date(); + date.setDate(date.getDate() - 60); + expect(relativeDate(date)).toBe('2 months ago'); + }); + }); });