-
Notifications
You must be signed in to change notification settings - Fork 272
Expand file tree
/
Copy pathindex.test.tsx
More file actions
65 lines (47 loc) · 2.19 KB
/
index.test.tsx
File metadata and controls
65 lines (47 loc) · 2.19 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { renderHook } from '#app/components/react-testing-library-with-providers';
import { waitFor } from '@testing-library/react';
import uasApiRequest from '#app/lib/uasApi';
import { buildGlobalId, ACTIVITY_TYPE } from '#app/lib/uasApi/uasUtility';
import useFetchSaveStatus from './index';
jest.mock('#app/lib/uasApi');
jest.mock('#app/lib/uasApi/uasUtility');
const mockUasApiRequest = uasApiRequest as jest.Mock;
const mockBuildGlobalId = buildGlobalId as jest.Mock;
describe('useFetchSaveStatus', () => {
const defaultArticleId = '123';
afterEach(() => {
jest.clearAllMocks();
});
test('returns isSaved = true when API returns 200', async () => {
mockBuildGlobalId.mockReturnValue('global-123');
mockUasApiRequest.mockResolvedValue({ ok: true, status: 200 });
const { result } = renderHook(() => useFetchSaveStatus(defaultArticleId));
await waitFor(() => expect(result.current.loading).toBe(false));
expect(result.current.isSaved).toBe(true);
expect(result.current.error).toBeNull();
expect(mockUasApiRequest).toHaveBeenCalledWith('GET', ACTIVITY_TYPE, {
globalId: 'global-123',
});
});
test('returns isSaved = false when API returns 204', async () => {
mockBuildGlobalId.mockReturnValue('global-123');
mockUasApiRequest.mockResolvedValue({ ok: true, status: 204 });
const { result } = renderHook(() => useFetchSaveStatus(defaultArticleId));
await waitFor(() => expect(result.current.loading).toBe(false));
expect(result.current.isSaved).toBe(false);
expect(result.current.error).toBeNull();
});
test('returns error and isSaved = false when API fails', async () => {
mockBuildGlobalId.mockReturnValue('global-123');
const apiError = new Error('API failed');
mockUasApiRequest.mockRejectedValue(apiError);
const { result } = renderHook(() => useFetchSaveStatus(defaultArticleId));
await waitFor(() => expect(result.current.loading).toBe(false));
expect(result.current.isSaved).toBe(false);
expect(result.current.error).toBe(apiError);
});
test('does not call API when articleId is empty', () => {
renderHook(() => useFetchSaveStatus(''));
expect(mockUasApiRequest).not.toHaveBeenCalled();
});
});