feat: 添加测试框架和覆盖率报告功能

feat(测试): 新增Playwright和Vitest测试配置
feat(测试): 添加测试覆盖率报告生成功能
feat(测试): 实现前后端测试脚本集成

fix(测试): 修复测试密码不匹配问题
fix(测试): 修正URL等待策略
fix(测试): 调整错误消息选择器

refactor(测试): 重构测试目录结构
refactor(测试): 优化测试用例组织方式

docs: 更新测试报告文档
docs: 添加测试覆盖率报告模板

ci: 添加Docker测试环境配置
ci: 实现测试自动化脚本

chore: 更新依赖版本
chore: 添加测试相关配置文件
This commit is contained in:
张翔
2026-03-25 09:03:37 +08:00
parent 117978e148
commit e2ad1331cc
126 changed files with 18083 additions and 7805 deletions
@@ -0,0 +1,267 @@
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
import { mount } from '@vue/test-utils'
import { createRouter, createMemoryHistory } from 'vue-router'
import ConfigManagement from '@/views/config/ConfigManagement.vue'
vi.mock('vue-router')
vi.mock('element-plus', () => ({
ElMessage: {
success: vi.fn(),
error: vi.fn(),
},
ElMessageBox: {
confirm: vi.fn(),
},
}))
vi.mock('@/utils/request', () => {
const mockRequest = {
get: vi.fn(),
post: vi.fn(),
put: vi.fn(),
delete: vi.fn(),
}
mockRequest.get.mockResolvedValue([])
mockRequest.post.mockResolvedValue({})
mockRequest.put.mockResolvedValue({})
mockRequest.delete.mockResolvedValue({})
return {
default: mockRequest,
}
})
describe('ConfigManagement Component', () => {
let router: any
let wrapper: any
beforeEach(() => {
router = createRouter({
history: createMemoryHistory(),
routes: [
{ path: '/', component: { template: '<div>Home</div>' } },
],
})
vi.clearAllMocks()
})
afterEach(() => {
if (wrapper) {
wrapper.unmount()
}
})
describe('component initialization', () => {
it('should render config management container', () => {
wrapper = mount(ConfigManagement, {
global: {
plugins: [router],
stubs: {
'el-card': true,
'el-button': true,
'el-table': true,
'el-table-column': true,
'el-tag': true,
'el-dialog': true,
'el-form': true,
'el-form-item': true,
'el-input': true,
'el-icon': true,
},
},
})
expect(wrapper.find('.config-management').exists()).toBe(true)
})
it('should initialize with empty data source', () => {
wrapper = mount(ConfigManagement, {
global: {
plugins: [router],
stubs: {
'el-card': true,
'el-button': true,
'el-table': true,
'el-table-column': true,
'el-tag': true,
'el-dialog': true,
'el-form': true,
'el-form-item': true,
'el-input': true,
'el-icon': true,
},
},
})
expect(wrapper.vm.dataSource).toBeDefined()
expect(Array.isArray(wrapper.vm.dataSource)).toBe(true)
})
it('should initialize with loading state false', () => {
wrapper = mount(ConfigManagement, {
global: {
plugins: [router],
stubs: {
'el-card': true,
'el-button': true,
'el-table': true,
'el-table-column': true,
'el-tag': true,
'el-dialog': true,
'el-form': true,
'el-form-item': true,
'el-input': true,
'el-icon': true,
},
},
})
expect(wrapper.vm.loading).toBeDefined()
expect(typeof wrapper.vm.loading).toBe('boolean')
})
it('should initialize with modal visible false', () => {
wrapper = mount(ConfigManagement, {
global: {
plugins: [router],
stubs: {
'el-card': true,
'el-button': true,
'el-table': true,
'el-table-column': true,
'el-tag': true,
'el-dialog': true,
'el-form': true,
'el-form-item': true,
'el-input': true,
'el-icon': true,
},
},
})
expect(wrapper.vm.modalVisible).toBe(false)
})
it('should initialize with empty form state', () => {
wrapper = mount(ConfigManagement, {
global: {
plugins: [router],
stubs: {
'el-card': true,
'el-button': true,
'el-table': true,
'el-table-column': true,
'el-tag': true,
'el-dialog': true,
'el-form': true,
'el-form-item': true,
'el-input': true,
'el-icon': true,
},
},
})
expect(wrapper.vm.formState.configName).toBe('')
expect(wrapper.vm.formState.configKey).toBe('')
expect(wrapper.vm.formState.configValue).toBe('')
})
})
describe('add config functionality', () => {
it('should have handleAdd method', () => {
wrapper = mount(ConfigManagement, {
global: {
plugins: [router],
stubs: {
'el-card': true,
'el-button': true,
'el-table': true,
'el-table-column': true,
'el-tag': true,
'el-dialog': true,
'el-form': true,
'el-form-item': true,
'el-input': true,
'el-icon': true,
},
},
})
expect(typeof wrapper.vm.handleAdd).toBe('function')
})
})
describe('edit config functionality', () => {
it('should have handleEdit method', () => {
wrapper = mount(ConfigManagement, {
global: {
plugins: [router],
stubs: {
'el-card': true,
'el-button': true,
'el-table': true,
'el-table-column': true,
'el-tag': true,
'el-dialog': true,
'el-form': true,
'el-form-item': true,
'el-input': true,
'el-icon': true,
},
},
})
expect(typeof wrapper.vm.handleEdit).toBe('function')
})
})
describe('delete config functionality', () => {
it('should have handleDelete method', () => {
wrapper = mount(ConfigManagement, {
global: {
plugins: [router],
stubs: {
'el-card': true,
'el-button': true,
'el-table': true,
'el-table-column': true,
'el-tag': true,
'el-dialog': true,
'el-form': true,
'el-form-item': true,
'el-input': true,
'el-icon': true,
},
},
})
expect(typeof wrapper.vm.handleDelete).toBe('function')
})
})
describe('form submission', () => {
it('should have handleModalOk method', () => {
wrapper = mount(ConfigManagement, {
global: {
plugins: [router],
stubs: {
'el-card': true,
'el-button': true,
'el-table': true,
'el-table-column': true,
'el-tag': true,
'el-dialog': true,
'el-form': true,
'el-form-item': true,
'el-input': true,
'el-icon': true,
},
},
})
expect(typeof wrapper.vm.handleModalOk).toBe('function')
})
})
})
@@ -0,0 +1,261 @@
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
import { mount } from '@vue/test-utils'
import { createRouter, createMemoryHistory } from 'vue-router'
import Dashboard from '@/views/system/Dashboard.vue'
vi.mock('vue-router')
vi.mock('@/api/user.api.ts', () => ({
getUserStats: vi.fn(),
getRecentLogins: vi.fn(),
}))
describe('Dashboard Component', () => {
let router: any
let wrapper: any
beforeEach(() => {
router = createRouter({
history: createMemoryHistory(),
routes: [
{ path: '/', component: { template: '<div>Dashboard</div>' } },
],
})
vi.clearAllMocks()
})
afterEach(() => {
if (wrapper) {
wrapper.unmount()
}
})
describe('component initialization', () => {
it('should render dashboard container', () => {
wrapper = mount(Dashboard, {
global: {
plugins: [router],
stubs: {
'el-row': true,
'el-col': true,
'el-card': true,
'el-statistic': true,
'el-icon': true,
'el-timeline': true,
'el-timeline-item': true,
},
},
})
expect(wrapper.find('.dashboard').exists()).toBe(true)
})
it('should initialize with loading state', () => {
wrapper = mount(Dashboard, {
global: {
plugins: [router],
stubs: {
'el-row': true,
'el-col': true,
'el-card': true,
'el-statistic': true,
'el-icon': true,
'el-timeline': true,
'el-timeline-item': true,
},
},
})
expect(wrapper.vm.loading).toBe(true)
})
it('should initialize with empty stats', () => {
wrapper = mount(Dashboard, {
global: {
plugins: [router],
stubs: {
'el-row': true,
'el-col': true,
'el-card': true,
'el-statistic': true,
'el-icon': true,
'el-timeline': true,
'el-timeline-item': true,
},
},
})
expect(wrapper.vm.stats).toEqual({
userCount: 0,
roleCount: 0,
todayLogin: 0,
operationLog: 0,
})
})
})
describe('statistics cards', () => {
it('should render user count card', () => {
wrapper = mount(Dashboard, {
global: {
plugins: [router],
stubs: {
'el-row': true,
'el-col': true,
'el-card': true,
'el-statistic': true,
'el-icon': true,
'el-timeline': true,
'el-timeline-item': true,
},
},
})
expect(wrapper.vm.stats.userCount).toBeDefined()
})
it('should render role count card', () => {
wrapper = mount(Dashboard, {
global: {
plugins: [router],
stubs: {
'el-row': true,
'el-col': true,
'el-card': true,
'el-statistic': true,
'el-icon': true,
'el-timeline': true,
'el-timeline-item': true,
},
},
})
expect(wrapper.vm.stats.roleCount).toBeDefined()
})
it('should render today login card', () => {
wrapper = mount(Dashboard, {
global: {
plugins: [router],
stubs: {
'el-row': true,
'el-col': true,
'el-card': true,
'el-statistic': true,
'el-icon': true,
'el-timeline': true,
'el-timeline-item': true,
},
},
})
expect(wrapper.vm.stats.todayLogin).toBeDefined()
})
it('should render operation log card', () => {
wrapper = mount(Dashboard, {
global: {
plugins: [router],
stubs: {
'el-row': true,
'el-col': true,
'el-card': true,
'el-statistic': true,
'el-icon': true,
'el-timeline': true,
'el-timeline-item': true,
},
},
})
expect(wrapper.vm.stats.operationLog).toBeDefined()
})
})
describe('recent logins', () => {
it('should initialize with empty recent logins', () => {
wrapper = mount(Dashboard, {
global: {
plugins: [router],
stubs: {
'el-row': true,
'el-col': true,
'el-card': true,
'el-statistic': true,
'el-icon': true,
'el-timeline': true,
'el-timeline-item': true,
},
},
})
expect(wrapper.vm.recentLogins).toEqual([])
})
it('should display empty state when no recent logins', () => {
wrapper = mount(Dashboard, {
global: {
plugins: [router],
stubs: {
'el-row': true,
'el-col': true,
'el-card': true,
'el-statistic': true,
'el-icon': true,
'el-timeline': true,
'el-timeline-item': true,
},
},
})
expect(wrapper.vm.recentLogins.length).toBe(0)
})
})
describe('data loading', () => {
it('should set loading to false after data loaded', async () => {
wrapper = mount(Dashboard, {
global: {
plugins: [router],
stubs: {
'el-row': true,
'el-col': true,
'el-card': true,
'el-statistic': true,
'el-icon': true,
'el-timeline': true,
'el-timeline-item': true,
},
},
})
expect(wrapper.vm.loading).toBe(true)
wrapper.vm.loading = false
await wrapper.vm.$nextTick()
expect(wrapper.vm.loading).toBe(false)
})
})
describe('document title', () => {
it('should have dashboard component mounted', () => {
wrapper = mount(Dashboard, {
global: {
plugins: [router],
stubs: {
'el-row': true,
'el-col': true,
'el-card': true,
'el-statistic': true,
'el-icon': true,
'el-timeline': true,
'el-timeline-item': true,
},
},
})
expect(wrapper.exists()).toBe(true)
})
})
})
@@ -0,0 +1,286 @@
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
import { mount } from '@vue/test-utils'
import { createRouter, createMemoryHistory } from 'vue-router'
import DictManagement from '@/views/config/DictManagement.vue'
vi.mock('vue-router')
vi.mock('element-plus', () => ({
ElMessage: {
success: vi.fn(),
error: vi.fn(),
},
ElMessageBox: {
confirm: vi.fn(),
},
}))
vi.mock('@/utils/request', () => {
const mockRequest = {
get: vi.fn(),
post: vi.fn(),
put: vi.fn(),
delete: vi.fn(),
}
mockRequest.get.mockResolvedValue([])
mockRequest.post.mockResolvedValue({})
mockRequest.put.mockResolvedValue({})
mockRequest.delete.mockResolvedValue({})
return {
default: mockRequest,
}
})
describe('DictManagement Component', () => {
let router: any
let wrapper: any
beforeEach(() => {
router = createRouter({
history: createMemoryHistory(),
routes: [
{ path: '/', component: { template: '<div>Home</div>' } },
],
})
vi.clearAllMocks()
})
afterEach(() => {
if (wrapper) {
wrapper.unmount()
}
})
describe('component initialization', () => {
it('should render dict management container', () => {
wrapper = mount(DictManagement, {
global: {
plugins: [router],
stubs: {
'el-card': true,
'el-button': true,
'el-table': true,
'el-table-column': true,
'el-tag': true,
'el-dialog': true,
'el-form': true,
'el-form-item': true,
'el-input': true,
'el-select': true,
'el-option': true,
'el-icon': true,
},
},
})
expect(wrapper.find('.dict-management').exists()).toBe(true)
})
it('should initialize with empty data source', () => {
wrapper = mount(DictManagement, {
global: {
plugins: [router],
stubs: {
'el-card': true,
'el-button': true,
'el-table': true,
'el-table-column': true,
'el-tag': true,
'el-dialog': true,
'el-form': true,
'el-form-item': true,
'el-input': true,
'el-select': true,
'el-option': true,
'el-icon': true,
},
},
})
expect(wrapper.vm.dataSource).toBeDefined()
expect(Array.isArray(wrapper.vm.dataSource)).toBe(true)
})
it('should initialize with loading state false', () => {
wrapper = mount(DictManagement, {
global: {
plugins: [router],
stubs: {
'el-card': true,
'el-button': true,
'el-table': true,
'el-table-column': true,
'el-tag': true,
'el-dialog': true,
'el-form': true,
'el-form-item': true,
'el-input': true,
'el-select': true,
'el-option': true,
'el-icon': true,
},
},
})
expect(wrapper.vm.loading).toBeDefined()
expect(typeof wrapper.vm.loading).toBe('boolean')
})
it('should initialize with modal visible false', () => {
wrapper = mount(DictManagement, {
global: {
plugins: [router],
stubs: {
'el-card': true,
'el-button': true,
'el-table': true,
'el-table-column': true,
'el-tag': true,
'el-dialog': true,
'el-form': true,
'el-form-item': true,
'el-input': true,
'el-select': true,
'el-option': true,
'el-icon': true,
},
},
})
expect(wrapper.vm.modalVisible).toBe(false)
})
it('should initialize with empty form state', () => {
wrapper = mount(DictManagement, {
global: {
plugins: [router],
stubs: {
'el-card': true,
'el-button': true,
'el-table': true,
'el-table-column': true,
'el-tag': true,
'el-dialog': true,
'el-form': true,
'el-form-item': true,
'el-input': true,
'el-select': true,
'el-option': true,
'el-icon': true,
},
},
})
expect(wrapper.vm.formState.dictName).toBe('')
expect(wrapper.vm.formState.dictType).toBe('')
expect(wrapper.vm.formState.status).toBe('0')
expect(wrapper.vm.formState.remark).toBe('')
})
})
describe('add dict functionality', () => {
it('should have handleAdd method', () => {
wrapper = mount(DictManagement, {
global: {
plugins: [router],
stubs: {
'el-card': true,
'el-button': true,
'el-table': true,
'el-table-column': true,
'el-tag': true,
'el-dialog': true,
'el-form': true,
'el-form-item': true,
'el-input': true,
'el-select': true,
'el-option': true,
'el-icon': true,
},
},
})
expect(typeof wrapper.vm.handleAdd).toBe('function')
})
})
describe('edit dict functionality', () => {
it('should have handleEdit method', () => {
wrapper = mount(DictManagement, {
global: {
plugins: [router],
stubs: {
'el-card': true,
'el-button': true,
'el-table': true,
'el-table-column': true,
'el-tag': true,
'el-dialog': true,
'el-form': true,
'el-form-item': true,
'el-input': true,
'el-select': true,
'el-option': true,
'el-icon': true,
},
},
})
expect(typeof wrapper.vm.handleEdit).toBe('function')
})
})
describe('delete dict functionality', () => {
it('should have handleDelete method', () => {
wrapper = mount(DictManagement, {
global: {
plugins: [router],
stubs: {
'el-card': true,
'el-button': true,
'el-table': true,
'el-table-column': true,
'el-tag': true,
'el-dialog': true,
'el-form': true,
'el-form-item': true,
'el-input': true,
'el-select': true,
'el-option': true,
'el-icon': true,
},
},
})
expect(typeof wrapper.vm.handleDelete).toBe('function')
})
})
describe('form submission', () => {
it('should have handleModalOk method', () => {
wrapper = mount(DictManagement, {
global: {
plugins: [router],
stubs: {
'el-card': true,
'el-button': true,
'el-table': true,
'el-table-column': true,
'el-tag': true,
'el-dialog': true,
'el-form': true,
'el-form-item': true,
'el-input': true,
'el-select': true,
'el-option': true,
'el-icon': true,
},
},
})
expect(typeof wrapper.vm.handleModalOk).toBe('function')
})
})
})
@@ -0,0 +1,181 @@
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
import { mount } from '@vue/test-utils'
import { createRouter, createMemoryHistory } from 'vue-router'
import Login from '@/views/system/Login.vue'
vi.mock('vue-router')
vi.mock('element-plus', () => ({
ElMessage: {
success: vi.fn(),
error: vi.fn(),
},
}))
vi.mock('@/utils/request', () => ({
default: {
post: vi.fn(),
},
}))
describe('Login Component', () => {
let router: any
let wrapper: any
beforeEach(() => {
router = createRouter({
history: createMemoryHistory(),
routes: [
{ path: '/', component: { template: '<div>Dashboard</div>' } },
{ path: '/login', component: { template: '<div>Login</div>' } },
],
})
vi.clearAllMocks()
localStorage.clear()
})
afterEach(() => {
if (wrapper) {
wrapper.unmount()
}
})
describe('component rendering', () => {
it('should render login form', () => {
wrapper = mount(Login, {
global: {
plugins: [router],
stubs: {
'el-card': true,
'el-form': true,
'el-form-item': true,
'el-input': true,
'el-button': true,
},
},
})
expect(wrapper.find('.login-container').exists()).toBe(true)
expect(wrapper.find('.login-card').exists()).toBe(true)
})
it('should initialize with empty form state', () => {
wrapper = mount(Login, {
global: {
plugins: [router],
stubs: {
'el-card': true,
'el-form': true,
'el-form-item': true,
'el-input': true,
'el-button': true,
},
},
})
expect(wrapper.vm.formState.username).toBe('')
expect(wrapper.vm.formState.password).toBe('')
})
it('should initialize loading as false', () => {
wrapper = mount(Login, {
global: {
plugins: [router],
stubs: {
'el-card': true,
'el-form': true,
'el-form-item': true,
'el-input': true,
'el-button': true,
},
},
})
expect(wrapper.vm.loading).toBe(false)
})
})
describe('form state management', () => {
it('should update username when input changes', async () => {
wrapper = mount(Login, {
global: {
plugins: [router],
stubs: {
'el-card': true,
'el-form': true,
'el-form-item': true,
'el-input': true,
'el-button': true,
},
},
})
wrapper.vm.formState.username = 'testuser'
await wrapper.vm.$nextTick()
expect(wrapper.vm.formState.username).toBe('testuser')
})
it('should update password when input changes', async () => {
wrapper = mount(Login, {
global: {
plugins: [router],
stubs: {
'el-card': true,
'el-form': true,
'el-form-item': true,
'el-input': true,
'el-button': true,
},
},
})
wrapper.vm.formState.password = 'password123'
await wrapper.vm.$nextTick()
expect(wrapper.vm.formState.password).toBe('password123')
})
})
describe('form submission', () => {
it('should have onFinish method', () => {
wrapper = mount(Login, {
global: {
plugins: [router],
stubs: {
'el-card': true,
'el-form': true,
'el-form-item': true,
'el-input': true,
'el-button': true,
},
},
})
expect(typeof wrapper.vm.onFinish).toBe('function')
})
})
describe('document title', () => {
it('should set document title on mount', () => {
const originalTitle = document.title
wrapper = mount(Login, {
global: {
plugins: [router],
stubs: {
'el-card': true,
'el-form': true,
'el-form-item': true,
'el-input': true,
'el-button': true,
},
},
})
expect(document.title).toBe('登录 - Novalon 管理系统')
document.title = originalTitle
})
})
})
@@ -0,0 +1,279 @@
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
import { mount } from '@vue/test-utils'
import { createRouter, createMemoryHistory } from 'vue-router'
import MenuManagement from '@/views/system/MenuManagement.vue'
vi.mock('vue-router')
vi.mock('element-plus', () => ({
ElMessage: {
success: vi.fn(),
error: vi.fn(),
},
ElMessageBox: {
confirm: vi.fn(),
},
}))
vi.mock('@/api/menu.api', () => ({
menuApi: {
getAll: vi.fn(),
create: vi.fn(),
update: vi.fn(),
delete: vi.fn(),
},
}))
vi.mock('@/utils/request', () => {
const mockRequest = {
get: vi.fn(),
post: vi.fn(),
put: vi.fn(),
delete: vi.fn(),
}
mockRequest.get.mockResolvedValue([])
mockRequest.post.mockResolvedValue({})
mockRequest.put.mockResolvedValue({})
mockRequest.delete.mockResolvedValue({})
return {
default: mockRequest,
}
})
describe('MenuManagement Component', () => {
let router: any
let wrapper: any
beforeEach(() => {
router = createRouter({
history: createMemoryHistory(),
routes: [
{ path: '/', component: { template: '<div>Home</div>' } },
],
})
vi.clearAllMocks()
})
afterEach(() => {
if (wrapper) {
wrapper.unmount()
}
})
describe('component initialization', () => {
it('should render menu management container', () => {
wrapper = mount(MenuManagement, {
global: {
plugins: [router],
stubs: {
'el-card': true,
'el-button': true,
'el-table': true,
'el-table-column': true,
'el-tag': true,
'el-dialog': true,
'el-form': true,
'el-form-item': true,
'el-input': true,
'el-input-number': true,
'el-select': true,
'el-option': true,
'el-icon': true,
},
},
})
expect(wrapper.find('.menu-management').exists()).toBe(true)
})
it('should initialize with empty data source', () => {
wrapper = mount(MenuManagement, {
global: {
plugins: [router],
stubs: {
'el-card': true,
'el-button': true,
'el-table': true,
'el-table-column': true,
'el-tag': true,
'el-dialog': true,
'el-form': true,
'el-form-item': true,
'el-input': true,
'el-input-number': true,
'el-select': true,
'el-option': true,
'el-icon': true,
},
},
})
expect(wrapper.vm.dataSource).toBeDefined()
expect(Array.isArray(wrapper.vm.dataSource)).toBe(true)
})
it('should initialize with loading state false', () => {
wrapper = mount(MenuManagement, {
global: {
plugins: [router],
stubs: {
'el-card': true,
'el-button': true,
'el-table': true,
'el-table-column': true,
'el-tag': true,
'el-dialog': true,
'el-form': true,
'el-form-item': true,
'el-input': true,
'el-input-number': true,
'el-select': true,
'el-option': true,
'el-icon': true,
},
},
})
expect(wrapper.vm.loading).toBeDefined()
expect(typeof wrapper.vm.loading).toBe('boolean')
})
it('should initialize with modal visible false', () => {
wrapper = mount(MenuManagement, {
global: {
plugins: [router],
stubs: {
'el-card': true,
'el-button': true,
'el-table': true,
'el-table-column': true,
'el-tag': true,
'el-dialog': true,
'el-form': true,
'el-form-item': true,
'el-input': true,
'el-input-number': true,
'el-select': true,
'el-option': true,
'el-icon': true,
},
},
})
expect(wrapper.vm.modalVisible).toBe(false)
})
it('should initialize with empty form state', () => {
wrapper = mount(MenuManagement, {
global: {
plugins: [router],
stubs: {
'el-card': true,
'el-button': true,
'el-table': true,
'el-table-column': true,
'el-tag': true,
'el-dialog': true,
'el-form': true,
'el-form-item': true,
'el-input': true,
'el-input-number': true,
'el-select': true,
'el-option': true,
'el-icon': true,
},
},
})
expect(wrapper.vm.formState.menuName).toBe('')
expect(wrapper.vm.formState.menuType).toBe('C')
expect(wrapper.vm.formState.perms).toBe('')
expect(wrapper.vm.formState.component).toBe('')
expect(wrapper.vm.formState.orderNum).toBe(0)
expect(wrapper.vm.formState.status).toBe('0')
})
})
describe('add menu functionality', () => {
it('should have handleAdd method', () => {
wrapper = mount(MenuManagement, {
global: {
plugins: [router],
stubs: {
'el-card': true,
'el-button': true,
'el-table': true,
'el-table-column': true,
'el-tag': true,
'el-dialog': true,
'el-form': true,
'el-form-item': true,
'el-input': true,
'el-input-number': true,
'el-select': true,
'el-option': true,
'el-icon': true,
},
},
})
expect(typeof wrapper.vm.handleAdd).toBe('function')
})
})
describe('edit menu functionality', () => {
it('should have handleEdit method', () => {
wrapper = mount(MenuManagement, {
global: {
plugins: [router],
stubs: {
'el-card': true,
'el-button': true,
'el-table': true,
'el-table-column': true,
'el-tag': true,
'el-dialog': true,
'el-form': true,
'el-form-item': true,
'el-input': true,
'el-input-number': true,
'el-select': true,
'el-option': true,
'el-icon': true,
},
},
})
expect(typeof wrapper.vm.handleEdit).toBe('function')
})
})
describe('delete menu functionality', () => {
it('should have handleDelete method', () => {
wrapper = mount(MenuManagement, {
global: {
plugins: [router],
stubs: {
'el-card': true,
'el-button': true,
'el-table': true,
'el-table-column': true,
'el-tag': true,
'el-dialog': true,
'el-form': true,
'el-form-item': true,
'el-input': true,
'el-input-number': true,
'el-select': true,
'el-option': true,
'el-icon': true,
},
},
})
expect(typeof wrapper.vm.handleDelete).toBe('function')
})
})
})
@@ -0,0 +1,383 @@
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
import { mount } from '@vue/test-utils'
import { createRouter, createMemoryHistory } from 'vue-router'
import RoleManagement from '@/views/system/RoleManagement.vue'
vi.mock('vue-router')
vi.mock('element-plus', () => ({
ElMessage: {
success: vi.fn(),
error: vi.fn(),
},
ElMessageBox: {
confirm: vi.fn(),
},
}))
vi.mock('@/api/role.api', () => ({
roleApi: {
getPage: vi.fn(),
create: vi.fn(),
update: vi.fn(),
delete: vi.fn(),
getAll: vi.fn(),
},
}))
vi.mock('@/api/permission.api', () => ({
permissionApi: {
getAll: vi.fn(),
},
}))
describe('RoleManagement Component', () => {
let router: any
let wrapper: any
beforeEach(() => {
router = createRouter({
history: createMemoryHistory(),
routes: [
{ path: '/', component: { template: '<div>Home</div>' } },
],
})
vi.clearAllMocks()
})
afterEach(() => {
if (wrapper) {
wrapper.unmount()
}
})
describe('component initialization', () => {
it('should render role management container', () => {
wrapper = mount(RoleManagement, {
global: {
plugins: [router],
stubs: {
'el-card': true,
'el-input': true,
'el-button': true,
'el-table': true,
'el-table-column': true,
'el-tag': true,
'el-pagination': true,
'el-dialog': true,
'el-form': true,
'el-form-item': true,
'el-tree': true,
'el-icon': true,
},
},
})
expect(wrapper.find('.role-management').exists()).toBe(true)
})
it('should initialize with empty search keyword', () => {
wrapper = mount(RoleManagement, {
global: {
plugins: [router],
stubs: {
'el-card': true,
'el-input': true,
'el-button': true,
'el-table': true,
'el-table-column': true,
'el-tag': true,
'el-pagination': true,
'el-dialog': true,
'el-form': true,
'el-form-item': true,
'el-tree': true,
'el-icon': true,
},
},
})
expect(wrapper.vm.searchKeyword).toBe('')
})
it('should initialize with empty data source', () => {
wrapper = mount(RoleManagement, {
global: {
plugins: [router],
stubs: {
'el-card': true,
'el-input': true,
'el-button': true,
'el-table': true,
'el-table-column': true,
'el-tag': true,
'el-pagination': true,
'el-dialog': true,
'el-form': true,
'el-form-item': true,
'el-tree': true,
'el-icon': true,
},
},
})
expect(wrapper.vm.dataSource).toEqual([])
})
it('should initialize with pagination on page 1', () => {
wrapper = mount(RoleManagement, {
global: {
plugins: [router],
stubs: {
'el-card': true,
'el-input': true,
'el-button': true,
'el-table': true,
'el-table-column': true,
'el-tag': true,
'el-pagination': true,
'el-dialog': true,
'el-form': true,
'el-form-item': true,
'el-tree': true,
'el-icon': true,
},
},
})
expect(wrapper.vm.pagination.current).toBe(1)
})
it('should initialize with modal visible false', () => {
wrapper = mount(RoleManagement, {
global: {
plugins: [router],
stubs: {
'el-card': true,
'el-input': true,
'el-button': true,
'el-table': true,
'el-table-column': true,
'el-tag': true,
'el-pagination': true,
'el-dialog': true,
'el-form': true,
'el-form-item': true,
'el-tree': true,
'el-icon': true,
},
},
})
expect(wrapper.vm.modalVisible).toBe(false)
})
it('should initialize with empty form state', () => {
wrapper = mount(RoleManagement, {
global: {
plugins: [router],
stubs: {
'el-card': true,
'el-input': true,
'el-button': true,
'el-table': true,
'el-table-column': true,
'el-tag': true,
'el-pagination': true,
'el-dialog': true,
'el-form': true,
'el-form-item': true,
'el-tree': true,
'el-icon': true,
},
},
})
expect(wrapper.vm.formState.roleName).toBe('')
expect(wrapper.vm.formState.roleKey).toBe('')
expect(wrapper.vm.formState.roleSort).toBe(1)
expect(wrapper.vm.formState.status).toBe(1)
expect(wrapper.vm.formState.permissions).toEqual([])
})
})
describe('search functionality', () => {
it('should have handleSearch method', () => {
wrapper = mount(RoleManagement, {
global: {
plugins: [router],
stubs: {
'el-card': true,
'el-input': true,
'el-button': true,
'el-table': true,
'el-table-column': true,
'el-tag': true,
'el-pagination': true,
'el-dialog': true,
'el-form': true,
'el-form-item': true,
'el-tree': true,
'el-icon': true,
},
},
})
expect(typeof wrapper.vm.handleSearch).toBe('function')
})
it('should update search keyword when input changes', async () => {
wrapper = mount(RoleManagement, {
global: {
plugins: [router],
stubs: {
'el-card': true,
'el-input': true,
'el-button': true,
'el-table': true,
'el-table-column': true,
'el-tag': true,
'el-pagination': true,
'el-dialog': true,
'el-form': true,
'el-form-item': true,
'el-tree': true,
'el-icon': true,
},
},
})
wrapper.vm.searchKeyword = 'admin'
await wrapper.vm.$nextTick()
expect(wrapper.vm.searchKeyword).toBe('admin')
})
})
describe('add role functionality', () => {
it('should have handleAdd method', () => {
wrapper = mount(RoleManagement, {
global: {
plugins: [router],
stubs: {
'el-card': true,
'el-input': true,
'el-button': true,
'el-table': true,
'el-table-column': true,
'el-tag': true,
'el-pagination': true,
'el-dialog': true,
'el-form': true,
'el-form-item': true,
'el-tree': true,
'el-icon': true,
},
},
})
expect(typeof wrapper.vm.handleAdd).toBe('function')
})
})
describe('pagination functionality', () => {
it('should have handleTableChange method', () => {
wrapper = mount(RoleManagement, {
global: {
plugins: [router],
stubs: {
'el-card': true,
'el-input': true,
'el-button': true,
'el-table': true,
'el-table-column': true,
'el-tag': true,
'el-pagination': true,
'el-dialog': true,
'el-form': true,
'el-form-item': true,
'el-tree': true,
'el-icon': true,
},
},
})
expect(typeof wrapper.vm.handleTableChange).toBe('function')
})
it('should have handleSizeChange method', () => {
wrapper = mount(RoleManagement, {
global: {
plugins: [router],
stubs: {
'el-card': true,
'el-input': true,
'el-button': true,
'el-table': true,
'el-table-column': true,
'el-tag': true,
'el-pagination': true,
'el-dialog': true,
'el-form': true,
'el-form-item': true,
'el-tree': true,
'el-icon': true,
},
},
})
expect(typeof wrapper.vm.handleSizeChange).toBe('function')
})
})
describe('sort functionality', () => {
it('should have handleSortChange method', () => {
wrapper = mount(RoleManagement, {
global: {
plugins: [router],
stubs: {
'el-card': true,
'el-input': true,
'el-button': true,
'el-table': true,
'el-table-column': true,
'el-tag': true,
'el-pagination': true,
'el-dialog': true,
'el-form': true,
'el-form-item': true,
'el-tree': true,
'el-icon': true,
},
},
})
expect(typeof wrapper.vm.handleSortChange).toBe('function')
})
it('should initialize with default sort info', () => {
wrapper = mount(RoleManagement, {
global: {
plugins: [router],
stubs: {
'el-card': true,
'el-input': true,
'el-button': true,
'el-table': true,
'el-table-column': true,
'el-tag': true,
'el-pagination': true,
'el-dialog': true,
'el-form': true,
'el-form-item': true,
'el-tree': true,
'el-icon': true,
},
},
})
expect(wrapper.vm.sortInfo.sortBy).toBe('id')
expect(wrapper.vm.sortInfo.sortOrder).toBe('asc')
})
})
})
@@ -0,0 +1,423 @@
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
import { mount } from '@vue/test-utils'
import { createRouter, createMemoryHistory } from 'vue-router'
import UserManagement from '@/views/system/UserManagement.vue'
vi.mock('vue-router')
vi.mock('element-plus', () => ({
ElMessage: {
success: vi.fn(),
error: vi.fn(),
},
ElMessageBox: {
confirm: vi.fn(),
},
}))
vi.mock('@/api/user.api', () => ({
userApi: {
getPage: vi.fn(),
create: vi.fn(),
update: vi.fn(),
delete: vi.fn(),
assignRoles: vi.fn(),
},
}))
vi.mock('@/api/role.api', () => ({
roleApi: {
getAll: vi.fn(),
},
}))
describe('UserManagement Component', () => {
let router: any
let wrapper: any
beforeEach(() => {
router = createRouter({
history: createMemoryHistory(),
routes: [
{ path: '/', component: { template: '<div>Home</div>' } },
],
})
vi.clearAllMocks()
})
afterEach(() => {
if (wrapper) {
wrapper.unmount()
}
})
describe('component initialization', () => {
it('should render user management container', () => {
wrapper = mount(UserManagement, {
global: {
plugins: [router],
stubs: {
'el-card': true,
'el-input': true,
'el-button': true,
'el-table': true,
'el-table-column': true,
'el-tag': true,
'el-pagination': true,
'el-dialog': true,
'el-form': true,
'el-form-item': true,
'el-select': true,
'el-option': true,
'el-icon': true,
},
},
})
expect(wrapper.find('.user-management').exists()).toBe(true)
})
it('should initialize with empty search keyword', () => {
wrapper = mount(UserManagement, {
global: {
plugins: [router],
stubs: {
'el-card': true,
'el-input': true,
'el-button': true,
'el-table': true,
'el-table-column': true,
'el-tag': true,
'el-pagination': true,
'el-dialog': true,
'el-form': true,
'el-form-item': true,
'el-select': true,
'el-option': true,
'el-icon': true,
},
},
})
expect(wrapper.vm.searchKeyword).toBe('')
})
it('should initialize with loading state false before data fetch', () => {
wrapper = mount(UserManagement, {
global: {
plugins: [router],
stubs: {
'el-card': true,
'el-input': true,
'el-button': true,
'el-table': true,
'el-table-column': true,
'el-tag': true,
'el-pagination': true,
'el-dialog': true,
'el-form': true,
'el-form-item': true,
'el-select': true,
'el-option': true,
'el-icon': true,
},
},
})
expect(wrapper.vm.loading).toBeDefined()
expect(typeof wrapper.vm.loading).toBe('boolean')
})
it('should initialize with empty data source', () => {
wrapper = mount(UserManagement, {
global: {
plugins: [router],
stubs: {
'el-card': true,
'el-input': true,
'el-button': true,
'el-table': true,
'el-table-column': true,
'el-tag': true,
'el-pagination': true,
'el-dialog': true,
'el-form': true,
'el-form-item': true,
'el-select': true,
'el-option': true,
'el-icon': true,
},
},
})
expect(wrapper.vm.dataSource).toEqual([])
})
it('should initialize with pagination on page 1', () => {
wrapper = mount(UserManagement, {
global: {
plugins: [router],
stubs: {
'el-card': true,
'el-input': true,
'el-button': true,
'el-table': true,
'el-table-column': true,
'el-tag': true,
'el-pagination': true,
'el-dialog': true,
'el-form': true,
'el-form-item': true,
'el-select': true,
'el-option': true,
'el-icon': true,
},
},
})
expect(wrapper.vm.pagination.current).toBe(1)
})
it('should initialize with modal visible false', () => {
wrapper = mount(UserManagement, {
global: {
plugins: [router],
stubs: {
'el-card': true,
'el-input': true,
'el-button': true,
'el-table': true,
'el-table-column': true,
'el-tag': true,
'el-pagination': true,
'el-dialog': true,
'el-form': true,
'el-form-item': true,
'el-select': true,
'el-option': true,
'el-icon': true,
},
},
})
expect(wrapper.vm.modalVisible).toBe(false)
})
it('should initialize with empty form state', () => {
wrapper = mount(UserManagement, {
global: {
plugins: [router],
stubs: {
'el-card': true,
'el-input': true,
'el-button': true,
'el-table': true,
'el-table-column': true,
'el-tag': true,
'el-pagination': true,
'el-dialog': true,
'el-form': true,
'el-form-item': true,
'el-select': true,
'el-option': true,
'el-icon': true,
},
},
})
expect(wrapper.vm.formState.username).toBe('')
expect(wrapper.vm.formState.password).toBe('')
expect(wrapper.vm.formState.nickname).toBe('')
expect(wrapper.vm.formState.email).toBe('')
expect(wrapper.vm.formState.phone).toBe('')
expect(wrapper.vm.formState.roles).toEqual([])
})
})
describe('search functionality', () => {
it('should have handleSearch method', () => {
wrapper = mount(UserManagement, {
global: {
plugins: [router],
stubs: {
'el-card': true,
'el-input': true,
'el-button': true,
'el-table': true,
'el-table-column': true,
'el-tag': true,
'el-pagination': true,
'el-dialog': true,
'el-form': true,
'el-form-item': true,
'el-select': true,
'el-option': true,
'el-icon': true,
},
},
})
expect(typeof wrapper.vm.handleSearch).toBe('function')
})
it('should update search keyword when input changes', async () => {
wrapper = mount(UserManagement, {
global: {
plugins: [router],
stubs: {
'el-card': true,
'el-input': true,
'el-button': true,
'el-table': true,
'el-table-column': true,
'el-tag': true,
'el-pagination': true,
'el-dialog': true,
'el-form': true,
'el-form-item': true,
'el-select': true,
'el-option': true,
'el-icon': true,
},
},
})
wrapper.vm.searchKeyword = 'testuser'
await wrapper.vm.$nextTick()
expect(wrapper.vm.searchKeyword).toBe('testuser')
})
})
describe('add user functionality', () => {
it('should have handleAdd method', () => {
wrapper = mount(UserManagement, {
global: {
plugins: [router],
stubs: {
'el-card': true,
'el-input': true,
'el-button': true,
'el-table': true,
'el-table-column': true,
'el-tag': true,
'el-pagination': true,
'el-dialog': true,
'el-form': true,
'el-form-item': true,
'el-select': true,
'el-option': true,
'el-icon': true,
},
},
})
expect(typeof wrapper.vm.handleAdd).toBe('function')
})
})
describe('pagination functionality', () => {
it('should have handleTableChange method', () => {
wrapper = mount(UserManagement, {
global: {
plugins: [router],
stubs: {
'el-card': true,
'el-input': true,
'el-button': true,
'el-table': true,
'el-table-column': true,
'el-tag': true,
'el-pagination': true,
'el-dialog': true,
'el-form': true,
'el-form-item': true,
'el-select': true,
'el-option': true,
'el-icon': true,
},
},
})
expect(typeof wrapper.vm.handleTableChange).toBe('function')
})
it('should have handleSizeChange method', () => {
wrapper = mount(UserManagement, {
global: {
plugins: [router],
stubs: {
'el-card': true,
'el-input': true,
'el-button': true,
'el-table': true,
'el-table-column': true,
'el-tag': true,
'el-pagination': true,
'el-dialog': true,
'el-form': true,
'el-form-item': true,
'el-select': true,
'el-option': true,
'el-icon': true,
},
},
})
expect(typeof wrapper.vm.handleSizeChange).toBe('function')
})
})
describe('sort functionality', () => {
it('should have handleSortChange method', () => {
wrapper = mount(UserManagement, {
global: {
plugins: [router],
stubs: {
'el-card': true,
'el-input': true,
'el-button': true,
'el-table': true,
'el-table-column': true,
'el-tag': true,
'el-pagination': true,
'el-dialog': true,
'el-form': true,
'el-form-item': true,
'el-select': true,
'el-option': true,
'el-icon': true,
},
},
})
expect(typeof wrapper.vm.handleSortChange).toBe('function')
})
it('should initialize with default sort info', () => {
wrapper = mount(UserManagement, {
global: {
plugins: [router],
stubs: {
'el-card': true,
'el-input': true,
'el-button': true,
'el-table': true,
'el-table-column': true,
'el-tag': true,
'el-pagination': true,
'el-dialog': true,
'el-form': true,
'el-form-item': true,
'el-select': true,
'el-option': true,
'el-icon': true,
},
},
})
expect(wrapper.vm.sortInfo.sortBy).toBe('id')
expect(wrapper.vm.sortInfo.sortOrder).toBe('asc')
})
})
})
@@ -0,0 +1,12 @@
import { describe, it, expect } from 'vitest'
describe('Vitest Configuration Test', () => {
it('should run a simple test', () => {
expect(1 + 1).toBe(2)
})
it('should handle async operations', async () => {
const result = await Promise.resolve(42)
expect(result).toBe(42)
})
})
+88
View File
@@ -0,0 +1,88 @@
export const mockUser = {
id: 1,
username: 'testuser',
nickname: 'Test User',
email: 'test@example.com',
phone: '13800138000',
avatar: 'https://example.com/avatar.jpg',
roles: ['admin'],
permissions: ['user:view', 'user:create', 'user:edit', 'user:delete'],
}
export const mockRole = {
id: 1,
roleName: '测试角色',
roleKey: 'test_role',
roleSort: 1,
status: '1',
remark: '测试角色备注',
createTime: new Date().toISOString(),
updateTime: new Date().toISOString(),
}
export const mockMenu = {
id: 1,
menuName: '系统管理',
parentId: 0,
orderNum: 1,
menuType: 'M',
component: 'system',
perms: 'system:view',
status: '1',
createTime: new Date().toISOString(),
updateTime: new Date().toISOString(),
}
export const mockDict = {
id: 1,
dictName: '用户状态',
dictType: 'user_status',
status: '1',
remark: '用户状态字典',
createTime: new Date().toISOString(),
updateTime: new Date().toISOString(),
}
export const mockConfig = {
id: 1,
configName: '系统名称',
configKey: 'sys.name',
configValue: 'Novalon管理系统',
configType: 'Y',
status: '1',
remark: '系统名称配置',
createTime: new Date().toISOString(),
updateTime: new Date().toISOString(),
}
export const mockNotice = {
id: 1,
noticeTitle: '系统通知',
noticeType: '1',
noticeContent: '这是一条测试通知',
status: '0',
createTime: new Date().toISOString(),
updateTime: new Date().toISOString(),
}
export const mockLoginRequest = {
username: 'admin',
password: 'admin123',
}
export const mockLoginResponse = {
token: 'mock-jwt-token',
user: mockUser,
}
export const mockApiResponse = <T>(data: T, code = 200, message = 'success') => ({
code,
message,
data,
})
export const mockErrorResponse = (code = 500, message = 'Internal Server Error') => ({
code,
message,
data: null,
})
+39
View File
@@ -0,0 +1,39 @@
import { vi } from 'vitest'
import { config } from '@vue/test-utils'
config.global.stubs = {
transition: false,
'transition-group': false,
}
Object.defineProperty(window, 'matchMedia', {
writable: true,
value: vi.fn().mockImplementation((query) => ({
matches: false,
media: query,
onchange: null,
addListener: vi.fn(),
removeListener: vi.fn(),
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
dispatchEvent: vi.fn(),
})),
})
Object.defineProperty(window, 'localStorage', {
value: {
getItem: vi.fn(),
setItem: vi.fn(),
removeItem: vi.fn(),
clear: vi.fn(),
},
})
Object.defineProperty(window, 'sessionStorage', {
value: {
getItem: vi.fn(),
setItem: vi.fn(),
removeItem: vi.fn(),
clear: vi.fn(),
},
})
+61
View File
@@ -0,0 +1,61 @@
import { VueWrapper } from '@vue/test-utils'
import { ComponentPublicInstance } from 'vue'
export interface TestHelpers {
findByText: (text: string) => HTMLElement | null
findByTestId: (testId: string) => HTMLElement | null
clickByText: (text: string) => Promise<void>
clickByTestId: (testId: string) => Promise<void>
fillByTestId: (testId: string, value: string) => Promise<void>
}
export function createTestHelpers(wrapper: VueWrapper<ComponentPublicInstance>): TestHelpers {
return {
findByText: (text: string) => {
return wrapper.element.textContent?.includes(text) ? wrapper.element : null
},
findByTestId: (testId: string) => {
return wrapper.element.querySelector(`[data-testid="${testId}"]`)
},
clickByText: async (text: string) => {
const element = wrapper.element.textContent?.includes(text) ? wrapper.element : null
if (element) {
element.click()
await wrapper.vm.$nextTick()
}
},
clickByTestId: async (testId: string) => {
const element = wrapper.element.querySelector(`[data-testid="${testId}"]`)
if (element) {
element.click()
await wrapper.vm.$nextTick()
}
},
fillByTestId: async (testId: string, value: string) => {
const element = wrapper.element.querySelector(`[data-testid="${testId}"]`) as HTMLInputElement
if (element) {
element.value = value
element.dispatchEvent(new Event('input', { bubbles: true }))
await wrapper.vm.$nextTick()
}
},
}
}
export function waitFor(condition: () => boolean, timeout = 5000): Promise<void> {
return new Promise((resolve, reject) => {
const startTime = Date.now()
const check = () => {
if (condition()) {
resolve()
} else if (Date.now() - startTime > timeout) {
reject(new Error(`Timeout waiting for condition`))
} else {
setTimeout(check, 100)
}
}
check()
})
}
@@ -0,0 +1,233 @@
import { describe, it, expect, beforeEach, vi } from 'vitest'
import { ElMessage } from 'element-plus'
import { handleApiError, ApiErrorHandler } from '@/utils/errorHandler'
vi.mock('element-plus', () => ({
ElMessage: {
error: vi.fn(),
success: vi.fn(),
},
}))
describe('errorHandler', () => {
beforeEach(() => {
vi.clearAllMocks()
vi.stubGlobal('localStorage', {
removeItem: vi.fn(),
})
vi.stubGlobal('window', {
location: { href: '' },
})
})
describe('handleApiError', () => {
it('should call ApiErrorHandler.handle', () => {
const mockError = { response: { status: 500, data: {} } }
const handleSpy = vi.spyOn(ApiErrorHandler, 'handle')
handleApiError(mockError)
expect(handleSpy).toHaveBeenCalledWith(mockError)
})
})
describe('ApiErrorHandler.handle', () => {
it('should handle network error', () => {
const mockError = new Error('Network Error')
const consoleSpy = vi.spyOn(console, 'error')
ApiErrorHandler.handle(mockError)
expect(ElMessage.error).toHaveBeenCalledWith('网络连接失败,请检查网络设置')
expect(consoleSpy).toHaveBeenCalledWith('Network Error:', mockError)
})
it('should handle 400 Bad Request', () => {
const mockError = {
response: {
status: 400,
data: { message: 'Invalid parameters' },
},
}
const consoleSpy = vi.spyOn(console, 'error')
ApiErrorHandler.handle(mockError)
expect(ElMessage.error).toHaveBeenCalledWith('Invalid parameters')
expect(consoleSpy).toHaveBeenCalledWith('Bad Request:', mockError.response.data)
})
it('should handle 401 Unauthorized', () => {
const mockError = {
response: {
status: 401,
data: { message: 'Unauthorized' },
},
}
const consoleSpy = vi.spyOn(console, 'error')
ApiErrorHandler.handle(mockError)
expect(ElMessage.error).toHaveBeenCalledWith('登录已过期,请重新登录')
expect(localStorage.removeItem).toHaveBeenCalledWith('token')
expect(window.location.href).toBe('/login')
expect(consoleSpy).toHaveBeenCalledWith('Unauthorized:', mockError.response.data)
})
it('should handle 403 Forbidden', () => {
const mockError = {
response: {
status: 403,
data: { message: 'Access denied' },
},
}
const consoleSpy = vi.spyOn(console, 'error')
ApiErrorHandler.handle(mockError)
expect(ElMessage.error).toHaveBeenCalledWith('没有权限访问该资源')
expect(consoleSpy).toHaveBeenCalledWith('Forbidden:', mockError.response.data)
})
it('should handle 404 Not Found', () => {
const mockError = {
response: {
status: 404,
data: { message: 'Resource not found' },
},
}
const consoleSpy = vi.spyOn(console, 'error')
ApiErrorHandler.handle(mockError)
expect(ElMessage.error).toHaveBeenCalledWith('Resource not found')
expect(consoleSpy).toHaveBeenCalledWith('Not Found:', mockError.response.data)
})
it('should handle 409 Conflict', () => {
const mockError = {
response: {
status: 409,
data: { message: 'Resource conflict' },
},
}
const consoleSpy = vi.spyOn(console, 'error')
ApiErrorHandler.handle(mockError)
expect(ElMessage.error).toHaveBeenCalledWith('Resource conflict')
expect(consoleSpy).toHaveBeenCalledWith('Conflict:', mockError.response.data)
})
it('should handle 422 Validation Error with details', () => {
const mockError = {
response: {
status: 422,
data: {
message: 'Validation failed',
details: {
username: 'Username is required',
password: 'Password is too short',
},
},
},
}
const consoleSpy = vi.spyOn(console, 'error')
ApiErrorHandler.handle(mockError)
expect(ElMessage.error).toHaveBeenCalledWith('Username is required、Password is too short')
expect(consoleSpy).toHaveBeenCalledWith('Validation Error:', mockError.response.data)
})
it('should handle 422 Validation Error without details', () => {
const mockError = {
response: {
status: 422,
data: { message: 'Validation failed' },
},
}
const consoleSpy = vi.spyOn(console, 'error')
ApiErrorHandler.handle(mockError)
expect(ElMessage.error).toHaveBeenCalledWith('Validation failed')
expect(consoleSpy).toHaveBeenCalledWith('Validation Error:', mockError.response.data)
})
it('should handle 500 Internal Server Error', () => {
const mockError = {
response: {
status: 500,
data: { message: 'Server error' },
},
}
const consoleSpy = vi.spyOn(console, 'error')
ApiErrorHandler.handle(mockError)
expect(ElMessage.error).toHaveBeenCalledWith('服务器内部错误,请稍后重试')
expect(consoleSpy).toHaveBeenCalledWith('Internal Server Error:', mockError.response.data)
})
it('should handle 502 Service Unavailable', () => {
const mockError = {
response: {
status: 502,
data: { message: 'Service unavailable' },
},
}
const consoleSpy = vi.spyOn(console, 'error')
ApiErrorHandler.handle(mockError)
expect(ElMessage.error).toHaveBeenCalledWith('服务暂时不可用,请稍后重试')
expect(consoleSpy).toHaveBeenCalledWith('Service Unavailable:', mockError.response.data)
})
it('should handle 503 Service Unavailable', () => {
const mockError = {
response: {
status: 503,
data: { message: 'Service unavailable' },
},
}
const consoleSpy = vi.spyOn(console, 'error')
ApiErrorHandler.handle(mockError)
expect(ElMessage.error).toHaveBeenCalledWith('服务暂时不可用,请稍后重试')
expect(consoleSpy).toHaveBeenCalledWith('Service Unavailable:', mockError.response.data)
})
it('should handle 504 Gateway Timeout', () => {
const mockError = {
response: {
status: 504,
data: { message: 'Gateway timeout' },
},
}
const consoleSpy = vi.spyOn(console, 'error')
ApiErrorHandler.handle(mockError)
expect(ElMessage.error).toHaveBeenCalledWith('服务暂时不可用,请稍后重试')
expect(consoleSpy).toHaveBeenCalledWith('Service Unavailable:', mockError.response.data)
})
it('should handle unknown status code', () => {
const mockError = {
response: {
status: 418,
data: { message: 'I am a teapot' },
},
}
const consoleSpy = vi.spyOn(console, 'error')
ApiErrorHandler.handle(mockError)
expect(ElMessage.error).toHaveBeenCalledWith('I am a teapot')
expect(consoleSpy).toHaveBeenCalledWith('Unknown Error:', mockError.response.data)
})
})
})