Skip to content

Commit

Permalink
feat: (#310) 유저에게 어떤 게시글에 대한 종류를 보고 있는지 정보를 반환하는 함수 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
Gilpop8663 committed Aug 10, 2023
1 parent c416bad commit 3fa6a32
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 1 deletion.
2 changes: 1 addition & 1 deletion frontend/__test__/getPathFragment.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getPathFragment } from '@utils/getPathFragment';

describe('useUrlDetection를 사용했을 때 path의 값만 나오도록 한다.', () => {
describe('getPathFragment 사용했을 때 path의 값만 나오도록 한다.', () => {
test('/posts/category/12인 경우, /posts/category를 반환한다.', () => {
const result = getPathFragment('/posts/category/12');

Expand Down
63 changes: 63 additions & 0 deletions frontend/__test__/getSelectedState.test.ts
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('내가 투표한 글');
});
});
37 changes: 37 additions & 0 deletions frontend/src/utils/post/getSelectedState.ts
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 '전체';
};

0 comments on commit 3fa6a32

Please sign in to comment.