store.test.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /**
  2. * Tests for cfc-frontend store/index.js (Vuex store)
  3. */
  4. import store from '@/store/index'
  5. beforeEach(() => {
  6. global.uni._storage = {}
  7. global.uni.request.mockReset()
  8. global.uni.showToast.mockReset()
  9. })
  10. describe('Vuex Store', () => {
  11. // --------------- initial state ---------------
  12. test('has correct initial state', () => {
  13. expect(store.state.token).toBe('')
  14. expect(store.state.userId).toBe('')
  15. expect(store.state.role).toBe('parent')
  16. expect(store.state.currentRole).toBe('parent')
  17. expect(store.state.familyId).toBe('')
  18. expect(store.state.nickname).toBe('')
  19. expect(store.state.currentChildId).toBe('')
  20. expect(store.state.children).toEqual([])
  21. expect(store.state.isSwitchedChild).toBe(false)
  22. })
  23. // --------------- mutations ---------------
  24. describe('mutations', () => {
  25. beforeEach(() => {
  26. // reset storage between tests
  27. global.uni._storage = {}
  28. })
  29. test('setToken stores token in state and storage', () => {
  30. store.commit('setToken', 'jwt-token')
  31. expect(store.state.token).toBe('jwt-token')
  32. expect(global.uni._storage.token).toBe('jwt-token')
  33. })
  34. test('setUserInfo sets user fields and saves to storage', () => {
  35. const userInfo = {
  36. userId: '100',
  37. role: 'parent',
  38. familyId: '1',
  39. nickname: '测试家长'
  40. }
  41. store.commit('setUserInfo', userInfo)
  42. expect(store.state.userId).toBe('100')
  43. expect(store.state.role).toBe('parent')
  44. expect(store.state.currentRole).toBe('parent')
  45. expect(store.state.familyId).toBe('1')
  46. expect(store.state.nickname).toBe('测试家长')
  47. expect(store.state.isSwitchedChild).toBe(false)
  48. // Verify storage sync
  49. // 注意:familyId 的持久化由 setFamilyId 负责(见下一个用例),
  50. // setUserInfo 只负责 state.familyId,不写 storage
  51. expect(global.uni._storage.userId).toBe('100')
  52. expect(global.uni._storage.role).toBe('parent')
  53. expect(global.uni._storage.isSwitchedChild).toBe(false)
  54. })
  55. test('setFamilyId persists familyId to storage for cold start', () => {
  56. store.commit('setFamilyId', '42')
  57. expect(store.state.familyId).toBe('42')
  58. expect(global.uni._storage.familyId).toBe('42')
  59. // 无家庭时写入空串而非 undefined,保证冷启动 getStorageSync 能取到键
  60. store.commit('setFamilyId', '')
  61. expect(store.state.familyId).toBe('')
  62. expect(global.uni._storage.familyId).toBe('')
  63. })
  64. test('setUserInfo clears switched-child flag', () => {
  65. global.uni._storage.isSwitchedChild = true
  66. store.commit('setUserInfo', {
  67. userId: '100',
  68. role: 'parent',
  69. familyId: '1',
  70. nickname: 'test'
  71. })
  72. expect(store.state.isSwitchedChild).toBe(false)
  73. })
  74. test('setChildren stores children and sets currentChildId', () => {
  75. const children = [{ id: 5, name: '小明' }, { id: 6, name: '小红' }]
  76. store.commit('setChildren', children)
  77. expect(store.state.children).toEqual(children)
  78. expect(store.state.currentChildId).toBe(5)
  79. })
  80. test('setCurrentChild updates currentChildId in state and storage', () => {
  81. store.commit('setCurrentChild', 5)
  82. expect(store.state.currentChildId).toBe(5)
  83. expect(global.uni._storage.currentChildId).toBe(5)
  84. })
  85. test('switchToChild sets currentRole to child and marks switched', () => {
  86. store.commit('switchToChild', 7)
  87. expect(store.state.currentRole).toBe('child')
  88. expect(store.state.currentChildId).toBe(7)
  89. expect(store.state.isSwitchedChild).toBe(true)
  90. expect(global.uni._storage.currentRole).toBe('child')
  91. expect(global.uni._storage.currentChildId).toBe(7)
  92. expect(global.uni._storage.isSwitchedChild).toBe(true)
  93. })
  94. test('switchBackToParent resets to parent role', () => {
  95. // First switch to child
  96. store.commit('switchToChild', 7)
  97. expect(store.state.currentRole).toBe('child')
  98. // Then switch back
  99. store.commit('switchBackToParent')
  100. expect(store.state.currentRole).toBe('parent')
  101. expect(store.state.currentChildId).toBe('')
  102. expect(store.state.isSwitchedChild).toBe(false)
  103. expect(global.uni._storage.currentRole).toBe('parent')
  104. expect(global.uni._storage.currentChildId).toBeUndefined()
  105. expect(global.uni._storage.isSwitchedChild).toBe(false)
  106. })
  107. test('switchViewMode changes currentRole', () => {
  108. store.commit('switchViewMode', 'teacher')
  109. expect(store.state.currentRole).toBe('teacher')
  110. expect(global.uni._storage.currentRole).toBe('teacher')
  111. })
  112. test('switchRole (compat) changes currentRole', () => {
  113. store.commit('switchRole', 'admin')
  114. expect(store.state.currentRole).toBe('admin')
  115. expect(global.uni._storage.currentRole).toBe('admin')
  116. })
  117. test('logout clears all state and storage', () => {
  118. // Set up some state
  119. store.commit('setToken', 'jwt')
  120. store.commit('setUserInfo', {
  121. userId: '100',
  122. role: 'parent',
  123. familyId: '1',
  124. nickname: 'test'
  125. })
  126. store.commit('setChildren', [{ id: 5, name: '小明' }])
  127. store.commit('switchToChild', 7)
  128. // Logout
  129. store.commit('logout')
  130. // State is reset
  131. expect(store.state.token).toBe('')
  132. expect(store.state.userId).toBe('')
  133. expect(store.state.role).toBe('parent')
  134. expect(store.state.currentRole).toBe('parent')
  135. expect(store.state.familyId).toBe('')
  136. expect(store.state.nickname).toBe('')
  137. expect(store.state.currentChildId).toBe('')
  138. expect(store.state.children).toEqual([])
  139. expect(store.state.isSwitchedChild).toBe(false)
  140. // Storage is cleared
  141. expect(global.uni._storage.token).toBeUndefined()
  142. expect(global.uni._storage.currentRole).toBeUndefined()
  143. expect(global.uni._storage.isSwitchedChild).toBeUndefined()
  144. })
  145. })
  146. // --------------- actions ---------------
  147. describe('actions', () => {
  148. beforeEach(() => {
  149. global.uni._storage = {}
  150. // Reset state
  151. store.commit('logout')
  152. })
  153. test('login dispatches setToken and setUserInfo', () => {
  154. const userInfo = {
  155. token: 'jwt-token',
  156. userId: '100',
  157. role: 'parent',
  158. familyId: '1',
  159. nickname: '测试家长'
  160. }
  161. store.dispatch('login', userInfo)
  162. expect(store.state.token).toBe('jwt-token')
  163. expect(store.state.userId).toBe('100')
  164. expect(store.state.role).toBe('parent')
  165. expect(store.state.nickname).toBe('测试家长')
  166. })
  167. test('updateChildren dispatches setChildren', () => {
  168. const children = [{ id: 1, name: '孩子1' }]
  169. store.dispatch('updateChildren', children)
  170. expect(store.state.children).toEqual(children)
  171. })
  172. })
  173. })