unis-oa/frontend/src/utils/returnPath.spec.ts

86 lines
3.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

// @vitest-environment jsdom
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { computed, ref } from 'vue'
import { createPinia, setActivePinia } from 'pinia'
import { useAuthStore } from '@/stores/auth'
import type { User } from '@/types'
// 落地页这一条判据只看屏幕宽度,所以把总闸做成开关,不去伪造 matchMedia
const phoneShell = ref(false)
vi.mock('@/composables/useMobileShell', () => ({
useMobileShell: () => ({ phoneShell: computed(() => phoneShell.value) })
}))
const { landingPath, safeRedirect } = await import('@/utils/returnPath')
/** 生产上「普通员工」那一档的真实形状:139 人挂在这,有 work:todo,偏偏没有 work:workbench。 */
const STAFF = ['portal:home', 'work:todo', 'work:profile', 'work:read']
const WITH_WORKBENCH = [...STAFF, 'work:workbench']
function signIn(permissions: string[]) {
setActivePinia(createPinia())
useAuthStore().$patch({
user: { id: 'u1', account: 'staff', display_name: '员工', permissions } as unknown as User,
token: 'token'
} as never)
}
beforeEach(() => {
phoneShell.value = false
signIn(WITH_WORKBENCH)
})
describe('打开系统第一屏落哪儿', () => {
it('桌面上还是门户首页,手机上有工作台就落那五张小卡片', () => {
expect(landingPath()).toBe('/home')
phoneShell.value = true
expect(landingPath()).toBe('/workbench')
})
it('手机上没有 work:workbench 就落待办那张卡片流,不退回门户那一屏', () => {
signIn(STAFF)
phoneShell.value = true
expect(landingPath()).toBe('/todo')
// 桌面不受这件事影响:门户照旧排第一
phoneShell.value = false
expect(landingPath()).toBe('/home')
})
it('连待办都没有就再往后退一格:我的 → 门户', () => {
phoneShell.value = true
signIn(['portal:home', 'work:profile'])
expect(landingPath()).toBe('/profile')
signIn([])
expect(landingPath()).toBe('/home')
})
it('只授权了「流程明细导出」的角色:桌面和手机都落那一页,不再被守卫弹回登录页', () => {
signIn(['monitor:export'])
expect(landingPath()).toBe('/export/detail')
phoneShell.value = true
expect(landingPath()).toBe('/export/detail')
// 这一条排在最后,不抢前面几档:一旦他有门户,第一屏还是门户
signIn(['monitor:export', 'portal:home'])
expect(landingPath()).toBe('/home')
})
it('免登与登录没带 redirect 时也按这个走(safeRedirect 的兜底就是它)', () => {
expect(safeRedirect('')).toBe('/home')
phoneShell.value = true
expect(safeRedirect(undefined)).toBe('/workbench')
signIn(STAFF)
expect(safeRedirect('')).toBe('/todo')
})
it('带了站内 redirect 就回去原页,屏幕宽度不插手', () => {
phoneShell.value = true
expect(safeRedirect('/todo/task-1')).toBe('/todo/task-1')
})
it('站外地址一律不认:手机上回落到卡片那一屏,而不是被带出系统', () => {
phoneShell.value = true
expect(safeRedirect('//evil.com')).toBe('/workbench')
expect(safeRedirect('/\\evil.com')).toBe('/workbench')
})
})