-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
현재 유저가 어떤 게시글 종류를 보고 있는지 정보를 반환하는 함수 구현 및 적용 (#318)
* feat: (#310) 유저에게 어떤 게시글에 대한 종류를 보고 있는지 정보를 반환하는 함수 구현 * feat: (#310) 레이아웃, 포스트 리스트 페이지에 코드 적용 * feat: (#310) 검색어 글자가 10글자가 넘어간다면 ...으로 축약해서 보여주도록 구현 test의 given을 구체화
- Loading branch information
1 parent
33b4a3b
commit ad50c70
Showing
8 changed files
with
179 additions
and
8 deletions.
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
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,101 @@ | ||
import { SelectedState, getSelectedState } from '@utils/post/getSelectedState'; | ||
|
||
import { MOCK_CATEGORY_LIST } from '@mocks/mockData/categoryList'; | ||
|
||
describe('getSelectedState 사용했을 때 현재 유저에게 어떤 게시글에 대한 종류를 보고 있는지에 대한 정보를 반환한다.', () => { | ||
test('현재 카테고리가 선택되어 있고, 카테고리 아이디가 1번일 때 해당하는 카테고리 이름을 반환한다.', () => { | ||
const categoryId = 1; | ||
const state: SelectedState = { | ||
postType: 'category', | ||
categoryId, | ||
keyword: '', | ||
categoryList: MOCK_CATEGORY_LIST, | ||
}; | ||
|
||
const result = getSelectedState(state); | ||
|
||
expect(categoryId).toBe(MOCK_CATEGORY_LIST[0].id); | ||
expect(result).toBe(MOCK_CATEGORY_LIST[0].name); | ||
}); | ||
|
||
test('현재 검색을 한 상태이면, 검색 키워드를 반환한다.', () => { | ||
const keyword = '갤럭시'; | ||
const state: SelectedState = { | ||
postType: 'search', | ||
categoryId: 0, | ||
keyword, | ||
categoryList: MOCK_CATEGORY_LIST, | ||
}; | ||
|
||
const result = getSelectedState(state); | ||
|
||
expect(result).toBe(keyword); | ||
}); | ||
|
||
test('검색어를 길게 설정한 경우 10자만 보여주고 ...으로 표시해서 보여준다.', () => { | ||
const keyword = '아이폰갤럭시뉴진스아이브세븐틴슈퍼주니어임'; | ||
const state: SelectedState = { | ||
postType: 'search', | ||
categoryId: 0, | ||
keyword, | ||
categoryList: MOCK_CATEGORY_LIST, | ||
}; | ||
|
||
const result = getSelectedState(state); | ||
|
||
expect(result).toBe('아이폰갤럭시뉴진스아...'); | ||
}); | ||
|
||
test('검색어를 10글자인 경우 10글자 전부 표시해서 보여준다.', () => { | ||
const keyword = '아이폰갤럭시뉴진스아'; | ||
const state: SelectedState = { | ||
postType: 'search', | ||
categoryId: 0, | ||
keyword, | ||
categoryList: MOCK_CATEGORY_LIST, | ||
}; | ||
|
||
const result = getSelectedState(state); | ||
|
||
expect(result).toBe('아이폰갤럭시뉴진스아'); | ||
}); | ||
|
||
test('현재 홈 화면에 있다면, "전체"를 반환한다', () => { | ||
const state: SelectedState = { | ||
postType: 'posts', | ||
categoryId: 0, | ||
keyword: '', | ||
categoryList: MOCK_CATEGORY_LIST, | ||
}; | ||
|
||
const result = getSelectedState(state); | ||
|
||
expect(result).toBe('전체'); | ||
}); | ||
|
||
test('현재 내가 작성한 글 페이지에 있다면, "내가 작성한 글"을 반환한다.', () => { | ||
const state: SelectedState = { | ||
postType: 'myPost', | ||
categoryId: 0, | ||
keyword: '', | ||
categoryList: MOCK_CATEGORY_LIST, | ||
}; | ||
|
||
const result = getSelectedState(state); | ||
|
||
expect(result).toBe('내가 작성한 글'); | ||
}); | ||
|
||
test('현재 내가 투표한 글 페이지에 있다면, "내가 투표한 글"을 반환한다.', () => { | ||
const state: SelectedState = { | ||
postType: 'myVote', | ||
categoryId: 0, | ||
keyword: '', | ||
categoryList: MOCK_CATEGORY_LIST, | ||
}; | ||
|
||
const result = getSelectedState(state); | ||
|
||
expect(result).toBe('내가 투표한 글'); | ||
}); | ||
}); |
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
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
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,41 @@ | ||
import { Category } from '@type/category'; | ||
|
||
import { PostRequestKind } from '@components/post/PostListPage/types'; | ||
|
||
export interface SelectedState { | ||
postType: PostRequestKind; | ||
categoryId: number; | ||
keyword: string; | ||
categoryList: Category[]; | ||
} | ||
|
||
const SLICED_LENGTH_NUMBER = 10; | ||
|
||
export const getSelectedState = ({ | ||
postType, | ||
categoryId, | ||
keyword, | ||
categoryList, | ||
}: SelectedState) => { | ||
if (postType === 'category') { | ||
const selectedCategory = categoryList.find(category => category.id === categoryId); | ||
|
||
return selectedCategory?.name ?? '전체'; | ||
} | ||
|
||
if (postType === 'search') { | ||
return keyword.length > SLICED_LENGTH_NUMBER | ||
? `${keyword.slice(0, SLICED_LENGTH_NUMBER)}...` | ||
: keyword; | ||
} | ||
|
||
if (postType === 'myPost') { | ||
return '내가 작성한 글'; | ||
} | ||
|
||
if (postType === 'myVote') { | ||
return '내가 투표한 글'; | ||
} | ||
|
||
return '전체'; | ||
}; |