diff --git a/src/utils/formatTimeAgo.test.ts b/src/utils/formatTimeAgo.test.ts new file mode 100644 index 00000000..7fb5ac6e --- /dev/null +++ b/src/utils/formatTimeAgo.test.ts @@ -0,0 +1,32 @@ +import formatTimeAgo from './formatTimeAgo'; // formatTimeAgo 함수를 임포트합니다. + +describe('시간이 얼마나 지났는지 값을 반환합니다', () => { + test('몇초 전인지 반환합니다', () => { + const now = new Date(); + const thirtySecondsAgo = new Date(now.getTime() - 30 * 1000); + expect(formatTimeAgo(thirtySecondsAgo)).toBe('30초 전'); + }); + + test('몇분 전인지 반환합니다', () => { + const now = new Date(); + const fiveMinutesAgo = new Date(now.getTime() - 5 * 60 * 1000); + expect(formatTimeAgo(fiveMinutesAgo)).toBe('5분 전'); + }); + + test('몇시간 전인지 반환합니다', () => { + const now = new Date(); + const threeHoursAgo = new Date(now.getTime() - 3 * 60 * 60 * 1000); + expect(formatTimeAgo(threeHoursAgo)).toBe('3시간 전'); + }); + + test('몇일 전인지 반환합니다', () => { + const now = new Date(); + const twoDaysAgo = new Date(now.getTime() - 2 * 24 * 60 * 60 * 1000); + expect(formatTimeAgo(twoDaysAgo)).toBe('2일 전'); + }); + + test('그 외의 날짜 형식을 반환합니다', () => { + const oldDate = new Date('2020-01-01'); + expect(formatTimeAgo(oldDate)).toMatch(/\d{2}\.\d{2}\.\d{2}/); + }); +}); diff --git a/src/utils/parseInviteCode.test.ts b/src/utils/parseInviteCode.test.ts new file mode 100644 index 00000000..5b56babe --- /dev/null +++ b/src/utils/parseInviteCode.test.ts @@ -0,0 +1,36 @@ +import {parseInviteCode} from './parseInviteCode'; + +describe('parseInviteCode Function', () => { + let consoleError: typeof console.error; + + beforeEach(() => { + consoleError = console.error; + console.error = jest.fn(); + }); + + afterEach(() => { + console.error = consoleError; + }); + it('올바른 토큰 형식입니다.', () => { + const validInviteCode = + 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJvayIsInB1cnBvc2UiOiJqb2luX3NwYWNlIiwic3BhY2VfaWQiOjEsImlhdCI6MTcwNTkzMzE4NCwiZXhwIjoxNzA1OTQwMzg0fQ.Gj-f92qBjLJ_1OVMx-KGs4a_wjLKmq3ZEtROLobl2h4'; + const expectedDecoded = { + exp: 1705940384, + iat: 1705933184, + iss: 'ok', + purpose: 'join_space', + space_id: 1, + }; + + const result = parseInviteCode(validInviteCode); + + expect(result).toEqual(expectedDecoded); + }); + + it('잘못된 토큰 형식입니다.', () => { + const invalidInviteCode = '이상한코드.dfadfa.d123123'; + const result = parseInviteCode(invalidInviteCode); + + expect(result).toBeNull(); + }); +}); diff --git a/src/utils/parseInviteCode.ts b/src/utils/parseInviteCode.ts index dd2eb553..4e1ec811 100644 --- a/src/utils/parseInviteCode.ts +++ b/src/utils/parseInviteCode.ts @@ -7,7 +7,7 @@ export function parseInviteCode(inviteCode: string): InviteCode | null { const decoded = jwtDecode(inviteCode); return decoded; } catch (error) { - console.error('JWT 디코딩 중 에러 발생:', error); + console.log('[친구초대]잘못된 토큰 형식입니다.', error); return null; } } diff --git a/src/utils/parsingAlarm.test.ts b/src/utils/parsingAlarm.test.ts new file mode 100644 index 00000000..89965c0e --- /dev/null +++ b/src/utils/parsingAlarm.test.ts @@ -0,0 +1,68 @@ +import {parsingAlarmTravel} from './parsingAlarm'; + +import {NotificationData, NotificationDetails} from '@/types/notification'; + +describe('parsingAlarmTravel Function', () => { + let consoleError: typeof console.error; + + beforeEach(() => { + consoleError = console.error; + console.error = jest.fn(); + }); + + afterEach(() => { + console.error = consoleError; + }); + + it('should return an empty array for empty or null input', () => { + expect(parsingAlarmTravel([])).toEqual([]); + // eslint-disable-next-line @typescript-eslint/no-explicit-any + expect(parsingAlarmTravel(null as any)).toEqual([]); + }); + + it('should parse valid notification details correctly', () => { + const notificationDetails: NotificationDetails[] = [ + { + id: 1, + type: 'VOTE_CREATED', + notificationInformation: JSON.stringify({ + spaceEventInfo: {spaceTitle: '테스트 여행지'}, + memberEventInfo: {memberNickname: '테스트 사용자'}, + voteEventInfo: {voteTitle: '테스트 투표'}, + createdAt: '2024-01-01T00:00:00Z', + }), + isRead: false, + receiverId: 123, + createdAt: '2024-01-01T00:00:00Z', + }, + ]; + + const expected: NotificationData[] = [ + { + url: '', + title: '[테스트 여행지] 테스트 사용자 님이 새로운 투표를 생성했습니다.', + time: '2024-01-01T00:00:00Z', + }, + ]; + + const result = parsingAlarmTravel(notificationDetails); + + expect(result).toEqual(expected); + }); + + it('should handle incorrect JSON format gracefully', () => { + const notificationDetailsWithBadJSON: NotificationDetails[] = [ + { + id: 2, + type: 'VOTE_CREATED', + notificationInformation: 'not a valid json', + isRead: false, + receiverId: 456, + createdAt: '2024-01-02T00:00:00Z', + }, + ]; + + expect(() => parsingAlarmTravel(notificationDetailsWithBadJSON)).not.toThrow(); + expect(parsingAlarmTravel(notificationDetailsWithBadJSON)).toEqual([]); + }); +});