Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 1x 1x 1x 7x 7x 7x 7x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import { http, HttpResponse } from 'msw';
import { User } from '@/types/service/user';
import { createMockErrorResponse, createMockSuccessResponse } from '../common/common-mock';
import { mockUserItems } from './user-mock';
// 1. 팔로우 등록 모킹
const followUserMock = http.post(`*/users/follow`, async ({ request }) => {
const url = new URL(request.url);
const followNickname = url.searchParams.get('followNickname');
const user = mockUserItems.find((v) => v.nickName === followNickname);
if (user?.isFollow === false) {
return HttpResponse.json(createMockSuccessResponse('팔로우 성공'));
}
if (user?.isFollow === null) {
return HttpResponse.json(
createMockErrorResponse({
status: 400,
detail: '회원 : 자기 자신을 팔로우할 수 없습니다.',
errorCode: 'NOT_SAME_FOLLOW',
}),
);
}
if (user?.isFollow === true) {
return HttpResponse.json(
createMockErrorResponse({
status: 400,
detail: '회원 : 이미 팔로우 중입니다.',
errorCode: 'ALREADY_EXIST_FOLLOW',
}),
);
}
});
// 2. 유저 프로필 변경 모킹
const updateUserItemMock = http.patch(`*/users/profile`, async ({ request }) => {
const body = (await request.json()) as User;
return HttpResponse.json(
createMockSuccessResponse({
...mockUserItems[0],
...body,
}),
);
});
// 4. 알림 설정 변경 모킹
const updateMyNotificationMock = http.patch(`*/users/notification`, async ({ request }) => {
const url = new URL(request.url);
const isNotificationEnabled = url.searchParams.get('isNotificationEnabled');
return HttpResponse.json(
createMockSuccessResponse({
...mockUserItems[0],
isNotificationEnabled: !!isNotificationEnabled,
}),
);
});
// 5. 유저 프로필 조회 모킹
const getUserItemMock = http.get(`*/users/:userId`, ({ params }) => {
const id = Number(params.userId);
const user = mockUserItems.find((item) => item.userId === id);
if (!user) {
return HttpResponse.json(
createMockErrorResponse({
status: 404,
detail: '존재하지 않는 유저입니다.',
errorCode: 'U001',
}),
{ status: 404 },
);
}
return HttpResponse.json(createMockSuccessResponse(user));
});
// 7. 닉네임 중복 검사 모킹
const getNicknameAvailabilityMock = http.get(`*/users/nickname/availability`, ({ request }) => {
const url = new URL(request.url);
const nickname = url.searchParams.get('nickname');
const user = mockUserItems.find((item) => item.nickName === nickname);
return HttpResponse.json(
createMockSuccessResponse({
data: { available: !user },
}),
);
});
// 8. 본인 프로필 조회 모킹
const getMeItemMock = http.get(`*/users/me`, () => {
const id = 1;
const user = mockUserItems.find((item) => item.userId === id);
return HttpResponse.json(createMockSuccessResponse(user));
});
// 7. 닉네임 중복 검사 모킹
const getEmailAvailabilityMock = http.get(`*/users/email/availability`, ({ request }) => {
const url = new URL(request.url);
const email = url.searchParams.get('email');
const user = mockUserItems.find((item) => item.email === email);
return HttpResponse.json(
createMockSuccessResponse({
data: { available: !user },
}),
);
});
const unfollowUserMock = http.delete(`*/users/unfollow`, ({ request }) => {
const url = new URL(request.url);
const unFollowNickname = url.searchParams.get('unFollowNickname');
const user = mockUserItems.find((v) => v.nickName === unFollowNickname);
if (user?.isFollow === false) {
return HttpResponse.json(createMockSuccessResponse('팔로우 취소 성공'));
}
if (user?.isFollow === null) {
return HttpResponse.json(
createMockErrorResponse({
status: 400,
detail: '회원 : 팔로우 취소 대상은 본인 될 수 없습니다.',
errorCode: 'NOT_SAME_FOLLOW',
}),
);
}
if (user?.isFollow === true) {
return HttpResponse.json(
createMockErrorResponse({
status: 400,
detail: '회원 : 팔로우 관계를 찾을 수 없습니다.',
errorCode: 'NOT_FOUND_FOLLOW',
}),
);
}
});
export const userHandlers = [
followUserMock,
getMeItemMock,
getUserItemMock,
updateMyNotificationMock,
updateUserItemMock,
getNicknameAvailabilityMock,
getEmailAvailabilityMock,
unfollowUserMock,
];
|