65 lines
4.9 KiB
JavaScript
65 lines
4.9 KiB
JavaScript
import {test,expect,vi,beforeAll,beforeEach} from 'vitest'
|
|
import {createPinia,setActivePinia,defineStore} from 'pinia'
|
|
import {reactive} from 'vue'
|
|
import {createRouter,createMemoryHistory} from 'vue-router'
|
|
const data=vi.hoisted(()=>({user:null,getRouters:vi.fn(),remove:vi.fn()}))
|
|
vi.mock('@/store/modules/user',()=>({default:()=>data.user}))
|
|
vi.mock('@/api/menu',()=>({getRouters:data.getRouters}))
|
|
vi.mock('@/plugins/auth',()=>({default:{hasPermiOr:()=>false,hasRoleOr:()=>false}}))
|
|
vi.mock('@/router',()=>({default:{addRoute:vi.fn()},dynamicRoutes:[],constantRoutes:[{path:'',children:[{path:'/index',name:'Index',meta:{title:'首页',affix:true,homePage:true}}]},{path:'/user',hidden:true,children:[{path:'profile',name:'Profile'}]}]}))
|
|
vi.mock('@/layout/index',()=>({default:{}}))
|
|
vi.mock('@/components/ParentView',()=>({default:{}}))
|
|
vi.mock('@/layout/components/InnerLink',()=>({default:{}}))
|
|
vi.mock('@/store/modules/settings',()=>({default:()=>({tagsViewPersist:true})}))
|
|
vi.mock('@/plugins/cache',()=>({default:{local:{getJSON:()=>[],setJSON:vi.fn(),remove:data.remove}}}))
|
|
let permissionStore,tagsStore
|
|
beforeAll(async()=>{vi.stubGlobal('defineStore',defineStore);permissionStore=(await import('@/store/modules/permission')).default;tagsStore=(await import('@/store/modules/tagsView')).default})
|
|
beforeEach(()=>{
|
|
setActivePinia(createPinia());data.user=reactive({roles:['archive_manager'],permissions:['archive:project:list']})
|
|
data.getRouters.mockResolvedValue({data:[{path:'/',component:'Layout',children:[{path:'index',component:'businessHome/index',meta:{title:'首页'}}]},{path:'/',component:'Layout',children:[{path:'project-archive',component:'archive/index',meta:{title:'项目资料'}}]}]})
|
|
})
|
|
test('the actual permission store excludes homepage from sidebar and topbar and selects documents as landing',async()=>{
|
|
const store=permissionStore();await store.generateRoutes()
|
|
expect(store.canHome).toBe(false);expect(store.landingPath).toBe('/project-archive')
|
|
expect(store.routes.flatMap(r=>r.children||[]).some(r=>r.meta?.homePage)).toBe(false)
|
|
expect(store.sidebarRouters.flatMap(r=>r.children||[]).some(r=>r.path==='index'||r.path==='/index')).toBe(false)
|
|
expect(store.topbarRouters.some(r=>r.path==='/index')).toBe(false)
|
|
})
|
|
test('authorized users receive exactly one homepage rather than a duplicate database route',async()=>{
|
|
data.user.permissions=['business:home:view'];const store=permissionStore();await store.generateRoutes()
|
|
expect(store.canHome).toBe(true);expect(store.landingPath).toBe('/index')
|
|
expect(store.routes.flatMap(r=>r.children||[]).filter(r=>r.path==='index'||r.path==='/index')).toHaveLength(1)
|
|
expect(store.topbarRouters.filter(r=>r.path==='/index')).toHaveLength(1)
|
|
})
|
|
test('account switching clears cached and persisted page tabs, not business records',()=>{
|
|
const store=tagsStore();store.visitedViews=[{path:'/index',meta:{affix:true}},{path:'/warehouse/stock'}];store.cachedViews=['/index','/warehouse/stock'];store.iframeViews=[{path:'/private'}]
|
|
store.resetUserViews();expect(store.visitedViews).toEqual([]);expect(store.cachedViews).toEqual([]);expect(store.iframeViews).toEqual([]);expect(data.remove).toHaveBeenCalledWith('tags-view-visited')
|
|
})
|
|
|
|
// 使用实际 Vue Router 验证注册结果:同名路由会移除先前记录,单独检查菜单数组无法发现此问题。
|
|
test('legacy testing menus do not replace production task routes with the same default name',async()=>{
|
|
data.getRouters.mockResolvedValue({data:[
|
|
{path:'/production',name:'Production',component:'Layout',children:[{path:'tasks',name:'Tasks',component:'production/tasks/index'}]},
|
|
{path:'/testing',name:'Testing',component:'Layout',children:[{path:'tasks',name:'Tasks',component:'testing/tasks/index'}]}
|
|
]})
|
|
const store=permissionStore(),routes=await store.generateRoutes()
|
|
for(const ordered of [routes,[...routes].reverse()]){
|
|
const runtime=createRouter({history:createMemoryHistory(),routes:[]})
|
|
ordered.forEach(route=>runtime.addRoute(route))
|
|
expect(runtime.resolve('/production/tasks').matched.at(-1)?.name).toBe('Tasks')
|
|
expect(runtime.resolve('/testing/tasks').matched.at(-1)?.name).toBe('TestingTasks')
|
|
}
|
|
expect(store.sidebarRouters.find(route=>route.path==='/testing').children[0].name).toBe('TestingTasks')
|
|
expect(store.topbarRouters.find(route=>route.path==='/testing').children[0].name).toBe('TestingTasks')
|
|
})
|
|
|
|
// 兼容旧安装只修正默认生成的名称,显式维护的路由名称和后端未授权的菜单不应被替换或补入。
|
|
test('testing route compatibility preserves explicit names and does not inject unauthorized menus',async()=>{
|
|
data.getRouters.mockResolvedValue({data:[{path:'/testing',name:'Testing',component:'Layout',children:[
|
|
{path:'tasks',name:'CustomRobotTasks',component:'testing/tasks/index'}
|
|
]}]})
|
|
const store=permissionStore(),routes=await store.generateRoutes()
|
|
expect(routes[0].children[0].name).toBe('CustomRobotTasks')
|
|
expect(routes.some(route=>route.path==='/production')).toBe(false)
|
|
})
|