-
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathuseFeedConversion.test.ts
More file actions
105 lines (87 loc) · 3 KB
/
useFeedConversion.test.ts
File metadata and controls
105 lines (87 loc) · 3 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import { describe, it, expect, beforeEach, afterEach, vi, type SpyInstance } from 'vitest';
import { renderHook, act } from '@testing-library/preact';
import { useFeedConversion } from '../hooks/useFeedConversion';
describe('useFeedConversion', () => {
let fetchMock: SpyInstance;
beforeEach(() => {
vi.clearAllMocks();
fetchMock = vi.spyOn(global, 'fetch');
});
afterEach(() => {
fetchMock.mockRestore();
});
it('should initialize with default state', () => {
const { result } = renderHook(() => useFeedConversion());
expect(result.current.isConverting).toBe(false);
expect(result.current.result).toBeNull();
expect(result.current.error).toBeNull();
});
it('should handle successful conversion', async () => {
const mockResult = {
id: 'test-id',
name: 'Test Feed',
url: 'https://example.com',
strategy: 'faraday',
feed_token: 'test-token',
public_url: 'https://example.com/feed.xml',
json_public_url: 'https://example.com/feed.json',
created_at: '2024-01-01T00:00:00Z',
updated_at: '2024-01-01T00:00:00Z',
};
fetchMock.mockResolvedValueOnce(
new Response(
JSON.stringify({
success: true,
data: { feed: mockResult },
}),
{
status: 201,
headers: { 'Content-Type': 'application/json' },
}
)
);
const { result } = renderHook(() => useFeedConversion());
await act(async () => {
await result.current.convertFeed('https://example.com', 'faraday', 'testtoken');
});
expect(result.current.isConverting).toBe(false);
expect(result.current.result).toEqual(mockResult);
expect(result.current.error).toBeNull();
expect(fetchMock).toHaveBeenCalledTimes(1);
});
it('should handle conversion error', async () => {
fetchMock.mockResolvedValueOnce(
new Response(
JSON.stringify({
success: false,
error: { message: 'Bad Request' },
}),
{
status: 400,
headers: { 'Content-Type': 'application/json' },
}
)
);
const { result } = renderHook(() => useFeedConversion());
await act(async () => {
await expect(result.current.convertFeed('https://example.com', 'faraday', 'testtoken')).rejects.toThrow(
'Bad Request'
);
});
expect(result.current.isConverting).toBe(false);
expect(result.current.result).toBeNull();
expect(result.current.error).toContain('Bad Request');
});
it('should handle network errors gracefully', async () => {
fetchMock.mockRejectedValueOnce(new Error('Network error'));
const { result } = renderHook(() => useFeedConversion());
await act(async () => {
await expect(result.current.convertFeed('https://example.com', 'faraday', 'testtoken')).rejects.toThrow(
'Network error'
);
});
expect(result.current.isConverting).toBe(false);
expect(result.current.result).toBeNull();
expect(result.current.error).toBe('Network error');
});
});