Skip to content

Commit

Permalink
feat: Add mock handlers for question API
Browse files Browse the repository at this point in the history
  • Loading branch information
timepresent95 committed Aug 20, 2024
1 parent 1eec860 commit 5984c8d
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 28 deletions.
80 changes: 55 additions & 25 deletions src/mocks/handlers/question.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,63 @@
//NOTE: 로그인 실패 -> 회원 가입 로직
// import {faker} from '@faker-js/faker';
import {http, HttpResponse} from 'msw';
import {http, HttpResponse, PathParams} from 'msw';

import {getApiUrl} from '../utils/api';

import {ResponseGetQuestion} from '@/api/quetion';
import {
BodyPatchAnswer,
BodyPostAnswer,
ParamPatchAnswer,
ResponseGetQuestion,
} from '@/api/quetion';
import {questionApis} from '@/api/routes';
import {verifyFakeJWT} from '@/mocks/utils/token';
import {Answer, Question} from '@/types';

export default [
http.get(getApiUrl(questionApis.getQuestion), ({request}) => {
const Authorized = request.headers.get('Authorization');
if (Authorized === null) {
return new HttpResponse(null, {
status: 401,
statusText: 'Unauthorized',
});
}

const [, accessToken] = Authorized.split(' ');
if (!verifyFakeJWT(accessToken)) {
return new HttpResponse(null, {
status: 403,
statusText: 'Forbidden',
});
}
const question: Question[] = [
{questionHistoryId: 0, question: '지금 이 순간 제일 듣고 싶은 단어는?'},
];
const answer: Answer[] = [];

return HttpResponse.json({
question: '오늘의 질문 입니다!',
} as ResponseGetQuestion);
export default [
http.get(getApiUrl(questionApis.getQuestion), () => {
const response = {
...question[0],
isAnswered: false,
};
return HttpResponse.json<ResponseGetQuestion>(response, {status: 200});
}),
http.post<PathParams, BodyPostAnswer>(
getApiUrl(questionApis.postAnswer),
async ({request}) => {
const body = await request.json();
answer[0] = {
nickname: 'fake nickname',
groupRole: 'SON',
emoji: null,
content: body.answer,
createAt: new Date(),
};
},
),
http.get(getApiUrl(questionApis.getAnswer), () => {
const response = {
answer: answer,
};
return HttpResponse.json(response, {status: 200});
}),
http.patch<ParamPatchAnswer, BodyPatchAnswer>(
getApiUrl(questionApis.patchAnser),
async ({request}) => {
const body = await request.json();
answer[0].content = body.answer;
return HttpResponse.json(null, {status: 200});
},
),
http.get(getApiUrl(questionApis.getList), () => {
const response = {
question,
};
return HttpResponse.json(response, {status: 200});
}),
http.post(getApiUrl(questionApis.cheerUp), () => {
return HttpResponse.json(null, {status: 200});
}),
];
4 changes: 2 additions & 2 deletions src/types/question.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {GoupRole} from './member';
import {GoupRole} from './user';

export type Question = {
questionHistoryId: number;
Expand All @@ -8,7 +8,7 @@ export type Question = {
export type Answer = {
nickname: string;
groupRole: GoupRole;
emoji: string;
emoji: string | null;
content: string;
createAt: Date;
};
1 change: 0 additions & 1 deletion src/utils/token.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {UserInfo} from '@/types';

//NOTE: 유저 정보를 토큰에서 얻기 위한 함수
//FIXME: BE에서 전달해준 payload가 정해지면 수정해야 함
export function getUserFromToken(token: string): UserInfo | null {
try {
const parts = token.split('.');
Expand Down

0 comments on commit 5984c8d

Please sign in to comment.