Skip to content

Commit

Permalink
⚙️ MSW Follow, Search, User Test 코드 (#51)
Browse files Browse the repository at this point in the history
* feat: User Interface에 locked 속성 추가, Mock data 반영

* feat: user handler -> follow, profile, search handler로 분리

같은 도메인에 따라 관리하기 위해서 user api 항목을 follow, profile, search로 분리하였습니다.
또한 follow handler의 코드가 일부 수정되었습니다.

* feat: worker와 test server에 followHandler 추가

* chore: relationshipStatus mock data 수정

* test: follow API 테스트 추가

* test: follow API error처리 테스트 추가

* feat: worker와 test server에 searchHandler, profileHandler 추가

* feat: profileHandler -> userHandler로 이름 수정

* feat: profileFeed Interface / mock data에서 Title 삭제

UI 수정으로 인해 삭제했습니다.

* feat: likeUser mock data 구현

* feat: searchHanlder likeUsers 연결

* feat: profileHandler -> userHandler로 수정

* feat: userHandler profileFeed 조회 userId 검사 구현

* chore: follow test toEqual -> toBe 수정

* feat: search test 구현

* feat: user test 구현

* feat: user error test 구현

* chore: user test console.log 삭제
  • Loading branch information
Legitgoons authored May 11, 2024
1 parent 68f618a commit 86deaf8
Show file tree
Hide file tree
Showing 16 changed files with 586 additions and 111 deletions.
8 changes: 6 additions & 2 deletions src/app/mocks/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ import { setupWorker } from 'msw/browser';
import { commentHandlers } from './handler/comment';
import { feedHandlers } from './handler/feed';
import { likeHandlers } from './handler/like';
import { userHandlers } from './handler/user';
import { followHandler } from './handler/follow';
import { searchHandler } from './handler/search';
import { userHandler } from './handler/user';

// 브라우저에서 실행하기 위한 mocking worker 초기화
export const worker = setupWorker(
...commentHandlers,
...feedHandlers,
...likeHandlers,
...userHandlers,
...followHandler,
...searchHandler,
...userHandler,
);
58 changes: 58 additions & 0 deletions src/app/mocks/consts/likeUser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { User } from '@/shared/consts';

interface Users {
[userId: number]: User;
}

export const likeUsers: Users = {
1: {
id: 2,
profileImage: 'https://picsum.photos/200/200',
name: '이의찬',
content: 'Legitgoons',
locked: false,
feedCount: 124,
followingCount: 2,
followerCount: 5341,
},
2: {
id: 3,
profileImage: 'https://picsum.photos/200/200',
name: '양재서',
content: 'psychology50',
locked: true,
feedCount: 6,
followingCount: 35,
followerCount: 423,
},
3: {
id: 5,
profileImage: 'https://picsum.photos/200/200',
name: '신얀',
content: 'yanni13',
locked: true,
feedCount: 51,
followingCount: 7897,
followerCount: 7890,
},
4: {
id: 4,
profileImage: 'https://picsum.photos/200/200',
name: '이수민',
content: 'SSXXMM22',
locked: true,
feedCount: 24,
followingCount: 42,
followerCount: 53251,
},
5: {
id: 7,
profileImage: 'https://picsum.photos/200/200',
name: '안성윤',
content: 'asn6878',
locked: true,
feedCount: 87,
followingCount: 67,
followerCount: 4556,
},
};
25 changes: 8 additions & 17 deletions src/app/mocks/consts/profileFeed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,22 @@ interface ProfileFeeds {
export const profileFeeds: ProfileFeeds = {
1: {
id: 1,
title: 'Feed Title 1',
content: 'Feed Content 1',
content: 'Profile Feed Content 1',
thubnailImage: 'https://picsum.photos/200/200',

likeCount: likes[1].totalCount,
commentCount: comments[1].length,
},
2: {
id: 2,
title: 'Feed Title 2',
content: 'Feed Content 2',
content: 'Profile Feed Content 2',
thubnailImage: 'https://picsum.photos/200/200',

likeCount: likes[2].totalCount,
commentCount: comments[2].length,
},
3: {
id: 3,
title: 'Feed Title 3',
content: 'Feed Content 3',
thubnailImage: 'https://picsum.photos/200/200',

Expand All @@ -36,53 +33,47 @@ export const profileFeeds: ProfileFeeds = {
},
4: {
id: 4,
title: 'Feed Title 4',
content: 'Feed Content 4',
content: 'Profile Feed Content 4',
thubnailImage: 'https://picsum.photos/200/200',

likeCount: likes[4].totalCount,
commentCount: comments[4].length,
},
5: {
id: 5,
title: 'Feed Title 5',
content: 'Feed Content 5',
content: 'Profile Feed Content 5',
thubnailImage: 'https://picsum.photos/200/200',

likeCount: likes[5].totalCount,
commentCount: comments[5].length,
},
6: {
id: 6,
title: 'Feed Title 5',
content: 'Feed Content 5',
content: 'Profile Feed Content 5',
thubnailImage: 'https://picsum.photos/200/200',

likeCount: likes[6].totalCount,
commentCount: comments[6].length,
},
7: {
id: 7,
title: 'Feed Title 7',
content: 'Feed Content 7',
content: 'Profile Feed Content 7',
thubnailImage: 'https://picsum.photos/200/200',

likeCount: likes[7].totalCount,
commentCount: comments[7].length,
},
8: {
id: 8,
title: 'Feed Title 8',
content: 'Feed Content 8',
content: 'Profile Feed Content 8',
thubnailImage: 'https://picsum.photos/200/200',

likeCount: likes[8].totalCount,
commentCount: comments[8].length,
},
9: {
id: 9,
title: 'Feed Title 9',
content: 'Feed Content 9',
content: 'Profile Feed Content 9',
thubnailImage: '',

likeCount: likes[9].totalCount,
Expand Down
2 changes: 1 addition & 1 deletion src/app/mocks/consts/relationshipStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ export const relationshipStatus: Relationship = {
6: 'pending',
7: 'following',
8: 'none',
9: 'pending',
9: 'none',
};
9 changes: 9 additions & 0 deletions src/app/mocks/consts/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export const users: Users = {
profileImage: '',
name: '강병준',
content: 'bangdori',
locked: false,
feedCount: 100,
followingCount: 347,
followerCount: 25,
Expand All @@ -19,6 +20,7 @@ export const users: Users = {
profileImage: '',
name: '이의찬',
content: 'Legitgoons',
locked: false,
feedCount: 124,
followingCount: 2,
followerCount: 5341,
Expand All @@ -28,6 +30,7 @@ export const users: Users = {
profileImage: '',
name: '양재서',
content: 'psychology50',
locked: true,
feedCount: 6,
followingCount: 35,
followerCount: 423,
Expand All @@ -37,6 +40,7 @@ export const users: Users = {
profileImage: 'https://picsum.photos/32/32',
name: '이수민',
content: 'SSXXMM22',
locked: true,
feedCount: 24,
followingCount: 42,
followerCount: 53251,
Expand All @@ -46,6 +50,7 @@ export const users: Users = {
profileImage: 'https://picsum.photos/32/32',
name: '신얀',
content: 'yanni13',
locked: true,
feedCount: 51,
followingCount: 7897,
followerCount: 7890,
Expand All @@ -55,6 +60,7 @@ export const users: Users = {
profileImage: 'https://picsum.photos/32/32',
name: '이주원',
content: '2weeksone',
locked: false,
feedCount: 97,
followingCount: 98,
followerCount: 8975,
Expand All @@ -64,6 +70,7 @@ export const users: Users = {
profileImage: 'https://picsum.photos/32/32',
name: '안성윤',
content: 'asn6878',
locked: true,
feedCount: 87,
followingCount: 67,
followerCount: 4556,
Expand All @@ -73,6 +80,7 @@ export const users: Users = {
profileImage: 'https://picsum.photos/32/32',
name: '이진우',
content: 'jinlee1703',
locked: false,
feedCount: 32,
followingCount: 24,
followerCount: 543,
Expand All @@ -82,6 +90,7 @@ export const users: Users = {
profileImage: 'https://picsum.photos/32/32',
name: '최희진',
content: 'heejinnn',
locked: false,
feedCount: 66,
followingCount: 1,
followerCount: 1,
Expand Down
87 changes: 87 additions & 0 deletions src/app/mocks/handler/follow.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { http } from 'msw';

import {
createHttpSuccessResponse,
createHttpErrorResponse,
} from '../dir/response';

import { relationshipStatus } from '../consts/relationshipStatus';
import { users } from '../consts/user';

export const followHandler = [
// 1️⃣ 팔로우 요청
http.post('/users/:user_id/follow', ({ params }) => {
const { user_id } = params;

if (isNaN(Number(user_id))) {
return createHttpErrorResponse('4220');
}

const formattedUserId = Number(user_id);
const followInfo = relationshipStatus[formattedUserId];

switch (followInfo) {
case 'self':
return createHttpErrorResponse('4220');
case 'none':
if (users[formattedUserId].locked) {
relationshipStatus[formattedUserId] = 'pending';
} else relationshipStatus[formattedUserId] = 'following';
break;
case 'following':
return createHttpErrorResponse('4220');
case 'pending':
return createHttpErrorResponse('4220');
default:
return createHttpErrorResponse('4040');
}

return createHttpSuccessResponse({});
}),
// 2️⃣ 언팔로우 & 팔로우 요청 취소
http.delete('/users/:user_id/follow', ({ params }) => {
const { user_id } = params;

if (isNaN(Number(user_id))) {
return createHttpErrorResponse('4220');
}

const formattedUserId = Number(user_id);
const followInfo = relationshipStatus[formattedUserId];

switch (followInfo) {
case 'self':
return createHttpErrorResponse('4220');
case 'none':
return createHttpErrorResponse('4220');
case 'following':
relationshipStatus[formattedUserId] = 'none';
break;
case 'pending':
relationshipStatus[formattedUserId] = 'none';
break;

default:
return createHttpErrorResponse('4040');
}

return createHttpSuccessResponse({});
}),
// 3️⃣ 팔로우 확인
http.get('/users/:user_id/follow', ({ params }) => {
const { user_id } = params;

if (isNaN(Number(user_id))) {
return createHttpErrorResponse('4220');
}

const formattedUserId = Number(user_id);
const followInfo = relationshipStatus[formattedUserId];

if (!followInfo) {
return createHttpErrorResponse('4040');
}

return createHttpSuccessResponse({ relationshipStatus: followInfo });
}),
];
42 changes: 42 additions & 0 deletions src/app/mocks/handler/search.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { http } from 'msw';
import { User } from '@/shared/consts';

import {
createHttpSuccessResponse,
createHttpErrorResponse,
} from '../dir/response';

import { users } from '../consts/user';
import { likeUsers } from '../consts/likeUser';

export const searchHandler = [
// 1️⃣ 사용자 검색
http.get('/users', ({ request }) => {
const url = new URL(request.url);
const query = url.searchParams.get('q');

if (!query) {
return createHttpErrorResponse('4220');
}

const filteredUsers: User[] = Object.values(users).filter((user) =>
user.name.includes(query),
);
return createHttpSuccessResponse({ users: filteredUsers });
}),
// 2️⃣ 좋아요 사용자 검색
http.get('/like', ({ request }) => {
const url = new URL(request.url);
const query = url.searchParams.get('q');

if (!query) {
return createHttpErrorResponse('4220');
}

const filteredUsers: User[] = Object.values(likeUsers).filter((user) =>
user.name.includes(query),
);

return createHttpSuccessResponse({ likeUsers: filteredUsers });
}),
];
Loading

0 comments on commit 86deaf8

Please sign in to comment.