-
Notifications
You must be signed in to change notification settings - Fork 3
[feature] stringToDate 유틸리티 함수 단위 테스트 추가 #442
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
Merged
seongwon030
merged 12 commits into
develop-fe
from
feature/#441-add-string-to-date-unit-test-FE-123
Jul 13, 2025
Merged
Changes from 5 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
adc74e0
refactor: stringToDate 함수분리
seongwon030 987c1d5
refactor: 명시적 튜플 타입 단언으로 split 결과 타입 안정성 강화
seongwon030 c0de6ab
feat: parseRecruitmentPeriod 단위 테스트 추가
seongwon030 24930ef
feat: 날짜 파싱 실패 시 에러 메시지 출력 로직 추가
seongwon030 f41a55c
feat: 날짜 문자열 변환 유틸리티 함수 추가
seongwon030 d0c9b4d
feat: 에러 케이스 추가
seongwon030 28de158
refactor: 에러 테스트 제거
seongwon030 a5ab9b9
refactor: stringToDate 함수에 date-fns parse와 isValid 적용
seongwon030 f49d960
fix: stringToDate에 날짜 형식 자리수 검증 정규식 추가
seongwon030 e7efb5c
test: stringToDate에 잘못된 날짜 형식 입력 시 에러 발생 테스트 추가
seongwon030 cddbfbb
refactor: stringToDate를 parseRecruitmentDateString으로 변경
seongwon030 53975a8
refactor: 파일명 stringToDate에서 recruitmentPeriodParser로 변경
seongwon030 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,35 @@ | ||
| import { parseRecruitmentPeriod } from './stringToDate'; | ||
|
|
||
| describe('parseRecruitmentPeriod 함수 테스트', () => { | ||
| it('올바른 형식의 날짜를 파싱한다.', () => { | ||
| const input = '2024.03.20 14:00 ~ 2024.03.25 18:00'; | ||
| const result = parseRecruitmentPeriod(input); | ||
|
|
||
| expect(result.recruitmentStart).toEqual(new Date('2024-03-20T14:00:00')); | ||
| expect(result.recruitmentEnd).toEqual(new Date('2024-03-25T18:00:00')); | ||
| }); | ||
|
|
||
| it('날짜가 기간형식이 아닌 단일 날짜 형식인 경우 null을 반환한다.', () => { | ||
| const input = '2024.03.20 14:00'; | ||
| const result = parseRecruitmentPeriod(input); | ||
|
|
||
| expect(result.recruitmentStart).toBeNull(); | ||
| expect(result.recruitmentEnd).toBeNull(); | ||
| }); | ||
|
|
||
| it('빈 문자열이라면 null을 반환한다.', () => { | ||
| const input = ''; | ||
| const result = parseRecruitmentPeriod(input); | ||
|
|
||
| expect(result.recruitmentStart).toBeNull(); | ||
| expect(result.recruitmentEnd).toBeNull(); | ||
| }); | ||
|
|
||
| it('날짜 사이 ~가 없다면 null을 반환한다.', () => { | ||
| const input = '2024.03.20 14:00 - 2024.03.25 18:00'; | ||
| const result = parseRecruitmentPeriod(input); | ||
|
|
||
| expect(result.recruitmentStart).toBeNull(); | ||
| expect(result.recruitmentEnd).toBeNull(); | ||
| }); | ||
| }); |
This file contains hidden or 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,29 @@ | ||
| import { stringToDate } from './stringToDate'; | ||
|
|
||
| describe('stringToDate 함수 테스트', () => { | ||
| it('날짜와 시간이 포함된 문자열을 Date 객체로 정확히 바꾼다', () => { | ||
| const input = '2025.05.25 13:45'; | ||
| const result = new Date('2025-05-25T13:45:00'); | ||
| expect(stringToDate(input)).toEqual(result); | ||
| }); | ||
|
|
||
| it('자정 시간 "YYYY.MM.DD 00:00" 형식을 올바르게 Date 객체로 변환한다', () => { | ||
| const input = '2025.05.25 00:00'; | ||
| const result = new Date('2025-05-25T00:00:00'); | ||
| expect(stringToDate(input)).toEqual(result); | ||
| }); | ||
|
|
||
| it('공백이 없는 문자열이 주어지면 예외를 발생시킨다.', () => { | ||
| const input = '2025.05.2513:45'; | ||
| expect(() => stringToDate(input)).toThrow( | ||
| '유효하지 않은 날짜 형식입니다. 형식은 "YYYY.MM.DD HH:mm" 이어야 합니다.', | ||
| ); | ||
| }); | ||
|
|
||
| it('시간 형식이 누락된 경우 예외가 발생한다', () => { | ||
| const input = '2025.05.25 '; | ||
| expect(() => stringToDate(input)).toThrow( | ||
| '유효하지 않은 날짜 형식입니다. 형식은 "YYYY.MM.DD HH:mm" 이어야 합니다.', | ||
| ); | ||
| }); | ||
| }); |
This file contains hidden or 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 |
|---|---|---|
| @@ -1,20 +1,32 @@ | ||
| export function parseRecruitmentPeriod(periodStr: string): { | ||
| recruitmentStart: Date | null; | ||
| recruitmentEnd: Date | null; | ||
| } { | ||
| export const stringToDate = (s: string): Date => { | ||
| const [datePart, timePart] = s.split(' ') as [string, string]; | ||
| if (!datePart || !timePart) { | ||
| throw new Error( | ||
| '유효하지 않은 날짜 형식입니다. 형식은 "YYYY.MM.DD HH:mm" 이어야 합니다.', | ||
| ); | ||
| } | ||
|
|
||
| const isoDate = datePart.replace(/\./g, '-'); | ||
| const date = new Date(`${isoDate}T${timePart}:00`); | ||
|
|
||
| if (isNaN(date.getTime())) { | ||
| throw new Error( | ||
| '유효하지 않은 날짜 형식입니다. 형식은 "YYYY.MM.DD HH:mm" 이어야 합니다.', | ||
| ); | ||
| } | ||
| return date; | ||
| }; | ||
|
|
||
| export const parseRecruitmentPeriod = ( | ||
| periodStr: string, | ||
| ): { recruitmentStart: Date | null; recruitmentEnd: Date | null } => { | ||
| const parts = periodStr.split('~').map((s) => s.trim()); | ||
| if (parts.length !== 2) { | ||
| return { recruitmentStart: null, recruitmentEnd: null }; | ||
| } | ||
|
|
||
| const convertToDate = (s: string): Date => { | ||
| const [datePart, timePart] = s.split(' '); | ||
| const isoDate = datePart.replace(/\./g, '-'); | ||
| return new Date(`${isoDate}T${timePart}:00`); | ||
| }; | ||
seongwon030 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return { | ||
| recruitmentStart: convertToDate(parts[0]), | ||
| recruitmentEnd: convertToDate(parts[1]), | ||
| recruitmentStart: stringToDate(parts[0]), | ||
| recruitmentEnd: stringToDate(parts[1]), | ||
| }; | ||
seongwon030 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| }; | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.