You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

28 lines
1.8 KiB

import { shallowMount } from '@vue/test-utils'
import AlumniNews from '@/views/dashboard/components/AlumniNews'
import { alumniNews } from '@/api/home/index'
jest.mock('@/api/home/index', () => ({ alumniNews: jest.fn() }))
const flush = async() => { await Promise.resolve(); await Promise.resolve() }
let wrapper
afterEach(() => { if (wrapper) wrapper.destroy(); jest.clearAllMocks() })
it('shows single-line linked titles and requests the next page', async() => {
alumniNews.mockResolvedValueOnce({ data: [{ id: 1, title: '企业新闻', source_url: 'https://example.com/article' }], total: 9, current_page: 1, last_page: 2 })
wrapper = shallowMount(AlumniNews); await flush()
expect(wrapper.find('a').attributes()).toMatchObject({ href: 'https://example.com/article', target: '_blank', rel: 'noopener noreferrer', title: '企业新闻' })
expect(wrapper.findAll('button').at(0).attributes('disabled')).toBeDefined()
alumniNews.mockResolvedValueOnce({ data: [{ id: 2, title: '下一页新闻', source_url: 'javascript:alert(1)' }], total: 9, current_page: 2, last_page: 2 })
await wrapper.findAll('button').at(1).trigger('click'); await flush()
expect(alumniNews).toHaveBeenLastCalledWith({ page: 2 })
expect(wrapper.find('a').exists()).toBe(false)
expect(wrapper.text()).toContain('下一页新闻')
expect(wrapper.findAll('button').at(1).attributes('disabled')).toBeDefined()
})
it('shows a retry action for failures and an empty state after retry', async() => {
alumniNews.mockRejectedValueOnce(new Error('network'))
wrapper = shallowMount(AlumniNews); await flush()
expect(wrapper.text()).toContain('加载失败')
alumniNews.mockResolvedValueOnce({ data: [], total: 0, current_page: 1, last_page: 1 })
await wrapper.find('.news-retry').trigger('click'); await flush()
expect(wrapper.text()).toContain('本年度暂无已留存新闻')
})