src
├── mocks
│ ├── api
│ │ ├── api 명
│ │ │ ├── index.ts
│ │ │ └── data.ts
│ ├── handlers.ts
│ ├── browser.ts
| └── index.ts
import initMocks from "./mocks/index.ts";
****
if (process.env.NODE_ENV === "development") {
await initMocks();
}
main.tsx
에서 위 코드로 현재 개발모드인지 확인후에 실행시킴
// api/data/searchResultData.ts
export const searchResultData = [
{
image: "https://...생략",
title: "조각집",
type: "rect",
},
{
image: "https://...생략",
title: "안녕",
type: "rect",
},
{
image: "https://...생략",
title: "고고",
type: "rect",
},
];
// api/searchResult.ts
import { rest } from "msw";
import { searchResultData } from "./data/searchResultData";
const handlers = [
rest.get("/kim", (req, res, ctx) => {
return res(
ctx.json({ status: 200, message: "success", data: searchResultData })
);
}),
];
export default handlers;
이렇게 데이터랑 핸들러를 생성해 주면 된다.
이렇게 만든 핸들러를 handlers 에서 다 합쳐주면 됨.
// handlers.ts
import searchResultHandler from "./api/searchResult";
export const handlers = [...searchResultHandler];
내가만든 custom hook 사용해서 default 값 넣고 url 주소 넣으면 msw에 작성한 데이터 받아올 수 있음