store.test.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. expect(global.uni._storage.userId).toBe('100')
  50. expect(global.uni._storage.role).toBe('parent')
  51. expect(global.uni._storage.familyId).toBe('1')
  52. expect(global.uni._storage.isSwitchedChild).toBe(false)
  53. })
  54. test('setUserInfo clears switched-child flag', () => {
  55. global.uni._storage.isSwitchedChild = true
  56. store.commit('setUserInfo', {
  57. userId: '100',
  58. role: 'parent',
  59. familyId: '1',
  60. nickname: 'test'
  61. })
  62. expect(store.state.isSwitchedChild).toBe(false)
  63. })
  64. test('setChildren stores children and sets currentChildId', () => {
  65. const children = [{ id: 5, name: '小明' }, { id: 6, name: '小红' }]
  66. store.commit('setChildren', children)
  67. expect(store.state.children).toEqual(children)
  68. expect(store.state.currentChildId).toBe(5)
  69. })
  70. test('setCurrentChild updates currentChildId in state and storage', () => {
  71. store.commit('setCurrentChild', 5)
  72. expect(store.state.currentChildId).toBe(5)
  73. expect(global.uni._storage.currentChildId).toBe(5)
  74. })
  75. test('switchToChild sets currentRole to child and marks switched', () => {
  76. store.commit('switchToChild', 7)
  77. expect(store.state.currentRole).toBe('child')
  78. expect(store.state.currentChildId).toBe(7)
  79. expect(store.state.isSwitchedChild).toBe(true)
  80. expect(global.uni._storage.currentRole).toBe('child')
  81. expect(global.uni._storage.currentChildId).toBe(7)
  82. expect(global.uni._storage.isSwitchedChild).toBe(true)
  83. })
  84. test('switchBackToParent resets to parent role', () => {
  85. // First switch to child
  86. store.commit('switchToChild', 7)
  87. expect(store.state.currentRole).toBe('child')
  88. // Then switch back
  89. store.commit('switchBackToParent')
  90. expect(store.state.currentRole).toBe('parent')
  91. expect(store.state.currentChildId).toBe('')
  92. expect(store.state.isSwitchedChild).toBe(false)
  93. expect(global.uni._storage.currentRole).toBe('parent')
  94. expect(global.uni._storage.currentChildId).toBeUndefined()
  95. expect(global.uni._storage.isSwitchedChild).toBe(false)
  96. })
  97. test('switchViewMode changes currentRole', () => {
  98. store.commit('switchViewMode', 'teacher')
  99. expect(store.state.currentRole).toBe('teacher')
  100. expect(global.uni._storage.currentRole).toBe('teacher')
  101. })
  102. test('switchRole (compat) changes currentRole', () => {
  103. store.commit('switchRole', 'admin')
  104. expect(store.state.currentRole).toBe('admin')
  105. expect(global.uni._storage.currentRole).toBe('admin')
  106. })
  107. test('logout clears all state and storage', () => {
  108. // Set up some state
  109. store.commit('setToken', 'jwt')
  110. store.commit('setUserInfo', {
  111. userId: '100',
  112. role: 'parent',
  113. familyId: '1',
  114. nickname: 'test'
  115. })
  116. store.commit('setChildren', [{ id: 5, name: '小明' }])
  117. store.commit('switchToChild', 7)
  118. // Logout
  119. store.commit('logout')
  120. // State is reset
  121. expect(store.state.token).toBe('')
  122. expect(store.state.userId).toBe('')
  123. expect(store.state.role).toBe('parent')
  124. expect(store.state.currentRole).toBe('parent')
  125. expect(store.state.familyId).toBe('')
  126. expect(store.state.nickname).toBe('')
  127. expect(store.state.currentChildId).toBe('')
  128. expect(store.state.children).toEqual([])
  129. expect(store.state.isSwitchedChild).toBe(false)
  130. // Storage is cleared
  131. expect(global.uni._storage.token).toBeUndefined()
  132. expect(global.uni._storage.currentRole).toBeUndefined()
  133. expect(global.uni._storage.isSwitchedChild).toBeUndefined()
  134. })
  135. })
  136. // --------------- actions ---------------
  137. describe('actions', () => {
  138. beforeEach(() => {
  139. global.uni._storage = {}
  140. // Reset state
  141. store.commit('logout')
  142. })
  143. test('login dispatches setToken and setUserInfo', () => {
  144. const userInfo = {
  145. token: 'jwt-token',
  146. userId: '100',
  147. role: 'parent',
  148. familyId: '1',
  149. nickname: '测试家长'
  150. }
  151. store.dispatch('login', userInfo)
  152. expect(store.state.token).toBe('jwt-token')
  153. expect(store.state.userId).toBe('100')
  154. expect(store.state.role).toBe('parent')
  155. expect(store.state.nickname).toBe('测试家长')
  156. })
  157. test('updateChildren dispatches setChildren', () => {
  158. const children = [{ id: 1, name: '孩子1' }]
  159. store.dispatch('updateChildren', children)
  160. expect(store.state.children).toEqual(children)
  161. })
  162. })
  163. })