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

[ ๐ŸŒˆ Notice ] MSW ์‚ฌ์šฉ๋ฒ• #1

Closed
Happhee opened this issue Jan 1, 2023 · 0 comments
Closed

[ ๐ŸŒˆ Notice ] MSW ์‚ฌ์šฉ๋ฒ• #1

Happhee opened this issue Jan 1, 2023 · 0 comments
Assignees

Comments

@Happhee
Copy link
Contributor

Happhee commented Jan 1, 2023

โœจ Mocks Service Worker

MSW๋Š” Mock Service Worker๋กœ ์„œ๋น„์Šค ์›Œ์ปค๋ฅผ ์ด์šฉํ•˜์—ฌ API๋ฅผ ๋ชจํ‚นํ•˜๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ด๋‹ค.
๋„คํŠธ์›Œํฌ ์š”์ฒญ์„ ๊ฐ€๋กœ์ฑ„๋„๋ก ์„ค๊ณ„๋œ Service Worker API๋ฅผ ํ™œ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ชฉ ์‚ฌ์šฉ ์—ฌ๋ถ€ ๊ด€๊ณ„ ์—†์ด ๋™์ผํ•œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋™์ž‘์„ ๋ณด์žฅํ•œ๋‹ค.
๋˜ํ•œ, ๋ชจํ‚น์„ ์œ„ํ•ด ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ฝ”๋“œ๋ฅผ ๋ณ€๊ฒฝํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค๋Š” ๊ฒƒ์ด ๊ฐ€์žฅ ํฐ ์žฅ์ ์ด๋‹คโ—๏ธ

image
TypeScript + MSW

์„œ๋ฒ„ API๊ฐ€ ๋งŒ๋“ค์–ด์ง€๊ธฐ ์ „์— ํ…Œ์ŠคํŠธ๋ฅผ ์œ„ํ•œ API ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค โœจ

โœ… ํด๋” ๋ฐ ํŒŒ์ผ ์„ค๋ช…

image

image

  • types ๐Ÿ‘‰ ํ”Œ๋ ˆ์ด์–ด, ๋ฉ”์ด์ปค ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•ด ํ•„์š”ํ•œ ํƒ€์ž…๋“ค์„ ์ •์˜ํ•ด๋†“์€ ํด๋” , mocks์—์„œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค!
  • mocks ๐Ÿ‘‰ ๋ฐ์ดํ„ฐ, API ํ†ต์‹  ํ•ธ๋“ค๋Ÿฌ , browers์— ์ œ๊ณต, server ํ•ธ๋“ค๋Ÿฌ ๋ฅผ ๋ชจ์•„๋‘” ํด๋”

๐Ÿ“ mocks/data

  • mocking ํ•  ๋ฐ์ดํ„ฐ ๋งŒ๋“ค์–ด ๋†“์€ ํด๋” ์ž…๋‹ˆ๋‹ค.

๐Ÿ“ mocks/handlers

  • ๊ฐ€์งœ API๋ฅผ ๊ตฌํ˜„ํ•˜๋ ค๋ฉด ์š”์ฒญ์ด ๋“ค์–ด์™”์„ ๋•Œ ์ž„์˜์˜ ์‘๋‹ต์„ ํ•ด์ฃผ๋Š” ํ•ธ๋“ค๋Ÿฌ(handler) ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•œ ํด๋”์ž…๋‹ˆ๋‹ค

๐Ÿ“„ mocks/browsers.ts (๋ธŒ๋ผ์šฐ์ € ํ™˜๊ฒฝ) or server.ts (๋…ธ๋“œ ํ™˜๊ฒฝ)

  • setupWorker() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์„œ๋น„์Šค ์›Œ์ปค๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.
  • handlers์—์„œ ์ž‘์„ฑํ•œ ์š”์ฒญ ํ•ธ๋“ค๋Ÿฌ ์ฝ”๋“œ๋ฅผ ๋ถˆ๋Ÿฌ์™€์„œ ๊ทธ๋Œ€๋กœ setupWorker() ํ•จ์ˆ˜์˜ ์ธ์ž๋กœ ๋„˜๊ฒจ์ค€๋‹ค.

โœจ MSW ์‹คํ–‰ํ•˜๊ธฐ

_app.tsx์— ํ•˜๋‹จ์˜ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜์—ฌ mocking์„ ํ™œ์„ฑํ™”ํ•œ๋‹ค.

if (process.env.NODE_ENV === 'development') {
  worker.start();
}

๐Ÿ‘‡ yarn start ๋กœ ์‹คํ–‰ํ•˜๋ฉด Devtools > Console์— Mocking enabled๊ฐ€ ๋‚˜ํƒ€๋‚œ๋‹ค.
image


โœ… ์‚ฌ์šฉ๋ฒ•

๐Ÿ‘‡ 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;

๐ŸŒผ ์ €๋Š” ์–ธ์ œ๋‚˜ ๋Œ€๊ธฐ ์ค‘์ด๋‹ˆ ๋ชจ๋ฅด๋Š” ๋ถ€๋ถ„์ด๋‚˜ ๊ถ๊ธˆํ•œ ๋ถ€๋ถ„์ด ์žˆ๋‹ค๋ฉด ์–ธ์ œ๋“ ์ง€ ์„œํžˆ๋ฅผ ๋ถˆ๋Ÿฌ์ฃผ์„ธ์š” ๐ŸŒผ

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

1 participant