| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- /**
- * 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)
- })
- })
- })
|