-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseApi.test.ts
More file actions
31 lines (24 loc) · 998 Bytes
/
useApi.test.ts
File metadata and controls
31 lines (24 loc) · 998 Bytes
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
import useApi, {ApiState, ApiStatus} from "./useApi";
import {renderHook} from '@testing-library/react-hooks';
jest.useFakeTimers();
const testApiState = (state: ApiState, status: ApiStatus, error: any) => {
expect(state.error).toBe(error);
expect(state.status).toBe(status);
expect(state.isInitial).toBe(status === ApiStatus.initial);
expect(state.isLoading).toBe(status === ApiStatus.loading);
expect(state.isSuccess).toBe(status === ApiStatus.success);
expect(state.isCanceled).toBe(status === ApiStatus.canceled);
expect(state.isFailed).toBe(status === ApiStatus.initial);
};
describe('useApi', () => {
it('initial values', () => {
const api = jest.fn() as () => Promise<number | null>;
const { result } = renderHook(() => {
return useApi(api, null);
});
expect(api).not.toBeCalled();
const [value, , state] = result.current;
expect(value).toBe(null);
testApiState(state, ApiStatus.initial, null);
});
});