Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add test code #261 #267

Merged
merged 1 commit into from
Jan 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/utils/formatTimeAgo.test.ts
Original file line number Diff line number Diff line change
@@ -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}/);
});
});
36 changes: 36 additions & 0 deletions src/utils/parseInviteCode.test.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});
2 changes: 1 addition & 1 deletion src/utils/parseInviteCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export function parseInviteCode(inviteCode: string): InviteCode | null {
const decoded = jwtDecode<InviteCode>(inviteCode);
return decoded;
} catch (error) {
console.error('JWT 디코딩 중 에러 발생:', error);
console.log('[친구초대]잘못된 토큰 형식입니다.', error);
return null;
}
}
68 changes: 68 additions & 0 deletions src/utils/parsingAlarm.test.ts
Original file line number Diff line number Diff line change
@@ -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([]);
});
});
Loading