chore(test): 清理测试用例与类型导入
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
|
||||
import axios from 'axios'
|
||||
import type { AxiosInstance, InternalAxiosRequestConfig, AxiosResponse, AxiosHeaders } from 'axios'
|
||||
import type { AxiosInstance } from 'axios'
|
||||
|
||||
// 需要在导入 client 之前设置 mock
|
||||
vi.mock('@/i18n', () => ({
|
||||
|
||||
@@ -9,7 +9,6 @@ import { defineComponent, ref, onMounted, nextTick } from 'vue'
|
||||
|
||||
// Mock API
|
||||
const mockGetDashboardStats = vi.fn()
|
||||
const mockRefreshUser = vi.fn()
|
||||
|
||||
vi.mock('@/api', () => ({
|
||||
authAPI: {
|
||||
|
||||
@@ -96,10 +96,12 @@ describe('useClipboard', () => {
|
||||
})
|
||||
|
||||
it('Clipboard API 失败时降级到 fallback', async () => {
|
||||
;(navigator.clipboard.writeText as any).mockRejectedValue(new Error('API failed'))
|
||||
const writeTextMock = navigator.clipboard.writeText as any
|
||||
writeTextMock.mockRejectedValue(new Error('API failed'))
|
||||
|
||||
// jsdom 没有 execCommand,手动定义
|
||||
;(document as any).execCommand = vi.fn().mockReturnValue(true)
|
||||
const documentAny = document as any
|
||||
documentAny.execCommand = vi.fn().mockReturnValue(true)
|
||||
|
||||
const { copyToClipboard, copied } = useClipboard()
|
||||
const result = await copyToClipboard('fallback text')
|
||||
@@ -112,7 +114,8 @@ describe('useClipboard', () => {
|
||||
it('非安全上下文使用 fallback', async () => {
|
||||
Object.defineProperty(window, 'isSecureContext', { value: false, writable: true })
|
||||
|
||||
;(document as any).execCommand = vi.fn().mockReturnValue(true)
|
||||
const documentAny = document as any
|
||||
documentAny.execCommand = vi.fn().mockReturnValue(true)
|
||||
|
||||
const { copyToClipboard, copied } = useClipboard()
|
||||
const result = await copyToClipboard('insecure context text')
|
||||
@@ -124,8 +127,11 @@ describe('useClipboard', () => {
|
||||
})
|
||||
|
||||
it('所有复制方式均失败时调用 showError', async () => {
|
||||
;(navigator.clipboard.writeText as any).mockRejectedValue(new Error('fail'))
|
||||
;(document as any).execCommand = vi.fn().mockReturnValue(false)
|
||||
const writeTextMock = navigator.clipboard.writeText as any
|
||||
writeTextMock.mockRejectedValue(new Error('fail'))
|
||||
|
||||
const documentAny = document as any
|
||||
documentAny.execCommand = vi.fn().mockReturnValue(false)
|
||||
|
||||
const { copyToClipboard, copied } = useClipboard()
|
||||
const result = await copyToClipboard('text')
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
|
||||
import { useTableLoader } from '@/composables/useTableLoader'
|
||||
import { nextTick } from 'vue'
|
||||
|
||||
// Mock @vueuse/core 的 useDebounceFn
|
||||
vi.mock('@vueuse/core', () => ({
|
||||
@@ -212,7 +211,7 @@ describe('useTableLoader', () => {
|
||||
})
|
||||
})
|
||||
|
||||
const { load, items } = useTableLoader({ fetchFn })
|
||||
const { load } = useTableLoader({ fetchFn })
|
||||
|
||||
// 第一次加载
|
||||
const p1 = load()
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||
import { createRouter, createMemoryHistory } from 'vue-router'
|
||||
import { setActivePinia, createPinia } from 'pinia'
|
||||
import { defineComponent, h } from 'vue'
|
||||
|
||||
// Mock 导航加载状态
|
||||
vi.mock('@/composables/useNavigationLoading', () => {
|
||||
@@ -47,61 +45,6 @@ vi.mock('@/api/auth', () => ({
|
||||
getPublicSettings: vi.fn(),
|
||||
}))
|
||||
|
||||
const DummyComponent = defineComponent({
|
||||
render() {
|
||||
return h('div', 'dummy')
|
||||
},
|
||||
})
|
||||
|
||||
/**
|
||||
* 创建带守卫逻辑的测试路由
|
||||
* 模拟 router/index.ts 中的 beforeEach 守卫逻辑
|
||||
*/
|
||||
function createTestRouter() {
|
||||
const router = createRouter({
|
||||
history: createMemoryHistory(),
|
||||
routes: [
|
||||
{ path: '/login', component: DummyComponent, meta: { requiresAuth: false, title: 'Login' } },
|
||||
{
|
||||
path: '/register',
|
||||
component: DummyComponent,
|
||||
meta: { requiresAuth: false, title: 'Register' },
|
||||
},
|
||||
{ path: '/home', component: DummyComponent, meta: { requiresAuth: false, title: 'Home' } },
|
||||
{ path: '/dashboard', component: DummyComponent, meta: { title: 'Dashboard' } },
|
||||
{ path: '/keys', component: DummyComponent, meta: { title: 'API Keys' } },
|
||||
{ path: '/subscriptions', component: DummyComponent, meta: { title: 'Subscriptions' } },
|
||||
{ path: '/redeem', component: DummyComponent, meta: { title: 'Redeem' } },
|
||||
{
|
||||
path: '/admin/dashboard',
|
||||
component: DummyComponent,
|
||||
meta: { requiresAdmin: true, title: 'Admin Dashboard' },
|
||||
},
|
||||
{
|
||||
path: '/admin/users',
|
||||
component: DummyComponent,
|
||||
meta: { requiresAdmin: true, title: 'Admin Users' },
|
||||
},
|
||||
{
|
||||
path: '/admin/groups',
|
||||
component: DummyComponent,
|
||||
meta: { requiresAdmin: true, title: 'Admin Groups' },
|
||||
},
|
||||
{
|
||||
path: '/admin/subscriptions',
|
||||
component: DummyComponent,
|
||||
meta: { requiresAdmin: true, title: 'Admin Subscriptions' },
|
||||
},
|
||||
{
|
||||
path: '/admin/redeem',
|
||||
component: DummyComponent,
|
||||
meta: { requiresAdmin: true, title: 'Admin Redeem' },
|
||||
},
|
||||
],
|
||||
})
|
||||
|
||||
return router
|
||||
}
|
||||
|
||||
// 用于测试的 auth 状态
|
||||
interface MockAuthState {
|
||||
|
||||
@@ -250,7 +250,8 @@ describe('useAppStore', () => {
|
||||
|
||||
describe('公开设置加载', () => {
|
||||
it('从 window.__APP_CONFIG__ 初始化', () => {
|
||||
;(window as any).__APP_CONFIG__ = {
|
||||
const windowAny = window as any
|
||||
windowAny.__APP_CONFIG__ = {
|
||||
site_name: 'TestSite',
|
||||
site_logo: '/logo.png',
|
||||
version: '1.0.0',
|
||||
@@ -278,7 +279,8 @@ describe('useAppStore', () => {
|
||||
})
|
||||
|
||||
it('clearPublicSettingsCache 清除缓存', () => {
|
||||
;(window as any).__APP_CONFIG__ = { site_name: 'Test' }
|
||||
const windowAny = window as any
|
||||
windowAny.__APP_CONFIG__ = { site_name: 'Test' }
|
||||
const store = useAppStore()
|
||||
store.initFromInjectedConfig()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user