gongxiangp2/components/department-dashboard/DepartmentShell.detail-generator.test.tsx

38 lines
2.1 KiB
TypeScript

import { fireEvent, render, screen, waitFor } from '@testing-library/react'
import DepartmentShell from './DepartmentShell'
jest.mock('./DepartmentAccountMenu', () => ({ __esModule: true, default: () => null }))
jest.mock('./DepartmentHeader', () => {
const React = require('react')
return { __esModule: true, default: React.forwardRef(function Header(_props: unknown, ref: unknown) { return <button ref={ref as React.Ref<HTMLButtonElement>}>导航</button> }) }
})
beforeEach(() => {
window.history.replaceState(null, '', '/workspaces/dashboard/core-metrics')
window.matchMedia = jest.fn().mockReturnValue({ matches: false, addEventListener: jest.fn(), removeEventListener: jest.fn() })
})
test.each([false, undefined])('服务端能力为 %s 时不显示新功能入口', async canUse => {
global.fetch = jest.fn().mockResolvedValue({ ok: true, json: async () => ({ canUse }) })
render(<DepartmentShell><p>内容</p></DepartmentShell>)
await waitFor(() => expect(global.fetch).toHaveBeenCalled())
fireEvent.click(screen.getByRole('button', { name: '资产与AI自动化' }))
expect(screen.queryByRole('link', { name: /详情页生成/ })).not.toBeInTheDocument()
})
test('只有数据库能力响应明确为 true 才显示详情页生成入口', async () => {
global.fetch = jest.fn().mockResolvedValue({ ok: true, json: async () => ({ canUse: true }) })
render(<DepartmentShell><p>内容</p></DepartmentShell>)
fireEvent.click(screen.getByRole('button', { name: '资产与AI自动化' }))
expect(await screen.findByRole('link', { name: /详情页生成/ })).toHaveAttribute('href', '/assets/detail-generator')
expect(global.fetch).toHaveBeenCalledWith('/api/detail-generator/capabilities', expect.objectContaining({ cache: 'no-store' }))
})
test('权限请求失败保持入口隐藏', async () => {
global.fetch = jest.fn().mockRejectedValue(new Error('offline'))
render(<DepartmentShell><p>内容</p></DepartmentShell>)
await waitFor(() => expect(global.fetch).toHaveBeenCalled())
fireEvent.click(screen.getByRole('button', { name: '资产与AI自动化' }))
expect(screen.queryByRole('link', { name: /详情页生成/ })).not.toBeInTheDocument()
})