44 lines
1.2 KiB
TypeScript
44 lines
1.2 KiB
TypeScript
import { createPinia, setActivePinia } from 'pinia'
|
||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||
|
||
import { useUserStore } from '@/stores/user'
|
||
|
||
// Mock @vueuse/core 的 useLocalStorage,用 Vue 的 ref 替代以避免 localStorage 副作用
|
||
vi.mock('@vueuse/core', async () => {
|
||
const { ref } = await import('vue')
|
||
return {
|
||
useLocalStorage: (_key: string, defaultValue: unknown) => ref(defaultValue),
|
||
}
|
||
})
|
||
|
||
describe('useUserStore — Token 管理', () => {
|
||
beforeEach(() => {
|
||
setActivePinia(createPinia())
|
||
})
|
||
|
||
it('setToken 应正确设置 token 值', () => {
|
||
const store = useUserStore()
|
||
store.setToken('abc123')
|
||
expect(store.token).toBe('abc123')
|
||
})
|
||
|
||
it('setRToken 应正确设置 refreshToken 值', () => {
|
||
const store = useUserStore()
|
||
store.setRToken('refresh_xyz')
|
||
expect(store.rtoken).toBe('refresh_xyz')
|
||
})
|
||
|
||
it('setRTokenTime 应正确设置刷新时间戳', () => {
|
||
const store = useUserStore()
|
||
const timestamp = Date.now()
|
||
store.setRTokenTime(timestamp)
|
||
expect(store.rtokenTime).toBe(timestamp)
|
||
})
|
||
|
||
it('setDeviceType 应正确设置设备类型', () => {
|
||
const store = useUserStore()
|
||
store.setDeviceType('mobile')
|
||
expect(store.deviceType).toBe('mobile')
|
||
})
|
||
})
|