Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

회원 닉네임 수정, 회원 탈퇴 fetch 함수 구현 및 MSW 코드 작성 #178

Merged
merged 4 commits into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion frontend/__test__/api/user.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { getUserInfo, transformUserInfoResponse } from '@api/wus/userInfo';
import {
cancelMembership,
getUserInfo,
modifyNickname,
transformUserInfoResponse,
} from '@api/wus/userInfo';

import { MOCK_USER_INFO } from '@mocks/mockData/user';

Expand All @@ -16,4 +21,16 @@ describe('서버와 통신하여 유저의 정보를 불러올 수 있어야 한

expect(userInfoKeys).toEqual(['nickname', 'postCount', 'userPoint', 'voteCount', 'badge']);
});

test('유저의 닉네임을 수정한다', async () => {
await modifyNickname('wood');

expect(MOCK_USER_INFO.nickname).toBe('wood');
});

test('유저가 회원 탈퇴를 한다', async () => {
await cancelMembership();

expect(MOCK_USER_INFO.nickname).toBe('cancel');
});
});
12 changes: 10 additions & 2 deletions frontend/src/api/wus/userInfo.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { UserInfoResponse, User } from '@type/user';
import type { UserInfoResponse, User, ModifyNicknameRequest } from '@type/user';

import { getFetch } from '@utils/fetch';
import { deleteFetch, getFetch, patchFetch } from '@utils/fetch';

export const transformUserInfoResponse = (userInfo: UserInfoResponse): User => {
const { nickname, postCount, userPoint, voteCount, badge } = userInfo;
Expand All @@ -19,3 +19,11 @@ export const getUserInfo = async () => {

return transformUserInfoResponse(userInfo);
};

export const modifyNickname = async (nickname: string) => {
await patchFetch<ModifyNicknameRequest>('/members/me/nickname', { nickname });
};

export const cancelMembership = async () => {
await deleteFetch('/members/me/delete');
};
12 changes: 12 additions & 0 deletions frontend/src/mocks/wus/userInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,16 @@ export const mockUserInfo = [
rest.get('/members/me', (req, res, ctx) => {
return res(ctx.status(200), ctx.json(MOCK_USER_INFO));
}),

rest.patch('/members/me/nickname', (req, res, ctx) => {
MOCK_USER_INFO.nickname = 'wood';

return res(ctx.status(200), ctx.json({ ok: true }));
}),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

닉네임이 성공적으로 수정되었습니다! 같은 구체적인 message 어떠신가요?!


rest.delete('/members/me/delete', (req, res, ctx) => {
MOCK_USER_INFO.nickname = 'cancel';

return res(ctx.status(204));
}),
];
4 changes: 4 additions & 0 deletions frontend/src/types/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ export interface UserInfoResponse {
voteCount: number;
badge?: string;
}

export interface ModifyNicknameRequest {
nickname: string;
}
11 changes: 5 additions & 6 deletions frontend/src/utils/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,20 @@ export const putFetch = async <T, R>(url: string, body: T): Promise<R | void> =>
return data;
};

export const patchFetch = async (url: string) => {
export const patchFetch = async <T>(url: string, body: T) => {
const response = await fetch(url, {
method: 'PATCH',
headers,
body: JSON.stringify(body),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍👍

});

const data = await response.json();

if (!response.ok) {
throw new Error(data.message);
}

return response;
};

export const deleteFetch = async (url: string) => {
Expand All @@ -74,11 +77,7 @@ export const deleteFetch = async (url: string) => {
headers,
});

const data = await response.json();

if (!response.ok) {
throw new Error(data.message);
}
return response;
};

export const multiPostFetch = async (url: string, body: FormData) => {
Expand Down