import { describe, it, expect, beforeEach, vi } from 'vitest' // mock storage:内存 Map 语义,与 Taro.setStorageSync/getStorageSync 等价 const store = new Map() vi.mock('@/utils/storage', () => ({ storage: { get: (key: string): T | null => (store.has(key) ? (store.get(key) as T) : null), set: (key: string, value: T): void => { store.set(key, value) }, remove: (key: string): void => { store.delete(key) } } })) import { getFamilies, getFamilyDetail, addFamily, joinFamily, canAddCared, addCared, getCaredByFamily, type CaredPerson } from '@/api/family' import { FAMILY_DATA_STORAGE_KEY } from '@/constants/family' const caredInput = (over: Partial = {}): Omit => ({ familyId: '735201884550361', name: '王奶奶', idNumber: '440301194001010000', address: '深圳市南山区', contacts: [], ...over }) beforeEach(() => { store.clear() }) describe('家庭圈 mock 数据层', () => { it('首次调用 getFamilies 写入样例并返回 4 条', async () => { const list = await getFamilies() expect(list).toHaveLength(4) expect(store.has(FAMILY_DATA_STORAGE_KEY)).toBe(true) }) it('addFamily 追加家庭且持久化,caredCount 从 0 开始', async () => { await getFamilies() const f = await addFamily('测试家庭') expect(f.name).toBe('测试家庭') expect(f.caredCount).toBe(0) const again = await getFamilies() expect(again).toHaveLength(5) expect(again.some((x) => x.id === f.id)).toBe(true) }) it('addFamily 空名抛错', async () => { await getFamilies() await expect(addFamily(' ')).rejects.toThrow('请输入家庭名') }) it('joinFamily 按编号生成新家庭', async () => { await getFamilies() const f = await joinFamily('1234567890') expect(f.name).toBe('加入的 7890') const again = await getFamilies() expect(again).toHaveLength(5) }) it('getFamilyDetail 返回家庭 + 成员 + 关爱对象', async () => { await getFamilies() const detail = await getFamilyDetail('846443196293189') expect(detail).not.toBeNull() expect(detail!.family.name).toBe('JollyPlace') expect(detail!.members.length).toBe(2) expect(detail!.cared.length).toBe(1) expect(detail!.cared[0].name).toBe('Jay') }) it('getFamilyDetail 不存在返回 null', async () => { await getFamilies() expect(await getFamilyDetail('nope')).toBeNull() }) it('canAddCared 未达上限 true,加满后 false', async () => { await getFamilies() // JollyPlace 已有 Jay(1/2),仍未达上限 expect(await canAddCared('846443196293189')).toBe(true) expect(await canAddCared('735201884550361')).toBe(true) await addCared(caredInput()) await addCared(caredInput({ name: '李爷爷' })) expect(await canAddCared('735201884550361')).toBe(false) }) it('addCared 写入并同步家庭 caredCount/caredLabel', async () => { await getFamilies() const saved = await addCared(caredInput()) expect(saved.id).toBeTruthy() const detail = await getFamilyDetail('735201884550361') expect(detail).not.toBeNull() expect(detail!.family.caredCount).toBe(1) expect(detail!.family.caredLabel).toBe('王奶奶') expect(detail!.cared[0].contacts).toEqual([]) }) it('addCared 达上限抛错', async () => { await getFamilies() await addCared(caredInput()) await addCared(caredInput({ name: '李爷爷' })) await expect(addCared(caredInput({ name: '第三人' }))).rejects.toThrow('已达上限') }) it('getCaredByFamily 返回该家庭的关爱对象数组', async () => { await getFamilies() const list = await getCaredByFamily('846443196293189') expect(list.map((c) => c.name)).toEqual(['Jay']) }) })