From 35e8eea1010bc7b04c9b8f20668fd98eb7fd9c58 Mon Sep 17 00:00:00 2001 From: jhynsoo Date: Wed, 2 Oct 2024 16:00:51 +0900 Subject: [PATCH] =?UTF-8?q?Test:=20formatDateTime,=20timeAgo=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/src/utils/formatDateTime.test.ts | 27 ++++++++++++ frontend/src/utils/formatDateTime.ts | 2 +- frontend/src/utils/timeAgo.test.ts | 51 +++++++++++++++++++++++ 3 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 frontend/src/utils/formatDateTime.test.ts create mode 100644 frontend/src/utils/timeAgo.test.ts 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'); + }); +});