-
Notifications
You must be signed in to change notification settings - Fork 273
Expand file tree
/
Copy pathindex.test.tsx
More file actions
73 lines (55 loc) · 2.31 KB
/
index.test.tsx
File metadata and controls
73 lines (55 loc) · 2.31 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
66
67
68
69
70
71
72
73
import { renderHook } from '#app/components/react-testing-library-with-providers';
import { waitFor } from '@testing-library/react';
import uasApiRequest from '#app/lib/uasApi';
import { buildGlobalId, FAVOURITES_CONFIG } from '#app/lib/uasApi/uasUtility';
import useUASFetchSaveStatus 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('useUASFetchSaveStatus', () => {
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(() =>
useUASFetchSaveStatus(defaultArticleId),
);
await waitFor(() => expect(result.current.isLoading).toBe(false));
expect(result.current.isSaved).toBe(true);
expect(result.current.error).toBeNull();
expect(mockUasApiRequest).toHaveBeenCalledWith(
'GET',
FAVOURITES_CONFIG.activityType,
expect.objectContaining({ 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(() =>
useUASFetchSaveStatus(defaultArticleId),
);
await waitFor(() => expect(result.current.isLoading).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(() =>
useUASFetchSaveStatus(defaultArticleId),
);
await waitFor(() => expect(result.current.isLoading).toBe(false));
expect(result.current.isSaved).toBe(false);
expect(result.current.error).toBe(apiError);
});
test('does not call API when articleId is empty', () => {
renderHook(() => useUASFetchSaveStatus(''));
expect(mockUasApiRequest).not.toHaveBeenCalled();
});
});