Test: formatDateTime, timeAgo 테스트 추가

This commit is contained in:
jhynsoo 2024-10-02 16:00:51 +09:00
parent 0b19d06d0c
commit 35e8eea101
3 changed files with 79 additions and 1 deletions

View File

@ -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);
});
});

View File

@ -1,7 +1,7 @@
export default function formatDateTime(dateTimeString: string): string { export default function formatDateTime(dateTimeString: string): string {
const [date, time] = dateTimeString.split('T'); const [date, time] = dateTimeString.split('T');
const [year, month, day] = date.split('-'); 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}`; return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
} }

View File

@ -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');
});
});