5dc53f57cc
- 新增系统监控模块(在线用户、定时任务、数据监控、服务器监控、缓存监控) - 新增部门管理占位页面 - 路由注册新增模块与懒加载 - DefaultLayout 侧边菜单与布局优化 - 新增前端单元测试与后端 RoleUpdateRequest 测试
50 lines
1.7 KiB
TypeScript
50 lines
1.7 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
|
import { renderHook } from '@testing-library/react'
|
|
|
|
vi.mock('@/stores/usePermissionStore', () => ({
|
|
usePermissionStore: vi.fn(),
|
|
}))
|
|
|
|
import { usePermissionStore } from '@/stores/usePermissionStore'
|
|
import { usePermission } from '@/hooks/usePermission'
|
|
|
|
const mockUsePermissionStore = vi.mocked(usePermissionStore)
|
|
|
|
describe('usePermission', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
})
|
|
|
|
it('should return hasPermission, hasRole, permissions, roles', () => {
|
|
const mockState = {
|
|
hasPermission: (p: string) => p === 'system:user:list',
|
|
hasRole: (r: string) => r === 'admin',
|
|
permissions: ['system:user:list', 'system:user:add'],
|
|
roles: ['admin'],
|
|
}
|
|
mockUsePermissionStore.mockImplementation((selector: any) => selector(mockState))
|
|
|
|
const { result } = renderHook(() => usePermission())
|
|
expect(result.current.hasPermission('system:user:list')).toBe(true)
|
|
expect(result.current.hasPermission('system:role:list')).toBe(false)
|
|
expect(result.current.hasRole('admin')).toBe(true)
|
|
expect(result.current.hasRole('user')).toBe(false)
|
|
expect(result.current.permissions).toEqual(['system:user:list', 'system:user:add'])
|
|
expect(result.current.roles).toEqual(['admin'])
|
|
})
|
|
|
|
it('should return empty arrays when no permissions', () => {
|
|
const mockState = {
|
|
hasPermission: () => false,
|
|
hasRole: () => false,
|
|
permissions: [],
|
|
roles: [],
|
|
}
|
|
mockUsePermissionStore.mockImplementation((selector: any) => selector(mockState))
|
|
|
|
const { result } = renderHook(() => usePermission())
|
|
expect(result.current.permissions).toEqual([])
|
|
expect(result.current.roles).toEqual([])
|
|
})
|
|
})
|