You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
104 lines
2.9 KiB
104 lines
2.9 KiB
|
2 months ago
|
/**
|
||
|
|
* 前端单元测试套件
|
||
|
|
*
|
||
|
|
* 使用 Vitest + Vue Test Utils 进行测试
|
||
|
|
*/
|
||
|
|
|
||
|
|
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||
|
|
import { mount, flushPromises } from '@vue/test-utils'
|
||
|
|
import { createPinia, setActivePinia } from 'pinia'
|
||
|
|
|
||
|
|
// ==================== 用户 Store 测试 ====================
|
||
|
|
|
||
|
|
describe('User Store', () => {
|
||
|
|
let pinia
|
||
|
|
|
||
|
|
beforeEach(() => {
|
||
|
|
pinia = createPinia()
|
||
|
|
setActivePinia(pinia)
|
||
|
|
localStorage.clear()
|
||
|
|
})
|
||
|
|
|
||
|
|
it('should initialize with empty token', async () => {
|
||
|
|
const { useUserStore } = await import('@/stores/user.js')
|
||
|
|
const userStore = useUserStore(pinia)
|
||
|
|
|
||
|
|
expect(userStore.token).toBe('')
|
||
|
|
expect(userStore.isLoggedIn).toBe(false)
|
||
|
|
})
|
||
|
|
|
||
|
|
it('should initialize with token from localStorage', async () => {
|
||
|
|
localStorage.setItem('access_token', 'test_token_123')
|
||
|
|
|
||
|
|
const { useUserStore } = await import('@/stores/user.js')
|
||
|
|
const store = useUserStore(pinia)
|
||
|
|
|
||
|
|
expect(store.token).toBe('test_token_123')
|
||
|
|
})
|
||
|
|
|
||
|
|
it('should logout and clear tokens', async () => {
|
||
|
|
localStorage.setItem('access_token', 'test_token')
|
||
|
|
localStorage.setItem('refresh_token', 'test_refresh')
|
||
|
|
|
||
|
|
const { useUserStore } = await import('@/stores/user.js')
|
||
|
|
const store = useUserStore(pinia)
|
||
|
|
|
||
|
|
store.logout()
|
||
|
|
|
||
|
|
expect(store.token).toBe('')
|
||
|
|
expect(localStorage.getItem('access_token')).toBeNull()
|
||
|
|
})
|
||
|
|
})
|
||
|
|
|
||
|
|
// ==================== 工具函数测试 ====================
|
||
|
|
|
||
|
|
describe('Utility Functions', () => {
|
||
|
|
it('should format volume correctly', () => {
|
||
|
|
const formatVolume = (volume) => {
|
||
|
|
if (volume >= 100000000) {
|
||
|
|
return (volume / 100000000).toFixed(2) + '亿'
|
||
|
|
} else if (volume >= 10000) {
|
||
|
|
return (volume / 10000).toFixed(2) + '万'
|
||
|
|
}
|
||
|
|
return volume.toString()
|
||
|
|
}
|
||
|
|
|
||
|
|
expect(formatVolume(150000000)).toBe('1.50 亿')
|
||
|
|
expect(formatVolume(150000)).toBe('15.00 万')
|
||
|
|
expect(formatVolume(1500)).toBe('1500')
|
||
|
|
})
|
||
|
|
|
||
|
|
it('should format price correctly', () => {
|
||
|
|
const formatPrice = (price) => {
|
||
|
|
return price.toFixed(2)
|
||
|
|
}
|
||
|
|
|
||
|
|
expect(formatPrice(4000.5)).toBe('4000.50')
|
||
|
|
expect(formatPrice(4000)).toBe('4000.00')
|
||
|
|
})
|
||
|
|
|
||
|
|
it('should calculate price change correctly', () => {
|
||
|
|
const calculateChange = (current, previous) => {
|
||
|
|
return ((current - previous) / previous * 100).toFixed(2)
|
||
|
|
}
|
||
|
|
|
||
|
|
expect(calculateChange(4100, 4000)).toBe('2.50')
|
||
|
|
expect(calculateChange(3900, 4000)).toBe('-2.50')
|
||
|
|
})
|
||
|
|
})
|
||
|
|
|
||
|
|
// ==================== API 客户端测试 ====================
|
||
|
|
|
||
|
|
describe('API Client', () => {
|
||
|
|
it('should include auth token in requests', () => {
|
||
|
|
localStorage.setItem('access_token', 'test_token_123')
|
||
|
|
expect(localStorage.getItem('access_token')).toBe('test_token_123')
|
||
|
|
})
|
||
|
|
|
||
|
|
it('should handle 401 errors', () => {
|
||
|
|
localStorage.removeItem('access_token')
|
||
|
|
localStorage.removeItem('refresh_token')
|
||
|
|
expect(localStorage.getItem('access_token')).toBeNull()
|
||
|
|
})
|
||
|
|
})
|