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

Jest 환경 설정 및 Jest를 사용한 API Test 코드 작성 + Storybook CI Error #306

Merged
merged 15 commits into from
Aug 12, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

cd client && npm run lint-staged
4 changes: 4 additions & 0 deletions .husky/pre-push
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

cd client && npm run test
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,10 @@
- [배포](https://www.chromatic.com/build?appId=62f3c06c73a375912ca8d9be&number=1)
- [repo](https://github.com/jin-Pro/CowDogTing_Storybook)
- ### API 문서

- [API](https://github.com/boostcampwm-2021/web10-CowDogTing/wiki/API-%EB%AC%B8%EC%84%9C)
- ### Test 문서

- [Jest](https://github.com/boostcampwm-2021/web10-CowDogTing/wiki/Jest-%EB%AC%B8%EC%84%9C)

- ### Type 문서
- [Type](https://github.com/boostcampwm-2021/web10-CowDogTing/wiki/Type-%EB%AC%B8%EC%84%9C)
31 changes: 16 additions & 15 deletions client/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
},
"plugins": ["react", "@typescript-eslint"],
"rules": {
"no-undef" : 1,
"react/require-default-props" : 0,
"no-nested-ternary" : 0,
"react/jsx-props-no-spreading" : 0,
Expand All @@ -32,21 +33,21 @@
"react/prop-types" : 0,
"react/react-in-jsx-scope" : 0,
"no-param-reassign" : 0,
"import/prefer-default-export": 0,
"react/function-component-definition" : 0,
"react/jsx-filename-extension" : 0,
"import/no-unresolved": 0,
"react/no-array-index-key" : 0,
"no-use-before-define" : 0,
"import/extensions" : 0,
"no-unused-vars" : 1,
"import/no-extraneous-dependencies" : 0,
"prettier/prettier": [
"error",
{
"endOfLine": "auto"
}
]
"import/prefer-default-export": 0,
"react/function-component-definition" : 0,
"react/jsx-filename-extension" : 0,
"import/no-unresolved": 0,
"react/no-array-index-key" : 0,
"no-use-before-define" : 0,
"import/extensions" : 0,
"no-unused-vars" : 1,
"import/no-extraneous-dependencies" : 0,
"prettier/prettier": [
"error",
{
"endOfLine": "auto"
}
]
},
"settings": {
"import/resolver": {
Expand Down
2 changes: 1 addition & 1 deletion client/.storybook/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module.exports = {
config.resolve.alias["@Organism"] = toPath("src/Component/Organism");
config.resolve.alias["@Template"] = toPath("src/Component/Template");
config.resolve.alias["@Hoc"] = toPath("src/Component/Hoc");
config.resolve.alias["@Util"] = toPath("src/Util");
config.resolve.alias["@Common"] = toPath("src/Util");
config.resolve.alias["@Recoil"] = toPath("src/Recoil");
config.resolve.alias["@Socket"] = toPath("src/Socket");
config.resolve.alias["@emotion/styled"] = toPath("node_modules/@emotion/styled");
Expand Down
2 changes: 1 addition & 1 deletion client/config-overrides.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module.exports = override(
"@Organism": resolve("./src/Component/Organism/"),
"@Template": resolve("./src/Component/Template/"),
"@Hoc": resolve("./src/Component/Hoc/"),
"@Util": resolve("./src/Util/"),
"@Common": resolve("./src/Common/"),
"@Asset": resolve("./src/Asset/"),
"@Recoil": resolve("./src/Recoil/"),
"@Socket": resolve("./src/Socket/"),
Expand Down
1 change: 1 addition & 0 deletions client/msw/handler/chatHandler/chatInfoHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const chatInfoHandler = () => {};
10 changes: 10 additions & 0 deletions client/msw/handler/chatHandler/chatMessagesHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { MockProps } from "..";

export const chatMessagesHandler: MockProps = (req, res, ctx) => {
const params = req.url.searchParams;
const chatRoomId = params.get("chatRoomId");
const index = params.get("index");
if (index === "0") return res(ctx.status(403), ctx.json({ errorMessage: "index에러" }));
if (chatRoomId === "0") return res(ctx.status(403), ctx.json({ errorMessage: "chatRoomId에러" }));
return res(ctx.json(true));
};
9 changes: 9 additions & 0 deletions client/msw/handler/chatHandler/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { rest } from "msw";
import { POST_CHAT_READ_API_URL, CHAT_INFO_URL, CHAT_MESSAGES_API_URL, JOIN_CHAT_URL } from "../../../src/Common/URL";

import { chatInfoHandler } from "./chatInfoHandler";
import { chatMessagesHandler } from "./chatMessagesHandler";
import { joinChatHandler } from "./joinChatHandler";
import { postChatReadHandler } from "./postChatReadHandler";

export const chatHandler = [rest.post(POST_CHAT_READ_API_URL, postChatReadHandler), rest.post(JOIN_CHAT_URL, joinChatHandler), rest.post(CHAT_INFO_URL, chatInfoHandler), rest.get(CHAT_MESSAGES_API_URL, chatMessagesHandler)];
1 change: 1 addition & 0 deletions client/msw/handler/chatHandler/joinChatHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const joinChatHandler = () => {};
10 changes: 10 additions & 0 deletions client/msw/handler/chatHandler/postChatReadHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { errorHandlerMaker, MockProps } from "..";

export const postChatReadHandler: MockProps = (req, res, ctx) => {
const errorHandler = errorHandlerMaker(res, ctx);
const {
body: { chatRoomId },
} = req;
if (chatRoomId === 0) return errorHandler("존재하지 않는 채팅방입니다.");
return res(ctx.json(5));
};
9 changes: 9 additions & 0 deletions client/msw/handler/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { RestRequest, PathParams, ResponseComposition, DefaultBodyType, RestContext } from "msw";

export type MockProps = (req: RestRequest<never, PathParams<string>>, res: ResponseComposition<DefaultBodyType>, ctx: RestContext) => any;
export const errorHandlerMaker = (res: ResponseComposition<DefaultBodyType>, ctx: RestContext) => (message: string) => res(ctx.status(403), ctx.json({ errorMessage: message }));
export * from "./teamHandler";
export * from "./validationHandler";
export * from "./chatHandler";
export * from "./userHandler";
export * from "./requestHandler";
12 changes: 12 additions & 0 deletions client/msw/handler/requestHandler/acceptHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { errorHandlerMaker, MockProps } from "..";

export const acceptHandler: MockProps = (req, res, ctx) => {
const errorHandler = errorHandlerMaker(res, ctx);
const {
body: { from, to },
} = req;
if (from === "") return errorHandler("from입력이 잘못되었습니다.");
if (to === "") return errorHandler("to입력이 잘못되었습니다.");
if (from === to) return errorHandler("from과 to는 같을 수 없습니다.");
return res(ctx.json(true));
};
12 changes: 12 additions & 0 deletions client/msw/handler/requestHandler/denyHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { errorHandlerMaker, MockProps } from "..";

export const denyHandler: MockProps = (req, res, ctx) => {
const errorHandler = errorHandlerMaker(res, ctx);
const {
body: { from, to },
} = req;
if (from === "") return errorHandler("from입력이 잘못되었습니다.");
if (to === "") return errorHandler("to입력이 잘못되었습니다.");
if (from === to) return errorHandler("from과 to는 같을 수 없습니다.");
return res(ctx.json(true));
};
8 changes: 8 additions & 0 deletions client/msw/handler/requestHandler/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { rest } from "msw";
import { acceptHandler } from "./acceptHandler";
import { denyHandler } from "./denyHandler";
import { requestHandler } from "./requestHandler";
import { requestApiHandler } from "./requestApiHandler";
import { ACCEPT_API_URL, DENY_API_URL, REQUEST_API_URL, REQUEST_URL } from "../../../src/Common/URL";

export const request = [rest.post(ACCEPT_API_URL, acceptHandler), rest.post(DENY_API_URL, denyHandler), rest.post(REQUEST_API_URL, requestApiHandler), rest.get(REQUEST_URL, requestHandler)];
12 changes: 12 additions & 0 deletions client/msw/handler/requestHandler/requestApiHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { errorHandlerMaker, MockProps } from "..";

export const requestApiHandler: MockProps = (req, res, ctx) => {
const errorHandler = errorHandlerMaker(res, ctx);
const {
body: { from, to },
} = req;
if (from === "") return errorHandler("from입력이 잘못되었습니다.");
if (to === "") return errorHandler("to입력이 잘못되었습니다.");
if (from === to) return errorHandler("from과 to는 같을 수 없습니다.");
return res(ctx.json(true));
};
3 changes: 3 additions & 0 deletions client/msw/handler/requestHandler/requestHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { errorHandlerMaker, MockProps } from "..";

export const requestHandler: MockProps = (req, res, ctx) => {};
9 changes: 9 additions & 0 deletions client/msw/handler/teamHandler/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { rest } from "msw";
import { TEAM_CREATE_API_URL, TEAM_EXIT_API_URL, TEAM_INFO_URL, TEAM_INVITE_API_URL, TEAM_UPDATE_API_URL } from "../../../src/Common/URL";
import { teamUpdateHandler } from "./teamUpdateHandler";
import { teamCreateHandler } from "./teamCreateHandler";
import { teamExitHandler } from "./teamExitHandler";
import { teamInviteHandler } from "./teamInviteHandler";
import { teamInfoHandler } from "./teamInfoHandler";

export const teamHandler = [rest.post(TEAM_UPDATE_API_URL, teamUpdateHandler), rest.post(TEAM_CREATE_API_URL, teamCreateHandler), rest.post(TEAM_INVITE_API_URL, teamInviteHandler), rest.post(TEAM_EXIT_API_URL, teamExitHandler), rest.post(TEAM_INFO_URL, teamInfoHandler)];
15 changes: 15 additions & 0 deletions client/msw/handler/teamHandler/teamCreateHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { MockProps } from "..";

export const teamCreateHandler: MockProps = (req, res, ctx) => {
const { name, description, location } = req.body;
if (name === "" || description === "" || location === "")
return res(
// Send a valid HTTP status code
ctx.status(403),
// And a response body, if necessary
ctx.json({
errorMessage: `error`,
})
);
return res(ctx.json({ gid: 1 }));
};
5 changes: 5 additions & 0 deletions client/msw/handler/teamHandler/teamExitHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { MockProps } from "..";

export const teamExitHandler: MockProps = (req, res, ctx) => {
return res(ctx.json(true));
};
1 change: 1 addition & 0 deletions client/msw/handler/teamHandler/teamInfoHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const teamInfoHandler = () => {};
15 changes: 15 additions & 0 deletions client/msw/handler/teamHandler/teamInviteHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { MockProps } from "..";

export const teamInviteHandler: MockProps = (req, res, ctx) => {
const {
body: { userId },
} = req;
if (userId !== "") return res(ctx.json(true));

return res(
ctx.status(403),
ctx.json({
errorMessage: `유저가 존재하지 않습니다.`,
})
);
};
15 changes: 15 additions & 0 deletions client/msw/handler/teamHandler/teamUpdateHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { MockProps } from "..";

export const teamUpdateHandler: MockProps = (req, res, ctx) => {
const { name, description, location } = req.body;
if (name === "" || description === "" || location === "")
return res(
// Send a valid HTTP status code
ctx.status(403),
// And a response body, if necessary
ctx.json({
errorMessage: `error`,
})
);
return res(ctx.json(true));
};
7 changes: 7 additions & 0 deletions client/msw/handler/userHandler/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { rest } from "msw";
import { profileHandler } from "./profileHandler";
import { userInfoHandler } from "./userInfoHandler";
import { userHandler } from "./userHandler";
import { PROFILE_API_URL, USER_INFO_API_URL, USER_URL } from "../../../src/Common/URL";

export const user = [rest.post(USER_URL, userHandler), rest.post(USER_INFO_API_URL, userInfoHandler), rest.post(PROFILE_API_URL, profileHandler)];
1 change: 1 addition & 0 deletions client/msw/handler/userHandler/profileHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const profileHandler = () => {};
1 change: 1 addition & 0 deletions client/msw/handler/userHandler/userHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const userHandler = () => {};
13 changes: 13 additions & 0 deletions client/msw/handler/userHandler/userInfoHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { errorHandlerMaker, MockProps } from "..";

export const userInfoHandler: MockProps = (req, res, ctx) => {
const errorHandler = errorHandlerMaker(res, ctx);
const {
body: { id, location, age, info },
} = req;
if (id === "") return errorHandler("id가 잘못되었습니다.");
if (location === "") return errorHandler("location이 잘못되었습니다.");
if (age === 0) return errorHandler("age설정이 잘못되었습니다.");
if (info === "") return errorHandler("info 입력이 잘못되었습니다.");
return res(ctx.json(true));
};
10 changes: 10 additions & 0 deletions client/msw/handler/validationHandler/checkIdValidationHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { errorHandlerMaker, MockProps } from "..";

export const checkIdValidationHandler: MockProps = (req, res, ctx) => {
const errorHandler = errorHandlerMaker(res, ctx);
const params = req.url.searchParams;
const uid = params.get("uid");
if (uid === "") return errorHandler("잘못된 id 형식입니다.");
if (uid === "test") return res(ctx.json(false));
return res(ctx.json(true));
};
8 changes: 8 additions & 0 deletions client/msw/handler/validationHandler/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { rest } from "msw";
import { checkIdValidationHandler } from "./checkIdValidationHandler";
import { loginHandler } from "./loginHandler";
import { registerHandler } from "./registerHandler";
import { logOutHandler } from "./logOutHandler";
import { CHECK_ID_VALIDATION_URL, LOGIN_API_URL, LOGOUT_API_URL, REGISTER_API_URL } from "../../../src/Common/URL";

export const validationHandler = [rest.get(LOGOUT_API_URL, logOutHandler), rest.post(REGISTER_API_URL, registerHandler), rest.post(LOGIN_API_URL, loginHandler), rest.get(CHECK_ID_VALIDATION_URL, checkIdValidationHandler)];
3 changes: 3 additions & 0 deletions client/msw/handler/validationHandler/logOutHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { MockProps } from "..";

export const logOutHandler: MockProps = (req, res, ctx) => res(ctx.json(true));
9 changes: 9 additions & 0 deletions client/msw/handler/validationHandler/loginHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { MockProps } from "..";

export const loginHandler: MockProps = (req, res, ctx) => {
const {
body: { uid, password },
} = req;
if (uid !== "test" || password !== "qwer1234") return res(ctx.status(403), ctx.json({ errorMessage: "로그인 실패" }));
return res(ctx.json(true));
};
15 changes: 15 additions & 0 deletions client/msw/handler/validationHandler/registerHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { errorHandlerMaker, MockProps } from "..";

export const registerHandler: MockProps = (req, res, ctx) => {
const errorHandler = errorHandlerMaker(res, ctx);
const {
body: { uid, password, location, age, sex, info },
} = req;
if (uid === "") return errorHandler("아이디 입력 오류");
if (password === "") return errorHandler("비밀번호 입력 오류");
if (location === "") return errorHandler("지역 입력 오류");
if (age === 0) return errorHandler("나이 입력 오류");
if (sex === "") return errorHandler("성별 입력 오류");
if (info === "") return errorHandler("정보 입력 오류");
return res(ctx.json(true));
};
5 changes: 5 additions & 0 deletions client/msw/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// src/mocks/server.js
import { setupServer } from "msw/node";
import { validationHandler, request as requestHandler, teamHandler, chatHandler, user as userHandler } from "./handler";

export const server = setupServer(...teamHandler, ...requestHandler, ...chatHandler, ...userHandler, ...validationHandler);
Loading