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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
|
// @vitest-environment jsdom
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { fetchFeeds, fetchItems, renderFeeds, renderItems, toggleStar, toggleRead } from './app.js';
// Mock fetch
const fetchMock = vi.fn();
global.fetch = fetchMock;
describe('Vanilla JS App', () => {
beforeEach(() => {
document.body.innerHTML = `
<div id="app">
<aside id="sidebar">
<nav id="feeds-nav">
<div class="search-container">
<input type="text" id="search-input" />
</div>
</nav>
</aside>
<main id="main">
<header id="main-header">
<h2 id="feed-title">All Items</h2>
</header>
<div id="entries-list"></div>
</main>
</div>
`;
fetchMock.mockReset();
});
describe('fetchFeeds', () => {
it('should fetch feeds and render them', async () => {
const mockFeeds = [{ id: 1, title: 'Test Feed', url: 'http://example.com' }];
fetchMock.mockResolvedValue({
ok: true,
json: async () => mockFeeds,
});
await fetchFeeds();
expect(fetchMock).toHaveBeenCalledWith('/api/feed/');
const feedItems = document.querySelectorAll('.feed-item');
// "All Items", "Unread Items", "Starred Items", plus 1 feed = 4 items
expect(feedItems.length).toBe(4);
expect(feedItems[3].textContent).toBe('Test Feed');
});
it('should handle errors gracefully', async () => {
fetchMock.mockRejectedValue(new Error('Network error'));
await expect(fetchFeeds()).rejects.toThrow('Network error');
expect(document.getElementById('feeds-nav').innerHTML).toContain('Error loading feeds');
});
});
describe('fetchItems', () => {
it('should fetch items and render them', async () => {
const mockItems = [{
id: 101,
title: 'Item 1',
url: 'http://example.com/1',
feed: { title: 'Feed A' },
starred: false,
read: false
}];
fetchMock.mockResolvedValue({
ok: true,
json: async () => mockItems,
});
await fetchItems();
expect(fetchMock).toHaveBeenCalledWith('/api/stream/');
const entries = document.querySelectorAll('.entry');
expect(entries.length).toBe(1);
expect(entries[0].querySelector('.entry-title').textContent).toBe('Item 1');
});
it('should handle filters', async () => {
fetchMock.mockResolvedValue({ ok: true, json: async () => [] });
await fetchItems(123, 'unread', 'query');
const expectedUrl = '/api/stream/?feed_id=123&read_filter=unread&q=query';
expect(fetchMock).toHaveBeenCalledWith(expect.stringContaining('feed_id=123'));
expect(fetchMock).toHaveBeenCalledWith(expect.stringContaining('read_filter=unread'));
expect(fetchMock).toHaveBeenCalledWith(expect.stringContaining('q=query'));
});
});
describe('renderFeeds', () => {
it('should render system feeds and user feeds', async () => {
const feeds = [
{ id: 1, title: 'Feed 1', url: 'u1' },
{ id: 2, title: 'Feed 2', url: 'u2' }
];
// Mock fetch for the click handler
fetchMock.mockResolvedValue({ ok: true, json: async () => [] });
renderFeeds(feeds);
const items = document.querySelectorAll('.feed-item');
expect(items.length).toBe(5); // All, Unread, Starred, Feed 1, Feed 2
// Click handler test: All Items
items[0].click();
expect(items[0].classList.contains('active')).toBe(true);
expect(document.getElementById('feed-title').textContent).toBe('All Items');
expect(fetchMock).toHaveBeenCalledWith('/api/stream/');
// Click handler test: Unread Items
items[1].click();
expect(items[1].classList.contains('active')).toBe(true);
expect(document.getElementById('feed-title').textContent).toBe('Unread Items');
expect(fetchMock).toHaveBeenCalledWith(expect.stringContaining('read_filter=unread'));
// Click handler test: Starred Items
items[2].click();
expect(items[2].classList.contains('active')).toBe(true);
expect(document.getElementById('feed-title').textContent).toBe('Starred Items');
expect(fetchMock).toHaveBeenCalledWith(expect.stringContaining('starred=true'));
// Click handler test: Specific Feed
items[3].click();
expect(items[3].classList.contains('active')).toBe(true);
expect(document.getElementById('feed-title').textContent).toBe('Feed 1');
expect(fetchMock).toHaveBeenCalledWith(expect.stringContaining('feed_id=1'));
// Wait for async operations to complete to avoid unhandled rejections
await new Promise(resolve => setTimeout(resolve, 0));
});
});
describe('renderItems', () => {
it('should render "No items found" if empty', () => {
renderItems([]);
expect(document.getElementById('entries-list').innerHTML).toContain('No items found');
});
it('should render items with correct controls', () => {
const items = [{
id: 1,
title: 'Test',
url: 'http://test.com',
starred: true,
read: false,
feed: { title: 'Feed' },
created_at: new Date().toISOString()
}];
renderItems(items);
const starBtn = document.querySelector('.btn-star');
expect(starBtn.textContent).toBe('★');
expect(starBtn.classList.contains('active')).toBe(true);
const readBtn = document.querySelector('.btn-read');
expect(readBtn.textContent).toBe('Mark Read');
expect(readBtn.classList.contains('unread')).toBe(true);
});
});
describe('Interaction Toggles', () => {
let btn;
beforeEach(() => {
btn = document.createElement('button');
document.body.appendChild(btn);
});
afterEach(() => {
if (btn) btn.remove();
});
it('should toggle star status', async () => {
fetchMock.mockResolvedValue({ ok: true });
const newStatus = await toggleStar(1, false, btn);
expect(fetchMock).toHaveBeenCalledWith(expect.stringContaining('/api/item/1'), expect.objectContaining({
method: 'PUT',
body: JSON.stringify({ id: 1, starred: true })
}));
expect(newStatus).toBe(true);
expect(btn.textContent).toBe('★');
});
it('should toggle read status', async () => {
fetchMock.mockResolvedValue({ ok: true });
// Setup DOM for title dimming
const header = document.createElement('div');
header.className = 'entry-header';
const title = document.createElement('a');
title.className = 'entry-title';
header.appendChild(btn); // btn inside header
header.appendChild(title);
document.body.appendChild(header);
const newStatus = await toggleRead(1, false, btn);
expect(fetchMock).toHaveBeenCalledWith(expect.stringContaining('/api/item/1'), expect.objectContaining({
method: 'PUT',
body: JSON.stringify({ id: 1, read: true })
}));
expect(newStatus).toBe(true);
expect(title.classList.contains('read')).toBe(true);
});
});
describe('Error Handling', () => {
it('fetchItems should handle existing list element error', async () => {
fetchMock.mockRejectedValue(new Error('Fetch failed'));
await expect(fetchItems()).rejects.toThrow('Fetch failed');
expect(document.getElementById('entries-list').innerHTML).toContain('Error loading items');
});
});
describe('init', () => {
it('should initialize app if elements exist', async () => {
// Mock fetch for the init calls
fetchMock.mockResolvedValue({ ok: true, json: async () => [] });
const addEventListenerSpy = vi.spyOn(document.getElementById('search-input'), 'addEventListener');
// init is already imported
const { init } = await import('./app.js');
// Reset mocks
fetchMock.mockClear();
init();
expect(fetchMock).toHaveBeenCalledTimes(2); // fetchFeeds + fetchItems
expect(addEventListenerSpy).toHaveBeenCalledWith('keypress', expect.any(Function));
// Wait for async operations to complete
await new Promise(resolve => setTimeout(resolve, 0));
});
it('should do nothing if feeds-nav missing', async () => {
document.body.innerHTML = ''; // Clear DOM
fetchMock.mockClear();
const { init } = await import('./app.js');
init();
expect(fetchMock).not.toHaveBeenCalled();
});
});
describe('Search Interaction', () => {
it('should trigger search on Enter', async () => {
// Mock fetch for the init calls & search
fetchMock.mockResolvedValue({ ok: true, json: async () => [] });
// Re-setup DOM and Init
const { init } = await import('./app.js');
init();
fetchMock.mockClear();
const searchInput = document.getElementById('search-input');
searchInput.value = 'test query';
// Create Enter keypress event
const event = new KeyboardEvent('keypress', { key: 'Enter' });
searchInput.dispatchEvent(event);
expect(fetchMock).toHaveBeenCalledWith(expect.stringContaining('q=test+query'));
expect(document.getElementById('feed-title').textContent).toBe('Search: test query');
// Wait for async operations to complete
await new Promise(resolve => setTimeout(resolve, 0));
});
it('should ignore empty search', async () => {
// Mock fetch for the init calls
fetchMock.mockResolvedValue({ ok: true, json: async () => [] });
const { init } = await import('./app.js');
init();
fetchMock.mockClear();
const searchInput = document.getElementById('search-input');
searchInput.value = ' ';
const event = new KeyboardEvent('keypress', { key: 'Enter' });
searchInput.dispatchEvent(event);
expect(fetchMock).not.toHaveBeenCalled();
// Wait for async operations to complete
await new Promise(resolve => setTimeout(resolve, 0));
});
});
});
|