Replace the feed select with a multi-feed filter modal
The header's feed <select> is now a funnel button that opens a modal with an "All feeds" checkbox and one checkbox per feed with unread items. The selection is either "All feeds" or at least one feed, is applied once when the modal closes, and is persisted in localStorage. - feedFilter holds string[] | null; setFeedFilter is a no-op when the selection is unchanged (order-insensitive), sparing callers the scroll/mark-read side effects. - Selected feeds stay listed at 0 unread so they can be unchecked; titles whose feed no longer exists are pruned after each fetch. - The persisted filter is cleared on logout and on 401 (handler extracted to httpAuthInterceptor.js so it is unit-testable). - The modal locks body scroll while open so scrolling behind it can't mark articles read.
This commit is contained in:
1 parent
8f7ddb77c4
commit
5ed780baa3
10 files changed
+1217
-114
No files matched your search
@@ -1,6 +1,6 @@
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
|
||||
import axios from 'axios'
|
||||
import { useFeeds } from '../useFeeds'
|
||||
import { useFeeds, logout, clearFeedFilter } from '../useFeeds'
|
||||
|
||||
vi.mock('axios')
|
||||
|
||||
@@ -17,6 +17,7 @@ describe('useFeeds', () => {
|
||||
beforeEach(() => {
|
||||
localStorage.setItem('user-token', 'test-token')
|
||||
localStorage.setItem('user-id', '7')
|
||||
localStorage.removeItem('feedFilter')
|
||||
vi.clearAllMocks()
|
||||
|
||||
feeds.value = []
|
||||
@@ -118,9 +119,9 @@ describe('useFeeds', () => {
|
||||
setInitialLoad(false)
|
||||
})
|
||||
|
||||
it('refreshes the filter dropdown\'s displayed per-feed counts when list-view scrolling marks an article read', async () => {
|
||||
it('refreshes the filter modal\'s displayed per-feed counts when list-view scrolling marks an article read', async () => {
|
||||
// displayedFeedUnreadCounts is a frozen snapshot of feedUnreadCounts (see
|
||||
// useFeeds.js) — AppNav's filter <select> renders it instead of the live
|
||||
// useFeeds.js) — AppNav's filter modal renders it instead of the live
|
||||
// computed so paging through article view doesn't force it to repaint.
|
||||
// List-view scrolling is a different code path (handleIntersection) and
|
||||
// should keep it live regardless.
|
||||
@@ -658,6 +659,31 @@ describe('useFeeds', () => {
|
||||
},
|
||||
}
|
||||
|
||||
const threeFeedsResponse = {
|
||||
data: {
|
||||
feeds: [
|
||||
{
|
||||
title: 'Feed A',
|
||||
items: [
|
||||
{ id: 1, title: 'A1', content: '', url: 'https://example.test/a1', timestamp: '2026-01-01 10:00:00' },
|
||||
],
|
||||
},
|
||||
{
|
||||
title: 'Feed B',
|
||||
items: [
|
||||
{ id: 2, title: 'B1', content: '', url: 'https://example.test/b1', timestamp: '2026-01-02 10:00:00' },
|
||||
],
|
||||
},
|
||||
{
|
||||
title: 'Feed C',
|
||||
items: [
|
||||
{ id: 3, title: 'C1', content: '', url: 'https://example.test/c1', timestamp: '2026-01-03 10:00:00' },
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
}
|
||||
|
||||
it('narrows the displayed list to a single feed and restores it on "All feeds"', async () => {
|
||||
axios.get.mockResolvedValueOnce(twoFeedsResponse)
|
||||
await fetchData()
|
||||
@@ -667,7 +693,7 @@ describe('useFeeds', () => {
|
||||
// Unfiltered: everything, newest first (Jan 3, Jan 2, Jan 1).
|
||||
expect(feeds.value.map(f => f.id)).toEqual([3, 2, 1])
|
||||
|
||||
await setFeedFilter('Feed A')
|
||||
await setFeedFilter(['Feed A'])
|
||||
expect(feeds.value.map(f => f.id)).toEqual([3, 1])
|
||||
|
||||
await setFeedFilter(null)
|
||||
@@ -679,7 +705,7 @@ describe('useFeeds', () => {
|
||||
axios.put.mockResolvedValue({ status: 200 })
|
||||
await fetchData()
|
||||
|
||||
await setFeedFilter('Feed A')
|
||||
await setFeedFilter(['Feed A'])
|
||||
setInitialLoad(true)
|
||||
|
||||
// The first Feed A article scrolls above the viewport → marked read and
|
||||
@@ -701,7 +727,7 @@ describe('useFeeds', () => {
|
||||
axios.put.mockResolvedValue({ status: 200 })
|
||||
await fetchData()
|
||||
|
||||
await setFeedFilter('Feed B')
|
||||
await setFeedFilter(['Feed B'])
|
||||
expect(feeds.value.map(f => f.id)).toEqual([2])
|
||||
|
||||
// Article view marks the displayed article read in place (feed.read =
|
||||
@@ -712,8 +738,8 @@ describe('useFeeds', () => {
|
||||
|
||||
// Switching to another feed and back must not resurrect it as if it
|
||||
// were still unread.
|
||||
await setFeedFilter('Feed A')
|
||||
await setFeedFilter('Feed B')
|
||||
await setFeedFilter(['Feed A'])
|
||||
await setFeedFilter(['Feed B'])
|
||||
|
||||
expect(feeds.value).toEqual([])
|
||||
})
|
||||
@@ -727,14 +753,14 @@ describe('useFeeds', () => {
|
||||
// Switching into Feed B while in article view immediately displays its
|
||||
// one article's full content — that counts as "viewed", so it's marked
|
||||
// read the same way toggleViewMode()/nextArticle()/prevArticle() do.
|
||||
await setFeedFilter('Feed B')
|
||||
await setFeedFilter(['Feed B'])
|
||||
|
||||
expect(axios.put).toHaveBeenCalledWith('/api/v1/article/read/2', null, expect.anything())
|
||||
expect(feeds.value[0].read).toBe(true)
|
||||
|
||||
// It doesn't resurface either, once dropped on the next switch.
|
||||
await setFeedFilter('Feed A')
|
||||
await setFeedFilter('Feed B')
|
||||
await setFeedFilter(['Feed A'])
|
||||
await setFeedFilter(['Feed B'])
|
||||
expect(feeds.value).toEqual([])
|
||||
})
|
||||
|
||||
@@ -744,7 +770,7 @@ describe('useFeeds', () => {
|
||||
|
||||
// List view only shows titles — switching filters there must not mark
|
||||
// anything read; that's still driven solely by scroll (handleIntersection).
|
||||
await setFeedFilter('Feed B')
|
||||
await setFeedFilter(['Feed B'])
|
||||
|
||||
expect(axios.put).not.toHaveBeenCalled()
|
||||
expect(feeds.value[0].read).toBeUndefined()
|
||||
@@ -755,7 +781,7 @@ describe('useFeeds', () => {
|
||||
axios.put.mockResolvedValue({ status: 200 })
|
||||
await fetchData()
|
||||
|
||||
await setFeedFilter('Feed B')
|
||||
await setFeedFilter(['Feed B'])
|
||||
expect(feeds.value.map(f => f.id)).toEqual([2])
|
||||
|
||||
// Mark all (visible = just Feed B) read — Feed B has no unread items left.
|
||||
@@ -763,10 +789,10 @@ describe('useFeeds', () => {
|
||||
await markAllRead()
|
||||
|
||||
// The list empties, but the filter stays put and the feed remains a
|
||||
// selectable option (so the <select> never dangles) even though it no
|
||||
// selectable option (so a checked feed never dangles) even though it no
|
||||
// longer has unread items.
|
||||
expect(feeds.value).toEqual([])
|
||||
expect(feedFilter.value).toBe('Feed B')
|
||||
expect(feedFilter.value).toEqual(['Feed B'])
|
||||
expect(feedTitles.value).toEqual(['Feed A', 'Feed B'])
|
||||
|
||||
// Switching away drops the now-empty feed from the options.
|
||||
@@ -783,9 +809,9 @@ describe('useFeeds', () => {
|
||||
expect(unreadCount.value).toBe(3)
|
||||
|
||||
// Filtered: only the selected feed's unread items.
|
||||
await setFeedFilter('Feed B')
|
||||
await setFeedFilter(['Feed B'])
|
||||
expect(unreadCount.value).toBe(1)
|
||||
await setFeedFilter('Feed A')
|
||||
await setFeedFilter(['Feed A'])
|
||||
expect(unreadCount.value).toBe(2)
|
||||
|
||||
await setFeedFilter(null)
|
||||
@@ -803,6 +829,325 @@ describe('useFeeds', () => {
|
||||
allItems.value.find(i => i.id === 3).read = true
|
||||
expect(feedUnreadCounts.value).toEqual({ 'Feed A': 1, 'Feed B': 1 })
|
||||
})
|
||||
|
||||
it('filters the displayed list and unread count to the union of several selected feeds', async () => {
|
||||
axios.get.mockResolvedValueOnce(threeFeedsResponse)
|
||||
await fetchData()
|
||||
|
||||
await setFeedFilter(['Feed A', 'Feed C'])
|
||||
|
||||
// Newest first, Feed B (id 2) excluded.
|
||||
expect(feeds.value.map(f => f.id)).toEqual([3, 1])
|
||||
expect(unreadCount.value).toBe(2)
|
||||
})
|
||||
|
||||
it('keeps every selected feed listed (even at 0 unread) once all of them are emptied', async () => {
|
||||
axios.get.mockResolvedValueOnce(threeFeedsResponse)
|
||||
axios.put.mockResolvedValue({ status: 200 })
|
||||
await fetchData()
|
||||
|
||||
await setFeedFilter(['Feed A', 'Feed C'])
|
||||
vi.spyOn(window, 'confirm').mockReturnValue(true)
|
||||
await markAllRead()
|
||||
|
||||
expect(feeds.value).toEqual([])
|
||||
expect(feedFilter.value).toEqual(['Feed A', 'Feed C'])
|
||||
// Feed B is still present because it still has an unread item; A and C
|
||||
// stay present purely because they're selected — see feedTitles.
|
||||
expect(feedTitles.value).toEqual(['Feed A', 'Feed B', 'Feed C'])
|
||||
})
|
||||
|
||||
it('limits mark-all-read to the selected feeds, leaving the rest untouched', async () => {
|
||||
axios.get.mockResolvedValueOnce(threeFeedsResponse)
|
||||
axios.put.mockResolvedValue({ status: 200 })
|
||||
await fetchData()
|
||||
|
||||
await setFeedFilter(['Feed A', 'Feed C'])
|
||||
vi.spyOn(window, 'confirm').mockReturnValue(true)
|
||||
await markAllRead()
|
||||
|
||||
expect(axios.put).toHaveBeenCalledWith('/api/v1/article/read/1', null, expect.anything())
|
||||
expect(axios.put).toHaveBeenCalledWith('/api/v1/article/read/3', null, expect.anything())
|
||||
expect(axios.put).not.toHaveBeenCalledWith('/api/v1/article/read/2', null, expect.anything())
|
||||
// Feed B's unread item survives in the master untouched.
|
||||
expect(allItems.value.map(f => f.id)).toEqual([2])
|
||||
})
|
||||
|
||||
it('collapses an empty array selection to "All feeds" rather than an empty filter', async () => {
|
||||
axios.get.mockResolvedValueOnce(twoFeedsResponse)
|
||||
await fetchData()
|
||||
|
||||
await setFeedFilter(['Feed A'])
|
||||
await setFeedFilter([])
|
||||
|
||||
expect(feedFilter.value).toBeNull()
|
||||
expect(feeds.value.map(f => f.id)).toEqual([3, 2, 1])
|
||||
})
|
||||
})
|
||||
|
||||
describe('feed filter persistence', () => {
|
||||
afterEach(() => {
|
||||
localStorage.removeItem('feedFilter')
|
||||
})
|
||||
|
||||
it('persists a feed selection to localStorage, and removes the key for "All feeds"', async () => {
|
||||
await setFeedFilter(['Feed A', 'Feed B'])
|
||||
expect(localStorage.getItem('feedFilter')).toBe(JSON.stringify(['Feed A', 'Feed B']))
|
||||
|
||||
await setFeedFilter(null)
|
||||
expect(localStorage.getItem('feedFilter')).toBeNull()
|
||||
})
|
||||
|
||||
it('clearFeedFilter resets the ref and removes the persisted key', async () => {
|
||||
await setFeedFilter(['Feed A'])
|
||||
|
||||
clearFeedFilter()
|
||||
|
||||
expect(feedFilter.value).toBeNull()
|
||||
expect(localStorage.getItem('feedFilter')).toBeNull()
|
||||
})
|
||||
|
||||
it('logout clears the persisted feed filter along with the session', async () => {
|
||||
axios.post.mockResolvedValueOnce({ status: 200 })
|
||||
await setFeedFilter(['Feed A'])
|
||||
|
||||
await logout()
|
||||
|
||||
expect(feedFilter.value).toBeNull()
|
||||
expect(localStorage.getItem('feedFilter')).toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
describe('pruneFeedFilter (via fetchData)', () => {
|
||||
// pruneFeedFilter's own axios.get call is routed by URL, alongside
|
||||
// fetchData's — vi.clearAllMocks() (see the outer beforeEach) clears call
|
||||
// history but not a mockImplementation, so each test here resets it
|
||||
// explicitly rather than leaking a stale router into later tests.
|
||||
afterEach(() => {
|
||||
axios.get.mockReset()
|
||||
})
|
||||
|
||||
function mockArticleAndFeedsGets({ articleFeeds, filterFeeds, filterFeedsRejects }) {
|
||||
axios.get.mockImplementation((url) => {
|
||||
if (url.startsWith('/api/v1/article/get/')) {
|
||||
return Promise.resolve({ data: { feeds: articleFeeds } })
|
||||
}
|
||||
if (url.startsWith('/api/v1/article/feeds/')) {
|
||||
if (filterFeedsRejects) return Promise.reject(filterFeedsRejects)
|
||||
return Promise.resolve({ data: { feeds: filterFeeds } })
|
||||
}
|
||||
return Promise.reject(new Error('unexpected axios.get url: ' + url))
|
||||
})
|
||||
}
|
||||
|
||||
async function flushMicrotasks(times = 10) {
|
||||
for (let i = 0; i < times; i++) await Promise.resolve()
|
||||
}
|
||||
|
||||
it('does not request the feeds endpoint when no filter is active', async () => {
|
||||
feedFilter.value = null
|
||||
mockArticleAndFeedsGets({ articleFeeds: [] })
|
||||
|
||||
await fetchData()
|
||||
|
||||
expect(axios.get).toHaveBeenCalledTimes(1)
|
||||
expect(axios.get).toHaveBeenCalledWith('/api/v1/article/get/7', expect.anything())
|
||||
})
|
||||
|
||||
it('drops a selected title whose feed no longer exists, keeping the one that does', async () => {
|
||||
feedFilter.value = ['Feed A', 'Gone']
|
||||
mockArticleAndFeedsGets({
|
||||
articleFeeds: [
|
||||
{ title: 'Feed A', items: [{ id: 1, title: 'A1', content: '', url: 'https://example.test/a1', timestamp: '2026-01-01 10:00:00' }] },
|
||||
{ title: 'Feed B', items: [{ id: 2, title: 'B1', content: '', url: 'https://example.test/b1', timestamp: '2026-01-02 10:00:00' }] },
|
||||
],
|
||||
filterFeeds: [{ title: 'Feed A' }],
|
||||
})
|
||||
|
||||
await fetchData()
|
||||
|
||||
expect(axios.get).toHaveBeenCalledWith('/api/v1/article/feeds/7', expect.anything())
|
||||
expect(feedFilter.value).toEqual(['Feed A'])
|
||||
expect(localStorage.getItem('feedFilter')).toBe(JSON.stringify(['Feed A']))
|
||||
expect(feeds.value.map(f => f.id)).toEqual([1])
|
||||
})
|
||||
|
||||
it('collapses to "All feeds" when every selected title is gone', async () => {
|
||||
feedFilter.value = ['Gone']
|
||||
mockArticleAndFeedsGets({
|
||||
articleFeeds: [
|
||||
{ title: 'Feed A', items: [{ id: 1, title: 'A1', content: '', url: 'https://example.test/a1', timestamp: '2026-01-01 10:00:00' }] },
|
||||
],
|
||||
filterFeeds: [{ title: 'Feed A' }],
|
||||
})
|
||||
|
||||
await fetchData()
|
||||
|
||||
expect(feedFilter.value).toBeNull()
|
||||
expect(localStorage.getItem('feedFilter')).toBeNull()
|
||||
expect(feeds.value.map(f => f.id)).toEqual([1])
|
||||
})
|
||||
|
||||
it('keeps a selected feed with 0 unread items selected, without re-persisting', async () => {
|
||||
feedFilter.value = ['Feed A']
|
||||
const setItemSpy = vi.spyOn(Storage.prototype, 'setItem')
|
||||
mockArticleAndFeedsGets({
|
||||
articleFeeds: [], // Feed A has no unread items right now
|
||||
filterFeeds: [{ title: 'Feed A' }], // but the feed itself still exists
|
||||
})
|
||||
|
||||
await fetchData()
|
||||
|
||||
expect(feedFilter.value).toEqual(['Feed A'])
|
||||
expect(setItemSpy).not.toHaveBeenCalledWith('feedFilter', expect.anything())
|
||||
|
||||
setItemSpy.mockRestore()
|
||||
})
|
||||
|
||||
it('leaves the filter untouched and surfaces no user-facing error when the feeds lookup fails', async () => {
|
||||
feedFilter.value = ['Feed A']
|
||||
localStorage.setItem('feedFilter', JSON.stringify(['Feed A']))
|
||||
const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {})
|
||||
mockArticleAndFeedsGets({
|
||||
articleFeeds: [
|
||||
{ title: 'Feed A', items: [{ id: 1, title: 'A1', content: '', url: 'https://example.test/a1', timestamp: '2026-01-01 10:00:00' }] },
|
||||
],
|
||||
filterFeedsRejects: new Error('network'),
|
||||
})
|
||||
|
||||
await fetchData()
|
||||
|
||||
expect(feedFilter.value).toEqual(['Feed A'])
|
||||
expect(localStorage.getItem('feedFilter')).toBe(JSON.stringify(['Feed A']))
|
||||
expect(showMessage.value).toBe(false)
|
||||
expect(message.value).toBe('')
|
||||
expect(consoleErrorSpy).toHaveBeenCalled()
|
||||
|
||||
consoleErrorSpy.mockRestore()
|
||||
})
|
||||
|
||||
it('does not request the feeds endpoint when the article fetch itself fails', async () => {
|
||||
feedFilter.value = ['Feed A']
|
||||
axios.get.mockImplementation((url) => {
|
||||
if (url.startsWith('/api/v1/article/get/')) return Promise.reject(new Error('network'))
|
||||
return Promise.reject(new Error('unexpected axios.get url: ' + url))
|
||||
})
|
||||
|
||||
await fetchData()
|
||||
|
||||
expect(axios.get).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
|
||||
it('does not resurrect the filter if it was cleared while the feeds lookup was still in flight', async () => {
|
||||
feedFilter.value = ['Feed A']
|
||||
const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {})
|
||||
let resolveFeeds
|
||||
axios.get.mockImplementation((url) => {
|
||||
if (url.startsWith('/api/v1/article/get/')) {
|
||||
return Promise.resolve({
|
||||
data: { feeds: [{ title: 'Feed A', items: [{ id: 1, title: 'A1', content: '', url: 'https://example.test/a1', timestamp: '2026-01-01 10:00:00' }] }] },
|
||||
})
|
||||
}
|
||||
if (url.startsWith('/api/v1/article/feeds/')) {
|
||||
return new Promise(resolve => { resolveFeeds = resolve })
|
||||
}
|
||||
return Promise.reject(new Error('unexpected axios.get url: ' + url))
|
||||
})
|
||||
|
||||
const fetchPromise = fetchData()
|
||||
await flushMicrotasks() // let fetchData's own GET resolve and pruneFeedFilter dispatch its GET
|
||||
|
||||
clearFeedFilter()
|
||||
resolveFeeds({ data: { feeds: [{ title: 'Feed A' }] } })
|
||||
await fetchPromise
|
||||
|
||||
expect(feedFilter.value).toBeNull()
|
||||
expect(consoleErrorSpy).not.toHaveBeenCalled()
|
||||
|
||||
consoleErrorSpy.mockRestore()
|
||||
})
|
||||
|
||||
it('keeps a filter selected mid-request rather than reverting it to the prune outcome', async () => {
|
||||
feedFilter.value = ['Feed A']
|
||||
let resolveFeeds
|
||||
axios.get.mockImplementation((url) => {
|
||||
if (url.startsWith('/api/v1/article/get/')) {
|
||||
return Promise.resolve({
|
||||
data: {
|
||||
feeds: [
|
||||
{ title: 'Feed A', items: [{ id: 1, title: 'A1', content: '', url: 'https://example.test/a1', timestamp: '2026-01-01 10:00:00' }] },
|
||||
{ title: 'Feed B', items: [{ id: 2, title: 'B1', content: '', url: 'https://example.test/b1', timestamp: '2026-01-02 10:00:00' }] },
|
||||
],
|
||||
},
|
||||
})
|
||||
}
|
||||
if (url.startsWith('/api/v1/article/feeds/')) {
|
||||
return new Promise(resolve => { resolveFeeds = resolve })
|
||||
}
|
||||
return Promise.reject(new Error('unexpected axios.get url: ' + url))
|
||||
})
|
||||
|
||||
const fetchPromise = fetchData()
|
||||
await flushMicrotasks()
|
||||
|
||||
await setFeedFilter(['Feed B'])
|
||||
// The (stale, already-in-flight) feeds list still includes Feed B, so the
|
||||
// switch survives the prune that resolves afterwards.
|
||||
resolveFeeds({ data: { feeds: [{ title: 'Feed A' }, { title: 'Feed B' }] } })
|
||||
await fetchPromise
|
||||
|
||||
expect(feedFilter.value).toEqual(['Feed B'])
|
||||
})
|
||||
|
||||
it('keeps isSyncing true until the feed-filter prune settles, not just the initial fetch', async () => {
|
||||
feedFilter.value = ['Feed A']
|
||||
let resolveFeeds
|
||||
axios.get.mockImplementation((url) => {
|
||||
if (url.startsWith('/api/v1/article/get/')) {
|
||||
return Promise.resolve({ data: { feeds: [{ title: 'Feed A', items: [] }] } })
|
||||
}
|
||||
if (url.startsWith('/api/v1/article/feeds/')) {
|
||||
return new Promise(resolve => { resolveFeeds = resolve })
|
||||
}
|
||||
return Promise.reject(new Error('unexpected axios.get url: ' + url))
|
||||
})
|
||||
|
||||
const fetchPromise = fetchData()
|
||||
await flushMicrotasks()
|
||||
|
||||
expect(isSyncing.value).toBe(true)
|
||||
|
||||
resolveFeeds({ data: { feeds: [{ title: 'Feed A' }] } })
|
||||
await fetchPromise
|
||||
|
||||
expect(isSyncing.value).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe('setFeedFilter no-op guard', () => {
|
||||
it('no-ops on an empty array while already unfiltered', async () => {
|
||||
feedFilter.value = null
|
||||
const scrollToSpy = vi.spyOn(window, 'scrollTo')
|
||||
|
||||
await setFeedFilter([])
|
||||
|
||||
expect(scrollToSpy).not.toHaveBeenCalled()
|
||||
expect(feedFilter.value).toBeNull()
|
||||
|
||||
scrollToSpy.mockRestore()
|
||||
})
|
||||
|
||||
it('does not mark anything read on a no-op call while in article view', async () => {
|
||||
feedFilter.value = ['Feed A']
|
||||
viewMode.value = 'article'
|
||||
feeds.value = [{ id: 1, feedTitle: 'Feed A', title: 'a' }]
|
||||
|
||||
await setFeedFilter(['Feed A'])
|
||||
|
||||
expect(axios.put).not.toHaveBeenCalled()
|
||||
|
||||
viewMode.value = 'list'
|
||||
})
|
||||
})
|
||||
|
||||
it('strips leftover embedded-video placeholder headings', async () => {
|
||||
|
||||
Reference in new issue
Block a user