Skip to content

Commit

Permalink
feat: useFetch, useMutation react-hook 제거
Browse files Browse the repository at this point in the history
  • Loading branch information
ukkodeveloper committed Sep 1, 2023
1 parent 3a82107 commit 3031a54
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 26 deletions.
26 changes: 13 additions & 13 deletions frontend/src/shared/hooks/useFetch.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { renderHook } from '@testing-library/react-hooks';
import { renderHook, waitFor } from '@testing-library/react';
import useFetch from './useFetch';

describe('useFetch 테스트', () => {
Expand All @@ -11,28 +11,28 @@ describe('useFetch 테스트', () => {
test('fetch가 성공했을 경우, isLoading 은 false, data는 mockData, error은 null이다.', async () => {
const mockData = { key: 'value' };
fetcher.mockResolvedValueOnce(mockData);
const { result, waitForNextUpdate } = renderHook(() => useFetch(fetcher, true));
const { result } = renderHook(() => useFetch(fetcher, true));

expect(result.current.isLoading).toBe(true);

await waitForNextUpdate();

expect(result.current.isLoading).toBe(false);
expect(result.current.data).toEqual(mockData);
expect(result.current.error).toBe(null);
waitFor(() => {
expect(result.current.isLoading).toBe(false);
expect(result.current.data).toEqual(mockData);
expect(result.current.error).toBe(null);
});
});

test('fetch가 실패했을 경우, isLoading 은 false, datas는 null, error은 mockError다.', async () => {
const mockError = { message: 'An error occurred' };
fetcher.mockRejectedValueOnce(mockError);
const { result, waitForNextUpdate } = renderHook(() => useFetch(fetcher, true));
const { result } = renderHook(() => useFetch(fetcher, true));

expect(result.current.isLoading).toBe(true);

await waitForNextUpdate();

expect(result.current.isLoading).toBe(false);
expect(result.current.data).toBe(null);
expect(result.current.error).toEqual(mockError);
await waitFor(() => {
expect(result.current.isLoading).toBe(false);
expect(result.current.data).toBe(null);
expect(result.current.error).toEqual(mockError);
});
});
});
27 changes: 14 additions & 13 deletions frontend/src/shared/hooks/useMutation.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { renderHook, act } from '@testing-library/react-hooks';
import { renderHook, act, waitFor } from '@testing-library/react';
import { useMutation } from './useMutation';

describe('useMutation 테스트', () => {
Expand All @@ -11,35 +11,36 @@ describe('useMutation 테스트', () => {
test('fetch가 성공했을 경우, isLoading 은 false, data는 mockData, error은 null이다.', async () => {
const mockData = { key: 'value' };
mutateFn.mockResolvedValueOnce(mockData);
const { result, waitForNextUpdate } = renderHook(() => useMutation(mutateFn));
const { result } = renderHook(() => useMutation(mutateFn));

act(() => {
result.current.mutateData();
});
expect(result.current.isLoading).toBe(true);

await waitForNextUpdate();
expect(result.current.isLoading).toBe(true);

expect(result.current.isLoading).toBe(false);
expect(result.current.data).toEqual(mockData);
expect(result.current.error).toBe(null);
await waitFor(() => {
expect(result.current.isLoading).toBe(false);
expect(result.current.data).toEqual(mockData);
expect(result.current.error).toBe(null);
});
});

test('fetch가 실패했을 경우, isLoading 은 false, datas는 null, error은 mockError다.', async () => {
const mockError = { message: 'An error occurred' };
mutateFn.mockRejectedValueOnce(mockError);
const { result, waitForNextUpdate } = renderHook(() => useMutation(mutateFn));
const { result } = renderHook(() => useMutation(mutateFn));

act(() => {
result.current.mutateData();
});

expect(result.current.isLoading).toBe(true);

await waitForNextUpdate();

expect(result.current.isLoading).toBe(false);
expect(result.current.data).toBe(null);
expect(result.current.error).toEqual(mockError);
await waitFor(() => {
expect(result.current.isLoading).toBe(false);
expect(result.current.data).toBe(null);
expect(result.current.error).toEqual(mockError);
});
});
});

0 comments on commit 3031a54

Please sign in to comment.