gongxiangp2/components/department-dashboard/DepartmentShell.performance-salary.test.tsx

55 lines
2.8 KiB
TypeScript

import { act, fireEvent, render, screen, waitFor } from '@testing-library/react'
import DepartmentShell from './DepartmentShell'
jest.mock('./DepartmentAccountMenu', () => ({ __esModule: true, default: () => null }))
beforeEach(() => {
window.history.replaceState(null, '', '/workspaces/organization/team-functions')
window.matchMedia = jest.fn().mockReturnValue({ matches: false, addEventListener: jest.fn(), removeEventListener: jest.fn() })
})
function mockCapabilities(result: unknown, ok = true) {
global.fetch = jest.fn().mockImplementation((url: string) => Promise.resolve({
ok,
json: async () => url === '/api/workspaces/capabilities' ? result : { canUse: false },
}))
}
async function renderShell() {
await act(async () => { render(<DepartmentShell><p>内容</p></DepartmentShell>) })
}
test.each([false, undefined, 'true', 1])('薪资能力不是明确 true 时隐藏入口:%s', async canViewPerformanceSalary => {
mockCapabilities({ canViewPerformanceSalary })
await renderShell()
await screen.findByRole('link', { name: /团队职能/ })
expect(screen.queryByRole('link', { name: /绩效薪资|绩效考核/ })).not.toBeInTheDocument()
})
test('授权账号显示改名后的入口和页面定位', async () => {
window.history.replaceState(null, '', '/workspaces/organization/performance-review')
mockCapabilities({ canViewPerformanceSalary: true })
await renderShell()
expect(await screen.findByRole('link', { name: '绩效薪资' })).toHaveAttribute('aria-current', 'page')
expect(screen.getByRole('link', { name: '绩效薪资' })).toHaveAttribute('href', '/workspaces/organization/performance-review')
expect(screen.queryByText('绩效考核')).not.toBeInTheDocument()
})
test.each(['http-error', 'offline'])('权限请求失败时仍隐藏:%s', async failure => {
if (failure === 'offline') global.fetch = jest.fn().mockRejectedValue(new Error('offline'))
else mockCapabilities({ canViewPerformanceSalary: true }, false)
await renderShell()
await screen.findByRole('link', { name: /团队职能/ })
await waitFor(() => expect(global.fetch).toHaveBeenCalledWith('/api/workspaces/capabilities', expect.objectContaining({ cache: 'no-store' })))
expect(screen.queryByRole('link', { name: '绩效薪资' })).not.toBeInTheDocument()
})
test('手机抽屉同样使用服务端能力隐藏入口', async () => {
window.matchMedia = jest.fn().mockReturnValue({ matches: true, addEventListener: jest.fn(), removeEventListener: jest.fn() })
mockCapabilities({ canViewPerformanceSalary: false })
await renderShell()
fireEvent.click(screen.getByRole('button', { name: '切换部门导航' }))
expect(screen.getByRole('dialog', { name: '品牌中心移动导航' })).toBeInTheDocument()
expect(screen.queryByRole('link', { name: '绩效薪资' })).not.toBeInTheDocument()
})