-
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.
feat: (#310) 유저에게 어떤 게시글에 대한 종류를 보고 있는지 정보를 반환하는 함수 구현
- Loading branch information
1 parent
c416bad
commit 3fa6a32
Showing
3 changed files
with
101 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
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,63 @@ | ||
import { getSelectedState } from '@utils/post/getSelectedState'; | ||
|
||
import { MOCK_CATEGORY_LIST } from '@mocks/mockData/categoryList'; | ||
|
||
describe('getSelectedState 사용했을 때 현재 유저에게 어떤 게시글에 대한 종류를 보고 있는지에 대한 정보를 반환한다.', () => { | ||
test('현재 카테고리가 선택되어 있고, 카테고리 아이디가 1번일 때 해당하는 카테고리 이름을 반환한다.', () => { | ||
const categoryId = 1; | ||
|
||
const result = getSelectedState({ | ||
postType: 'category', | ||
categoryId, | ||
keyword: '', | ||
categoryList: MOCK_CATEGORY_LIST, | ||
}); | ||
|
||
expect(categoryId).toBe(MOCK_CATEGORY_LIST[0].id); | ||
expect(result).toBe(MOCK_CATEGORY_LIST[0].name); | ||
}); | ||
|
||
test('현재 검색을 한 상태이면, 검색 키워드를 반환한다.', () => { | ||
const result = getSelectedState({ | ||
postType: 'search', | ||
categoryId: 0, | ||
keyword: '갤럭시', | ||
categoryList: MOCK_CATEGORY_LIST, | ||
}); | ||
|
||
expect(result).toBe('갤럭시'); | ||
}); | ||
|
||
test('현재 홈 화면에 있다면, "전체"를 반환한다', () => { | ||
const result = getSelectedState({ | ||
postType: 'posts', | ||
categoryId: 0, | ||
keyword: '', | ||
categoryList: MOCK_CATEGORY_LIST, | ||
}); | ||
|
||
expect(result).toBe('전체'); | ||
}); | ||
|
||
test('현재 내가 작성한 글 페이지에 있다면, "내가 작성한 글"을 반환한다.', () => { | ||
const result = getSelectedState({ | ||
postType: 'myPost', | ||
categoryId: 0, | ||
keyword: '', | ||
categoryList: MOCK_CATEGORY_LIST, | ||
}); | ||
|
||
expect(result).toBe('내가 작성한 글'); | ||
}); | ||
|
||
test('현재 내가 투표한 글 페이지에 있다면, "내가 투표한 글"을 반환한다.', () => { | ||
const result = getSelectedState({ | ||
postType: 'myVote', | ||
categoryId: 0, | ||
keyword: '', | ||
categoryList: MOCK_CATEGORY_LIST, | ||
}); | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { Category } from '@type/category'; | ||
|
||
import { PostRequestKind } from '@components/post/PostListPage/types'; | ||
|
||
interface SelectedState { | ||
postType: PostRequestKind; | ||
categoryId: number; | ||
keyword: string; | ||
categoryList: Category[]; | ||
} | ||
|
||
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; | ||
} | ||
|
||
if (postType === 'myPost') { | ||
return '내가 작성한 글'; | ||
} | ||
|
||
if (postType === 'myVote') { | ||
return '내가 투표한 글'; | ||
} | ||
|
||
return '전체'; | ||
}; |