11# React 프로젝트 예제
22
3- 기존 코드 :
3+ 기존 코드에서 이어서 실습하세요 :
44< https://github.com/ahastudio/CodingLife/tree/main/20211008/react >
55
66## Parcel 대신 esbuild 사용
@@ -131,6 +131,29 @@ export default function Main() {
131131}
132132```
133133
134+ ` src/components/Main.test.sx ` 파일로 테스트 코드 이동
135+ 및 ` RecoilRoot ` 로 Recoil에 대응:
136+
137+ ``` tsx
138+ import { render } from ' @testing-library/react' ;
139+
140+ import { RecoilRoot } from ' recoil' ;
141+
142+ import Main from ' ./Main' ;
143+
144+ describe (' Main' , () => {
145+ it (' renders greeting message' , () => {
146+ const { container } = render ((
147+ <RecoilRoot >
148+ <Main />
149+ </RecoilRoot >
150+ ));
151+
152+ expect (container ).toHaveTextContent (' Hello, world!' );
153+ });
154+ });
155+ ```
156+
134157` src/App.tsx ` 수정:
135158
136159``` tsx
@@ -147,6 +170,20 @@ export default function App() {
147170}
148171```
149172
173+ ` src/App.test.tsx ` 파일 수정:
174+
175+ ``` tsx
176+ import { render } from ' @testing-library/react' ;
177+
178+ import App from ' ./App' ;
179+
180+ describe (' App' , () => {
181+ it (' renders without exploding' , () => {
182+ render (<App />);
183+ });
184+ });
185+ ```
186+
150187` src/state.ts ` 파일 작성:
151188
152189``` ts
@@ -352,6 +389,122 @@ export default function Posts() {
352389}
353390```
354391
392+ ## Custom Hook 테스트
393+
394+ [ React Hooks Testing Library] ( https://react-hooks-testing-library.com/ )
395+ 설치.
396+
397+ ``` bash
398+ npm install --save-dev @testing-library/react-hooks
399+ ```
400+
401+ 먼저, Custom Hook을 mocking해서 컴포넌트 테스트한다.
402+
403+ ` src/PostForm.test.tsx ` 파일 작성:
404+
405+ ``` tsx
406+ import { render , fireEvent } from ' @testing-library/react' ;
407+
408+ import PostForm from ' ./PostForm' ;
409+
410+ import { usePosts } from ' ../hooks' ;
411+
412+ jest .mock (' ../hooks' );
413+
414+ describe (' PostForm' , () => {
415+ const addPost = jest .fn ();
416+
417+ beforeEach (() => {
418+ jest .clearAllMocks ();
419+
420+ const hook = usePosts as jest .MockedFunction <typeof usePosts >;
421+ hook .mockReturnValue ({
422+ posts: [],
423+ addPost ,
424+ });
425+ });
426+
427+ it (' listens to the “add” button click event' , () => {
428+ const { getByText } = render (<PostForm />);
429+
430+ fireEvent .click (getByText (' Add post!' ));
431+
432+ expect (addPost ).toBeCalled ();
433+ });
434+ });
435+ ```
436+
437+ ` src/Posts.test.tsx ` 파일 작성:
438+
439+ ``` tsx
440+ import { render } from ' @testing-library/react' ;
441+
442+ import Posts from ' ./Posts' ;
443+
444+ import { usePosts } from ' ../hooks' ;
445+
446+ jest .mock (' ../hooks' );
447+
448+ describe (' Posts' , () => {
449+ const posts = [
450+ { id: 1 , title: ' Title' , body: ' Body' },
451+ ];
452+
453+ beforeEach (() => {
454+ const hook = usePosts as jest .MockedFunction <typeof usePosts >;
455+ hook .mockReturnValue ({
456+ posts ,
457+ addPost: jest .fn (),
458+ });
459+ });
460+
461+ it (' renders a list of post' , () => {
462+ const { container } = render (<Posts />);
463+
464+ expect (container ).toHaveTextContent (' Title' );
465+ expect (container ).toHaveTextContent (' Body' );
466+ });
467+ });
468+ ```
469+
470+ 이제 Custom Hook을 테스트합니다.
471+
472+ ` src/hooks.test.tsx ` 파일 작성:
473+
474+ ``` tsx
475+ import { renderHook , act } from ' @testing-library/react-hooks' ;
476+
477+ import { RecoilRoot } from ' recoil' ;
478+
479+ import { usePosts } from ' ./hooks' ;
480+
481+ describe (' usePosts' , () => {
482+ const wrapper = ({ children }: {children: any }) => (
483+ <RecoilRoot >{ children } </RecoilRoot >
484+ );
485+
486+ const render = () => renderHook (() => usePosts (), { wrapper });
487+
488+ it (' uses a list of post' , () => {
489+ const { result } = render ();
490+
491+ expect (result .current .posts ).toHaveLength (0 );
492+ });
493+
494+ describe (' addPost' , () => {
495+ it (' appends a new post' , () => {
496+ const { result } = render ();
497+
498+ act (() => {
499+ result .current .addPost ();
500+ });
501+
502+ expect (result .current .posts ).toHaveLength (1 );
503+ });
504+ });
505+ });
506+ ```
507+
355508## Axios 사용
356509
357510목표:
0 commit comments