We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
MSW๋ Mock Service Worker๋ก ์๋น์ค ์์ปค๋ฅผ ์ด์ฉํ์ฌ API๋ฅผ ๋ชจํนํ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ด๋ค. ๋คํธ์ํฌ ์์ฒญ์ ๊ฐ๋ก์ฑ๋๋ก ์ค๊ณ๋ Service Worker API๋ฅผ ํ์ฉํ๊ธฐ ๋๋ฌธ์ ๋ชฉ ์ฌ์ฉ ์ฌ๋ถ ๊ด๊ณ ์์ด ๋์ผํ ์ ํ๋ฆฌ์ผ์ด์ ๋์์ ๋ณด์ฅํ๋ค. ๋ํ, ๋ชจํน์ ์ํด ์ ํ๋ฆฌ์ผ์ด์ ์ฝ๋๋ฅผ ๋ณ๊ฒฝํ ํ์๊ฐ ์๋ค๋ ๊ฒ์ด ๊ฐ์ฅ ํฐ ์ฅ์ ์ด๋คโ๏ธ
TypeScript + MSW
์๋ฒ API๊ฐ ๋ง๋ค์ด์ง๊ธฐ ์ ์ ํ ์คํธ๋ฅผ ์ํ API ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ์์ต๋๋ค โจ
ํธ๋ค๋ฌ(handler)
setupWorker()
_app.tsx์ ํ๋จ์ ์ฝ๋๋ฅผ ์คํํ์ฌ mocking์ ํ์ฑํํ๋ค.
if (process.env.NODE_ENV === 'development') { worker.start(); }
๐ yarn start ๋ก ์คํํ๋ฉด Devtools > Console์ Mocking enabled๊ฐ ๋ํ๋๋ค.
๐ mocks/handlers/voting.ts
import { rest } from 'msw'; import { votingData } from '../data/votingData'; // ์๋ฒ ์ฐ๊ฒฐ์ ์์ get ํต์ ์์ฑ export const getVoting = rest.get('/vote', (req, res, ctx) => { const search_vote_id = Number(req.url.searchParams.get('vote_id')); const voting_list = votingData.filter(({ vote_id }) => search_vote_id === vote_id); return res(ctx.json(voting_list[0])); });
๐ lib/hooks/voting.ts
import { AxiosResponse } from 'axios'; import useSWR from 'swr'; import { client } from '../axios'; import { VotingInfo } from './../../types/voting'; // useSWR์ ์ฌ์ฉํ ์ปค์คํ ํ ์์ฑ export const useGetVotingInfo = (vote_id: number) => { // ๋ฐ์ดํฐ ํ์ ๊น์ง ์ฌ์ฉ์ ์ง์ const { data, error } = useSWR<AxiosResponse<VotingInfo>>(`/vote?vote_id=${vote_id}`, client.get, { // ์คํจ์ ์ฌ์์ฒญ 3๋ฒ errorRetryCount: 3, }); return { votingInfo: data, isLoading: !error && !data, isError: error, }; };
lib/api
๐ pages/Voting.tsx
import React, { useEffect } from 'react'; import { useRecoilState } from 'recoil'; import { Error, Loading } from '../components/common'; import { Header } from '../components/Voting'; import { useGetVotingInfo } from '../lib/hooks/voting'; import { votingStateSelector } from '../recoil/player/selector'; const Voting = () => { // ์ปค์คํ ํ ์ผ๋ก ํฌํ ์ ๋ณด๋ฅผ ๊ฐ์ ธ์ค๊ณ , ์ด๋ฅผ ํด๋ผ์ด์ธํธ์ ์ ์ญ์ํ๋ก ๋๊ฒจ์ฃผ๊ธฐ const { votingInfo, isLoading, isError } = useGetVotingInfo(1); const [newVotingInfo, setNewVotingInfo] = useRecoilState(votingStateSelector); useEffect(() => { if (votingInfo) { setNewVotingInfo(votingInfo.data); } }, []); // ๋ฐ์ดํฐ ๋ฐํ์ ๋ฐ๋ฅธ ์ปดํฌ๋ํธ ๋ ๋๋ง if (isLoading) return <Loading />; if (isError) return <Error />; return ( <div> <p>{newVotingInfo.vote_title}</p> <Header /> </div> ); }; export default Voting;
๐ผ ์ ๋ ์ธ์ ๋ ๋๊ธฐ ์ค์ด๋ ๋ชจ๋ฅด๋ ๋ถ๋ถ์ด๋ ๊ถ๊ธํ ๋ถ๋ถ์ด ์๋ค๋ฉด ์ธ์ ๋ ์ง ์ํ๋ฅผ ๋ถ๋ฌ์ฃผ์ธ์ ๐ผ
The text was updated successfully, but these errors were encountered:
Happhee
No branches or pull requests
โจ Mocks Service Worker
TypeScript + MSW
์๋ฒ API๊ฐ ๋ง๋ค์ด์ง๊ธฐ ์ ์ ํ ์คํธ๋ฅผ ์ํ API ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ์์ต๋๋ค โจ
โ ํด๋ ๋ฐ ํ์ผ ์ค๋ช
๐ mocks/data
๐ mocks/handlers
ํธ๋ค๋ฌ(handler)
์ฝ๋๋ฅผ ์์ฑํ ํด๋์ ๋๋ค๐ mocks/browsers.ts (๋ธ๋ผ์ฐ์ ํ๊ฒฝ) or server.ts (๋ ธ๋ ํ๊ฒฝ)
setupWorker()
ํจ์๋ฅผ ์ฌ์ฉํด์ ์๋น์ค ์์ปค๋ฅผ ์์ฑํ๋ค.โจ MSW ์คํํ๊ธฐ
_app.tsx์ ํ๋จ์ ์ฝ๋๋ฅผ ์คํํ์ฌ mocking์ ํ์ฑํํ๋ค.
๐ yarn start ๋ก ์คํํ๋ฉด Devtools > Console์ Mocking enabled๊ฐ ๋ํ๋๋ค.
โ ์ฌ์ฉ๋ฒ
๐ mocks/handlers/voting.ts
๐ lib/hooks/voting.ts
โค๏ธโ๐ฅโค๏ธโ๐ฅ ์ปค์คํ ํ ์ด๋
lib/api
์์์ ๋ง๋๋ ์๋ฒ ํต์ ๊ตฌ์กฐ๊ฐ ๋ค๋ฅด๋ ํ์ธ๋ถํ๋๋ ค์ โค๏ธโ๐ฅโค๏ธโ๐ฅ๐ pages/Voting.tsx
๐ผ ์ ๋ ์ธ์ ๋ ๋๊ธฐ ์ค์ด๋ ๋ชจ๋ฅด๋ ๋ถ๋ถ์ด๋ ๊ถ๊ธํ ๋ถ๋ถ์ด ์๋ค๋ฉด ์ธ์ ๋ ์ง ์ํ๋ฅผ ๋ถ๋ฌ์ฃผ์ธ์ ๐ผ
The text was updated successfully, but these errors were encountered: