Files
test/tests/unit/stores/userStore.test.ts
2026-04-08 21:26:18 +08:00

44 lines
1.2 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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')
})
})