修改页面
This commit is contained in:
@@ -0,0 +1,140 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||
|
||||
// Mock AsyncStorage
|
||||
const storage: Record<string, string> = {}
|
||||
vi.mock('@react-native-async-storage/async-storage', () => ({
|
||||
default: {
|
||||
getItem: vi.fn((key: string) => Promise.resolve(storage[key] || null)),
|
||||
setItem: vi.fn((key: string, val: string) => { storage[key] = val; return Promise.resolve() }),
|
||||
removeItem: vi.fn((key: string) => { delete storage[key]; return Promise.resolve() }),
|
||||
multiRemove: vi.fn((keys: string[]) => { keys.forEach(k => delete storage[k]); return Promise.resolve() }),
|
||||
},
|
||||
}))
|
||||
|
||||
beforeEach(() => {
|
||||
Object.keys(storage).forEach(k => delete storage[k])
|
||||
})
|
||||
|
||||
// ========== API Service ==========
|
||||
describe('Mobile API Service', () => {
|
||||
it('should export api methods', async () => {
|
||||
const mod = await import('../services/api')
|
||||
expect(mod.api).toBeDefined()
|
||||
expect(typeof mod.api.get).toBe('function')
|
||||
expect(typeof mod.api.post).toBe('function')
|
||||
expect(typeof mod.api.put).toBe('function')
|
||||
expect(typeof mod.api.del).toBe('function')
|
||||
})
|
||||
|
||||
it('should export auth helpers', async () => {
|
||||
const mod = await import('../services/api')
|
||||
expect(typeof mod.saveToken).toBe('function')
|
||||
expect(typeof mod.getStoredUser).toBe('function')
|
||||
expect(typeof mod.getStoredToken).toBe('function')
|
||||
expect(typeof mod.clearAuth).toBe('function')
|
||||
})
|
||||
|
||||
it('saveToken + getStoredUser should persist auth', async () => {
|
||||
const { saveToken, getStoredUser, getStoredToken } = await import('../services/api')
|
||||
await saveToken('test-token', { id: '1', nickname: 'Tester', points: 100 })
|
||||
|
||||
const token = await getStoredToken()
|
||||
expect(token).toBe('test-token')
|
||||
|
||||
const user = await getStoredUser()
|
||||
expect(user.nickname).toBe('Tester')
|
||||
expect(user.points).toBe(100)
|
||||
})
|
||||
|
||||
it('clearAuth should remove all auth data', async () => {
|
||||
const { saveToken, clearAuth, getStoredToken, getStoredUser } = await import('../services/api')
|
||||
await saveToken('t', { id: '1', nickname: 'T' })
|
||||
await clearAuth()
|
||||
|
||||
expect(await getStoredToken()).toBeNull()
|
||||
expect(await getStoredUser()).toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
// ========== AuthProvider ==========
|
||||
describe('AuthProvider', () => {
|
||||
it('should export AuthProvider and useAuth', async () => {
|
||||
const mod = await import('../components/AuthProvider')
|
||||
expect(mod.AuthProvider).toBeDefined()
|
||||
expect(typeof mod.useAuth).toBe('function')
|
||||
})
|
||||
})
|
||||
|
||||
// ========== CheckIn Module ==========
|
||||
describe('CheckIn Module', () => {
|
||||
it('should export API functions', async () => {
|
||||
const mod = await import('../modules/checkin/index')
|
||||
expect(typeof mod.createItem).toBe('function')
|
||||
expect(typeof mod.listItems).toBe('function')
|
||||
expect(typeof mod.getTodayStatus).toBe('function')
|
||||
expect(typeof mod.getStreak).toBe('function')
|
||||
expect(typeof mod.getRecords).toBe('function')
|
||||
})
|
||||
|
||||
it('should export hooks', async () => {
|
||||
const mod = await import('../modules/checkin/index')
|
||||
expect(typeof mod.useCheckInItems).toBe('function')
|
||||
expect(typeof mod.useCheckInStatus).toBe('function')
|
||||
expect(typeof mod.useCheckInDetail).toBe('function')
|
||||
})
|
||||
|
||||
it('should export category constants', async () => {
|
||||
const mod = await import('../modules/checkin/index')
|
||||
expect(mod.CATEGORY_CONFIG.study.label).toBe('学习成长')
|
||||
expect(mod.CATEGORY_CONFIG.health.icon).toBe('❤️')
|
||||
expect(mod.CATEGORY_CONFIG.goal.color).toBe('#FF6B35')
|
||||
expect(mod.CYCLE_LABEL.daily).toBe('每日')
|
||||
})
|
||||
})
|
||||
|
||||
// ========== Social Module ==========
|
||||
describe('Social Module', () => {
|
||||
it('should export API functions', async () => {
|
||||
const mod = await import('../modules/social/index')
|
||||
expect(typeof mod.createPost).toBe('function')
|
||||
expect(typeof mod.getFeed).toBe('function')
|
||||
expect(typeof mod.likePost).toBe('function')
|
||||
expect(typeof mod.listFriends).toBe('function')
|
||||
expect(typeof mod.sendFriendRequest).toBe('function')
|
||||
})
|
||||
|
||||
it('should export hooks', async () => {
|
||||
const mod = await import('../modules/social/index')
|
||||
expect(typeof mod.useFeed).toBe('function')
|
||||
expect(typeof mod.useFriends).toBe('function')
|
||||
})
|
||||
})
|
||||
|
||||
// ========== Profile Module ==========
|
||||
describe('Profile Module', () => {
|
||||
it('should export API functions', async () => {
|
||||
const mod = await import('../modules/profile/index')
|
||||
expect(typeof mod.getAchievements).toBe('function')
|
||||
expect(typeof mod.updateProfile).toBe('function')
|
||||
})
|
||||
|
||||
it('should export hooks', async () => {
|
||||
const mod = await import('../modules/profile/index')
|
||||
expect(typeof mod.useProfile).toBe('function')
|
||||
expect(typeof mod.useAchievements).toBe('function')
|
||||
})
|
||||
})
|
||||
|
||||
// ========== Discover Module ==========
|
||||
describe('Discover Module', () => {
|
||||
it('should export API functions', async () => {
|
||||
const mod = await import('../modules/discover/index')
|
||||
expect(typeof mod.getLeaderboard).toBe('function')
|
||||
})
|
||||
|
||||
it('should export hooks', async () => {
|
||||
const mod = await import('../modules/discover/index')
|
||||
expect(typeof mod.useLeaderboard).toBe('function')
|
||||
expect(typeof mod.useDiscover).toBe('function')
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,39 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||
|
||||
// Mock AsyncStorage
|
||||
vi.mock('@react-native-async-storage/async-storage', () => ({
|
||||
default: {
|
||||
getItem: vi.fn(() => Promise.resolve(null)),
|
||||
setItem: vi.fn(() => Promise.resolve()),
|
||||
removeItem: vi.fn(() => Promise.resolve()),
|
||||
multiRemove: vi.fn(() => Promise.resolve()),
|
||||
},
|
||||
}))
|
||||
|
||||
describe('Mobile API Service', () => {
|
||||
beforeEach(() => {
|
||||
vi.resetAllMocks()
|
||||
})
|
||||
|
||||
it('should export api with get/post/put/del methods', async () => {
|
||||
const mod = await import('../services/api')
|
||||
expect(mod.api).toBeDefined()
|
||||
expect(typeof mod.api.get).toBe('function')
|
||||
expect(typeof mod.api.post).toBe('function')
|
||||
expect(typeof mod.api.put).toBe('function')
|
||||
expect(typeof mod.api.del).toBe('function')
|
||||
})
|
||||
|
||||
it('should export auth helper functions', async () => {
|
||||
const mod = await import('../services/api')
|
||||
expect(typeof mod.saveToken).toBe('function')
|
||||
expect(typeof mod.getStoredUser).toBe('function')
|
||||
expect(typeof mod.clearAuth).toBe('function')
|
||||
})
|
||||
|
||||
it('should export AuthProvider and useAuth', async () => {
|
||||
const mod = await import('../components/AuthProvider')
|
||||
expect(mod.AuthProvider).toBeDefined()
|
||||
expect(typeof mod.useAuth).toBe('function')
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user