import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest' import { mount, flushPromises } from '@vue/test-utils' import axios from 'axios' import AdminFeeds from '../AdminFeeds.vue' import AddUrl from '../modal/AddUrl.vue' vi.mock('axios') describe('AdminFeeds', () => { beforeEach(() => { localStorage.setItem('user-token', 'test-token') localStorage.setItem('user-id', '7') vi.clearAllMocks() }) // Each mount renders the AddUrl modal via , which // survives outside the wrapper's own element — unmount every mounted // instance so a leftover doesn't bleed into a later test's document.body. let mountedWrappers = [] function mountAdmin() { const wrapper = mount(AdminFeeds) mountedWrappers.push(wrapper) return wrapper } afterEach(() => { for (const wrapper of mountedWrappers) { try { wrapper.unmount() } catch { // already unmounted by the test itself — fine } } mountedWrappers = [] }) it('fetches and renders the feed list on mount', async () => { axios.get.mockResolvedValueOnce({ data: { feeds: [{ id: 1, title: 'Feed A', url: 'https://example.test/a.xml' }] }, }) const wrapper = mountAdmin() await flushPromises() expect(axios.get).toHaveBeenCalledWith('/api/v1/article/feeds/7', expect.anything()) expect(wrapper.text()).toContain('Feed A') }) it('shows the empty state when there are no feeds', async () => { axios.get.mockResolvedValueOnce({ data: { feeds: [] } }) const wrapper = mountAdmin() await flushPromises() expect(wrapper.text()).toContain('No feeds added yet.') }) it('shows an error message when the feed list fails to load', async () => { axios.get.mockRejectedValueOnce(new Error('Network Error')) const wrapper = mountAdmin() await flushPromises() expect(wrapper.text()).toContain('Failed to load feeds.') }) it('deletes a feed after confirmation', async () => { axios.get.mockResolvedValueOnce({ data: { feeds: [{ id: 1, title: 'Feed A', url: 'https://example.test/a.xml' }] }, }) axios.delete.mockResolvedValueOnce({ status: 204 }) const confirmSpy = vi.spyOn(window, 'confirm').mockReturnValue(true) const wrapper = mountAdmin() await flushPromises() await wrapper.find('.admin__delete').trigger('click') await flushPromises() expect(axios.delete).toHaveBeenCalledWith('/api/v1/article/feed/1', expect.anything()) expect(wrapper.text()).toContain('No feeds added yet.') confirmSpy.mockRestore() }) it('opens the add-feed modal from the Add RSS button', async () => { axios.get.mockResolvedValueOnce({ data: { feeds: [] } }) const wrapper = mountAdmin() await flushPromises() await wrapper.find('.admin__add').trigger('click') expect(wrapper.findComponent(AddUrl).props('show')).toBe(true) }) it('reloads the feed list when the modal reports a feed was added', async () => { axios.get .mockResolvedValueOnce({ data: { feeds: [] } }) .mockResolvedValueOnce({ data: { feeds: [{ id: 2, title: 'Feed B', url: 'https://example.test/b.xml' }] } }) const wrapper = mountAdmin() await flushPromises() expect(wrapper.text()).toContain('No feeds added yet.') await wrapper.findComponent(AddUrl).vm.$emit('added') await flushPromises() expect(axios.get).toHaveBeenCalledTimes(2) expect(wrapper.text()).toContain('Feed B') }) })