diff --git a/frontend/src/utils/formatDateTime.test.ts b/frontend/src/utils/formatDateTime.test.ts new file mode 100644 index 0000000..421ab34 --- /dev/null +++ b/frontend/src/utils/formatDateTime.test.ts @@ -0,0 +1,27 @@ +import { describe, it, expect } from 'vitest'; +import formatDateTime from './formatDateTime'; + +describe('formatDateTime', () => { + it('should format a valid date-time string correctly', () => { + const input = '2023-10-05T14:48:00'; + const expectedOutput = '2023-10-05 14:48:00'; + expect(formatDateTime(input)).toBe(expectedOutput); + }); + + it('should handle single-digit months and days correctly', () => { + const input = '2023-1-5T04:08:09'; + const expectedOutput = '2023-1-5 04:08:09'; + expect(formatDateTime(input)).toBe(expectedOutput); + }); + + it('should throw an error for an invalid date-time string', () => { + const input = '2023-10-05'; + expect(() => formatDateTime(input)).toThrow(); + }); + + it('should handle a date-time string without seconds correctly', () => { + const input = '2023-10-05T14:48'; + const expectedOutput = '2023-10-05 14:48:00'; + expect(formatDateTime(input)).toBe(expectedOutput); + }); +}); diff --git a/frontend/src/utils/formatDateTime.ts b/frontend/src/utils/formatDateTime.ts index 955f4ee..229486e 100644 --- a/frontend/src/utils/formatDateTime.ts +++ b/frontend/src/utils/formatDateTime.ts @@ -1,7 +1,7 @@ export default function formatDateTime(dateTimeString: string): string { const [date, time] = dateTimeString.split('T'); const [year, month, day] = date.split('-'); - const [hours, minutes, seconds] = time.split(':'); + const [hours, minutes, seconds = '00'] = time.split(':'); return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`; } diff --git a/frontend/src/utils/timeAgo.test.ts b/frontend/src/utils/timeAgo.test.ts new file mode 100644 index 0000000..fe872ce --- /dev/null +++ b/frontend/src/utils/timeAgo.test.ts @@ -0,0 +1,51 @@ +import timeAgo from './timeAgo'; + +describe('timeAgo', () => { + it('should return "1 second ago" for a date 1 second ago', () => { + const now = new Date(); + const past = new Date(now.getTime() - 1000); + expect(timeAgo(past)).toBe('1 second ago'); + }); + + it('should return "x seconds ago" for a date less than a minute ago', () => { + const now = new Date(); + const past = new Date(now.getTime() - 45000); // 45 seconds ago + expect(timeAgo(past)).toBe('45 seconds ago'); + }); + + it('should return "1 minute ago" for a date 1 minute ago', () => { + const now = new Date(); + const past = new Date(now.getTime() - 60000); // 1 minute ago + expect(timeAgo(past)).toBe('1 minute ago'); + }); + + it('should return "x minutes ago" for a date less than an hour ago', () => { + const now = new Date(); + const past = new Date(now.getTime() - 1800000); // 30 minutes ago + expect(timeAgo(past)).toBe('30 minutes ago'); + }); + + it('should return "1 hour ago" for a date 1 hour ago', () => { + const now = new Date(); + const past = new Date(now.getTime() - 3600000); // 1 hour ago + expect(timeAgo(past)).toBe('1 hour ago'); + }); + + it('should return "x hours ago" for a date less than a day ago', () => { + const now = new Date(); + const past = new Date(now.getTime() - 7200000); // 2 hours ago + expect(timeAgo(past)).toBe('2 hours ago'); + }); + + it('should return "1 day ago" for a date 1 day ago', () => { + const now = new Date(); + const past = new Date(now.getTime() - 86400000); // 1 day ago + expect(timeAgo(past)).toBe('1 day ago'); + }); + + it('should return "x days ago" for a date more than a day ago', () => { + const now = new Date(); + const past = new Date(now.getTime() - 172800000); // 2 days ago + expect(timeAgo(past)).toBe('2 days ago'); + }); +});