62 lines
2.7 KiB
TypeScript
62 lines
2.7 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
|
import { createPinia, setActivePinia } from 'pinia'
|
|
import { WORKSPACE_TAB_LIMIT, readWorkspaceTabsSnapshot, useWorkspaceTabsStore } from './workspaceTabs'
|
|
|
|
describe('workspace tabs store', () => {
|
|
beforeEach(() => {
|
|
sessionStorage.clear()
|
|
setActivePinia(createPinia())
|
|
vi.restoreAllMocks()
|
|
})
|
|
|
|
it('keeps one tab per route name and restores it only for the same account', () => {
|
|
vi.spyOn(Date, 'now').mockReturnValueOnce(10).mockReturnValueOnce(20)
|
|
const store = useWorkspaceTabsStore()
|
|
store.initialize('user-a')
|
|
store.open({ id: 'customers', title: '客户管理', path: '/customers' })
|
|
store.open({ id: 'customers', title: '客户详情', path: '/customers/10001' })
|
|
|
|
expect(store.tabs).toEqual([{ id: 'customers', title: '客户详情', path: '/customers/10001', lastVisitedAt: 20 }])
|
|
expect(readWorkspaceTabsSnapshot('user-a').activeId).toBe('customers')
|
|
expect(readWorkspaceTabsSnapshot('user-b').tabs).toEqual([])
|
|
})
|
|
|
|
it('evicts the least recently used clean inactive tab at the soft limit', () => {
|
|
let now = 0
|
|
vi.spyOn(Date, 'now').mockImplementation(() => ++now)
|
|
const store = useWorkspaceTabsStore()
|
|
store.initialize('user-a')
|
|
for (let index = 0; index < WORKSPACE_TAB_LIMIT; index += 1) {
|
|
store.open({ id: `route-${index}`, title: `页面 ${index}`, path: `/route-${index}` })
|
|
}
|
|
|
|
store.open({ id: 'route-new', title: '新页面', path: '/route-new' }, new Set(['route-0']))
|
|
|
|
expect(store.tabs).toHaveLength(WORKSPACE_TAB_LIMIT)
|
|
expect(store.tabs.some((tab) => tab.id === 'route-0')).toBe(true)
|
|
expect(store.tabs.some((tab) => tab.id === 'route-1')).toBe(false)
|
|
expect(store.activeId).toBe('route-new')
|
|
})
|
|
|
|
it('falls back to the most recently visited remaining tab and rotates the closed cache key', () => {
|
|
vi.spyOn(Date, 'now').mockReturnValueOnce(10).mockReturnValueOnce(20).mockReturnValueOnce(30)
|
|
const store = useWorkspaceTabsStore()
|
|
store.initialize('user-a')
|
|
store.open({ id: 'customers', title: '客户', path: '/customers' })
|
|
store.open({ id: 'warehouses', title: '仓库', path: '/warehouses' })
|
|
store.open({ id: 'pallets', title: '托盘', path: '/pallets' })
|
|
const previousKey = store.cacheKey('pallets')
|
|
|
|
const fallback = store.removeByIds(['pallets'])
|
|
|
|
expect(fallback?.id).toBe('warehouses')
|
|
expect(store.activeId).toBe('warehouses')
|
|
expect(store.cacheKey('pallets')).not.toBe(previousKey)
|
|
})
|
|
|
|
it('drops corrupt persisted data instead of breaking application startup', () => {
|
|
sessionStorage.setItem('cc-admin-workspace-tabs:v1:user-a', '{broken')
|
|
expect(readWorkspaceTabsSnapshot('user-a')).toEqual({ tabs: [], activeId: null })
|
|
})
|
|
})
|