/** * Tests for cfc-frontend store/index.js (Vuex store) */ import store from '@/store/index' beforeEach(() => { global.uni._storage = {} global.uni.request.mockReset() global.uni.showToast.mockReset() }) describe('Vuex Store', () => { // --------------- initial state --------------- test('has correct initial state', () => { expect(store.state.token).toBe('') expect(store.state.userId).toBe('') expect(store.state.role).toBe('parent') expect(store.state.currentRole).toBe('parent') expect(store.state.familyId).toBe('') expect(store.state.nickname).toBe('') expect(store.state.currentChildId).toBe('') expect(store.state.children).toEqual([]) expect(store.state.isSwitchedChild).toBe(false) }) // --------------- mutations --------------- describe('mutations', () => { beforeEach(() => { // reset storage between tests global.uni._storage = {} }) test('setToken stores token in state and storage', () => { store.commit('setToken', 'jwt-token') expect(store.state.token).toBe('jwt-token') expect(global.uni._storage.token).toBe('jwt-token') }) test('setUserInfo sets user fields and saves to storage', () => { const userInfo = { userId: '100', role: 'parent', familyId: '1', nickname: '测试家长' } store.commit('setUserInfo', userInfo) expect(store.state.userId).toBe('100') expect(store.state.role).toBe('parent') expect(store.state.currentRole).toBe('parent') expect(store.state.familyId).toBe('1') expect(store.state.nickname).toBe('测试家长') expect(store.state.isSwitchedChild).toBe(false) // Verify storage sync expect(global.uni._storage.userId).toBe('100') expect(global.uni._storage.role).toBe('parent') expect(global.uni._storage.familyId).toBe('1') expect(global.uni._storage.isSwitchedChild).toBe(false) }) test('setUserInfo clears switched-child flag', () => { global.uni._storage.isSwitchedChild = true store.commit('setUserInfo', { userId: '100', role: 'parent', familyId: '1', nickname: 'test' }) expect(store.state.isSwitchedChild).toBe(false) }) test('setChildren stores children and sets currentChildId', () => { const children = [{ id: 5, name: '小明' }, { id: 6, name: '小红' }] store.commit('setChildren', children) expect(store.state.children).toEqual(children) expect(store.state.currentChildId).toBe(5) }) test('setCurrentChild updates currentChildId in state and storage', () => { store.commit('setCurrentChild', 5) expect(store.state.currentChildId).toBe(5) expect(global.uni._storage.currentChildId).toBe(5) }) test('switchToChild sets currentRole to child and marks switched', () => { store.commit('switchToChild', 7) expect(store.state.currentRole).toBe('child') expect(store.state.currentChildId).toBe(7) expect(store.state.isSwitchedChild).toBe(true) expect(global.uni._storage.currentRole).toBe('child') expect(global.uni._storage.currentChildId).toBe(7) expect(global.uni._storage.isSwitchedChild).toBe(true) }) test('switchBackToParent resets to parent role', () => { // First switch to child store.commit('switchToChild', 7) expect(store.state.currentRole).toBe('child') // Then switch back store.commit('switchBackToParent') expect(store.state.currentRole).toBe('parent') expect(store.state.currentChildId).toBe('') expect(store.state.isSwitchedChild).toBe(false) expect(global.uni._storage.currentRole).toBe('parent') expect(global.uni._storage.currentChildId).toBeUndefined() expect(global.uni._storage.isSwitchedChild).toBe(false) }) test('switchViewMode changes currentRole', () => { store.commit('switchViewMode', 'teacher') expect(store.state.currentRole).toBe('teacher') expect(global.uni._storage.currentRole).toBe('teacher') }) test('switchRole (compat) changes currentRole', () => { store.commit('switchRole', 'admin') expect(store.state.currentRole).toBe('admin') expect(global.uni._storage.currentRole).toBe('admin') }) test('logout clears all state and storage', () => { // Set up some state store.commit('setToken', 'jwt') store.commit('setUserInfo', { userId: '100', role: 'parent', familyId: '1', nickname: 'test' }) store.commit('setChildren', [{ id: 5, name: '小明' }]) store.commit('switchToChild', 7) // Logout store.commit('logout') // State is reset expect(store.state.token).toBe('') expect(store.state.userId).toBe('') expect(store.state.role).toBe('parent') expect(store.state.currentRole).toBe('parent') expect(store.state.familyId).toBe('') expect(store.state.nickname).toBe('') expect(store.state.currentChildId).toBe('') expect(store.state.children).toEqual([]) expect(store.state.isSwitchedChild).toBe(false) // Storage is cleared expect(global.uni._storage.token).toBeUndefined() expect(global.uni._storage.currentRole).toBeUndefined() expect(global.uni._storage.isSwitchedChild).toBeUndefined() }) }) // --------------- actions --------------- describe('actions', () => { beforeEach(() => { global.uni._storage = {} // Reset state store.commit('logout') }) test('login dispatches setToken and setUserInfo', () => { const userInfo = { token: 'jwt-token', userId: '100', role: 'parent', familyId: '1', nickname: '测试家长' } store.dispatch('login', userInfo) expect(store.state.token).toBe('jwt-token') expect(store.state.userId).toBe('100') expect(store.state.role).toBe('parent') expect(store.state.nickname).toBe('测试家长') }) test('updateChildren dispatches setChildren', () => { const children = [{ id: 1, name: '孩子1' }] store.dispatch('updateChildren', children) expect(store.state.children).toEqual(children) }) }) })