mirror of
https://github.com/XiaoDaiGua-Ray/ray-template.git
synced 2026-12-02 17:53:47 +08:00
Improve theme consistency, keyboard navigation, and reusable frontend components. Harden fullstack initialization and expose scoped lint and Vitest commands.
143 lines
4.3 KiB
TypeScript
143 lines
4.3 KiB
TypeScript
import * as adminApi from '@/api/admin'
|
|
import { AdminApiError } from '@/api/admin/client'
|
|
import { userOptionsSchema } from '@/api/admin/contracts'
|
|
import { router } from '@/router'
|
|
import { piniaSigningStore } from '@/store/modules/signing'
|
|
import UserManagement from '@/views/user-management'
|
|
import { useUserErrorMessage } from '@/views/user-management/error-message'
|
|
import { flushPromises, mount } from '@vue/test-utils'
|
|
import { NTree } from 'naive-ui'
|
|
import { safeParse } from 'valibot'
|
|
import setupMiniApp from '../utils/setupMiniApp'
|
|
|
|
it('indexes unordered departments in linear work and preserves the tree order', async () => {
|
|
const { app } = await setupMiniApp()
|
|
let parentReads = 0
|
|
const departments = Array.from({ length: 1000 }, (_, index) => ({
|
|
id: String(index),
|
|
name: `Team ${index}`,
|
|
get parentId() {
|
|
parentReads++
|
|
|
|
return index < 2 ? '999' : null
|
|
},
|
|
}))
|
|
|
|
vi.spyOn(adminApi, 'getUserOptions').mockResolvedValue({
|
|
departments,
|
|
roles: [],
|
|
})
|
|
const wrapper = mount(UserManagement, {
|
|
global: {
|
|
plugins: [router],
|
|
stubs: { RTablePro: true, Tree: true, RIcon: true },
|
|
},
|
|
})
|
|
|
|
try {
|
|
await flushPromises()
|
|
const tree = wrapper.getComponent(NTree)
|
|
const children = tree.props('data')![0].children!
|
|
|
|
expect(children).toHaveLength(998)
|
|
expect(children[0].key).toBe('2')
|
|
expect(children.at(-1)?.children?.map(({ label }) => label)).toEqual([
|
|
'Team 0',
|
|
'Team 1',
|
|
])
|
|
expect(tree.props('defaultExpandedKeys')).toEqual(['', '999'])
|
|
expect(parentReads).toBeLessThanOrEqual(departments.length * 2)
|
|
} finally {
|
|
wrapper.unmount()
|
|
app.unmount()
|
|
vi.restoreAllMocks()
|
|
}
|
|
})
|
|
|
|
it('accepts a deep valid department tree and rejects cycles and missing parents', () => {
|
|
const departments = Array.from({ length: 2000 }, (_, index) => ({
|
|
id: String(index),
|
|
name: String(index),
|
|
parentId: index ? String(index - 1) : null,
|
|
})).reverse()
|
|
const valid = () =>
|
|
safeParse(userOptionsSchema, { departments, roles: [] }).success
|
|
const root = departments.at(-1)!
|
|
|
|
expect(valid()).toBe(true)
|
|
root.parentId = '1999'
|
|
expect(valid()).toBe(false)
|
|
root.parentId = 'missing'
|
|
expect(valid()).toBe(false)
|
|
})
|
|
|
|
it('keeps the current list and selection context when opening and closing an editor', async () => {
|
|
localStorage.clear()
|
|
const { app } = await setupMiniApp()
|
|
|
|
await piniaSigningStore().signing({ name: 'admin', pwd: '123456' })
|
|
const fetchUsers = vi.spyOn(adminApi, 'getUsers')
|
|
const message = window.$message
|
|
|
|
window.$message = {
|
|
error: vi.fn(),
|
|
success: vi.fn(),
|
|
} as unknown as Window['$message']
|
|
const wrapper = mount(UserManagement, {
|
|
attachTo: document.body,
|
|
global: {
|
|
plugins: [router],
|
|
stubs: {
|
|
Modal: { inheritAttrs: false, template: '<div><slot /></div>' },
|
|
RIcon: { inheritAttrs: false, template: '<span />' },
|
|
},
|
|
},
|
|
})
|
|
|
|
try {
|
|
await vi.waitFor(() => expect(wrapper.text()).toContain('Alex Chen'), {
|
|
timeout: 3000,
|
|
})
|
|
expect(fetchUsers).toHaveBeenCalledTimes(1)
|
|
await wrapper
|
|
.findAll('button')
|
|
.find((button) => button.text().trim() === '新增用户')!
|
|
.trigger('click')
|
|
await flushPromises()
|
|
expect(wrapper.find('input[name="username"]').exists()).toBe(true)
|
|
expect(wrapper.text()).toContain('Alex Chen')
|
|
expect(fetchUsers).toHaveBeenCalledTimes(1)
|
|
await wrapper
|
|
.findAll('button')
|
|
.find((button) => button.text().trim() === '取消')!
|
|
.trigger('click')
|
|
await flushPromises()
|
|
expect(wrapper.text()).toContain('Alex Chen')
|
|
expect(fetchUsers).toHaveBeenCalledTimes(1)
|
|
} finally {
|
|
wrapper.unmount()
|
|
app.unmount()
|
|
window.$message = message
|
|
localStorage.clear()
|
|
vi.restoreAllMocks()
|
|
}
|
|
})
|
|
|
|
it('shows localized limit and outage messages and falls back for unknown server errors', async () => {
|
|
const { app } = await setupMiniApp()
|
|
|
|
try {
|
|
const message = useUserErrorMessage()
|
|
const label = (code: number, key: string) =>
|
|
message(new AdminApiError(code, key, key))
|
|
|
|
expect(label(429, 'tooManyRequests')).toBe('请求过于频繁,请稍后再试。')
|
|
expect(label(503, 'serviceUnavailable')).toBe(
|
|
'服务暂时不可用,请稍后重试。',
|
|
)
|
|
expect(label(500, 'unexpectedCode')).toBe('请求失败,请稍后重试。')
|
|
} finally {
|
|
app.unmount()
|
|
}
|
|
})
|