gongxiangp2/components/Header.test.tsx

88 lines
2.4 KiB
TypeScript

import { fireEvent, render, screen } from '@testing-library/react'
import Header from '@/components/Header'
jest.mock('next-auth/react', () => ({
signOut: jest.fn(),
}))
jest.mock('next/navigation', () => ({
useRouter: () => ({
push: jest.fn(),
refresh: jest.fn(),
}),
}))
jest.mock('@/lib/network', () => ({
initializeNetwork: jest.fn().mockResolvedValue({ isInternal: true }),
setupNetworkChangeDetection: jest.fn(),
addNetworkChangeListener: jest.fn(() => jest.fn()),
}))
jest.mock('@/components/UpdateLogModal', () => ({
__esModule: true,
default: () => null,
showUpdateLog: jest.fn(),
useUpdateLog: () => ({
showModal: false,
currentLog: null,
showAllHistory: false,
handleClose: jest.fn(),
}),
}))
jest.mock('@/components/MaintenanceBanner', () => ({
__esModule: true,
default: () => null,
}))
jest.mock('@/components/HeaderTaobaoSearch', () => ({
__esModule: true,
default: () => null,
}))
describe('Header 用户菜单', () => {
beforeEach(() => {
jest.clearAllMocks()
global.fetch = jest.fn().mockResolvedValue({
ok: true,
json: async () => ({
headerBgColor: '#1f2937',
headerLogoUrl: '/api/favicon',
siteName: '由壹点3D素材',
}),
}) as jest.Mock
})
test('保留常用入口并移除网络诊断和清除缓存', () => {
render(
<Header
session={
{
user: {
id: 'user-1',
name: '测试用户',
email: 'user@example.com',
role: 'USER',
},
} as any
}
/>,
)
const menuButton = screen.getByRole('button', { name: '打开用户菜单' })
expect(menuButton).toHaveAttribute('aria-expanded', 'false')
fireEvent.click(menuButton)
expect(menuButton).toHaveAttribute('aria-expanded', 'true')
expect(screen.getByRole('link', { name: '个人中心' })).toBeInTheDocument()
expect(screen.getByRole('link', { name: '考核调研' })).toBeInTheDocument()
expect(screen.getByRole('link', { name: '作品过稿看板' })).toBeInTheDocument()
expect(screen.getByRole('button', { name: '更新记录' })).toBeInTheDocument()
expect(screen.getByRole('button', { name: '退出登录' })).toBeInTheDocument()
expect(screen.queryByText('网络诊断')).not.toBeInTheDocument()
expect(screen.queryByText('清除缓存并刷新')).not.toBeInTheDocument()
})
})