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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
|
import React from 'react';
import '@testing-library/jest-dom';
import { render, screen, waitFor, fireEvent } from '@testing-library/react';
import { describe, it, expect, vi, beforeEach } from 'vitest';
import Settings from './Settings';
describe('Settings Component', () => {
beforeEach(() => {
vi.resetAllMocks();
global.fetch = vi.fn();
// Mock confirm
global.confirm = vi.fn(() => true);
});
it('renders feed list', async () => {
const mockFeeds = [
{ _id: 1, title: 'Tech News', url: 'http://tech.com/rss', category: 'tech' },
{ _id: 2, title: 'Gaming', url: 'http://gaming.com/rss', category: 'gaming' },
];
vi.mocked(global.fetch).mockResolvedValueOnce({
ok: true,
json: async () => mockFeeds,
} as Response);
render(<Settings />);
await waitFor(() => {
expect(screen.getByText('Tech News')).toBeInTheDocument();
expect(screen.getByText('http://tech.com/rss')).toBeInTheDocument();
expect(screen.getByText('Gaming')).toBeInTheDocument();
});
});
it('adds a new feed', async () => {
vi.mocked(global.fetch)
.mockResolvedValueOnce({ ok: true, json: async () => [] } as Response) // Initial load
.mockResolvedValueOnce({ ok: true, json: async () => ({}) } as Response) // Add feed
.mockResolvedValueOnce({
ok: true,
json: async () => [{ _id: 3, title: 'New Feed', url: 'http://new.com/rss' }],
} as Response); // Refresh load
render(<Settings />);
// Wait for initial load to finish
await waitFor(() => {
expect(screen.queryByText('Loading...')).not.toBeInTheDocument();
});
const input = screen.getByPlaceholderText('https://example.com/feed.xml');
const button = screen.getByText('Add Feed');
fireEvent.change(input, { target: { value: 'http://new.com/rss' } });
fireEvent.click(button);
await waitFor(() => {
expect(global.fetch).toHaveBeenCalledWith(
'/api/feed/',
expect.objectContaining({
method: 'POST',
body: JSON.stringify({ url: 'http://new.com/rss' }),
})
);
});
// Wait for refresh
await waitFor(() => {
expect(screen.getByText('New Feed')).toBeInTheDocument();
});
});
it('deletes a feed', async () => {
const mockFeeds = [
{ _id: 1, title: 'Tech News', url: 'http://tech.com/rss', category: 'tech' },
];
vi.mocked(global.fetch)
.mockResolvedValueOnce({ ok: true, json: async () => mockFeeds } as Response) // Initial load
.mockResolvedValueOnce({ ok: true } as Response); // Delete
render(<Settings />);
await waitFor(() => {
expect(screen.queryByText('Loading...')).not.toBeInTheDocument();
expect(screen.getByText('Tech News')).toBeInTheDocument();
});
const deleteBtn = screen.getByTitle('Delete Feed');
fireEvent.click(deleteBtn);
await waitFor(() => {
expect(global.confirm).toHaveBeenCalled();
expect(global.fetch).toHaveBeenCalledWith(
'/api/feed/1',
expect.objectContaining({ method: 'DELETE' })
);
expect(screen.queryByText('Tech News')).not.toBeInTheDocument();
});
});
it('imports an OPML file', async () => {
vi.mocked(global.fetch)
.mockResolvedValueOnce({ ok: true, json: async () => [] } as Response) // Initial load
.mockResolvedValueOnce({ ok: true, json: async () => ({ status: 'ok' }) } as Response) // Import
.mockResolvedValueOnce({
ok: true,
json: async () => [{ _id: 1, title: 'Imported Feed', url: 'http://imported.com/rss' }],
} as Response); // Refresh load
render(<Settings />);
const file = new File(['<opml>...</opml>'], 'feeds.opml', { type: 'text/xml' });
const fileInput = screen.getByLabelText(/import feeds/i, { selector: 'input[type="file"]' });
const importButton = screen.getByText('Import');
fireEvent.change(fileInput, { target: { files: [file] } });
await waitFor(() => {
expect(importButton).not.toBeDisabled();
});
fireEvent.click(importButton);
await waitFor(() => {
expect(global.fetch).toHaveBeenCalledWith(
'/api/import',
expect.objectContaining({
method: 'POST',
body: expect.any(FormData),
})
);
});
// Check if refresh happens
await waitFor(() => {
expect(screen.getByText('Imported Feed')).toBeInTheDocument();
});
});
it('triggers a crawl', async () => {
vi.mocked(global.fetch)
.mockResolvedValueOnce({ ok: true, json: async () => [] } as Response) // Initial load
.mockResolvedValueOnce({ ok: true, json: async () => ({ message: 'crawl started' }) } as Response); // Crawl
// Mock alert
const alertMock = vi.spyOn(window, 'alert').mockImplementation(() => { });
render(<Settings />);
// Wait for load
await waitFor(() => {
expect(screen.queryByText('Loading...')).not.toBeInTheDocument();
});
const crawlBtn = screen.getByText(/crawl all feeds now/i);
fireEvent.click(crawlBtn);
await waitFor(() => {
expect(global.fetch).toHaveBeenCalledWith(
'/api/crawl',
expect.objectContaining({ method: 'POST' })
);
expect(alertMock).toHaveBeenCalledWith('Crawl started!');
});
alertMock.mockRestore();
});
it('handles API errors', async () => {
vi.mocked(global.fetch)
.mockResolvedValueOnce({ ok: true, json: async () => [] } as Response) // Initial load load
.mockResolvedValueOnce({ ok: false, json: async () => ({}) } as Response); // Add feed error
render(<Settings />);
// Wait for load
await waitFor(() => {
expect(screen.queryByText('Loading...')).not.toBeInTheDocument();
});
const input = screen.getByPlaceholderText('https://example.com/feed.xml');
const button = screen.getByText('Add Feed');
fireEvent.change(input, { target: { value: 'http://fail.com/rss' } });
fireEvent.click(button);
await waitFor(() => {
expect(screen.getByText(/failed to add feed/i)).toBeInTheDocument();
});
});
it('handles font theme change', async () => {
const setFontTheme = vi.fn();
vi.mocked(global.fetch).mockResolvedValueOnce({ ok: true, json: async () => [] } as Response);
render(<Settings fontTheme="default" setFontTheme={setFontTheme} />);
// Wait for load
await waitFor(() => {
expect(screen.queryByText('Loading...')).not.toBeInTheDocument();
});
const select = screen.getByLabelText(/font theme/i);
fireEvent.change(select, { target: { value: 'serif' } });
expect(setFontTheme).toHaveBeenCalledWith('serif');
});
});
|