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

120 lines
6.9 KiB
TypeScript

import { getServerSession } from 'next-auth'
import { redirect } from 'next/navigation'
import { prisma } from '@/lib/prisma'
import WorkspaceItemPage, { generateMetadata } from '@/app/workspaces/[section]/[item]/page'
import PerformanceSalaryDashboard from './PerformanceSalaryDashboard'
jest.mock('next-auth', () => ({ getServerSession: jest.fn() }))
jest.mock('next/navigation', () => ({ redirect: jest.fn(), notFound: jest.fn() }))
jest.mock('@/lib/auth', () => ({ authOptions: {} }))
jest.mock('@/lib/prisma', () => ({ prisma: { user: { findUnique: jest.fn() } } }))
jest.mock('./WorkspacePlaceholder', () => ({ __esModule: true, default: () => null }))
jest.mock('./DepartmentShell', () => ({ __esModule: true, default: () => null }))
jest.mock('./AutomationWorkflowsDashboard', () => ({ __esModule: true, default: () => null }))
jest.mock('./PermissionsDashboard', () => ({ __esModule: true, default: () => null }))
jest.mock('./BrandHealthDashboard', () => ({ __esModule: true, default: () => null }))
jest.mock('./BusinessTrendsDashboard', () => ({ __esModule: true, default: () => null }))
jest.mock('./TeamCapacityDashboard', () => ({ __esModule: true, default: () => null }))
jest.mock('./CoreMetricsDashboard', () => ({ __esModule: true, default: () => null }))
jest.mock('./OrganizationStructureDashboard', () => ({ __esModule: true, default: () => null }))
jest.mock('./TeamFunctionsDashboard', () => ({ __esModule: true, default: () => null }))
jest.mock('./ValueMetricsDashboard', () => ({ __esModule: true, default: () => null }))
jest.mock('./BrandKnowledgeDashboard', () => ({ __esModule: true, default: () => null }))
jest.mock('./PerformanceConversionDashboard', () => ({ __esModule: true, default: () => null }))
const getSession = getServerSession as jest.Mock
const findUser = prisma.user.findUnique as jest.Mock
const props = { params: { section: 'organization', item: 'performance-review' } }
beforeEach(() => {
jest.resetAllMocks()
;(redirect as unknown as jest.Mock).mockImplementation((path: string) => { throw new Error(`redirect:${path}`) })
})
test('未登录直接访问先去登录', async () => {
getSession.mockResolvedValue(null)
await expect(WorkspaceItemPage(props)).rejects.toThrow('redirect:/login')
expect(findUser).not.toHaveBeenCalled()
})
test('品牌中心范围外直接访问被拦截', async () => {
getSession.mockResolvedValue({ user: { id: 'outside', canAccessBrandCenter: false } })
await expect(WorkspaceItemPage(props)).rejects.toThrow('redirect:/assets/material-library')
})
test.each(['USER', 'DEPT_ADMIN', 'ADMIN', 'SYSTEM_ADMIN'])('普通 %s 直接访问仍被拦截,旧超管会话不能绕过', async role => {
getSession.mockResolvedValue({ user: { id: 'ordinary', role, isSuperAdmin: true, canAccessBrandCenter: true } })
findUser.mockResolvedValue({ id: 'ordinary', isSuperAdmin: false, isActive: true, status: 'active', archivedAt: null })
await expect(WorkspaceItemPage(props)).rejects.toThrow('redirect:/workspaces/dashboard/core-metrics')
})
test.each([
{ id: '463c5a60-c8a8-45dd-b4cd-4a31a6b1b84f', isSuperAdmin: false },
{ id: 'super-admin', isSuperAdmin: true },
])('服务端允许已授权账号且页面/metadata 名称一致:%j', async identity => {
getSession.mockResolvedValue({ user: { id: identity.id, canAccessBrandCenter: true } })
findUser.mockResolvedValue({ ...identity, isActive: true, status: 'active', archivedAt: null })
const page = await WorkspaceItemPage(props)
expect(page.type).toBe(PerformanceSalaryDashboard)
expect(page.props.view).toBe('details')
expect(page.props.role).toBe('ai-planning')
expect(generateMetadata(props).title).toBe('绩效薪资 | 组织管理 | 品牌中心部门看板')
})
test.each([
['configuration', 'configuration'],
['unknown', 'details'],
])('授权访问按 URL 选择视图:%s', async (view, expectedView) => {
getSession.mockResolvedValue({ user: { id: 'super-admin', canAccessBrandCenter: true } })
findUser.mockResolvedValue({ id: 'super-admin', isSuperAdmin: true, isActive: true, status: 'active', archivedAt: null })
const page = await WorkspaceItemPage({ ...props, searchParams: { view } })
expect(page.type).toBe(PerformanceSalaryDashboard)
expect(page.props.view).toBe(expectedView)
})
test('后台配置链接同样经过薪资权限门禁', async () => {
getSession.mockResolvedValue({ user: { id: 'ordinary', canAccessBrandCenter: true } })
findUser.mockResolvedValue({ id: 'ordinary', isSuperAdmin: false, isActive: true, status: 'active', archivedAt: null })
await expect(WorkspaceItemPage({ ...props, searchParams: { view: 'configuration' } })).rejects.toThrow('redirect:/workspaces/dashboard/core-metrics')
})
test.each(['ai-planning', 'photography', 'spatial-visual', 'management'])('岗位直达链接仍拦截旧超管会话:%s', async role => {
getSession.mockResolvedValue({ user: { id: 'ordinary', isSuperAdmin: true, canAccessBrandCenter: true } })
findUser.mockResolvedValue({ id: 'ordinary', isSuperAdmin: false, isActive: true, status: 'active', archivedAt: null })
await expect(WorkspaceItemPage({ ...props, searchParams: { role } })).rejects.toThrow('redirect:/workspaces/dashboard/core-metrics')
})
test.each([
['ai-planning', 'ai-planning'],
['photography', 'photography'],
['spatial-visual', 'spatial-visual'],
['management', 'management'],
['unknown', 'ai-planning'],
[['management', 'photography'], 'ai-planning'],
])('授权访问解析岗位参数:%j', async (role, expectedRole) => {
getSession.mockResolvedValue({ user: { id: 'super-admin', canAccessBrandCenter: true } })
findUser.mockResolvedValue({ id: 'super-admin', isSuperAdmin: true, isActive: true, status: 'active', archivedAt: null })
const page = await WorkspaceItemPage({ ...props, searchParams: { role } })
expect(page.props.role).toBe(expectedRole)
})
test('岗位参数不覆盖后台配置视图', async () => {
getSession.mockResolvedValue({ user: { id: 'super-admin', canAccessBrandCenter: true } })
findUser.mockResolvedValue({ id: 'super-admin', isSuperAdmin: true, isActive: true, status: 'active', archivedAt: null })
const page = await WorkspaceItemPage({ ...props, searchParams: { view: 'configuration', role: 'management' } })
expect(page.props.view).toBe('configuration')
})
test.each(['2027-01', '2026-09'])('已授权岗位直达链接解析薪资月份:%s', async month => {
getSession.mockResolvedValue({ user: { id: 'super-admin', canAccessBrandCenter: true } })
findUser.mockResolvedValue({ id: 'super-admin', isSuperAdmin: true, isActive: true, status: 'active', archivedAt: null })
const page = await WorkspaceItemPage({ ...props, searchParams: { role: 'management', month } })
expect(page.props.month).toBe(month)
})
test('其他组织管理板块不受薪资专属权限限制', async () => {
await WorkspaceItemPage({ params: { section: 'organization', item: 'team-functions' } })
expect(getSession).not.toHaveBeenCalled()
expect(findUser).not.toHaveBeenCalled()
})