-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathuseFetch.test.ts
More file actions
38 lines (30 loc) · 1.22 KB
/
useFetch.test.ts
File metadata and controls
38 lines (30 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { renderHook, waitFor } from '@testing-library/react';
import useFetch from './useFetch';
describe('useFetch 테스트', () => {
let fetcher: jest.Mock;
beforeEach(() => {
fetcher = jest.fn();
});
test('fetch가 성공했을 경우, isLoading 은 false, data는 mockData, error은 null이다.', async () => {
const mockData = { key: 'value' };
fetcher.mockResolvedValueOnce(mockData);
const { result } = renderHook(() => useFetch(fetcher, true));
expect(result.current.isLoading).toBe(true);
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 } = renderHook(() => useFetch(fetcher, true));
expect(result.current.isLoading).toBe(true);
await waitFor(() => {
expect(result.current.isLoading).toBe(false);
expect(result.current.data).toBe(null);
expect(result.current.error).toEqual(mockError);
});
});
});