-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #267 from Strong-Potato/261-fix-qa-issue
feat: add test code #261
- Loading branch information
Showing
4 changed files
with
137 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}/); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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([]); | ||
}); | ||
}); |